Skip to content

Commit b353ca2

Browse files
committed
fix: resolve supported chains
1 parent e184e87 commit b353ca2

File tree

4 files changed

+88
-80
lines changed

4 files changed

+88
-80
lines changed

sdk/src/gateway/layerzero.ts

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ContractFunctionExecutionError,
77
encodeAbiParameters,
88
encodePacked,
9+
extractChain,
910
Hex,
1011
InsufficientFundsError,
1112
isAddress,
@@ -57,6 +58,7 @@ import {
5758
toHexScriptPubKey,
5859
viemClient,
5960
} from './utils';
61+
import { supportedChainsMapping } from './utils/common';
6062

6163
bitcoin.initEccLib(ecc);
6264

@@ -84,13 +86,19 @@ export class LayerZeroClient {
8486
return this.getChainDeploymentsPromiseCache;
8587
}
8688

87-
// Resolve viem chain name to layerzero chain name
88-
private resolveViemChainName(chainKey: string): string {
89-
switch (chainKey) {
89+
// Resolve viem and layerzero chain names
90+
resolveViemChainName(chainKey: string): string {
91+
switch (chainKey.toLowerCase()) {
9092
case bsc.name.toLowerCase():
9193
return 'bsc';
9294
case optimism.name.toLowerCase():
9395
return 'optimism';
96+
case sei.name.toLowerCase():
97+
return 'sei';
98+
case soneium.name.toLowerCase():
99+
return 'soneium';
100+
case berachain.name.toLowerCase():
101+
return 'bera';
94102
default:
95103
return chainKey;
96104
}
@@ -99,7 +107,6 @@ export class LayerZeroClient {
99107
async getEidForChain(chainKey: string) {
100108
const data = await this.getChainDeployments();
101109
const resolvedChainName = this.resolveViemChainName(chainKey);
102-
console.log('resolvedChainName', resolvedChainName);
103110
const eid = data[resolvedChainName]?.deployments?.find((item) => item.version === 2)?.eid;
104111
return eid !== undefined && eid !== null ? Number(eid) : null;
105112
}
@@ -139,26 +146,32 @@ export class LayerZeroClient {
139146

140147
async getSupportedChainsInfo(): Promise<Array<LayerZeroChainInfo>> {
141148
const chains = await this.getChainDeployments();
142-
const chainLookup = Object.fromEntries(
143-
Object.entries(chains).map(([, chainData]) => [
144-
chainData.chainKey,
145-
{
146-
eid: chainData.deployments?.find((item) => item.version === 2)?.eid,
147-
nativeChainId: chainData.chainDetails?.nativeChainId,
148-
},
149-
])
150-
);
151-
152149
const deployments = await this.getWbtcDeployments();
153-
return Object.entries(deployments).map(([chainKey, deployment]) => {
154-
const chainInfo = chainLookup[chainKey];
155-
return {
156-
name: chainKey,
157-
eid: chainInfo?.eid,
158-
oftAddress: deployment.address,
159-
nativeChainId: chainInfo?.nativeChainId,
160-
};
150+
151+
const supportedLayerZeroChainKeys = new Set<string>();
152+
const layerZeroKeyToViemName: Record<string, string> = {};
153+
154+
Object.values(supportedChainsMapping).forEach((chainConfig) => {
155+
const viemChainName = chainConfig.name.toLowerCase();
156+
const layerZeroChainKey = this.resolveViemChainName(viemChainName);
157+
supportedLayerZeroChainKeys.add(layerZeroChainKey);
158+
layerZeroKeyToViemName[layerZeroChainKey] = viemChainName;
161159
});
160+
161+
// Filter layerzero chains that are in supportedChainsMapping
162+
return Object.entries(chains)
163+
.filter(([layerZeroChainKey]) => supportedLayerZeroChainKeys.has(layerZeroChainKey))
164+
.map(([layerZeroChainKey, chainData]) => {
165+
const viemChainName = layerZeroKeyToViemName[layerZeroChainKey];
166+
const deployment = deployments[layerZeroChainKey];
167+
168+
return {
169+
name: viemChainName,
170+
eid: chainData.deployments?.find((item) => item.version === 2)?.eid,
171+
oftAddress: deployment.address,
172+
nativeChainId: chainData.chainDetails?.nativeChainId,
173+
};
174+
});
162175
}
163176

164177
async getChainId(eid: number): Promise<number | null> {
@@ -185,38 +198,13 @@ export class LayerZeroClient {
185198
}
186199

187200
// Viem chain names are used to identify chains
201+
function resolveChainId(chain: number): string {
202+
return getChainConfig(chain).name.toLowerCase();
203+
}
204+
188205
function resolveChainName(chain: number | string): string {
189206
if (typeof chain === 'number') {
190-
switch (chain) {
191-
case bob.id:
192-
return bob.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();
215-
case mainnet.id:
216-
return mainnet.name.toLowerCase();
217-
default:
218-
throw new Error(`Unsupported chain ID: ${chain}`);
219-
}
207+
return resolveChainId(chain);
220208
}
221209
return chain.toLowerCase();
222210
}
@@ -266,8 +254,8 @@ export class LayerZeroGatewayClient extends GatewayApiClient {
266254
}
267255

268256
async getQuote(params: GetQuoteParams<LayerZeroQuoteParamsExt>): Promise<ExecuteQuoteParams> {
269-
const fromChain = resolveChainName(params.fromChain);
270-
const toChain = resolveChainName(params.toChain);
257+
const fromChain = typeof params.fromChain === 'number' ? resolveChainId(params.fromChain) : params.fromChain;
258+
const toChain = typeof params.toChain === 'number' ? resolveChainId(params.toChain) : params.toChain;
271259

272260
if (fromChain === 'bitcoin' && toChain === bob.name.toLowerCase()) {
273261
// Handle bitcoin -> bob: use normal flow

sdk/src/gateway/types/layerzero.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export type LayerZeroSendParam = {
6969
};
7070

7171
export type LayerZeroChainInfo = {
72-
name: string;
72+
name: string; // viem chain name
7373
eid?: string;
7474
oftAddress: string;
7575
nativeChainId?: number;

sdk/src/gateway/utils/common.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function formatBtc(btc: bigint) {
138138
return formatUnits(btc, 8);
139139
}
140140

141-
const supportedChainsMapping = {
141+
export const supportedChainsMapping = {
142142
bob,
143143
ethereum: mainnet,
144144
sonic,
@@ -161,9 +161,14 @@ const chainIdToChainConfigMapping = Object.values(supportedChainsMapping).reduce
161161
);
162162

163163
function getChainIdByName(chainName: string) {
164-
const chain = supportedChainsMapping[chainName.toLowerCase()];
164+
const chain = Object.values(supportedChainsMapping).find(
165+
(chain) => chain.name.toLowerCase() === chainName.toLowerCase()
166+
);
167+
165168
if (!chain) {
166-
throw new Error(`Chain id for "${chainName}" not found. Allowed values ${Object.keys(supportedChainsMapping)}`);
169+
throw new Error(
170+
`Chain id for "${chainName}" not found. Allowed values ${Object.values(supportedChainsMapping).map((chain) => chain.name)}`
171+
);
167172
}
168173
return chain.id;
169174
}

sdk/test/layerzero.test.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { HDKey } from '@scure/bip32';
2424
import { Hex } from 'viem';
2525
import { privateKeyToAccount } from 'viem/accounts';
2626
import { getCrossChainStatus } from '../src/gateway/utils/layerzero';
27+
import { supportedChainsMapping } from '../src/gateway/utils/common';
2728

2829
describe('LayerZero Tests', () => {
2930
it.skip('should get chains', async () => {
@@ -168,23 +169,37 @@ describe('LayerZero Tests', () => {
168169
console.log(txHash);
169170
}, 120000);
170171

171-
it('should be able to get a layerzero send quote for every chain combination', async () => {
172+
it.skip('should be able to get a layerzero send quote for every chain', async () => {
172173
const client = new LayerZeroGatewayClient();
173174

174-
const chains = await client.getSupportedChainsInfo();
175-
176-
console.log('chains', chains);
175+
const l0ChainsInfo = await client.getSupportedChainsInfo();
176+
const toChain = l0ChainsInfo.find((chain) => chain.name === 'bob')!;
177+
178+
for (const fromChain of l0ChainsInfo) {
179+
if (fromChain.name !== toChain.name) {
180+
const quote = await client.getQuote({
181+
fromChain: fromChain.nativeChainId as number,
182+
toChain: toChain.nativeChainId as number,
183+
fromToken: fromChain.oftAddress as string,
184+
toToken: toChain.oftAddress as string,
185+
fromUserAddress: '0xEf7Ff7Fb24797656DF41616e807AB4016AE9dCD5',
186+
toUserAddress: '0xEf7Ff7Fb24797656DF41616e807AB4016AE9dCD5',
187+
amount: 100,
188+
});
189+
console.log(fromChain.name, toChain.name);
190+
}
191+
}
177192
}, 120000);
178193

179-
it('should get a layerzero send quote and execute it', async () => {
194+
it.skip('should get a layerzero send quote and execute it', async () => {
180195
const client = new LayerZeroGatewayClient();
181196

182197
const quote = await client.getQuote({
183198
fromChain: 'BNB Smart Chain',
184-
fromToken: (await client.getSupportedChainsInfo()).find((chain) => chain.name === 'base')
199+
fromToken: (await client.getSupportedChainsInfo()).find((chain) => chain.name === 'BNB Smart Chain')
185200
?.oftAddress as string,
186201
toChain: 'base',
187-
toToken: (await client.getSupportedChainsInfo()).find((chain) => chain.name === 'optimism')
202+
toToken: (await client.getSupportedChainsInfo()).find((chain) => chain.name === 'base')
188203
?.oftAddress as string,
189204
fromUserAddress: '0xEf7Ff7Fb24797656DF41616e807AB4016AE9dCD5',
190205
toUserAddress: '0xEf7Ff7Fb24797656DF41616e807AB4016AE9dCD5',
@@ -193,24 +208,24 @@ describe('LayerZero Tests', () => {
193208

194209
console.log('quote', quote);
195210

196-
// const publicClient = createPublicClient({
197-
// chain: base,
198-
// transport: http(),
199-
// });
211+
const publicClient = createPublicClient({
212+
chain: bsc,
213+
transport: http(),
214+
});
200215

201-
// const walletClient = createWalletClient({
202-
// chain: base,
203-
// transport: http(),
204-
// account: privateKeyToAccount(process.env.PRIVATE_KEY as Hex),
205-
// });
216+
const walletClient = createWalletClient({
217+
chain: bsc,
218+
transport: http(),
219+
account: privateKeyToAccount(process.env.PRIVATE_KEY as Hex),
220+
});
206221

207-
// const txHash = await client.executeQuote({
208-
// quote,
209-
// walletClient,
210-
// publicClient: publicClient as PublicClient<Transport>,
211-
// });
222+
const txHash = await client.executeQuote({
223+
quote,
224+
walletClient,
225+
publicClient: publicClient as PublicClient<Transport>,
226+
});
212227

213-
// console.log(txHash);
228+
console.log(txHash);
214229
}, 120000);
215230

216231
it('should get chain id for eid', async () => {

0 commit comments

Comments
 (0)