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: handle viem chains in wagmi adapter constructor #2955

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0520586
refactor: handle viem chains in wagmi adapter constructor
enesozturk Sep 27, 2024
7911f64
chore: add viem network images
enesozturk Sep 27, 2024
2ed8b25
chore: export viem chain types and update wagmi client accordingly
enesozturk Sep 30, 2024
61b3fdb
chore: update lock file
enesozturk Sep 30, 2024
98eb7a9
chore: save state
enesozturk Oct 1, 2024
48b4ab8
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 1, 2024
b4a21f6
chore: update lock file
enesozturk Oct 1, 2024
2ec036e
refactor: update CaipNetwork type and all related references
enesozturk Oct 3, 2024
c16c2f9
refactor: update lab and examples
enesozturk Oct 3, 2024
51dcd3a
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 3, 2024
dc64936
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 3, 2024
24c0b75
fix: unit tests
enesozturk Oct 3, 2024
b1b292f
chore: lint and type issues
enesozturk Oct 3, 2024
a06c31b
chore: tests
enesozturk Oct 3, 2024
fcd4cfc
chore: remove unexpected vite config ts file
enesozturk Oct 3, 2024
18c0a97
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 3, 2024
d2c095b
chore: update core deps for path exports and vue
enesozturk Oct 3, 2024
422e8da
chore: GH code review and lab issues
enesozturk Oct 3, 2024
6a5fcf9
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 3, 2024
ae9a796
chore: update lock file
enesozturk Oct 3, 2024
78540eb
chore: revert lab build script
enesozturk Oct 3, 2024
6b8d90f
fix: switch network issues
enesozturk Oct 3, 2024
eab8d45
chore: wagmi network images
enesozturk Oct 3, 2024
a535c36
chore: network types
enesozturk Oct 3, 2024
9e974a7
chore: update networks type definitions to pass at least one object a…
enesozturk Oct 3, 2024
08a3444
fix: siwe chains array param
enesozturk Oct 3, 2024
46140c0
fix: siwe switch network name
enesozturk Oct 3, 2024
2e9b1ea
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 3, 2024
b5616c0
chore: sample app build issues
enesozturk Oct 4, 2024
213d9e6
refactor: remove networks param from adapter contruct functions, upda…
enesozturk Oct 4, 2024
47d4100
refactor: code review improvements
enesozturk Oct 4, 2024
792f594
chore: add changeset file
enesozturk Oct 4, 2024
b3c686b
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 4, 2024
83e4d19
fix: solana swtich network chain id param
enesozturk Oct 4, 2024
329cb2f
fix: build issues
enesozturk Oct 4, 2024
f3aa17a
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 6, 2024
64c42e9
fix: type issues
enesozturk Oct 6, 2024
0fc1498
Merge branch 'main' into refactor/accept-viem-chains-for-wagmi-adapter
enesozturk Oct 6, 2024
d73524a
chore: lock file
enesozturk Oct 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function Ethers5TransactionTest() {
}
}

return Number(chainId) !== mainnet.chainId && address ? (
return chainId !== mainnet.id && address ? (
<Stack direction={['column', 'column', 'row']}>
<Button
data-testid="sign-transaction-button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export function Ethers5WriteContractTest() {
setLoading(false)
}
}
const allowedChains = [sepolia.chainId, optimism.chainId]
const allowedChains = [sepolia.id, optimism.id]

return allowedChains.includes(Number(chainId)) && address ? (
return allowedChains.find(chain => chain === chainId) && address ? (
<Stack direction={['column', 'column', 'row']}>
<Button
data-testid="sign-transaction-button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function EthersTransactionTest() {
}
}

return Number(chainId) !== mainnet.chainId && address ? (
return chainId !== mainnet.id && address ? (
<Stack direction={['column', 'column', 'row']}>
<Button
data-testid="sign-transaction-button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export function EthersWriteContractTest() {
setLoading(false)
}
}
const allowedChains = [sepolia.chainId, optimism.chainId]
const allowedChains = [sepolia.id, optimism.id]

