Skip to content

Commit adf5b60

Browse files
authored
Merge pull request #578 from everclearorg/fix/aave-swap
feat: extend stargate chain support
2 parents 023e553 + db2cfb6 commit adf5b60

File tree

3 files changed

+289
-22
lines changed

3 files changed

+289
-22
lines changed

packages/adapters/rebalance/src/adapters/stargate/stargate.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import { jsonifyError, Logger } from '@mark/logger';
1414
import { BridgeAdapter, MemoizedTransactionRequest, RebalanceTransactionMemo } from '../../types';
1515
import { STARGATE_OFT_ABI } from './abi';
1616
import {
17-
STARGATE_USDT_POOL_ETH,
1817
USDT_ETH,
19-
LZ_ENDPOINT_ID_TON,
18+
STARGATE_POOL_ADDRESSES,
19+
CHAIN_ID_TO_LZ_ENDPOINT,
2020
StargateSendParam,
2121
StargateMessagingFee,
2222
LzMessageStatus,
@@ -27,6 +27,7 @@ import {
2727
tonAddressToBytes32,
2828
USDT_TON_STARGATE,
2929
} from './types';
30+
import { getDestinationAssetAddress } from '../../shared/asset';
3031

3132
// LayerZero Scan API base URL
3233
const LZ_SCAN_API_URL = 'https://scan.layerzero-api.com';
@@ -56,6 +57,26 @@ export class StargateBridgeAdapter implements BridgeAdapter {
5657
return SupportedBridge.Stargate;
5758
}
5859

60+
/**
61+
* Resolve the destination token address for a route
62+
* For TON, uses the Stargate-specific hex address; for EVM chains, looks up via chain config
63+
*/
64+
private resolveDstToken(route: RebalanceRoute): string | null {
65+
if (route.destination === 30826) return USDT_TON_STARGATE;
66+
return (
67+
getDestinationAssetAddress(route.asset, route.origin, route.destination, this.chains, this.logger) ?? null
68+
);
69+
}
70+
71+
/**
72+
* Get the LayerZero endpoint ID for a chain
73+
*/
74+
protected getLzEndpointId(chainId: number): number {
75+
const eid = CHAIN_ID_TO_LZ_ENDPOINT[chainId];
76+
if (eid === undefined) throw new Error(`No LayerZero endpoint ID configured for chain ${chainId}`);
77+
return eid;
78+
}
79+
5980
/**
6081
* Get the expected amount received after bridging via Stargate
6182
*
@@ -99,8 +120,11 @@ export class StargateBridgeAdapter implements BridgeAdapter {
99120
return null;
100121
}
101122

102-
// For TON destination, use the Stargate-specific token address format
103-
const dstToken = route.destination === 30826 ? USDT_TON_STARGATE : route.asset;
123+
const dstToken = this.resolveDstToken(route);
124+
if (!dstToken) {
125+
this.logger.warn('Could not resolve destination token', { route });
126+
return null;
127+
}
104128

105129
// Use a placeholder address for quote - actual address will be used in send()
106130
const placeholderAddress = '0x1234567890abcdef1234567890abcdef12345678';
@@ -162,7 +186,7 @@ export class StargateBridgeAdapter implements BridgeAdapter {
162186

163187
// Prepare send parameters for quote
164188
const sendParam: StargateSendParam = {
165-
dstEid: LZ_ENDPOINT_ID_TON,
189+
dstEid: this.getLzEndpointId(route.destination),
166190
to: pad('0x0000000000000000000000000000000000000000' as `0x${string}`, { size: 32 }),
167191
amountLD: BigInt(amount),
168192
minAmountLD: BigInt(0), // Will be calculated after quote
@@ -304,8 +328,11 @@ export class StargateBridgeAdapter implements BridgeAdapter {
304328
return null;
305329
}
306330

307-
// For TON destination, use the Stargate-specific token address format
308-
const dstToken = route.destination === 30826 ? USDT_TON_STARGATE : route.asset;
331+
const dstToken = this.resolveDstToken(route);
332+
if (!dstToken) {
333+
this.logger.warn('Could not resolve destination token', { route });
334+
return null;
335+
}
309336

310337
// Calculate minimum amount with slippage (0.5%)
311338
const slippageBps = 50n;
@@ -471,7 +498,7 @@ export class StargateBridgeAdapter implements BridgeAdapter {
471498

472499
// Prepare send parameters
473500
const sendParam: StargateSendParam = {
474-
dstEid: LZ_ENDPOINT_ID_TON,
501+
dstEid: this.getLzEndpointId(route.destination),
475502
to: recipientBytes32,
476503
amountLD: BigInt(amount),
477504
minAmountLD: minAmount,
@@ -745,13 +772,11 @@ export class StargateBridgeAdapter implements BridgeAdapter {
745772
* Get the Stargate pool address for an asset
746773
*/
747774
protected getPoolAddress(asset: string, chainId: number): `0x${string}` {
748-
// For USDT on Ethereum mainnet
749-
if (asset.toLowerCase() === USDT_ETH.toLowerCase() && chainId === 1) {
750-
return STARGATE_USDT_POOL_ETH;
751-
}
752-
753-
// Add more pool addresses as needed
754-
throw new Error(`No Stargate pool found for asset ${asset} on chain ${chainId}`);
775+
const chainPools = STARGATE_POOL_ADDRESSES[chainId];
776+
if (!chainPools) throw new Error(`No Stargate pools configured for chain ${chainId}`);
777+
const pool = chainPools[asset.toLowerCase()];
778+
if (!pool) throw new Error(`No Stargate pool found for asset ${asset} on chain ${chainId}`);
779+
return pool;
755780
}
756781

757782
/**

packages/adapters/rebalance/src/adapters/stargate/types.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,47 @@ export const STARGATE_USDT_POOL_ETH = '0x933597a323Eb81cAe705C5bC29985172fd5A397
1818
// USDT token on Ethereum mainnet
1919
export const USDT_ETH = '0xdAC17F958D2ee523a2206206994597C13D831ec7' as `0x${string}`;
2020

21+
// Token addresses - Base
22+
export const USDC_BASE = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913' as `0x${string}`;
23+
24+
// Token addresses - Arbitrum
25+
export const USDC_ARB = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831' as `0x${string}`;
26+
export const USDT_ARB = '0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9' as `0x${string}`;
27+
28+
// Token addresses - Mantle
29+
export const USDC_MANTLE = '0x09Bc4E0D864854c6aFB6eB9A9cdF58aC190D0dF9' as `0x${string}`;
30+
export const USDT_MANTLE = '0x201EBa5CC46D216Ce6DC03F6a759e8E766e956aE' as `0x${string}`;
31+
32+
// Stargate Pool addresses - Base
33+
export const STARGATE_USDC_POOL_BASE = '0x27a16dc786820B16E5c9028b75B99F6f604b5d26' as `0x${string}`;
34+
35+
// Stargate Pool addresses - Arbitrum
36+
export const STARGATE_USDC_POOL_ARB = '0xe8CDF27AcD73a434D661C84887215F7598e7d0d3' as `0x${string}`;
37+
export const STARGATE_USDT_POOL_ARB = '0xcE8CcA271Ebc0533920C83d39F417ED6A0abB7D0' as `0x${string}`;
38+
39+
// Stargate Pool addresses - Mantle
40+
export const STARGATE_USDC_POOL_MANTLE = '0xAc290Ad4e0c891FDc295ca4F0a6214cf6dC6acDC' as `0x${string}`;
41+
export const STARGATE_USDT_POOL_MANTLE = '0xB715B85682B731dB9D5063187C450095c91C57FC' as `0x${string}`;
42+
2143
// ============================================================================
2244
// LayerZero V2 Endpoint IDs
2345
// Reference: https://docs.layerzero.network/v2/deployments/chains
2446
// ============================================================================
2547

2648
export const LZ_ENDPOINT_ID_ETH = 30101; // Ethereum mainnet
49+
export const LZ_ENDPOINT_ID_BASE = 30184; // Base mainnet
50+
export const LZ_ENDPOINT_ID_ARB = 30110; // Arbitrum mainnet
51+
export const LZ_ENDPOINT_ID_MANTLE = 30181; // Mantle mainnet
2752
export const LZ_ENDPOINT_ID_TON = 30826; // TON mainnet
2853

54+
export const CHAIN_ID_TO_LZ_ENDPOINT: Record<number, number> = {
55+
1: LZ_ENDPOINT_ID_ETH,
56+
8453: LZ_ENDPOINT_ID_BASE,
57+
42161: LZ_ENDPOINT_ID_ARB,
58+
5000: LZ_ENDPOINT_ID_MANTLE,
59+
30826: LZ_ENDPOINT_ID_TON,
60+
};
61+
2962
// ============================================================================
3063
// Chain IDs
3164
// ============================================================================
@@ -36,6 +69,23 @@ export const TAC_CHAIN_ID = 239;
3669
// TON does not have an EVM chain ID, we use LayerZero endpoint ID
3770
export const TON_CHAIN_ID = 30826;
3871

72+
// ============================================================================
73+
// Stargate Pool Addresses
74+
// ============================================================================
75+
76+
export const STARGATE_POOL_ADDRESSES: Record<number, Record<string, `0x${string}`>> = {
77+
1: { [USDT_ETH.toLowerCase()]: STARGATE_USDT_POOL_ETH },
78+
8453: { [USDC_BASE.toLowerCase()]: STARGATE_USDC_POOL_BASE },
79+
42161: {
80+
[USDC_ARB.toLowerCase()]: STARGATE_USDC_POOL_ARB,
81+
[USDT_ARB.toLowerCase()]: STARGATE_USDT_POOL_ARB,
82+
},
83+
5000: {
84+
[USDC_MANTLE.toLowerCase()]: STARGATE_USDC_POOL_MANTLE,
85+
[USDT_MANTLE.toLowerCase()]: STARGATE_USDT_POOL_MANTLE,
86+
},
87+
};
88+
3989
// ============================================================================
4090
// Stargate API Configuration
4191
// Reference: https://stargate.finance/api/v1/quotes
@@ -122,6 +172,9 @@ export const USDT_TON_STARGATE = '0xb113a994b5024a16719f69139328eb759596c38a25f5
122172
*/
123173
export const STARGATE_CHAIN_NAMES: Record<number, string> = {
124174
1: 'ethereum',
175+
8453: 'base',
176+
42161: 'arbitrum',
177+
5000: 'mantle',
125178
30826: 'ton',
126179
239: 'tac',
127180
};

0 commit comments

Comments
 (0)