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

refactor: local storage keys to appkit + network switch fixes #2861

Merged
merged 22 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
eea9b15
refactor: update local storage keys
enesozturk Sep 17, 2024
920ba16
chore: format files
enesozturk Sep 17, 2024
66a7bc6
chore: revert storage util
enesozturk Sep 17, 2024
3730a42
chore: linter and test issues
enesozturk Sep 17, 2024
67065e8
chore: add changeset file
enesozturk Sep 17, 2024
a288e87
Trigger workflow
enesozturk Sep 17, 2024
bb68d9c
refactor: update local storage get and sets
enesozturk Sep 18, 2024
499dcd2
fix: build and test issues
enesozturk Sep 18, 2024
69ca8d2
chore: type issues
enesozturk Sep 18, 2024
7ceb8eb
Merge branch 'main' of github.com:WalletConnect/web3modal into refact…
tomiir Sep 18, 2024
61981a1
chore: always stringify value. Fix test
tomiir Sep 18, 2024
874a621
chore: remove unused var
tomiir Sep 18, 2024
ccc5ebd
chore: close toast when rejecting message
tomiir Sep 18, 2024
bf7aefb
chore: use SafeLocalStorage on StorageUtil. Adjust tests
tomiir Sep 18, 2024
80659df
Merge branch 'main' into refactor/local-storage-keys-to-appkit-2
tomiir Sep 19, 2024
97b643e
chore: removeo nly
tomiir Sep 19, 2024
00b6aa7
Merge branch 'main' into refactor/local-storage-keys-to-appkit-2
tomiir Sep 19, 2024
d51e6da
Merge branch 'main' of github.com:WalletConnect/web3modal into refact…
tomiir Sep 19, 2024
169e9a9
fix: do not instantiate public key if namespace is not solana
tomiir Sep 19, 2024
a26c6d7
Merge branch 'refactor/local-storage-keys-to-appkit-2' of github.com:…
tomiir Sep 19, 2024
162d3db
fix: switch network flow
tomiir Sep 19, 2024
1eb7feb
chore: remove log
tomiir Sep 19, 2024
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
38 changes: 38 additions & 0 deletions .changeset/wise-bobcats-hang.md
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-utils': patch
'@reown/appkit': patch
'@reown/appkit-common': patch
'@reown/appkit-wallet': patch
'@reown/appkit-core': patch
'@apps/demo': patch
'@apps/gallery': patch
'@apps/laboratory': 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-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-cdn': patch
'@reown/appkit-ethers': patch
'@reown/appkit-ethers5': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-solana': patch
'@reown/appkit-ui': patch
'@reown/appkit-wagmi': patch
---

Updates the localstorage keys
23 changes: 5 additions & 18 deletions apps/laboratory/src/pages/library/multichain-wagmi-solana.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@ import { ConstantsUtil } from '../../utils/ConstantsUtil'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { WagmiProvider } from 'wagmi'
import {
arbitrum,
mainnet,
polygon,
base,
binanceSmartChain,
solana,
solanaTestnet,
solanaDevnet,
arbitrum,
optimism,
zkSync,
sepolia
solanaTestnet,
solanaDevnet
} from '@reown/appkit/networks'
import { AppKitButtons } from '../../components/AppKitButtons'
import { HuobiWalletAdapter, SolflareWalletAdapter } from '@solana/wallet-adapter-wallets'
import { MultiChainTestsWagmiSolana } from '../../components/MultiChainTestsWagmiSolana'

const queryClient = new QueryClient()

const networks = [mainnet, optimism, polygon, zkSync, arbitrum, sepolia]
const networks = [mainnet, polygon, solana, arbitrum, optimism, solanaTestnet, solanaDevnet]

