Skip to content

Commit

Permalink
fix: defaultNetwork prop is not being used on initialization (#3869)
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk authored Feb 14, 2025
1 parent a618020 commit b264e3b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
23 changes: 23 additions & 0 deletions .changeset/great-clocks-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit': 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-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
'@reown/appkit-wallet-button': patch
---

Fixes defaultNetwork prop that is not being used on initialization
17 changes: 13 additions & 4 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2136,16 +2136,25 @@ export class AppKit {
}

private getDefaultNetwork() {
const caipNetworkId = StorageUtil.getActiveCaipNetworkId()
const caipNetworkIdFromStorage = StorageUtil.getActiveCaipNetworkId()

if (caipNetworkId) {
const caipNetwork = this.caipNetworks?.find(n => n.caipNetworkId === caipNetworkId)
if (caipNetworkIdFromStorage) {
const caipNetwork = this.caipNetworks?.find(n => n.caipNetworkId === caipNetworkIdFromStorage)

if (caipNetwork) {
return caipNetwork
}

return this.getUnsupportedNetwork(caipNetworkId)
if (this.defaultCaipNetwork) {
// It's still a case that the network in storage might not be found in the networks array
return this.defaultCaipNetwork
}

return this.getUnsupportedNetwork(caipNetworkIdFromStorage)
}

if (this.defaultCaipNetwork) {
return this.defaultCaipNetwork
}

return this.caipNetworks?.[0]
Expand Down
41 changes: 39 additions & 2 deletions packages/appkit/tests/client/initialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
AlertController,
ChainController,
EventsController,
OptionsController
OptionsController,
StorageUtil
} from '@reown/appkit-core'
import { ErrorUtil } from '@reown/appkit-utils'

import { AppKit } from '../../src/client'
import { mainnet, sepolia, solana } from '../mocks/Networks'
import { mainnet, polygon, sepolia, solana } from '../mocks/Networks'
import { mockOptions } from '../mocks/Options'
import { mockBlockchainApiController, mockStorageUtil, mockWindowAndDocument } from '../test-utils'

Expand Down Expand Up @@ -88,6 +89,42 @@ describe('Base', () => {
bip122: 'ordinal'
})
})

it('should use default network prop when defaultNetwork prop is not included in the networks array', () => {
vi.spyOn(StorageUtil, 'getActiveCaipNetworkId').mockReturnValueOnce(undefined)
const setActiveCaipNetwork = vi.spyOn(ChainController, 'setActiveCaipNetwork')

new AppKit({
...mockOptions,
defaultNetwork: polygon
})

expect(setActiveCaipNetwork).toHaveBeenCalledWith(mainnet)
})

it('should use default network prop when there is no network in storage', () => {
vi.spyOn(StorageUtil, 'getActiveCaipNetworkId').mockReturnValueOnce(undefined)
const setActiveCaipNetwork = vi.spyOn(ChainController, 'setActiveCaipNetwork')

new AppKit({
...mockOptions,
defaultNetwork: sepolia
})

expect(setActiveCaipNetwork).toHaveBeenCalledWith(sepolia)
})

it('should not use default network prop when there is a network in storage', () => {
vi.spyOn(StorageUtil, 'getActiveCaipNetworkId').mockReturnValueOnce(sepolia.caipNetworkId)
const setActiveCaipNetwork = vi.spyOn(ChainController, 'setActiveCaipNetwork')

new AppKit({
...mockOptions,
defaultNetwork: polygon
})

expect(setActiveCaipNetwork).toHaveBeenCalledWith(sepolia)
})
})

describe('Alert Errors', () => {
Expand Down

0 comments on commit b264e3b

Please sign in to comment.