return allowedChains.includes(Number(chainId)) && address ? (
return allowedChains.find(chain => chain === chainId) && address ? (
<Stack direction={['column', 'column', 'row']}>
<Button
data-testid="sign-transaction-button"
Expand Down
5 changes: 2 additions & 3 deletions apps/laboratory/src/components/Solana/SolanaTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function SolanaTests() {
</Heading>
<SolanaSignMessageTest />
</Box>
{caipNetwork?.chainId === solana.chainId && (
{caipNetwork?.id === solana.id && (
<Box>
<Text fontSize="md" color="yellow">
Please be aware that you are connected to the mainnet. Be careful with your
Expand Down Expand Up @@ -99,8 +99,7 @@ export function SolanaTests() {
<SolanaSignJupiterSwapTest />
</Box>

{(caipNetwork?.chainId === solanaTestnet.chainId ||
caipNetwork?.chainId === solanaDevnet.chainId) && (
{(caipNetwork?.id === solanaTestnet.id || caipNetwork?.id === solanaDevnet.id) && (
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this ok?

<Stack divider={<StackDivider />} spacing="4">
<Box>
<Heading size="xs" textTransform="uppercase" pb="2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function SolanaWriteContractTest() {
async function onIncrementCounter() {
setLoading(true)

const PROGRAM_ID = new PublicKey(detectProgramId(caipNetwork?.chainId?.toString() ?? ''))
const PROGRAM_ID = new PublicKey(detectProgramId(caipNetwork?.id?.toString() ?? ''))

try {
if (!walletProvider?.publicKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function UpaSolanaSignMessageTest() {
pubkey: address
}
}
const chain = caipNetwork?.id
const chain = caipNetwork?.id?.toString()
enesozturk marked this conversation as resolved.
Show resolved Hide resolved
await walletProvider.request(payload, chain)

toast({
Expand Down
6 changes: 5 additions & 1 deletion apps/laboratory/src/pages/library/wagmi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ const wagmiAdapter = new WagmiAdapter({

const modal = createAppKit({
adapters: [wagmiAdapter],
networks: ConstantsUtil.EvmNetworks,
networks: wagmiAdapter.caipNetworks,
projectId: ConstantsUtil.ProjectId,
chainImages: {
enesozturk marked this conversation as resolved.
Show resolved Hide resolved
787: 'https://imagedelivery.net/_aTEfDRm7z3tKgu9JhfeKA/59c89b74-8c5a-49e8-467b-607a2c0ac900/md',
4337: 'https://imagedelivery.net/_aTEfDRm7z3tKgu9JhfeKA/e8537239-736f-4db2-f941-07ff4eb7de00/md'
},
features: {
analytics: true,
email: true,
Expand Down
29 changes: 19 additions & 10 deletions apps/laboratory/src/utils/ConstantsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
solanaTestnet,
solanaDevnet,
base,
gnosis
gnosis,
type AppKitNetwork
} from '@reown/appkit/networks'
import { getLocalStorageItem } from './LocalStorage'
import type { CaipNetwork } from '@reown/appkit'
Expand Down Expand Up @@ -50,19 +51,27 @@ if (typeof window !== 'undefined') {

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

const EvmNetworks = [mainnet, optimism, polygon, zkSync, arbitrum, base, sepolia, gnosis]
const EvmNetworks = [mainnet, optimism, polygon, zkSync, arbitrum, base, sepolia, gnosis] as [
AppKitNetwork,
...AppKitNetwork[]
]

export const solanaNotExist = {
id: 'solana:chaindoesntexist',
chainId: 'chaindoesntexist',
id: 'chaindoesntexist',
caipNetworkId: 'solana:chaindoesntexist',
chainNamespace: 'solana',
name: 'Solana Unsupported',
currency: 'SOL',
explorerUrl: 'https://explorer.solana.com/?cluster=unsupported',
rpcUrl: 'https://api.unsupported.solana.com',
chainNamespace: 'solana'
nativeCurrency: { name: 'Solana', symbol: 'SOL', decimals: 9 },
blockExplorers: {
default: { name: 'Solscan', url: 'https://explorer.solana.com/?cluster=unsupported' }
},
rpcUrls: { default: { http: ['https://api.unsupported.solana.com'] } }
} as CaipNetwork

const SolanaNetworks = [solana, solanaTestnet, solanaDevnet, solanaNotExist]
const SolanaNetworks = [solana, solanaTestnet, solanaDevnet, solanaNotExist] as [
AppKitNetwork,
...AppKitNetwork[]
]

export const ConstantsUtil = {
SigningSucceededToastTitle: 'Signing Succeeded',
Expand Down Expand Up @@ -120,5 +129,5 @@ export const ConstantsUtil = {
ProjectId: projectId,
EvmNetworks,
SolanaNetworks,
AllNetworks: [...EvmNetworks, ...SolanaNetworks]
AllNetworks: [...EvmNetworks, ...SolanaNetworks] as [AppKitNetwork, ...AppKitNetwork[]]
}
4 changes: 2 additions & 2 deletions apps/laboratory/src/utils/EthersConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
aurora,
avalanche,
base,
binanceSmartChain,
bsc,
celo,
gnosis,
mainnet,
Expand All @@ -21,7 +21,7 @@ export const EthersConstants = {
arbitrum,
polygon,
avalanche,
binanceSmartChain,
bsc,
optimism,
gnosis,
zkSync,
Expand Down
2 changes: 1 addition & 1 deletion apps/laboratory/src/utils/SiweUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const siweConfig = createSIWEConfig({
getMessageParams: async () => ({
domain: window.location.host,
uri: window.location.origin,
chains: chains.map(chain => chain.chainId as number),
chains: chains.map(chain => chain.id as number),
statement: 'Please sign with your account',
iat: new Date().toISOString()
}),
Expand Down
4 changes: 2 additions & 2 deletions apps/laboratory/src/utils/SolanaConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export const SolanaConstantsUtil = {
chains: [solana, solanaTestnet, solanaDevnet],
programIds: [
{
chainId: solanaDevnet.chainId,
chainId: solanaDevnet.id,
programId: 'Cb5aXEgXptKqHHWLifvXu5BeAuVLjojQ5ypq6CfQj1hy'
},
{
chainId: solanaTestnet.chainId,
chainId: solanaTestnet.id,
programId: 'FZn4xQoKKvcxDADDRdqNAAPnVv9qYCbUTbP3y4Rn1BBr'
}
]
Expand Down
2 changes: 1 addition & 1 deletion apps/laboratory/tests/email.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ emailTest('it should reject sign', async () => {

emailTest('it should switch network and sign', async ({ library }) => {
let targetChain = library === 'solana' ? 'Solana Testnet' : 'Polygon'
let caipNetworkId = library === 'solana' ? solanaTestnet.id : polygon.id
let caipNetworkId: number | string = library === 'solana' ? solanaTestnet.id : polygon.id

await page.switchNetwork(targetChain)
await validator.expectSwitchedNetworkOnNetworksView(targetChain)
Expand Down
10 changes: 9 additions & 1 deletion apps/laboratory/tests/shared/validators/ModalValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export class ModalValidator {

async expectCaipAddressHaveCorrectNetworkId(caipNetworkId: CaipNetworkId) {
const address = this.page.getByTestId('appkit-caip-address')
await expect(address, 'Correct CAIP address should be present').toContainText(caipNetworkId)
await expect(address, 'Correct CAIP address should be present').toContainText(
caipNetworkId.toString()
)
}

async expectNetwork(network: string) {
Expand Down Expand Up @@ -237,6 +239,12 @@ export class ModalValidator {
throw new Error('Call status not confirmed')
}

async expectNetworkVisible(name: string) {
const network = this.page.getByTestId(`w3m-network-switch-${name}`)
await expect(network).toBeVisible()
await expect(network).toBeDisabled()
}

async expectNetworksDisabled(name: string) {
const disabledNetwork = this.page.getByTestId(`w3m-network-switch-${name}`)
await expect(disabledNetwork.locator('button')).toBeDisabled()
Expand Down
2 changes: 1 addition & 1 deletion apps/laboratory/tests/siwe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ siweWalletTest('it should be authenticated', async () => {
})

siweWalletTest('it should require re-authentication when switching networks', async () => {
await modalPage.switchNetwork('ZkSync')
await modalPage.switchNetwork('Polygon')
await modalValidator.expectUnauthenticated()
await modalPage.promptSiwe()
await walletPage.handleRequest({ accept: true })
Expand Down
4 changes: 2 additions & 2 deletions examples/next-wagmi-app-router/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cookieStorage, createStorage } from '@wagmi/core'
import { WagmiAdapter } from '@reown/appkit-adapter-wagmi'
import { mainnet, arbitrum } from '@reown/appkit/networks'
import { mainnet, arbitrum, type AppKitNetwork } from '@reown/appkit/networks'

// Get projectId from https://cloud.reown.com
export const projectId = process.env.NEXT_PUBLIC_PROJECT_ID
Expand All @@ -9,7 +9,7 @@ if (!projectId) {
throw new Error('Project ID is not defined')
}

export const networks = [mainnet, arbitrum]
export const networks = [mainnet, arbitrum] as [AppKitNetwork, ...AppKitNetwork[]]

// Set up the Wagmi Adapter (Config)
export const wagmiAdapter = new WagmiAdapter({
Expand Down
6 changes: 3 additions & 3 deletions examples/next-wagmi-app-router/src/context/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import { wagmiAdapter, projectId } from '@/config'
import { wagmiAdapter, projectId, networks } from '@/config'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createAppKit } from '@reown/appkit/react'
import { mainnet } from '@reown/appkit/networks'
import { mainnet, arbitrum, type AppKitNetwork } from '@reown/appkit/networks'
import React, { type ReactNode } from 'react'
import { cookieToInitialState, WagmiProvider, type Config } from 'wagmi'

Expand All @@ -26,7 +26,7 @@ const metadata = {
export const modal = createAppKit({
adapters: [wagmiAdapter],
projectId,
networks: wagmiAdapter.caipNetworks,
networks,
defaultNetwork: mainnet,
metadata: metadata,
themeMode: 'light',
Expand Down
4 changes: 2 additions & 2 deletions examples/next-wagmi-pages-router/src/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { State, WagmiProvider } from 'wagmi'

import { cookieStorage, createStorage } from '@wagmi/core'
import { WagmiAdapter } from '@reown/appkit-adapter-wagmi'
import { mainnet, arbitrum } from '@reown/appkit/networks'
import { mainnet, arbitrum, type AppKitNetwork } from '@reown/appkit/networks'
import { createAppKit } from '@reown/appkit/react'

// Setup queryClient
Expand All @@ -16,7 +16,7 @@ const projectId = process.env.NEXT_PUBLIC_PROJECT_ID

if (!projectId) throw new Error('Project ID is not defined')

export const networks = [mainnet, arbitrum]
export const networks = [mainnet, arbitrum] as [AppKitNetwork, ...AppKitNetwork[]]

export const wagmiAdapter = new WagmiAdapter({
storage: createStorage({
Expand Down
15 changes: 13 additions & 2 deletions examples/next-wagmi/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { cookieStorage, createStorage } from '@wagmi/core'
import { WagmiAdapter } from '@reown/appkit-adapter-wagmi'
import { mainnet, arbitrum, avalanche, base, optimism, polygon } from '@reown/appkit/networks'
import {
mainnet,
arbitrum,
avalanche,
base,
optimism,
polygon,
type AppKitNetwork
} from '@reown/appkit/networks'

export const projectId = process.env['NEXT_PUBLIC_PROJECT_ID']

if (!projectId) {
throw new Error('Project ID is not defined')
}

export const networks = [mainnet, arbitrum, avalanche, base, optimism, polygon]
export const networks = [mainnet, arbitrum, avalanche, base, optimism, polygon] as [
AppKitNetwork,
...AppKitNetwork[]
]

export const wagmiAdapter = new WagmiAdapter({
storage: createStorage({
Expand Down
4 changes: 2 additions & 2 deletions examples/react-wagmi/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useAppKitState,
useAppKitTheme
} from '@reown/appkit/react'
import { mainnet, polygon } from '@reown/appkit/networks'
import { mainnet, polygon, bsc } from '@reown/appkit/networks'
import { WagmiAdapter } from '@reown/appkit-adapter-wagmi'
import { WagmiProvider } from 'wagmi'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
Expand All @@ -23,7 +23,7 @@ if (!projectId) {
// 2. Setup wagmi adapter
const wagmiAdapter = new WagmiAdapter({
projectId,
networks: [mainnet, polygon]
networks: [mainnet, polygon, bsc]
})

// 3. Create modal
Expand Down
Loading
Loading