Skip to content

Commit 830c08d

Browse files
authored
Add support for non-viem, custom Sequence chains (#882)
1 parent 46bc602 commit 830c08d

1 file changed

Lines changed: 48 additions & 5 deletions

File tree

  • packages/wallet/core/src/relayer/standard/rpc

packages/wallet/core/src/relayer/standard/rpc/index.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from './relayer.gen.js'
88
import { FeeOption, FeeQuote, OperationStatus, Relayer } from '../../relayer.js'
99
import { Address, Hex, Bytes, AbiFunction } from 'ox'
10-
import { Constants, Payload } from '@0xsequence/wallet-primitives'
10+
import { Constants, Payload, Network } from '@0xsequence/wallet-primitives'
1111
import { ETHTxnStatus, FeeToken as RpcFeeToken } from './relayer.gen.js'
1212
import { decodePrecondition } from '../../../preconditions/index.js'
1313
import {
@@ -25,12 +25,55 @@ export * from './relayer.gen.js'
2525

2626
export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>
2727

28+
/**
29+
* Convert a Sequence Network to a viem Chain
30+
*/
31+
const networkToChain = (network: Network.Network): Chain => {
32+
return {
33+
id: network.chainId,
34+
name: network.title || network.name,
35+
nativeCurrency: {
36+
name: network.nativeCurrency.name,
37+
symbol: network.nativeCurrency.symbol,
38+
decimals: network.nativeCurrency.decimals,
39+
},
40+
rpcUrls: {
41+
default: {
42+
http: [network.rpcUrl],
43+
},
44+
},
45+
blockExplorers: network.blockExplorer
46+
? {
47+
default: {
48+
name: network.blockExplorer.name || 'Explorer',
49+
url: network.blockExplorer.url,
50+
},
51+
}
52+
: undefined,
53+
contracts: network.ensAddress
54+
? {
55+
ensUniversalResolver: {
56+
address: network.ensAddress as `0x${string}`,
57+
},
58+
}
59+
: undefined,
60+
} as Chain
61+
}
62+
2863
export const getChain = (chainId: number): Chain => {
29-
const chain = Object.values(chains).find((c: any) => typeof c === 'object' && 'id' in c && c.id === chainId)
30-
if (!chain) {
31-
throw new Error(`Chain with id ${chainId} not found`)
64+
// First try to get the chain from Sequence's network configurations
65+
const sequenceNetwork = Network.getNetworkFromChainId(chainId)
66+
if (sequenceNetwork) {
67+
return networkToChain(sequenceNetwork)
3268
}
33-
return chain as Chain
69+
70+
// Fall back to viem's built-in chains
71+
const viemChain = Object.values(chains).find((c: any) => typeof c === 'object' && 'id' in c && c.id === chainId)
72+
if (viemChain) {
73+
return viemChain as Chain
74+
}
75+
76+
throw new Error(`Chain with id ${chainId} not found in Sequence networks or viem chains`)
3477
}
3578

3679
export class RpcRelayer implements Relayer {

0 commit comments

Comments
 (0)