Skip to content

Commit 7e414bc

Browse files
committed
refactor: get rid of manual bookkeeping of wallet chain id and rely more on wagmi
1 parent 8ddd2c5 commit 7e414bc

File tree

7 files changed

+17
-22
lines changed

7 files changed

+17
-22
lines changed

apps/main/src/dao/store/createAppSlice.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const createAppSlice = (set: SetState<State>, get: GetState<State>): AppSlice =>
4141
const isNetworkSwitched = prevApi?.chainId != api.chainId
4242

4343
log('Hydrating DAO', api?.chainId, {
44-
wallet: wallet?.chainId ?? '',
4544
isNetworkSwitched,
4645
})
4746

apps/main/src/dex/store/createGlobalSlice.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@ const createGlobalSlice = (set: SetState<State>, get: GetState<State>): GlobalSl
6262
}),
6363
)
6464
},
65-
hydrate: async (curveApi, prevCurveApi, wallet) => {
65+
hydrate: async (curveApi, prevCurveApi) => {
6666
if (!curveApi) return
6767

6868
const state = get()
6969
const isNetworkSwitched = prevCurveApi?.chainId !== curveApi.chainId
7070
const isUserSwitched = prevCurveApi?.signerAddress !== curveApi.signerAddress
7171
const { chainId } = curveApi
7272
log('Hydrating DEX', curveApi?.chainId, {
73-
wallet: wallet?.chainId ?? '',
7473
isNetworkSwitched,
7574
isUserSwitched,
7675
hasRPC: !curveApi.isNoRPC,

apps/main/src/lend/store/createAppSlice.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const createAppSlice = (set: SetState<State>, get: GetState<State>): AppSlice =>
4040
}),
4141
)
4242
},
43-
hydrate: async (api, prevApi, wallet) => {
43+
hydrate: async (api, prevApi) => {
4444
get().updateGlobalStoreByKey('hydratedChainId', null)
4545
if (!api) return
4646

@@ -49,7 +49,6 @@ const createAppSlice = (set: SetState<State>, get: GetState<State>): AppSlice =>
4949
const state = get()
5050

5151
log('Hydrating Lend', api.chainId, {
52-
wallet: wallet?.chainId ?? '',
5352
chainId: [prevApi?.chainId, api.chainId],
5453
signerAddress: [prevApi?.signerAddress, api.signerAddress],
5554
})

apps/main/src/loan/store/createAppSlice.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ const createAppSlice = (set: SetState<State>, get: GetState<State>): AppSlice =>
4141
)
4242
},
4343

44-
hydrate: async (curveApi, prevCurveApi, wallet) => {
44+
hydrate: async (curveApi, prevCurveApi) => {
4545
if (!curveApi) return
4646

4747
const { loans, collaterals } = get()
4848

4949
const isNetworkSwitched = !!prevCurveApi?.chainId && prevCurveApi.chainId !== curveApi.chainId
5050
const isUserSwitched = !!prevCurveApi?.signerAddress && prevCurveApi.signerAddress !== curveApi.signerAddress
5151
log('Hydrate crvUSD', curveApi?.chainId, {
52-
wallet: wallet?.chainId ?? '',
5352
isNetworkSwitched,
5453
isUserSwitched,
5554
})

packages/curve-ui-kit/src/features/connect-wallet/lib/ConnectionContext.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ export function useWagmiWallet() {
4545
client?.transport.request && {
4646
provider: { request: client.transport.request },
4747
account: { address }, // the ensName is set later when detected
48-
chainId: client.chain.id,
4948
}
5049
hackSignerInCypress(wallet)
5150
return { wallet, provider: wallet ? new BrowserProvider(wallet.provider) : null }
52-
}, [address, client?.chain.id, client?.transport.request]) ?? null),
51+
}, [address, client?.transport.request]) ?? null),
5352
}
5453
}
5554

