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: approved caip networks data for universal provider #2872

Merged
merged 32 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
349759b
fix: get correct data from session namespaces
zoruka Sep 18, 2024
cc38934
chore: add changeset
zoruka Sep 18, 2024
6466d87
chore: use intermediate variable
tomiir Sep 18, 2024
6e03d9e
Merge branch 'main' into fix/approved-caip-networks-data-upa
zoruka Sep 19, 2024
89addff
Merge branch 'main' into fix/approved-caip-networks-data-upa
tomiir Sep 19, 2024
90642b9
chore: add changeset
zoruka Sep 19, 2024
9369844
Merge branch 'main' into fix/approved-caip-networks-data-upa
zoruka Sep 19, 2024
f26bd70
Merge branch 'main' into fix/approved-caip-networks-data-upa
tomiir Sep 19, 2024
f8dde80
chore: remove only and skip
tomiir Sep 19, 2024
db909f9
chore: re-disable test
tomiir Sep 19, 2024
a6b371e
fix: WC Connector not available
tomiir Sep 19, 2024
36f395b
fix: all wallets not being displayed
tomiir Sep 19, 2024
d87ded3
chore: always get walletconnect connector by id
tomiir Sep 19, 2024
2b9b166
chore: fix test behavior
tomiir Sep 19, 2024
959d0fe
fix: set network for default network when not setted
zoruka Sep 19, 2024
9b0c8fa
Merge branch 'fix/approved-caip-networks-data-upa' of github.com:Wall…
zoruka Sep 19, 2024
cf4985b
Merge branch 'main' into fix/approved-caip-networks-data-upa
tomiir Sep 20, 2024
5750208
Merge branch 'main' of github.com:WalletConnect/web3modal into fix/ap…
zoruka Sep 20, 2024
59c51b7
Merge branch 'fix/approved-caip-networks-data-upa' of github.com:Wall…
zoruka Sep 20, 2024
4b1057c
Merge branch 'main' into fix/approved-caip-networks-data-upa
zoruka Sep 23, 2024
aff519e
Merge branch 'main' of github.com:WalletConnect/web3modal into fix/ap…
zoruka Sep 24, 2024
a0070ec
fix: attempt to fix the switch network logic
zoruka Sep 24, 2024
4fbc273
test: reenable tests
zoruka Sep 24, 2024
fd8c176
Merge branch 'main' of github.com:WalletConnect/web3modal into fix/ap…
zoruka Sep 25, 2024
0550022
test: fix networn disabled tests
zoruka Sep 25, 2024
0a5cef4
fix: attempt to fix network switch problems
zoruka Sep 25, 2024
ea845d7
Merge branch 'main' into fix/approved-caip-networks-data-upa
zoruka Sep 26, 2024
0171b9c
fix: ts issue
zoruka Sep 26, 2024
dac4168
test: fix for solana networks
zoruka Sep 26, 2024
8981fe0
fix: switch network when using auth
zoruka Sep 26, 2024
d64fac6
test: readd solana test for disabled network
zoruka Sep 26, 2024
db27a61
Merge branch 'main' into fix/approved-caip-networks-data-upa
zoruka Sep 26, 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/blue-pens-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': 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-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-polkadot': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': 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
'@reown/appkit-wallet': patch
---

Get correct data from session namespaces and set it for approved caip networks data on UniversalProvider usage"
38 changes: 38 additions & 0 deletions .changeset/fluffy-crabs-bathe.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-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': 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-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': 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
---

Implement the correct logic for getting available networks when using universal provider
5 changes: 3 additions & 2 deletions apps/laboratory/src/utils/ConstantsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
solana,
solanaTestnet,
solanaDevnet,
base
base,
gnosis
} from '@reown/appkit/networks'
import { getLocalStorageItem } from './LocalStorage'

