Skip to content

Commit

Permalink
chore: only sync identity on address change (#3392)
Browse files Browse the repository at this point in the history
Co-authored-by: Felipe Mendes <zo.fmendes@gmail.com>
  • Loading branch information
tomiir and zoruka authored Dec 5, 2024
1 parent 9d5e4c6 commit 1021422
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
20 changes: 20 additions & 0 deletions .changeset/funny-mayflies-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@reown/appkit': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

Only syncs identity on address updates
55 changes: 27 additions & 28 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1418,44 +1418,43 @@ export class AppKit {
}: Pick<AdapterBlueprint.ConnectResult, 'address' | 'chainId'> & {
chainNamespace: ChainNamespace
}) {
this.setPreferredAccountType(
AccountController.state.preferredAccountType
? AccountController.state.preferredAccountType
: 'eoa',
ChainController.state.activeChain as ChainNamespace
)

this.setCaipAddress(
`${chainNamespace}:${chainId}:${address}` as `${ChainNamespace}:${string}:${string}`,
chainNamespace
)
// Only update state when needed
if (address.toLowerCase() !== AccountController.state.address?.toLowerCase()) {
this.setCaipAddress(`${chainNamespace}:${chainId}:${address}`, chainNamespace)
await this.syncIdentity({ address, chainId: Number(chainId), chainNamespace })
}

this.setStatus('connected', chainNamespace)

if (chainNamespace === ChainController.state.activeChain) {
const caipNetwork = this.caipNetworks?.find(
n => n.id === chainId && n.chainNamespace === chainNamespace
)
const fallBackCaipNetwork = this.caipNetworks?.find(n => n.chainNamespace === chainNamespace)

if (caipNetwork) {
this.setCaipNetwork(caipNetwork)
} else {
this.setCaipNetwork(this.caipNetworks?.find(n => n.chainNamespace === chainNamespace))
}

this.setCaipNetwork(caipNetwork || fallBackCaipNetwork)
this.syncConnectedWalletInfo(chainNamespace)
const adapter = this.getAdapter(chainNamespace)
await this.syncBalance({ address, chainId, chainNamespace })
}
}

const balance = await adapter?.getBalance({
address,
chainId,
caipNetwork: caipNetwork || this.getCaipNetwork(),
tokens: this.options.tokens
})
if (balance) {
this.setBalance(balance.balance, balance.symbol, chainNamespace)
}
await this.syncIdentity({ address, chainId: Number(chainId), chainNamespace })
private async syncBalance(params: {
address: string
chainId: string | number
chainNamespace: ChainNamespace
}) {
const adapter = this.getAdapter(params.chainNamespace)
const caipNetwork = this.caipNetworks?.find(
c => c.chainNamespace === params.chainNamespace && c.id === params.chainId
)
const balance = await adapter?.getBalance({
address: params.address,
chainId: params.chainId,
caipNetwork: caipNetwork || this.getCaipNetwork(),
tokens: this.options.tokens
})
if (balance) {
this.setBalance(balance.balance, balance.symbol, params.chainNamespace)
}
}

Expand Down
22 changes: 22 additions & 0 deletions packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,28 @@ describe('Base', () => {
)
})

it('should sync identity only if address changed', async () => {
const mockAccountData = {
address: '0x123',
chainId: '1',
chainNamespace: 'eip155' as const
}
vi.mocked(BlockchainApiController.fetchIdentity).mockResolvedValue({
name: 'John Doe',
avatar: null
})

vi.mocked(AccountController).state = { address: '0x123' } as any

await appKit['syncAccount'](mockAccountData)

expect(BlockchainApiController.fetchIdentity).not.toHaveBeenCalled()

await appKit['syncAccount']({ ...mockAccountData, address: '0x456' })

expect(BlockchainApiController.fetchIdentity).toHaveBeenCalledOnce()
})

it('should disconnect correctly', async () => {
vi.mocked(ChainController).state = {
chains: new Map([['eip155', { namespace: 'eip155' }]]),
Expand Down

0 comments on commit 1021422

Please sign in to comment.