packages/curve-ui-kit/src/features/connect-wallet/lib/ConnectionProvider.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ReactNode, useEffect, useState } from 'react'
2-
import { useSwitchChain } from 'wagmi'
2+
import { useChainId, useSwitchChain } from 'wagmi'
33
import type { NetworkDef } from '@ui/utils'
44
import { ConnectionContext, useWagmiWallet } from '@ui-kit/features/connect-wallet/lib/ConnectionContext'
55
import { ConnectState } from '@ui-kit/features/connect-wallet/lib/types'
@@ -27,6 +27,7 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
2727
children: ReactNode
2828
}) => {
2929
const [connectState, setConnectState] = useState<ConnectState>(LOADING)
30+
const chainId = useChainId()
3031
const { switchChainAsync } = useSwitchChain()
3132
const { wallet, provider, isReconnecting } = useWagmiWallet()
3233
const isFocused = useIsDocumentFocused()
@@ -38,12 +39,12 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
3839
* This is separate from the rest of initApp to avoid unnecessary reinitialization, as isFocused can change frequently.
3940
*/
4041
async function updateWalletChain() {
41-
const chainId = Number(network.chainId) as TChainId
42-
if (wallet && wallet?.chainId !== chainId) {
42+
const networkChainId = Number(network.chainId) as TChainId
43+
if (networkChainId !== chainId) {
4344
setConnectState(LOADING)
44-
if (isFocused && !(await switchChainAsync({ chainId: chainId as WagmiChainId }))) {
45+
if (isFocused && !(await switchChainAsync({ chainId: networkChainId as WagmiChainId }))) {
4546
setConnectState(FAILURE)
46-
onChainUnavailable([chainId, wallet?.chainId as TChainId])
47+
onChainUnavailable([networkChainId, chainId as TChainId])
4748
}
4849
return // hook is called again after since it depends on walletChainId
4950
}
@@ -52,7 +53,7 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
5253
console.error('Error updating wallet chain', e)
5354
setConnectState(FAILURE)
5455
})
55-
}, [isFocused, network.chainId, onChainUnavailable, switchChainAsync, wallet])
56+
}, [isFocused, chainId, network.chainId, onChainUnavailable, switchChainAsync, wallet])
5657

5758
useEffect(() => {
5859
if (isReconnecting) return // wait for wagmi to auto-reconnect
@@ -63,14 +64,14 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
6364
* Initialize the app by connecting to the wallet and setting up the library.
6465
*/
6566
const initApp = async () => {
66-
const chainId = Number(network.chainId) as TChainId
67+
const networkChainId = Number(network.chainId) as TChainId
6768
try {
68-
if (wallet && wallet?.chainId !== chainId) {
69+
if (chainId !== networkChainId) {
6970
return // wait for the wallet to be connected to the right chain
7071
}
7172

7273
const prevLib = globalLibs.get(libKey)
73-
if (isWalletMatching(wallet, prevLib, chainId)) {
74+
if (isWalletMatching(wallet, prevLib, networkChainId)) {
7475
setConnectState(SUCCESS)
7576
return // already connected to the right chain and wallet, no need to reinitialize
7677
}
@@ -79,7 +80,7 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
7980
setConnectState(LOADING)
8081
console.info(
8182
`Initializing ${libKey} for ${network.name} (${network.chainId})`,
82-
wallet ? `Wallet ${wallet?.account?.address} with chain ${wallet?.chainId}` : 'without wallet',
83+
wallet ? `Wallet ${wallet?.account?.address} with chain ${chainId}` : 'without wallet',
8384
prevLib
8485
? `Old library had ${prevLib.signerAddress ? `signer ${prevLib.signerAddress}` : 'no signer'} with chain ${prevLib.chainId}`
8586
: `First initialization`,
@@ -97,7 +98,7 @@ export const ConnectionProvider = <TChainId extends number, NetworkConfig extend
9798
}
9899
void initApp()
99100
return () => abort.abort()
100-
}, [isReconnecting, libKey, network, wallet])
101+
}, [chainId, isReconnecting, libKey, network, wallet])
101102

102103
const curveApi = globalLibs.getMatching('curveApi', wallet, network?.chainId)
103104
const llamaApi = globalLibs.getMatching('llamaApi', wallet, network?.chainId)

packages/curve-ui-kit/src/features/connect-wallet/lib/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import type { IChainId as CurveChainId, INetworkName as CurveNetworkId } from '@
55
import { type default as llamaApi } from '@curvefi/llamalend-api'
66
import type { IChainId as LlamaChainId, INetworkName as LlamaNetworkId } from '@curvefi/llamalend-api/lib/interfaces'
77

8-
export type Wallet<TChainId extends number = number> = {
8+
export type Wallet = {
99
readonly provider?: Eip1193Provider
1010
readonly account: { address: Address; ensName?: string }
11-
readonly chainId: TChainId
1211
}
1312

1413
export enum ConnectState {

0 commit comments

Comments
 (0)