Skip to content

Commit 9c482b1

Browse files
committed
feat: chain name resolving
1 parent 3cb5e4d commit 9c482b1

File tree

1 file changed

+59
-11
lines changed

1 file changed

+59
-11
lines changed

sdk/src/gateway/layerzero.ts

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@ import {
1818
toHex,
1919
zeroAddress,
2020
} from 'viem';
21-
import { bob, bobSepolia, mainnet } from 'viem/chains';
21+
import {
22+
bob,
23+
mainnet,
24+
base,
25+
berachain,
26+
bsc,
27+
unichain,
28+
avalanche,
29+
sonic,
30+
soneium,
31+
telos,
32+
swellchain,
33+
optimism,
34+
sei,
35+
} from 'viem/chains';
2236
import { layerZeroOftAbi, quoterV2Abi } from './abi';
2337
import { AllWalletClientParams, GatewayApiClient } from './client';
2438
import { getTokenAddress, getTokenSlots } from './tokens';
@@ -70,9 +84,23 @@ export class LayerZeroClient {
7084
return this.getChainDeploymentsPromiseCache;
7185
}
7286

87+
// Resolve viem chain name to layerzero chain name
88+
private resolveViemChainName(chainKey: string): string {
89+
switch (chainKey) {
90+
case bsc.name.toLowerCase():
91+
return 'bsc';
92+
case optimism.name.toLowerCase():
93+
return 'optimism';
94+
default:
95+
return chainKey;
96+
}
97+
}
98+
7399
async getEidForChain(chainKey: string) {
74100
const data = await this.getChainDeployments();
75-
const eid = data[chainKey]?.deployments?.find((item) => item.version === 2)?.eid;
101+
const resolvedChainName = this.resolveViemChainName(chainKey);
102+
console.log('resolvedChainName', resolvedChainName);
103+
const eid = data[resolvedChainName]?.deployments?.find((item) => item.version === 2)?.eid;
76104
return eid !== undefined && eid !== null ? Number(eid) : null;
77105
}
78106

@@ -105,7 +133,8 @@ export class LayerZeroClient {
105133

106134
async getOftAddressForChain(chainKey: string): Promise<string | null> {
107135
const deployments = await this.getWbtcDeployments();
108-
return deployments[chainKey]?.address || null;
136+
const resolvedChainName = this.resolveViemChainName(chainKey);
137+
return deployments[resolvedChainName]?.address || null;
109138
}
110139

111140
async getSupportedChainsInfo(): Promise<Array<LayerZeroChainInfo>> {
@@ -155,15 +184,36 @@ export class LayerZeroClient {
155184
}
156185
}
157186

187+
// Viem chain names are used to identify chains
158188
function resolveChainName(chain: number | string): string {
159189
if (typeof chain === 'number') {
160190
switch (chain) {
161191
case bob.id:
162192
return bob.name.toLowerCase();
163-
case bobSepolia.id:
164-
return bobSepolia.name.toLowerCase();
193+
case base.id:
194+
return base.name.toLowerCase();
195+
case berachain.id:
196+
return berachain.name.toLowerCase();
197+
case bsc.id:
198+
return bsc.name.toLowerCase();
199+
case unichain.id:
200+
return unichain.name.toLowerCase();
201+
case avalanche.id:
202+
return avalanche.name.toLowerCase();
203+
case sonic.id:
204+
return sonic.name.toLowerCase();
205+
case soneium.id:
206+
return soneium.name.toLowerCase();
207+
case telos.id:
208+
return telos.name.toLowerCase();
209+
case swellchain.id:
210+
return swellchain.name.toLowerCase();
211+
case optimism.id:
212+
return optimism.name.toLowerCase();
213+
case sei.id:
214+
return sei.name.toLowerCase();
165215
case mainnet.id:
166-
return 'mainnet';
216+
return mainnet.name.toLowerCase();
167217
default:
168218
throw new Error(`Unsupported chain ID: ${chain}`);
169219
}
@@ -175,11 +225,9 @@ function resolveChainName(chain: number | string): string {
175225
export class LayerZeroGatewayClient extends GatewayApiClient {
176226
private l0Client: LayerZeroClient;
177227

178-
constructor(chainId: number, options?: { rpcUrl?: string }) {
179-
if (chainId !== bob.id) {
180-
throw new Error('LayerZeroGatewayClient only supports BOB mainnet');
181-
}
182-
super(chainId, options);
228+
// TODO: remove constructor, set the config from `getQuote`
229+
constructor(options?: { rpcUrl?: string }) {
230+
super(bob.id, options);
183231
this.l0Client = new LayerZeroClient();
184232
}
185233

0 commit comments

Comments
 (0)