Skip to content

Commit

Permalink
chore: fix wagmi not disconnecting and add adapter tests (#2751)
Browse files Browse the repository at this point in the history
Co-authored-by: tomiir <[email protected]>
  • Loading branch information
magiziz and tomiir authored Aug 27, 2024
1 parent c168e33 commit 5e22d6b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 20 deletions.
26 changes: 9 additions & 17 deletions packages/base/adapters/evm/wagmi/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ export class EVMWagmiClient {
// -- Private variables -------------------------------------------------------
private appKit: AppKit | undefined = undefined

private hasSyncedConnectedAccount = false

private wagmiConfig: AdapterOptions<Config>['wagmiConfig']

// -- Public variables --------------------------------------------------------
Expand Down Expand Up @@ -455,11 +453,10 @@ export class EVMWagmiClient {

private async syncAccount({
address,
isConnected,
isDisconnected,
chainId,
connector,
addresses
addresses,
status
}: Partial<
Pick<
GetAccountReturnType,
Expand All @@ -477,10 +474,9 @@ export class EVMWagmiClient {
if (this.appKit?.getCaipAddress() === caipAddress) {
return
}

if (isConnected && address && chainId) {
this.syncNetwork(address, chainId, isConnected)
this.appKit?.setIsConnected(isConnected, this.chain)
if (status === 'connected' && address && chainId) {
this.syncNetwork(address, chainId, true)
this.appKit?.setIsConnected(true, this.chain)
this.appKit?.setCaipAddress(caipAddress, this.chain)
await Promise.all([
this.syncProfile(address, chainId),
Expand All @@ -500,15 +496,12 @@ export class EVMWagmiClient {
this.chain
)
}

this.hasSyncedConnectedAccount = true
} else if (isDisconnected && this.hasSyncedConnectedAccount) {
} else if (status === 'disconnected') {
this.appKit?.resetAccount(this.chain)
this.appKit?.resetWcConnection()
this.appKit?.resetNetwork()
this.appKit?.setAllAccounts([], this.chain)

this.hasSyncedConnectedAccount = false
this.appKit?.setIsConnected(false, this.chain)
}
}

Expand All @@ -535,9 +528,8 @@ export class EVMWagmiClient {
} else {
this.appKit?.setAddressExplorerUrl(undefined, this.chain)
}
if (this.hasSyncedConnectedAccount) {
await this.syncBalance(address, chainId)
}

await this.syncBalance(address, chainId)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"build": "tsc --build",
"watch": "tsc --watch",
"typecheck": "tsc --noEmit",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"test": "vitest run --dir tests"
},
"dependencies": {
"@walletconnect/ethereum-provider": "2.15.1",
Expand All @@ -67,7 +68,8 @@
"react-dom": "18.2.0",
"viem": "2.19.6",
"vue": "3.4.3",
"wagmi": "2.12.5"
"wagmi": "2.12.5",
"vitest": "2.0.3"
},
"peerDependencies": {
"@wagmi/connectors": ">=4",
Expand Down
53 changes: 53 additions & 0 deletions packages/wagmi/tests/adapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, test, vi } from 'vitest'
import { connect, disconnect, getAccount as getAccount_wagmi } from '@wagmi/core'
import { ConstantsUtil } from '@web3modal/scaffold-utils'
import { appKitMock, mockAccount, wagmiConfigMock } from './mocks/adapter.mock'
import { mainnet } from 'viem/chains'

describe('wagmi adapter', () => {
test('should connect to adapter', async () => {
expect(appKitMock.getConnectors().length).toBe(1)
expect(appKitMock.getIsConnectedState()).toBe(false)
expect(appKitMock.getCaipAddress()).toBeUndefined()

const setApprovedCaipNetworksData = vi
.spyOn(appKitMock, 'setApprovedCaipNetworksData')
.mockResolvedValue()

await connect(wagmiConfigMock, { connector: wagmiConfigMock.connectors[0]! })

expect(setApprovedCaipNetworksData).toHaveBeenCalledOnce()

expect(appKitMock.getIsConnectedState()).toBe(true)
expect(appKitMock.getCaipAddress()).toBe(
`${ConstantsUtil.EIP155}:${mainnet.id}:${mockAccount.address}`
)

const wagmiAccount = getAccount_wagmi(wagmiConfigMock)

expect(wagmiAccount.status).toBe('connected')
expect(wagmiAccount.address).toBe(mockAccount.address)
})

test('should disconnect from adapter', async () => {
// Check if already connected
expect(appKitMock.getIsConnectedState()).toBe(true)

const resetAccount = vi.spyOn(appKitMock, 'resetAccount').mockResolvedValue()
const resetWcConnection = vi.spyOn(appKitMock, 'resetWcConnection').mockResolvedValue()
const resetNetwork = vi.spyOn(appKitMock, 'resetNetwork').mockResolvedValue()

await disconnect(wagmiConfigMock)

expect(resetAccount).toHaveBeenCalledOnce()
expect(resetWcConnection).toHaveBeenCalledOnce()
expect(resetNetwork).toHaveBeenCalledOnce()

expect(appKitMock.getIsConnectedState()).toBe(false)

const wagmiAccount = getAccount_wagmi(wagmiConfigMock)

expect(wagmiAccount.status).toBe('disconnected')
expect(wagmiAccount.address).toBeUndefined()
})
})
38 changes: 38 additions & 0 deletions packages/wagmi/tests/mocks/adapter.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { EVMWagmiClient } from '@web3modal/base/adapters/evm/wagmi'
import { mainnet } from 'viem/chains'
import { createConfig, http } from 'wagmi'
import { mock } from 'wagmi/connectors'

import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'
import { AppKit, type SdkVersion } from '@web3modal/base'

const privateKey = generatePrivateKey()
export const mockAccount = privateKeyToAccount(privateKey)

export const wagmiConfigMock = createConfig({
chains: [mainnet],
connectors: [mock({ accounts: [mockAccount.address] })],
transports: {
[mainnet.id]: http()
}
})

const wagmiAdapterMock = new EVMWagmiClient({
wagmiConfig: wagmiConfigMock
})

const mockAppKitData = {
defaultChain: wagmiAdapterMock.defaultChain,
adapters: [wagmiAdapterMock],
metadata: {
description: 'Desc',
name: 'Name',
url: 'url.com',
icons: ['icon.png']
},
projectId: '1234',
sdkVersion: 'react-wagmi-4.0.13' as SdkVersion,
sdkType: 'w3m' as const
}

export const appKitMock = new AppKit(mockAppKitData)
2 changes: 1 addition & 1 deletion packages/wagmi/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"baseUrl": "../../"
},
"extends": "../../tsconfig.json",
"include": ["exports"]
"include": ["exports", "tests"]
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e22d6b

Please sign in to comment.