Skip to content

Commit

Permalink
refactor(network): remove name property and improve network ID types (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
drichar authored Jan 17, 2025
1 parent 74fdf73 commit dbbad1b
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/use-wallet-react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useStore } from '@tanstack/react-store'
import { WalletAccount, WalletManager, WalletMetadata } from '@txnlab/use-wallet'
import { NetworkId, WalletAccount, WalletManager, WalletMetadata } from '@txnlab/use-wallet'
import algosdk from 'algosdk'
import * as React from 'react'

Expand Down Expand Up @@ -40,7 +40,7 @@ export const useWallet = () => {

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

const setActiveNetwork = async (networkId: string): Promise<void> => {
const setActiveNetwork = async (networkId: NetworkId | string): Promise<void> => {
if (networkId === activeNetwork) {
return
}
Expand Down
3 changes: 2 additions & 1 deletion packages/use-wallet-solid/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useStore } from '@tanstack/solid-store'
import algosdk from 'algosdk'
import { JSX, createContext, createMemo, onMount, useContext } from 'solid-js'
import type {
NetworkId,
WalletAccount,
WalletId,
WalletManager,
Expand Down Expand Up @@ -78,7 +79,7 @@ export function useWallet() {

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

const setActiveNetwork = async (networkId: string): Promise<void> => {
const setActiveNetwork = async (networkId: NetworkId | string): Promise<void> => {
if (networkId === activeNetwork()) {
return
}
Expand Down
9 changes: 7 additions & 2 deletions packages/use-wallet-vue/src/useWallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { useStore } from '@tanstack/vue-store'
import { WalletManager, type WalletAccount, type WalletMetadata } from '@txnlab/use-wallet'
import {
NetworkId,
WalletManager,
type WalletAccount,
type WalletMetadata
} from '@txnlab/use-wallet'
import algosdk from 'algosdk'
import { computed, inject, ref } from 'vue'

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

const activeNetwork = useStore(manager.store, (state) => state.activeNetwork)
const setActiveNetwork = async (networkId: string): Promise<void> => {
const setActiveNetwork = async (networkId: NetworkId | string): Promise<void> => {
if (networkId === activeNetwork.value) {
return
}
Expand Down
1 change: 0 additions & 1 deletion packages/use-wallet/src/__tests__/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ describe('WalletManager', () => {
it('initializes with custom network', () => {
const networks = new NetworkConfigBuilder()
.addNetwork('custom', {
name: 'Custom Network',
algod: {
token: 'token',
baseServer: 'https://custom-network.com',
Expand Down
8 changes: 0 additions & 8 deletions packages/use-wallet/src/__tests__/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('Network Configuration', () => {
const networks = createNetworkConfig()

expect(networks.mainnet).toEqual({
name: 'MainNet',
algod: {
token: '',
baseServer: 'https://mainnet-api.4160.nodely.dev',
Expand Down Expand Up @@ -48,13 +47,11 @@ describe('Network Configuration', () => {
headers: { 'X-API-Key': 'key' }
})
// Other properties should remain unchanged
expect(networks.mainnet.name).toBe('MainNet')
expect(networks.mainnet.isTestnet).toBe(false)
})

it('allows adding custom networks', () => {
const customNetwork = {
name: 'Custom Network',
algod: {
token: 'token',
baseServer: 'server',
Expand All @@ -75,7 +72,6 @@ describe('Network Configuration', () => {

expect(() =>
builder.addNetwork('mainnet', {
name: 'Custom MainNet',
algod: {
token: '',
baseServer: ''
Expand Down Expand Up @@ -104,7 +100,6 @@ describe('Network Configuration', () => {
describe('isNetworkConfig', () => {
it('validates correct network configs', () => {
const validConfig = {
name: 'Test Network',
algod: {
token: 'token',
baseServer: 'server'
Expand All @@ -115,7 +110,6 @@ describe('Network Configuration', () => {

it('validates network configs with optional properties', () => {
const validConfig = {
name: 'Test Network',
algod: {
token: 'token',
baseServer: 'server'
Expand All @@ -130,10 +124,8 @@ describe('Network Configuration', () => {
it('rejects invalid network configs', () => {
expect(isNetworkConfig(null)).toBe(false)
expect(isNetworkConfig({})).toBe(false)
expect(isNetworkConfig({ name: 'Test' })).toBe(false)
expect(
isNetworkConfig({
name: 'Test',
algod: { baseServer: 'server' }
})
).toBe(false)
Expand Down
4 changes: 2 additions & 2 deletions packages/use-wallet/src/manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Store } from '@tanstack/store'
import algosdk from 'algosdk'
import { Logger, LogLevel, logger } from 'src/logger'
import { createNetworkConfig, isNetworkConfig, type NetworkConfig } from 'src/network'
import { createNetworkConfig, isNetworkConfig, NetworkId, type NetworkConfig } from 'src/network'
import { StorageAdapter } from 'src/storage'
import {
DEFAULT_STATE,
Expand Down Expand Up @@ -313,7 +313,7 @@ export class WalletManager {
return this.algodClient
}

public setActiveNetwork = async (networkId: string): Promise<void> => {
public setActiveNetwork = async (networkId: NetworkId | string): Promise<void> => {
if (this.activeNetwork === networkId) {
return
}
Expand Down
9 changes: 1 addition & 8 deletions packages/use-wallet/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface AlgodConfig {
}

export interface NetworkConfig {
name: string
algod: AlgodConfig
genesisHash?: string
genesisId?: string
Expand All @@ -19,7 +18,6 @@ export interface NetworkConfig {
// Default configurations
export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
mainnet: {
name: 'MainNet',
algod: {
token: '',
baseServer: 'https://mainnet-api.4160.nodely.dev',
Expand All @@ -31,7 +29,6 @@ export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
caipChainId: 'algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k'
},
testnet: {
name: 'TestNet',
algod: {
token: '',
baseServer: 'https://testnet-api.4160.nodely.dev',
Expand All @@ -43,7 +40,6 @@ export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
caipChainId: 'algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDe'
},
betanet: {
name: 'BetaNet',
algod: {
token: '',
baseServer: 'https://betanet-api.4160.nodely.dev',
Expand All @@ -55,7 +51,6 @@ export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
caipChainId: 'algorand:mFgazF-2uRS1tMiL9dsj01hJGySEmPN2'
},
fnet: {
name: 'FNet',
algod: {
token: '',
baseServer: 'https://fnet-api.4160.nodely.dev',
Expand All @@ -67,7 +62,6 @@ export const DEFAULT_NETWORKS: Record<string, NetworkConfig> = {
caipChainId: 'algorand:kUt08LxeVAAGHnh4JoAoAMM9ql_hBwSo'
},
localnet: {
name: 'LocalNet',
algod: {
token: 'a'.repeat(64),
baseServer: 'http://localhost',
Expand Down Expand Up @@ -204,7 +198,7 @@ function isValidToken(
export function isNetworkConfig(config: unknown): config is NetworkConfig {
if (typeof config !== 'object' || config === null) return false

const { name, algod, isTestnet, genesisHash, genesisId, caipChainId } = config as NetworkConfig
const { algod, isTestnet, genesisHash, genesisId, caipChainId } = config as NetworkConfig

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

return (
typeof name === 'string' &&
isValidAlgod &&
(isTestnet === undefined || typeof isTestnet === 'boolean') &&
(genesisHash === undefined || typeof genesisHash === 'string') &&
Expand Down
3 changes: 2 additions & 1 deletion packages/use-wallet/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import algosdk from 'algosdk'
import { logger } from 'src/logger'
import { NetworkId } from 'src/network'
import { WalletId, type WalletAccount } from 'src/wallets/types'
import type { Store } from '@tanstack/store'

Expand Down Expand Up @@ -151,7 +152,7 @@ export function setAccounts(

export function setActiveNetwork(
store: Store<State>,
{ networkId, algodClient }: { networkId: string; algodClient: algosdk.Algodv2 }
{ networkId, algodClient }: { networkId: NetworkId | string; algodClient: algosdk.Algodv2 }
) {
store.setState((state) => ({
...state,
Expand Down

0 comments on commit dbbad1b

Please sign in to comment.