Skip to content

Commit

Permalink
feat: move the algod client to store (#224)
Browse files Browse the repository at this point in the history
* #213 move the algod client to store

* PersistedState type
  • Loading branch information
acfunk authored Sep 3, 2024
1 parent 173f787 commit 07e7273
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
2 changes: 0 additions & 2 deletions packages/use-wallet-solid/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ export function useWallet() {
const { token, baseServer, port, headers } = manager().networkConfig[networkId]
const newClient = new algosdk.Algodv2(token, baseServer, port, headers)

manager().algodClient = newClient

manager().store.setState((state) => ({
...state,
activeNetwork: networkId,
Expand Down
3 changes: 2 additions & 1 deletion packages/use-wallet-vue/src/__tests__/useWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ describe('useWallet', () => {
mockSetAlgodClient(newAlgodClient)
mockWalletManager.store.setState((state) => ({
...state,
activeNetwork: networkId
activeNetwork: networkId,
algodClient: newAlgodClient
}))
}

Expand Down
5 changes: 2 additions & 3 deletions packages/use-wallet/src/__tests__/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,10 @@ describe('WalletManager', () => {

describe('savePersistedState', () => {
it('saves state to local storage', async () => {
const stateToSave: State = {
const stateToSave: Omit<State, 'algodClient'> = {
wallets: {},
activeWallet: null,
activeNetwork: NetworkId.MAINNET,
algodClient: new Algodv2('', 'https://testnet-api.algonode.cloud/')
activeNetwork: NetworkId.MAINNET
}

const manager = new WalletManager({
Expand Down
3 changes: 2 additions & 1 deletion packages/use-wallet/src/__tests__/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ describe('Mutations', () => {
expect(store.state.activeNetwork).toBe(NetworkId.TESTNET)

const networkId = NetworkId.MAINNET
setActiveNetwork(store, { networkId })
const algodClient = new Algodv2('', 'https://mainnet-api.algonode.cloud/')
setActiveNetwork(store, { networkId, algodClient })
expect(store.state.activeNetwork).toBe(networkId)
})
})
Expand Down
49 changes: 34 additions & 15 deletions packages/use-wallet/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,30 @@ export interface WalletManagerConfig {
algod?: NetworkConfig
}

export type PersistedState = Omit<State, 'algodClient'>

export class WalletManager {
public _clients: Map<WalletId, BaseWallet> = new Map()

public networkConfig: NetworkConfigMap
public algodClient: algosdk.Algodv2

public store: Store<State>
public subscribe: (callback: (state: State) => void) => () => void

constructor({ wallets = [], network = NetworkId.TESTNET, algod = {} }: WalletManagerConfig = {}) {
const initialState = this.loadPersistedState() || {
...defaultState,
activeNetwork: network
}
this.networkConfig = this.initNetworkConfig(network, algod)

const persistedState = this.loadPersistedState()
const initialState: State = persistedState
? {
...persistedState,
algodClient: this.createAlgodClient(this.networkConfig[persistedState.activeNetwork])
}
: {
...defaultState,
activeNetwork: network,
algodClient: this.createAlgodClient(this.networkConfig[network])
}

this.store = new Store<State>(initialState, {
onUpdate: () => this.savePersistedState()
Expand All @@ -65,15 +75,23 @@ export class WalletManager {
return unsubscribe
}

this.networkConfig = this.initNetworkConfig(network, algod)
this.algodClient = this.createAlgodClient(this.networkConfig[initialState.activeNetwork])

this.initializeWallets(wallets)
}

// ---------- Store ------------------------------------------------- //

private loadPersistedState(): State | null {
public get algodClient(): algosdk.Algodv2 {
return this.store.state.algodClient
}

public set algodClient(algodClient: algosdk.Algodv2) {
this.store.setState((state) => ({
...state,
algodClient
}))
}

private loadPersistedState(): PersistedState | null {
try {
const serializedState = StorageAdapter.getItem(LOCAL_STORAGE_KEY)
if (serializedState === null) {
Expand All @@ -84,7 +102,7 @@ export class WalletManager {
console.warn('[Store] Parsed state:', parsedState)
throw new Error('Persisted state is invalid')
}
return parsedState as State
return parsedState as PersistedState
} catch (error: any) {
console.error(`[Store] Could not load state from local storage: ${error.message}`)
return null
Expand All @@ -93,8 +111,9 @@ export class WalletManager {

private savePersistedState(): void {
try {
const state = this.store.state
const serializedState = JSON.stringify(state)
const { wallets, activeWallet, activeNetwork } = this.store.state
const persistedState: PersistedState = { wallets, activeWallet, activeNetwork }
const serializedState = JSON.stringify(persistedState)
StorageAdapter.setItem(LOCAL_STORAGE_KEY, serializedState)
} catch (error) {
console.error('[Store] Could not save state to local storage:', error)
Expand Down Expand Up @@ -205,7 +224,7 @@ export class WalletManager {
}

private createAlgodClient(config: AlgodConfig): algosdk.Algodv2 {
console.info(`[Manager] Creating Algodv2 client for ${this.activeNetwork}...`)
if (this.store) console.info(`[Manager] Creating Algodv2 client for ${this.activeNetwork}...`)
const { token = '', baseServer, port = '', headers = {} } = config
return new algosdk.Algodv2(token, baseServer, port, headers)
}
Expand All @@ -219,8 +238,8 @@ export class WalletManager {
return
}

setActiveNetwork(this.store, { networkId })
this.algodClient = this.createAlgodClient(this.networkConfig[networkId])
const algodClient = this.createAlgodClient(this.networkConfig[networkId])
setActiveNetwork(this.store, { networkId, algodClient })

console.info(`[Manager] ✅ Active network set to ${networkId}.`)
}
Expand Down
8 changes: 6 additions & 2 deletions packages/use-wallet/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ export function setAccounts(
})
}

export function setActiveNetwork(store: Store<State>, { networkId }: { networkId: NetworkId }) {
export function setActiveNetwork(
store: Store<State>,
{ networkId, algodClient }: { networkId: NetworkId; algodClient: Algodv2 }
) {
store.setState((state) => ({
...state,
activeNetwork: networkId
activeNetwork: networkId,
algodClient
}))
}

Expand Down

0 comments on commit 07e7273

Please sign in to comment.