Skip to content

Commit dbbad1b

Browse files
authored
refactor(network): remove name property and improve network ID types (#338)
* refactor(network): remove unused name property from NetworkConfig Remove the unused `name` property from `NetworkConfig` interface and all related code. This property was not serving any functional purpose in the network configuration system. - Remove `name` field from `NetworkConfig` interface - Remove `name` property from all default network configurations - Update tests to remove name-related assertions and test cases * fix(network): allow custom network IDs in setActiveNetwork Update type signature of `setActiveNetwork` to accept both `NetworkId` enum values and custom string network IDs. This change is necessary to support custom networks while maintaining backwards compatibility with the predefined `NetworkId` enum values. - Update type signature in React, Solid, and Vue adapters - Update core `WalletManager` and store types - Maintain existing runtime behavior while improving type flexibility
1 parent 74fdf73 commit dbbad1b

File tree

8 files changed

+16
-25
lines changed

8 files changed

+16
-25
lines changed

packages/use-wallet-react/src/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useStore } from '@tanstack/react-store'
2-
import { WalletAccount, WalletManager, WalletMetadata } from '@txnlab/use-wallet'
2+
import { NetworkId, WalletAccount, WalletManager, WalletMetadata } from '@txnlab/use-wallet'
33
import algosdk from 'algosdk'
44
import * as React from 'react'
55

