-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor/switch network and local storage controls #2888
Changes from all commits
bf297e3
91d9794
587e96f
9130337
42eb1d8
528496f
36527d9
dd1c571
468ec8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
'@reown/appkit-adapter-ethers5': patch | ||
'@reown/appkit-adapter-ethers': patch | ||
'@reown/appkit-adapter-solana': patch | ||
'@reown/appkit-adapter-wagmi': patch | ||
'@reown/appkit-scaffold-ui': patch | ||
'@apps/laboratory': patch | ||
'@reown/appkit': patch | ||
'@reown/appkit-common': patch | ||
'@reown/appkit-core': patch | ||
'@apps/demo': patch | ||
'@apps/gallery': patch | ||
'@examples/html-ethers': patch | ||
'@examples/html-ethers5': patch | ||
'@examples/html-wagmi': patch | ||
'@examples/next-ethers': patch | ||
'@examples/next-wagmi': patch | ||
'@examples/react-ethers': patch | ||
'@examples/react-ethers5': patch | ||
'@examples/react-solana': patch | ||
'@examples/react-wagmi': patch | ||
'@examples/vue-ethers5': patch | ||
'@examples/vue-solana': patch | ||
'@examples/vue-wagmi': patch | ||
'@reown/appkit-adapter-polkadot': patch | ||
'@reown/appkit-utils': patch | ||
'@reown/appkit-cdn': patch | ||
'@reown/appkit-ethers': patch | ||
'@reown/appkit-ethers5': patch | ||
'@reown/appkit-polyfills': patch | ||
'@reown/appkit-siwe': patch | ||
'@reown/appkit-solana': patch | ||
'@reown/appkit-ui': patch | ||
'@reown/appkit-wagmi': patch | ||
'@reown/appkit-wallet': patch | ||
--- | ||
|
||
Updates active network state management and local storage controls |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -613,12 +613,10 @@ export class EthersAdapter { | |
if (provider) { | ||
const { addresses, chainId } = await EthersHelpersUtil.getUserInfo(provider) | ||
const firstAddress = addresses?.[0] | ||
const caipNetwork = this.caipNetworks.find(c => c.chainId === chainId) | ||
const caipAddress = `${this.chainNamespace}:${chainId}:${firstAddress}` as CaipAddress | ||
|
||
if (firstAddress && chainId && caipNetwork) { | ||
if (firstAddress && chainId) { | ||
this.appKit?.setCaipAddress(caipAddress, this.chainNamespace) | ||
this.appKit?.setCaipNetwork(caipNetwork) | ||
ProviderUtil.setProviderId('eip155', providerId) | ||
ProviderUtil.setProvider<Provider>('eip155', provider) | ||
this.appKit?.setStatus('connected', this.chainNamespace) | ||
|
@@ -660,8 +658,6 @@ export class EthersAdapter { | |
: [{ address, type: preferredAccountType as 'eoa' | 'smartAccount' }], | ||
this.chainNamespace | ||
) | ||
const caipNetwork = this.caipNetworks.find(c => c.chainId === chainId) | ||
this.appKit?.setCaipNetwork(caipNetwork) | ||
this.appKit?.setStatus('connected', this.chainNamespace) | ||
this.appKit?.setCaipAddress( | ||
`${this.chainNamespace}:${chainId}:${address}`, | ||
|
@@ -717,13 +713,13 @@ export class EthersAdapter { | |
} | ||
} | ||
|
||
const chainChangedHandler = (networkId: string) => { | ||
if (networkId) { | ||
const networkIdNumber = | ||
typeof networkId === 'string' | ||
? EthersHelpersUtil.hexStringToNumber(networkId) | ||
: Number(networkId) | ||
const caipNetwork = this.caipNetworks.find(c => c.chainId === networkIdNumber) | ||
const chainChangedHandler = (chainId: string) => { | ||
const chainIdNumber = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should control if the received Same logic applied to all clients |
||
typeof chainId === 'string' ? EthersHelpersUtil.hexStringToNumber(chainId) : Number(chainId) | ||
const caipNetwork = this.caipNetworks.find(c => c.chainId === chainIdNumber) | ||
const currentCaipNetwork = this.appKit?.getCaipNetwork() | ||
|
||
if (!currentCaipNetwork || currentCaipNetwork?.id !== caipNetwork?.id) { | ||
this.appKit?.setCaipNetwork(caipNetwork) | ||
} | ||
} | ||
|
@@ -836,10 +832,7 @@ export class EthersAdapter { | |
|
||
this.appKit?.setLoading(true) | ||
const chainId = NetworkUtil.caipNetworkIdToNumber(this.appKit?.getCaipNetwork()?.id) | ||
const caipNetwork = this.caipNetworks.find(c => c.chainId === chainId) | ||
|
||
this.appKit?.setCaipAddress(`eip155:${chainId}:${address}`, this.chainNamespace) | ||
this.appKit?.setCaipNetwork(caipNetwork) | ||
this.appKit?.setStatus('connected', this.chainNamespace) | ||
this.appKit?.setPreferredAccountType(type as W3mFrameTypes.AccountType, this.chainNamespace) | ||
|
||
|
@@ -1014,13 +1007,12 @@ export class EthersAdapter { | |
} | ||
|
||
public async switchNetwork(caipNetwork: CaipNetwork) { | ||
const requestSwitchNetwork = async (provider: Provider) => { | ||
async function requestSwitchNetwork(provider: Provider) { | ||
try { | ||
await provider.request({ | ||
method: 'wallet_switchEthereumChain', | ||
params: [{ chainId: EthersHelpersUtil.numberToHexString(caipNetwork.chainId) }] | ||
}) | ||
this.appKit?.setCaipNetwork(caipNetwork) | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (switchError: any) { | ||
if ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,7 +165,6 @@ describe('EthersAdapter', () => { | |
]) | ||
}) | ||
) | ||
expect(mockAppKit.setCaipNetwork).toHaveBeenCalledWith(newNetwork) | ||
}) | ||
|
||
it('should add network if not recognized by wallet', async () => { | ||
|
@@ -344,7 +343,6 @@ describe('EthersAdapter', () => { | |
|
||
expect(mockAppKit.setLoading).toHaveBeenCalledWith(true) | ||
expect(mockAppKit.setCaipAddress).toHaveBeenCalledWith(`eip155:${1}:${address}`, 'eip155') | ||
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() | ||
expect(mockAppKit.setStatus).toHaveBeenCalledWith('connected', 'eip155') | ||
expect(mockAppKit.setPreferredAccountType).toHaveBeenCalledWith(type, 'eip155') | ||
|
||
|
@@ -388,7 +386,6 @@ describe('EthersAdapter', () => { | |
[{ address: mockAddress, type: mockPreferredAccountType }], | ||
'eip155' | ||
) | ||
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() | ||
expect(mockAppKit.setStatus).toHaveBeenCalledWith('connected', 'eip155') | ||
expect(mockAppKit.setCaipAddress).toHaveBeenCalledWith( | ||
`eip155:${mockChainId}:${mockAddress}`, | ||
|
@@ -527,7 +524,6 @@ describe('EthersAdapter', () => { | |
SafeLocalStorageKeys.WALLET_NAME, | ||
'MetaMask' | ||
) | ||
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() | ||
expect(mockAppKit.setCaipAddress).toHaveBeenCalled() | ||
expect(ProviderUtil.setProviderId).toHaveBeenCalledWith('eip155', 'injected') | ||
expect(ProviderUtil.setProvider).toHaveBeenCalledWith('eip155', mockProvider) | ||
|
@@ -584,7 +580,7 @@ describe('EthersAdapter', () => { | |
const chainChangedHandler = mockProvider.on.mock.calls.find( | ||
(call: string[]) => call[0] === 'chainChanged' | ||
)[1] | ||
await chainChangedHandler('0x1') | ||
await chainChangedHandler('0x137') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be trigger the |
||
|
||
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -722,13 +722,13 @@ export class Ethers5Adapter { | |
} | ||
} | ||
|
||
const chainChangedHandler = (networkId: string) => { | ||
if (networkId) { | ||
const networkIdNumber = | ||
typeof networkId === 'string' | ||
? EthersHelpersUtil.hexStringToNumber(networkId) | ||
: Number(networkId) | ||
const caipNetwork = this.caipNetworks.find(c => c.chainId === networkIdNumber) | ||
const chainChangedHandler = (chainId: string) => { | ||
const chainIdNumber = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
typeof chainId === 'string' ? EthersHelpersUtil.hexStringToNumber(chainId) : Number(chainId) | ||
const caipNetwork = this.caipNetworks.find(c => c.chainId === chainIdNumber) | ||
const currentCaipNetwork = this.appKit?.getCaipNetwork() | ||
|
||
if (!currentCaipNetwork || currentCaipNetwork?.id !== caipNetwork?.id) { | ||
this.appKit?.setCaipNetwork(caipNetwork) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -349,7 +349,6 @@ describe('EthersAdapter', () => { | |
|
||
expect(mockAppKit.setLoading).toHaveBeenCalledWith(true) | ||
expect(mockAppKit.setCaipAddress).toHaveBeenCalledWith(`eip155:${1}:${address}`, 'eip155') | ||
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() | ||
expect(mockAppKit.setStatus).toHaveBeenCalledWith('connected', 'eip155') | ||
expect(mockAppKit.setPreferredAccountType).toHaveBeenCalledWith(type, 'eip155') | ||
|
||
|
@@ -532,7 +531,6 @@ describe('EthersAdapter', () => { | |
SafeLocalStorageKeys.WALLET_NAME, | ||
'MetaMask' | ||
) | ||
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() | ||
expect(ProviderUtil.setProviderId).toHaveBeenCalledWith('eip155', 'injected') | ||
expect(ProviderUtil.setProvider).toHaveBeenCalledWith('eip155', mockProvider) | ||
expect(mockAppKit.setStatus).toHaveBeenCalledWith('connected', 'eip155') | ||
|
@@ -588,7 +586,7 @@ describe('EthersAdapter', () => { | |
const chainChangedHandler = mockProvider.on.mock.calls.find( | ||
(call: string[]) => call[0] === 'chainChanged' | ||
)[1] | ||
await chainChangedHandler('0x1') | ||
await chainChangedHandler('0x2') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same - updating the value to make sure it's triggering |
||
|
||
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ import { Connection } from '@solana/web3.js' | |
import { | ||
AccountController, | ||
ApiController, | ||
AssetController, | ||
ChainController, | ||
CoreHelperUtil, | ||
EventsController | ||
|
@@ -311,10 +310,6 @@ export class SolanaAdapter implements ChainAdapter { | |
|
||
this.syncRequestedNetworks(this.caipNetworks) | ||
|
||
AssetController.subscribeNetworkImages(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessary. I'll handle the network images fetching in another PR |
||
this.syncNetwork() | ||
}) | ||
|
||
ChainController.subscribeKey('activeCaipNetwork', (newCaipNetwork: CaipNetwork | undefined) => { | ||
const newChain = this.caipNetworks.find( | ||
_chain => _chain.chainId === newCaipNetwork?.id.split(':')[1] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -394,7 +394,6 @@ export class WagmiAdapter implements ChainAdapter { | |
await SIWEController.signOut() | ||
} | ||
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID) | ||
SafeLocalStorage.removeItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK) | ||
SafeLocalStorage.removeItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR) | ||
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_NAME) | ||
this.appKit?.setClientId(null) | ||
|
@@ -609,7 +608,6 @@ export class WagmiAdapter implements ChainAdapter { | |
this.appKit?.resetNetwork() | ||
this.appKit?.setAllAccounts([], this.chainNamespace) | ||
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID) | ||
SafeLocalStorage.removeItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK) | ||
|
||
return | ||
} | ||
|
@@ -647,7 +645,6 @@ export class WagmiAdapter implements ChainAdapter { | |
} | ||
} else if (status === 'connected' && address && chainId) { | ||
const caipAddress = `eip155:${chainId}:${address}` as CaipAddress | ||
this.appKit?.resetAccount(this.chainNamespace) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we've connected, we don't have to resetAccount. This should have been removed long ago - I remember we removed these |
||
this.syncNetwork(address, chainId, true) | ||
this.appKit?.setCaipAddress(caipAddress, this.chainNamespace) | ||
await Promise.all([ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do all the
caipNetwork
setters from a single place and do it only when it's necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++++++