const wagmiAdapter = new WagmiAdapter({
ssr: true,
Expand All @@ -39,16 +35,7 @@ const solanaWeb3JsAdapter = new SolanaAdapter({

const modal = createAppKit({
adapters: [wagmiAdapter, solanaWeb3JsAdapter],
networks: [
mainnet,
polygon,
base,
binanceSmartChain,
arbitrum,
solana,
solanaTestnet,
solanaDevnet
],
networks,
defaultNetwork: mainnet,
projectId: ConstantsUtil.ProjectId,
features: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test.skip('it should show disabled networks', async () => {
await modalPage.closeModal()
})

test('it should switch networks and sign', async () => {
test.only('it should switch networks and sign', async () => {
const chains = ['Polygon', 'Solana']

async function processChain(index: number) {
Expand Down
2 changes: 1 addition & 1 deletion apps/laboratory/tests/shared/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export async function expectConnection(
await modalValidator.expectConnected()
await walletValidator.expectConnected()
await modalValidator.page.evaluate(
`window.localStorage.setItem('WALLETCONNECT_DEEPLINK_CHOICE', '')`
`window.localStorage.setItem('@appkit/deeplink_choice', JSON.stringify({ href: '', name: '' }))`
)
}
4 changes: 4 additions & 0 deletions apps/laboratory/tests/shared/validators/ModalValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export class ModalValidator {
async expectRejectedSign() {
// We use Chakra Toast and it's not quite straightforward to set the `data-testid` attribute on the toast element.
await expect(this.page.getByText(ConstantsUtil.SigningFailedToastTitle)).toBeVisible()
const closeButton = this.page.locator('#toast-close-button')

await expect(closeButton).toBeVisible()
await closeButton.click()
}

async expectSwitchedNetwork(network: string) {
Expand Down
30 changes: 18 additions & 12 deletions packages/adapters/ethers/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { EthersHelpersUtil, type ProviderId, type ProviderType } from '@reown/ap
import { ConstantsUtil } from '@reown/appkit-utils'
import { arbitrum, mainnet, polygon } from '@reown/appkit/networks'
import { ProviderUtil } from '@reown/appkit/store'
import { SafeLocalStorage } from '@reown/appkit-common'
import { WcConstantsUtil, type BlockchainApiLookupEnsName } from '@reown/appkit'
import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common'
import { type BlockchainApiLookupEnsName } from '@reown/appkit'
import { InfuraProvider, JsonRpcProvider } from 'ethers'

import type { CaipNetwork, ChainNamespace } from '@reown/appkit-common'
Expand Down Expand Up @@ -519,8 +519,14 @@ describe('EthersAdapter', () => {
const mockProvider = { request: vi.fn() }
await client['setProvider'](mockProvider as any, 'injected', 'MetaMask')

expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID, 'injected')
expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_NAME, 'MetaMask')
expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(
SafeLocalStorageKeys.WALLET_ID,
'injected'
)
expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(
SafeLocalStorageKeys.WALLET_NAME,
'MetaMask'
)
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled()
expect(mockAppKit.setCaipAddress).toHaveBeenCalled()
expect(ProviderUtil.setProviderId).toHaveBeenCalledWith('eip155', 'injected')
Expand Down Expand Up @@ -557,7 +563,7 @@ describe('EthersAdapter', () => {
)[1]
await disconnectHandler()

expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
expect(mockProvider.removeListener).toHaveBeenCalledTimes(3)
})

Expand Down Expand Up @@ -595,9 +601,9 @@ describe('EthersAdapter', () => {
}

vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation(key => {
if (key === WcConstantsUtil.WALLET_ID) return ConstantsUtil.INJECTED_CONNECTOR_ID
if (key === WcConstantsUtil.WALLET_NAME) return 'MetaMask'
return null
if (key === SafeLocalStorageKeys.WALLET_ID) return ConstantsUtil.INJECTED_CONNECTOR_ID
if (key === SafeLocalStorageKeys.WALLET_NAME) return 'MetaMask'
return undefined
})

vi.spyOn(client as any, 'setProvider').mockImplementation(() => Promise.resolve())
Expand All @@ -613,7 +619,7 @@ describe('EthersAdapter', () => {

client['checkActiveProviders'](mockConfig as ProviderType)

expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
expect(client['setProvider']).toHaveBeenCalledWith(
mockInjectedProvider,
ConstantsUtil.INJECTED_CONNECTOR_ID
Expand All @@ -625,7 +631,7 @@ describe('EthersAdapter', () => {
})

it('should not set provider when wallet ID is not found', () => {
vi.spyOn(SafeLocalStorage, 'getItem').mockReturnValue(null)
vi.spyOn(SafeLocalStorage, 'getItem').mockReturnValue(undefined)

const mockConfig = {
injected: mockInjectedProvider,
Expand All @@ -635,7 +641,7 @@ describe('EthersAdapter', () => {

client['checkActiveProviders'](mockConfig as ProviderType)

expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
expect(client['setProvider']).not.toHaveBeenCalled()
expect(client['setupProviderListeners']).not.toHaveBeenCalled()
})
Expand All @@ -649,7 +655,7 @@ describe('EthersAdapter', () => {

client['checkActiveProviders'](mockConfig as ProviderType)

expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
expect(client['setProvider']).not.toHaveBeenCalled()
expect(client['setupProviderListeners']).not.toHaveBeenCalled()
})
Expand Down
30 changes: 18 additions & 12 deletions packages/adapters/ethers5/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { EthersHelpersUtil, type ProviderId, type ProviderType } from '@reown/ap
import { ConstantsUtil } from '@reown/appkit-utils'
import { arbitrum, mainnet, polygon } from '@reown/appkit/networks'
import { ProviderUtil } from '@reown/appkit/store'
import { SafeLocalStorage } from '@reown/appkit-common'
import { WcConstantsUtil, type BlockchainApiLookupEnsName } from '@reown/appkit'
import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common'
import { type BlockchainApiLookupEnsName } from '@reown/appkit'
import { ethers } from 'ethers5'
import type { CaipNetwork, ChainNamespace } from '@reown/appkit-common'

Expand Down Expand Up @@ -524,8 +524,14 @@ describe('EthersAdapter', () => {
const mockProvider = { request: vi.fn() }
await client['setProvider'](mockProvider as any, 'injected', 'MetaMask')

expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID, 'injected')
expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_NAME, 'MetaMask')
expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(
SafeLocalStorageKeys.WALLET_ID,
'injected'
)
expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(
SafeLocalStorageKeys.WALLET_NAME,
'MetaMask'
)
expect(mockAppKit.setCaipNetwork).toHaveBeenCalled()
expect(ProviderUtil.setProviderId).toHaveBeenCalledWith('eip155', 'injected')
expect(ProviderUtil.setProvider).toHaveBeenCalledWith('eip155', mockProvider)
Expand Down Expand Up @@ -561,7 +567,7 @@ describe('EthersAdapter', () => {
)[1]
await disconnectHandler()

expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
expect(mockProvider.removeListener).toHaveBeenCalledTimes(3)
})

Expand Down Expand Up @@ -599,9 +605,9 @@ describe('EthersAdapter', () => {
}

vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation(key => {
if (key === WcConstantsUtil.WALLET_ID) return ConstantsUtil.INJECTED_CONNECTOR_ID
if (key === WcConstantsUtil.WALLET_NAME) return 'MetaMask'
return null
if (key === SafeLocalStorageKeys.WALLET_ID) return ConstantsUtil.INJECTED_CONNECTOR_ID
if (key === SafeLocalStorageKeys.WALLET_NAME) return 'MetaMask'
return undefined
})

vi.spyOn(client as any, 'setProvider').mockImplementation(() => Promise.resolve())
Expand All @@ -617,7 +623,7 @@ describe('EthersAdapter', () => {

client['checkActiveProviders'](mockConfig as ProviderType)

expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
expect(client['setProvider']).toHaveBeenCalledWith(
mockInjectedProvider,
ConstantsUtil.INJECTED_CONNECTOR_ID
Expand All @@ -629,7 +635,7 @@ describe('EthersAdapter', () => {
})

it('should not set provider when wallet ID is not found', () => {
vi.spyOn(SafeLocalStorage, 'getItem').mockReturnValue(null)
vi.spyOn(SafeLocalStorage, 'getItem').mockReturnValue(undefined)

const mockConfig = {
injected: mockInjectedProvider,
Expand All @@ -639,7 +645,7 @@ describe('EthersAdapter', () => {

client['checkActiveProviders'](mockConfig as ProviderType)

expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
expect(client['setProvider']).not.toHaveBeenCalled()
expect(client['setupProviderListeners']).not.toHaveBeenCalled()
})
Expand All @@ -653,7 +659,7 @@ describe('EthersAdapter', () => {

client['checkActiveProviders'](mockConfig as ProviderType)

expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
expect(client['setProvider']).not.toHaveBeenCalled()
expect(client['setupProviderListeners']).not.toHaveBeenCalled()
})
Expand Down
6 changes: 1 addition & 5 deletions packages/adapters/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,10 @@ export class SolanaAdapter implements ChainAdapter {
this.networkControllerClient = {
switchCaipNetwork: async caipNetwork => {
if (caipNetwork) {
SafeLocalStorage.setItem(
SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK,
JSON.stringify(caipNetwork)
)
tomiir marked this conversation as resolved.
Show resolved Hide resolved
try {
await this.switchNetwork(caipNetwork)
} catch (error) {
// SolStoreUtil.setError(error)
console.warn('Error switching network', error)
}
}
},
Expand Down
7 changes: 4 additions & 3 deletions packages/adapters/solana/src/providers/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export class AuthProvider extends ProviderEventEmitter implements Provider, Prov

get publicKey(): PublicKey | undefined {
const session = this.getSession()

if (session) {
const namespace = this.getActiveNamespace()
if (session && namespace === 'solana') {
return new PublicKey(session.address)
}

Expand Down Expand Up @@ -201,7 +201,8 @@ export class AuthProvider extends ProviderEventEmitter implements Provider, Prov
required?: Required
): Required extends true ? PublicKey : PublicKey | undefined {
const session = this.getSession()
if (!session) {
const namespace = this.getActiveNamespace()
if (!session || namespace !== 'solana') {
if (required) {
throw new Error('Account is required')
}
Expand Down
6 changes: 1 addition & 5 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,6 @@ export class WagmiAdapter implements ChainAdapter {

this.networkControllerClient = {
switchCaipNetwork: async caipNetwork => {
SafeLocalStorage.setItem(
SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK,
JSON.stringify(caipNetwork)
)
tomiir marked this conversation as resolved.
Show resolved Hide resolved
const chainId = Number(NetworkUtil.caipNetworkIdToNumber(caipNetwork?.id))

if (chainId && this.wagmiConfig) {
Expand Down Expand Up @@ -607,7 +603,7 @@ export class WagmiAdapter implements ChainAdapter {
>) {
const isConnected = ChainController.state.activeCaipAddress

if (status === 'disconnected' && isConnected) {
if (status === 'disconnected' && !isConnected) {
this.appKit?.resetAccount(this.chainNamespace)
this.appKit?.resetWcConnection()
this.appKit?.resetNetwork()
Expand Down
9 changes: 3 additions & 6 deletions packages/adapters/wagmi/src/connectors/UniversalConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { WcHelpersUtil } from '@reown/appkit'
import type { AppKitOptions } from '@reown/appkit'
import type { AppKit } from '@reown/appkit'
import { convertToAppKitChains } from '../utils/helpers.js'
import { SafeLocalStorage, SafeLocalStorageKeys, type CaipNetwork } from '@reown/appkit-common'
import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common'

type UniversalConnector = Connector & {
onDisplayUri(uri: string): void
Expand Down Expand Up @@ -232,11 +232,8 @@ export function walletConnect(parameters: AppKitOptionsParams, appKit: AppKit) {

if (chainId && currentChainId !== chainId) {
const storedCaipNetwork = SafeLocalStorage.getItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK)
if (storedCaipNetwork) {
const parsedCaipNetwork = JSON.parse(storedCaipNetwork) as CaipNetwork
if (parsedCaipNetwork.chainNamespace === 'eip155') {
await this.switchChain?.({ chainId: Number(parsedCaipNetwork.chainId) })
}
if (storedCaipNetwork && storedCaipNetwork.chainNamespace === 'eip155') {
await this.switchChain?.({ chainId: Number(storedCaipNetwork.chainId) })
} else {
await this.switchChain?.({ chainId })
}
Expand Down
1 change: 0 additions & 1 deletion packages/appkit-utils/src/ethers/EthersConstantsUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const EthersConstantsUtil = {
WALLET_ID: '@w3m/wallet_id',
ERROR_CODE_UNRECOGNIZED_CHAIN_ID: 4902,
ERROR_CODE_DEFAULT: 5000
} as const
2 changes: 0 additions & 2 deletions packages/appkit-utils/src/solana/SolanaConstantsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export const SolConstantsUtil = {
/**
* Mainnet program ID
*/
WALLET_ID: '@w3m/solana_wallet',
CAIP_CHAIN_ID: '@w3m/solana_caip_chain',
ERROR_CODE_UNRECOGNIZED_CHAIN_ID: 4902,
ERROR_CODE_DEFAULT: 5000,
DEFAULT_CHAIN: {
Expand Down
Loading
Loading