@@ -40,7 +40,7 @@ export const useWallet = () => {
4040

4141
const activeNetwork = useStore(manager.store, (state) => state.activeNetwork)
4242

43-
const setActiveNetwork = async (networkId: string): Promise<void> => {
43+
const setActiveNetwork = async (networkId: NetworkId | string): Promise<void> => {
4444
if (networkId === activeNetwork) {
4545
return
4646
}

packages/use-wallet-solid/src/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useStore } from '@tanstack/solid-store'
22
import algosdk from 'algosdk'
33
import { JSX, createContext, createMemo, onMount, useContext } from 'solid-js'
44
import type {
5+
NetworkId,
56
WalletAccount,
67
WalletId,
78
WalletManager,
@@ -78,7 +79,7 @@ export function useWallet() {
7879

7980
const activeNetwork = useStore(manager().store, (state) => state.activeNetwork)
8081

81-
const setActiveNetwork = async (networkId: string): Promise<void> => {
82+
const setActiveNetwork = async (networkId: NetworkId | string): Promise<void> => {
8283
if (networkId === activeNetwork()) {
8384
return
8485
}

packages/use-wallet-vue/src/useWallet.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { useStore } from '@tanstack/vue-store'
2-
import { WalletManager, type WalletAccount, type WalletMetadata } from '@txnlab/use-wallet'
2+
import {
3+
NetworkId,
4+
WalletManager,
5+
type WalletAccount,
6+
type WalletMetadata
7+
} from '@txnlab/use-wallet'
38
import algosdk from 'algosdk'
49
import { computed, inject, ref } from 'vue'
510

@@ -34,7 +39,7 @@ export function useWallet() {
3439
const isReady = computed(() => managerStatus.value === 'ready')
3540

3641
const activeNetwork = useStore(manager.store, (state) => state.activeNetwork)
37-
const setActiveNetwork = async (networkId: string): Promise<void> => {
42+
const setActiveNetwork = async (networkId: NetworkId | string): Promise<void> => {
3843
if (networkId === activeNetwork.value) {
3944
return
4045
}

packages/use-wallet/src/__tests__/manager.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ describe('WalletManager', () => {
177177
it('initializes with custom network', () => {
178178
const networks = new NetworkConfigBuilder()
179179
.addNetwork('custom', {
180-
name: 'Custom Network',
181180
algod: {
182181
token: 'token',
183182
baseServer: 'https://custom-network.com',

packages/use-wallet/src/__tests__/network.test.ts

-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe('Network Configuration', () => {
1616
const networks = createNetworkConfig()
1717

1818
expect(networks.mainnet).toEqual({
19-
name: 'MainNet',
2019
algod: {
2120
token: '',
2221
baseServer: 'https://mainnet-api.4160.nodely.dev',
@@ -48,13 +47,11 @@ describe('Network Configuration', () => {
4847
headers: { 'X-API-Key': 'key' }
4948
})
5049
// Other properties should remain unchanged
51-
expect(networks.mainnet.name).toBe('MainNet')
5250
expect(networks.mainnet.isTestnet).toBe(false)
5351
})
5452

5553
it('allows adding custom networks', () => {
5654
const customNetwork = {
57-
name: 'Custom Network',
5855
algod: {
5956
token: 'token',
6057
baseServer: 'server',
@@ -75,7 +72,6 @@ describe('Network Configuration', () => {
7572

7673
expect(() =>
7774
builder.addNetwork('mainnet', {
78-
name: 'Custom MainNet',
7975
algod: {
8076
token: '',
8177
baseServer: ''
@@ -104,7 +100,6 @@ describe('Network Configuration', () => {
104100
describe('isNetworkConfig', () => {
105101
it('validates correct network configs', () => {
106102
const validConfig = {
107-
name: 'Test Network',
108103
algod: {
109104
token: 'token',
110105
baseServer: 'server'
@@ -115,7 +110,6 @@ describe('Network Configuration', () => {
115110

116111
it('validates network configs with optional properties', () => {
117112
const validConfig = {
118-
name: 'Test Network',
119113
algod: {
120114
token: 'token',
121115
baseServer: 'server'
@@ -130,10 +124,8 @@ describe('Network Configuration', () => {
130124
it('rejects invalid network configs', () => {
131125
expect(isNetworkConfig(null)).toBe(false)
132126
expect(isNetworkConfig({})).toBe(false)
133-
expect(isNetworkConfig({ name: 'Test' })).toBe(false)
134127
expect(
135128
isNetworkConfig({
136-
name: 'Test',
137129
algod: { baseServer: 'server' }
138130
})
139131
).toBe(false)

packages/use-wallet/src/manager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Store } from '@tanstack/store'
22
import algosdk from 'algosdk'
33
import { Logger, LogLevel, logger } from 'src/logger'
4-
import { createNetworkConfig, isNetworkConfig, type NetworkConfig } from 'src/network'
4+
import { createNetworkConfig, isNetworkConfig, NetworkId, type NetworkConfig } from 'src/network'
55
import { StorageAdapter } from 'src/storage'
66
import {
77
DEFAULT_STATE,
@@ -313,7 +313,7 @@ export class WalletManager {
313313
return this.algodClient
314314
}
315315

316-
public setActiveNetwork = async (networkId: string): Promise<void> => {
316+
public setActiveNetwork = async (networkId: NetworkId | string): Promise<void> => {
317317
if (this.activeNetwork === networkId) {
318318
return
319319
}

packages/use-wallet/src/network.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export interface AlgodConfig {
88
}
99

1010
export interface NetworkConfig {
11-
name: string
1211
algod: AlgodConfig
1312
genesisHash?: string
1413
genesisId?: string
@@ -19,7 +18,6 @@ export interface NetworkConfig {
1918
// Default configurations
2019
export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
2120
mainnet: {
22-
name: 'MainNet',
2321
algod: {
2422
token: '',
2523
baseServer: 'https://mainnet-api.4160.nodely.dev',
@@ -31,7 +29,6 @@ export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
3129
caipChainId: 'algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k'
3230
},
3331
testnet: {
34-
name: 'TestNet',
3532
algod: {
3633
token: '',
3734
baseServer: 'https://testnet-api.4160.nodely.dev',
@@ -43,7 +40,6 @@ export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
4340
caipChainId: 'algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDe'
4441
},
4542
betanet: {
46-
name: 'BetaNet',
4743
algod: {
4844
token: '',
4945
baseServer: 'https://betanet-api.4160.nodely.dev',
@@ -55,7 +51,6 @@ export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
5551
caipChainId: 'algorand:mFgazF-2uRS1tMiL9dsj01hJGySEmPN2'
5652
},
5753
fnet: {
58-
name: 'FNet',
5954
algod: {
6055
token: '',
6156
baseServer: 'https://fnet-api.4160.nodely.dev',
@@ -67,7 +62,6 @@ export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
6762
caipChainId: 'algorand:kUt08LxeVAAGHnh4JoAoAMM9ql_hBwSo'
6863
},
6964
localnet: {
70-
name: 'LocalNet',
7165
algod: {
7266
token: 'a'.repeat(64),
7367
baseServer: 'http://localhost',
@@ -204,7 +198,7 @@ function isValidToken(
204198
export function isNetworkConfig(config: unknown): config is NetworkConfig {
205199
if (typeof config !== 'object' || config === null) return false
206200

207-
const { name, algod, isTestnet, genesisHash, genesisId, caipChainId } = config as NetworkConfig
201+
const { algod, isTestnet, genesisHash, genesisId, caipChainId } = config as NetworkConfig
208202

209203
const isValidAlgod =
210204
typeof algod === 'object' &&
@@ -213,7 +207,6 @@ export function isNetworkConfig(config: unknown): config is NetworkConfig {
213207
typeof algod.baseServer === 'string'
214208

215209
return (
216-
typeof name === 'string' &&
217210
isValidAlgod &&
218211
(isTestnet === undefined || typeof isTestnet === 'boolean') &&
219212
(genesisHash === undefined || typeof genesisHash === 'string') &&

packages/use-wallet/src/store.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import algosdk from 'algosdk'
22
import { logger } from 'src/logger'
3+
import { NetworkId } from 'src/network'
34
import { WalletId, type WalletAccount } from 'src/wallets/types'
45
import type { Store } from '@tanstack/store'
56

@@ -151,7 +152,7 @@ export function setAccounts(
151152

152153
export function setActiveNetwork(
153154
store: Store<State>,
154-
{ networkId, algodClient }: { networkId: string; algodClient: algosdk.Algodv2 }
155+
{ networkId, algodClient }: { networkId: NetworkId | string; algodClient: algosdk.Algodv2 }
155156
) {
156157
store.setState((state) => ({
157158
...state,

0 commit comments

Comments
 (0)