Expand Down Expand Up @@ -48,7 +49,7 @@ if (typeof window !== 'undefined') {

const customWallet = storedCustomWallet ? [JSON.parse(storedCustomWallet)] : []

const EvmNetworks = [mainnet, optimism, polygon, zkSync, arbitrum, base, sepolia]
const EvmNetworks = [mainnet, optimism, polygon, zkSync, arbitrum, base, sepolia, gnosis]
const SolanaNetworks = [solana, solanaTestnet, solanaDevnet]

export const ConstantsUtil = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ test('it should fetch balance as expected', async () => {
await modalValidator.expectBalanceFetched('ETH')
})

test.skip('it should show disabled networks', async () => {
const disabledNetworks = 'Arbitrum'
test('it should show disabled networks', async () => {
const disabledNetworks = 'Gnosis'

await modalPage.openModal()
await modalPage.openNetworks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ test('it should fetch balance as expected', async () => {
await modalValidator.expectBalanceFetched('ETH')
})

test.skip('it should show disabled networks', async () => {
const disabledNetworks = 'Arbitrum'
test('it should show disabled networks', async () => {
const disabledNetworks = 'Gnosis'

await modalPage.openModal()
await modalPage.openNetworks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ test('it should fetch balance as expected', async () => {
await modalValidator.expectBalanceFetched('ETH')
})

test.skip('it should show disabled networks', async () => {
const disabledNetworks = 'Arbitrum'
test('it should show disabled networks', async () => {
const disabledNetworks = 'Gnosis'

await modalPage.openModal()
await modalPage.openNetworks()
Expand All @@ -58,7 +58,6 @@ test('it should switch networks and sign', async () => {

const chainName = chains[index] ?? DEFAULT_CHAIN_NAME
await modalPage.switchNetwork(chainName)
await modalValidator.expectSwitchedNetwork(chainName)
await modalPage.closeModal()

// -- Sign ------------------------------------------------------------------
Expand All @@ -78,7 +77,6 @@ test('it should switch between multiple accounts', async () => {
const chainName = 'Ethereum'
await modalPage.switchNetwork(chainName)
await modalPage.page.waitForTimeout(500)
await modalValidator.expectSwitchedNetwork(chainName)
await modalPage.closeModal()
const originalAddress = await modalPage.getAddress()
await modalPage.openAccount()
Expand Down
1 change: 1 addition & 0 deletions apps/laboratory/tests/siwe-sa.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ smartAccountSiweTest.beforeAll(async ({ browser, library }) => {
// Switch to a SA enabled network
await validator.expectDisconnected()
await page.switchNetworkWithNetworkButton('Polygon')
await validator.expectSwitchedNetworkOnNetworksView('Polygon')
await page.closeModal()
const tempEmail = await email.getEmailAddressToUse()
await page.emailFlow(tempEmail, context, mailsacApiKey)
Expand Down
2 changes: 2 additions & 0 deletions apps/laboratory/tests/smart-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ smartAccountTest.beforeAll(async ({ browser, library }) => {

// Switch to a SA enabled network
await page.switchNetworkWithNetworkButton('Polygon')
await validator.expectSwitchedNetworkOnNetworksView('Polygon')
await page.closeModal()

const tempEmail = await email.getEmailAddressToUse()
await page.emailFlow(tempEmail, context, mailsacApiKey)

Expand Down
9 changes: 7 additions & 2 deletions apps/laboratory/tests/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ sampleWalletTest('it should fetch balance as expected', async ({ library }) => {
await modalValidator.expectBalanceFetched(library === 'solana' ? 'SOL' : 'ETH')
})

sampleWalletTest.skip('it should show disabled networks', async ({ library }) => {
const disabledNetworks = library === 'solana' ? 'Solana Unsupported' : 'Arbitrum'
sampleWalletTest('it should show disabled networks', async ({ library }) => {
if (library === 'solana') {
// Solana doesn't have disabled networks
return
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a solanaUnsupported chain do we not?


const disabledNetworks = 'Gnosis'

await modalPage.openModal()
await modalPage.openNetworks()
Expand Down
79 changes: 32 additions & 47 deletions packages/adapters/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
NetworkControllerClient,
Connector
} from '@reown/appkit-core'
import type { AdapterType, CaipAddress, CaipNetwork, CaipNetworkId } from '@reown/appkit-common'
import type { AdapterType, CaipAddress, CaipNetwork } from '@reown/appkit-common'
import type { ChainNamespace } from '@reown/appkit-common'

import { watchStandard } from './utils/watchStandard.js'
Expand All @@ -43,7 +43,7 @@ import type { AppKit } from '@reown/appkit'
import type { AppKitOptions as CoreOptions } from '@reown/appkit'
import { ProviderUtil } from '@reown/appkit/store'
import { W3mFrameProviderSingleton } from '@reown/appkit/auth-provider'
import { ConstantsUtil, PresetsUtil } from '@reown/appkit-utils'
import { ConstantsUtil } from '@reown/appkit-utils'
import { createSendTransaction } from './utils/createSendTransaction.js'
import { CoinbaseWalletProvider } from './providers/CoinbaseWalletProvider.js'

Expand Down Expand Up @@ -153,34 +153,19 @@ export class SolanaAdapter implements ChainAdapter {
}
},

getApprovedCaipNetworksData: async () =>
new Promise(resolve => {
const walletId = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID)

if (!walletId) {
throw new Error('No wallet id found to get approved networks data')
}

const providerConfigs = {
[ConstantsUtil.AUTH_CONNECTOR_ID]: {
supportsAllNetworks: true,
approvedCaipNetworkIds: PresetsUtil.WalletConnectRpcChainIds.map(
id => `${ConstantsUtil.EIP155}:${id}`
) as CaipNetworkId[]
}
}

const networkData = providerConfigs[walletId as unknown as keyof typeof providerConfigs]
getApprovedCaipNetworksData: async () => {
if (this.provider) {
return Promise.resolve({
supportsAllNetworks: false,
approvedCaipNetworkIds: this.provider.chains.map(chain => chain.id)
})
}

if (networkData) {
resolve(networkData)
} else {
resolve({
supportsAllNetworks: true,
approvedCaipNetworkIds: []
})
}
return Promise.resolve({
supportsAllNetworks: false,
approvedCaipNetworkIds: []
})
}
}

this.connectionControllerClient = {
Expand Down Expand Up @@ -376,10 +361,7 @@ export class SolanaAdapter implements ChainAdapter {
if (address && caipNetwork) {
SolStoreUtil.setConnection(new Connection(caipNetwork.rpcUrl, this.connectionSettings))
this.appKit?.setAllAccounts([{ address, type: 'eoa' }], this.chainNamespace)
this.appKit?.setCaipAddress(
`${caipNetworkId as CaipNetworkId}:${address}`,
this.chainNamespace
)
this.appKit?.setCaipAddress(`${caipNetworkId}:${address}` as CaipAddress, this.chainNamespace)
await this.syncNetwork(address)
} else {
this.appKit?.resetWcConnection()
Expand All @@ -398,7 +380,7 @@ export class SolanaAdapter implements ChainAdapter {
}

if (!this.appKit?.getCaipNetwork()) {
throw new Error('CaipNetwork is not set')
this.appKit?.setCaipNetwork(this.defaultCaipNetwork)
}

const balance =
Expand Down Expand Up @@ -480,26 +462,29 @@ export class SolanaAdapter implements ChainAdapter {
this.appKit?.setLoading(true)
const address = await provider.connect()
const caipChainId = SafeLocalStorage.getItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK_ID)
let connectionChain: CaipNetwork | undefined = undefined

const activeCaipNetwork = this.appKit?.getCaipNetwork()

// eslint-disable-next-line no-nested-ternary
connectionChain = caipChainId
? SolHelpersUtil.getChainFromCaip(this.caipNetworks, caipChainId)
: activeCaipNetwork?.chainNamespace === 'solana'
? this.caipNetworks.find(chain => chain.chainNamespace === 'solana')
: activeCaipNetwork || this.caipNetworks.find(chain => chain.chainNamespace === 'solana')
const connectionChain =
provider.chains.find(chain => chain.id === caipChainId) || provider.chains[0]

if (connectionChain) {
const caipAddress = `solana:${connectionChain.chainId}:${address}` as CaipAddress
const caipAddress = `solana:${connectionChain.chainId}:${address}` as const
this.appKit?.setCaipAddress(caipAddress, this.chainNamespace)

await this.switchNetwork(connectionChain)

ProviderUtil.setProvider(this.chainNamespace, provider)
this.provider = provider
ProviderUtil.setProviderId(this.chainNamespace, 'walletConnect')

switch (provider.type) {
case 'WALLET_CONNECT':
ProviderUtil.setProviderId(this.chainNamespace, 'walletConnect')
break
case 'AUTH':
ProviderUtil.setProviderId(this.chainNamespace, 'w3mAuth')
break
default:
ProviderUtil.setProviderId(this.chainNamespace, 'injected')
}

SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_ID, provider.name)

Expand Down Expand Up @@ -610,6 +595,8 @@ export class SolanaAdapter implements ChainAdapter {
getActiveChain: () => this.appKit?.getCaipNetwork()
})

this.addProvider(walletConnectProvider)

return walletConnectProvider
}

Expand Down Expand Up @@ -671,9 +658,7 @@ export class SolanaAdapter implements ChainAdapter {

for (const provider of providers) {
this.availableProviders = this.availableProviders.filter(p => p.name !== provider.name)
if (provider.type !== 'WALLET_CONNECT') {
this.availableProviders.push(provider)
}
this.availableProviders.push(provider)
tomiir marked this conversation as resolved.
Show resolved Hide resolved

if (provider.name === activeProviderName && isSolana) {
this.setProvider(provider)
Expand Down
16 changes: 2 additions & 14 deletions packages/adapters/solana/src/providers/WalletConnectProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { isVersionedTransaction } from '@solana/wallet-adapter-base'
import type { CaipNetwork, ChainId } from '@reown/appkit-common'
import { withSolanaNamespace } from '../utils/withSolanaNamespace.js'
import { WcHelpersUtil } from '@reown/appkit'
import { WalletConnectMethodNotSupportedError } from './shared/Errors.js'

export type WalletConnectProviderConfig = {
Expand Down Expand Up @@ -261,20 +262,7 @@ export class WalletConnectProvider extends ProviderEventEmitter implements Provi
}

private get sessionChains() {
const solanaNamespace = this.session?.namespaces['solana']

if (!solanaNamespace) {
return []
}

const chains = solanaNamespace.chains || []
const accountsChains = solanaNamespace.accounts.map(account => {
const [chainNamespace, chainId] = account.split(':')

return `${chainNamespace}:${chainId}`
})

return Array.from(new Set([...chains, ...accountsChains]))
return WcHelpersUtil.getChainsFromNamespaces(this.session?.namespaces)
}

private serializeTransaction(transaction: AnyTransaction) {
Expand Down
12 changes: 9 additions & 3 deletions packages/adapters/solana/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ describe('SolanaAdapter', () => {
const mockProvider = {
connect: vi.fn().mockResolvedValue('DjPi1LtwrXJMAh2AUvuUMajCpMJEKg8N1J1PbLGjCH5B'),
name: 'MockProvider',
on: vi.fn()
on: vi.fn(),
chains: [{ chainId: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' }]
}
vi.spyOn(SafeLocalStorage, 'getItem').mockReturnValue(
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
Expand All @@ -164,11 +165,16 @@ describe('SolanaAdapter', () => {
'solana'
)
expect(ProviderUtil.setProvider).toHaveBeenCalledWith('solana', mockProvider)
expect(ProviderUtil.setProviderId).toHaveBeenCalledWith('solana', 'walletConnect')
expect(ProviderUtil.setProviderId).toHaveBeenCalledWith('solana', 'injected')
})

it('should add provider', () => {
const mockProvider = { name: 'MockProvider', type: 'INJECTED', icon: 'mock-icon' }
const mockProvider = {
name: 'MockProvider',
type: 'INJECTED',
icon: 'mock-icon',
chains: [{ chainId: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' }]
}
client['addProvider'](mockProvider as any)

expect(client['availableProviders']).toContain(mockProvider)
Expand Down
Loading
Loading