Skip to content
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

fix: identity call where if the network is not supported by wallet and switched to another one #3881

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .changeset/short-dots-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit': patch
'@reown/appkit-core': patch
'@reown/appkit-adapter-bitcoin': 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-cli': patch
'@reown/appkit-common': 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
'@reown/appkit-wallet-button': patch
---

Fixes fetch identity call where if the network is not supported by wallet and if switched to another network
5 changes: 5 additions & 0 deletions apps/laboratory/tests/shared/validators/ModalValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ export class ModalValidator {
await expect(title).toBeVisible()
}

async expectSwitchChainWithNetworkButton(chainName: string) {
const switchNetworkViewLocator = this.page.locator('wui-network-button')
await expect(switchNetworkViewLocator).toHaveText(chainName)
}

async expectSwitchedNetworkWithNetworkView() {
const switchNetworkViewLocator = this.page.locator('w3m-network-switch-view')
await expect(switchNetworkViewLocator).toBeVisible()
Expand Down
23 changes: 23 additions & 0 deletions apps/laboratory/tests/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,29 @@ sampleWalletTest(
}
)

sampleWalletTest(
"it should switch to first available network when wallet doesn't support the active network of the appkit",
async ({ library }) => {
if (library === 'solana') {
return
}

await walletPage.disconnectConnection()
await modalValidator.expectDisconnected()

await modalPage.switchNetworkWithNetworkButton('Aurora')
await modalValidator.expectSwitchChainWithNetworkButton('Aurora')
await modalPage.closeModal()

await modalPage.qrCodeFlow(modalPage, walletPage)
await modalValidator.expectConnected()
await modalPage.openModal()
await modalPage.openNetworks()
await modalValidator.expectSwitchedNetwork('Ethereum')
await modalPage.closeModal()
}
)

sampleWalletTest('it should connect and disconnect using hook', async () => {
await walletPage.disconnectConnection()
await modalValidator.expectDisconnected()
Expand Down
8 changes: 4 additions & 4 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1783,17 +1783,17 @@
}: Pick<AdapterBlueprint.ConnectResult, 'address' | 'chainId'> & {
chainNamespace: ChainNamespace
}) {
const activeCaipNetwork = this.caipNetworks?.find(
n => n.caipNetworkId === `${chainNamespace}:${chainId}`
)
const caipNetworkId = `${chainNamespace}:${chainId}` as CaipNetworkId

Check failure on line 1786 in packages/appkit/src/client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

This assertion is unnecessary since it does not change the type of the expression
const activeCaipNetwork = this.caipNetworks?.find(n => n.caipNetworkId === caipNetworkId)

if (chainNamespace !== ConstantsUtil.CHAIN.EVM || activeCaipNetwork?.testnet) {
return
}

try {
const { name, avatar } = await this.fetchIdentity({
address
address,
caipNetworkId
})

this.setProfileName(name, chainNamespace)
Expand Down
5 changes: 4 additions & 1 deletion packages/appkit/tests/client/listeners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ describe('Listeners', () => {
`${mockAccount.chainNamespace}:${mockAccount.chainId}:${mockAccount.address}`,
'eip155'
)
expect(fetchIdentitySpy).toHaveBeenCalledWith({ address: mockAccount.address })
expect(fetchIdentitySpy).toHaveBeenCalledWith({
address: mockAccount.address,
caipNetworkId: `${mockAccount.chainNamespace}:${mockAccount.chainId}`
})
expect(setProfileNameSpy).toHaveBeenCalledWith(identity.name, 'eip155')
expect(setProfileImageSpy).toHaveBeenCalledWith(identity.avatar, 'eip155')
})
Expand Down
5 changes: 4 additions & 1 deletion packages/appkit/tests/client/public-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,10 @@ describe('Base Public methods', () => {
})

it('should fetch identity', async () => {
const mockRequest = { caipChainId: mainnet.caipNetworkId, address: '0x123' }
const mockRequest = {
caipNetworkId: mainnet.caipNetworkId,
address: '0x123'
}
const fetchIdentity = vi.spyOn(BlockchainApiController, 'fetchIdentity')
fetchIdentity.mockResolvedValue({
name: 'John Doe',
Expand Down
20 changes: 13 additions & 7 deletions packages/core/src/controllers/BlockchainApiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ export const BlockchainApiController = {
}
},

async isNetworkSupported(network?: CaipNetworkId) {
if (!network) {
async isNetworkSupported(networkId?: CaipNetworkId) {
if (!networkId) {
return false
}
try {
Expand All @@ -157,8 +157,9 @@ export const BlockchainApiController = {
return false
}

return state.supportedChains.http.includes(network)
return state.supportedChains.http.includes(networkId)
},

async getSupportedNetworks({ projectId }: { projectId: string }) {
const supportedChains = await state.api.get<BlockchainApiControllerState['supportedChains']>({
path: 'v1/supported-chains',
Expand All @@ -171,10 +172,15 @@ export const BlockchainApiController = {

return supportedChains
},
async fetchIdentity({ address }: BlockchainApiIdentityRequest) {
const isSupported = await BlockchainApiController.isNetworkSupported(
ChainController.state.activeCaipNetwork?.caipNetworkId
)

async fetchIdentity({
address,
caipNetworkId
}: BlockchainApiIdentityRequest & {
caipNetworkId: CaipNetworkId
}) {
const isSupported = await BlockchainApiController.isNetworkSupported(caipNetworkId)

if (!isSupported) {
return { avatar: '', name: '' }
}
Expand Down
Loading