@gmx-io/sdk 1.5.0-alpha-16 → 1.5.0-alpha-18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/src/clients/v2/__tests__/decrease.spec.js +1 -1
- package/build/cjs/src/clients/v2/__tests__/increase.spec.js +1 -1
- package/build/cjs/src/clients/v2/__tests__/multichain.spec.js +68 -223
- package/build/cjs/src/clients/v2/__tests__/multichainTestUtil.js +3 -18
- package/build/cjs/src/clients/v2/__tests__/swap.spec.js +1 -1
- package/build/cjs/src/clients/v2/index.js +12 -17
- package/build/cjs/src/configs/__tests__/multichainTokens.spec.js +54 -0
- package/build/cjs/src/configs/multichainTokens.js +298 -0
- package/build/cjs/src/utils/express/utils/estimateExpressParams.js +2 -1
- package/build/cjs/src/utils/fees/executionFee.js +1 -8
- package/build/cjs/src/utils/gas/applyBuffer.js +1 -1
- package/build/cjs/src/utils/{multichainTransferApi → gmxAccountApi}/api.js +32 -15
- package/build/cjs/src/utils/multichain/__tests__/api.spec.js +4 -94
- package/build/cjs/src/utils/multichain/api.js +4 -82
- package/build/cjs/src/utils/multichain/estimateComposeGas.js +1 -1
- package/build/cjs/src/utils/multichain/quotes.js +0 -1
- package/build/cjs/src/utils/orderTransactions/api.js +10 -15
- package/build/cjs/src/utils/rpc/createViemRpc.js +1 -1
- package/build/cjs/src/utils/trades/api.js +3 -6
- package/build/esm/src/clients/v2/__tests__/decrease.spec.js +1 -1
- package/build/esm/src/clients/v2/__tests__/increase.spec.js +1 -1
- package/build/esm/src/clients/v2/__tests__/multichain.spec.js +69 -224
- package/build/esm/src/clients/v2/__tests__/multichainTestUtil.js +3 -18
- package/build/esm/src/clients/v2/__tests__/swap.spec.js +1 -1
- package/build/esm/src/clients/v2/index.js +8 -13
- package/build/esm/src/configs/__tests__/multichainTokens.spec.js +52 -0
- package/build/esm/src/configs/multichainTokens.js +287 -0
- package/build/esm/src/utils/express/utils/estimateExpressParams.js +3 -2
- package/build/esm/src/utils/fees/executionFee.js +0 -6
- package/build/esm/src/utils/gas/applyBuffer.js +1 -1
- package/build/esm/src/utils/{multichainTransferApi → gmxAccountApi}/api.js +30 -14
- package/build/esm/src/utils/multichain/__tests__/api.spec.js +7 -97
- package/build/esm/src/utils/multichain/api.js +3 -79
- package/build/esm/src/utils/multichain/estimateComposeGas.js +1 -1
- package/build/esm/src/utils/multichain/quotes.js +0 -1
- package/build/esm/src/utils/orderTransactions/api.js +10 -15
- package/build/esm/src/utils/rpc/createViemRpc.js +1 -1
- package/build/esm/src/utils/trades/api.js +3 -6
- package/build/types/src/clients/v2/__tests__/multichainTestUtil.d.ts +1 -7
- package/build/types/src/clients/v2/index.d.ts +6 -18
- package/build/types/src/configs/multichainTokens.d.ts +30 -0
- package/build/types/src/utils/fees/executionFee.d.ts +0 -1
- package/build/types/src/utils/{multichainTransferApi → gmxAccountApi}/api.d.ts +17 -13
- package/build/types/src/utils/multichain/api.d.ts +4 -63
- package/build/types/src/utils/multichain/quotes.d.ts +0 -1
- package/build/types/src/utils/rpc/index.d.ts +2 -1
- package/package.json +10 -1
- package/build/cjs/src/utils/multichainTransferApi/__tests__/api.spec.js +0 -232
- package/build/esm/src/utils/multichainTransferApi/__tests__/api.spec.js +0 -230
- /package/build/cjs/src/utils/{multichainTransferApi → gmxAccountApi}/index.js +0 -0
- /package/build/esm/src/utils/{multichainTransferApi → gmxAccountApi}/index.js +0 -0
- /package/build/types/src/{utils/multichainTransferApi/__tests__/api.spec.d.ts → configs/__tests__/multichainTokens.spec.d.ts} +0 -0
- /package/build/types/src/utils/{multichainTransferApi → gmxAccountApi}/index.d.ts +0 -0
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.executeSameChainWithdraw = exports.executeSameChainDeposit = exports.buildCrossChainWithdrawBridgeOutParams = exports.buildSameChainWithdrawTxn = exports.buildSameChainWithdrawBridgeOutParams = exports.buildSameChainDepositTxn = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const abis_1 = require("../../abis");
|
|
6
6
|
const contracts_1 = require("../../configs/contracts");
|
|
7
7
|
const multichain_1 = require("../../configs/multichain");
|
|
8
8
|
const tokens_1 = require("../../configs/tokens");
|
|
9
|
-
const estimateComposeGas_1 = require("./estimateComposeGas");
|
|
10
|
-
const quotes_1 = require("./quotes");
|
|
11
|
-
const sendParams_1 = require("./sendParams");
|
|
12
9
|
/**
|
|
13
10
|
* Wallet sits on the settlement chain and funds the GMX account directly via
|
|
14
11
|
* `MultichainTransferRouter.multicall([sendWnt|sendTokens, bridgeIn])`.
|
|
@@ -80,73 +77,6 @@ function buildSameChainWithdrawTxn(request) {
|
|
|
80
77
|
return { to: router, data, value: 0n };
|
|
81
78
|
}
|
|
82
79
|
exports.buildSameChainWithdrawTxn = buildSameChainWithdrawTxn;
|
|
83
|
-
/**
|
|
84
|
-
* Builds a Stargate `send` / `sendToken` transaction on the source chain that bridges funds
|
|
85
|
-
* into the GMX account on the destination settlement chain.
|
|
86
|
-
*
|
|
87
|
-
* RPCs:
|
|
88
|
-
* - `destinationRpc`: settlement chain — used to estimate compose gas (omitted if `composeGas` is provided)
|
|
89
|
-
* - `sourceRpc`: source chain — used to quote the LayerZero native fee from Stargate
|
|
90
|
-
*/
|
|
91
|
-
async function buildCrossChainDepositTxn(request, rpcs) {
|
|
92
|
-
rpcs = rpcs ?? {};
|
|
93
|
-
const { chainId, srcChainId, account, tokenAddress, sourceStargatePoolAddress, isNativeOnSource, amount, tokenKind = "trade", nativeDropAmount, innerData, } = request;
|
|
94
|
-
let composeGas = request.composeGas;
|
|
95
|
-
if (composeGas === undefined) {
|
|
96
|
-
if (!rpcs.destinationRpc) {
|
|
97
|
-
throw new Error("destinationRpc or precomputed composeGas is required");
|
|
98
|
-
}
|
|
99
|
-
if (!request.destinationStargatePoolAddress) {
|
|
100
|
-
throw new Error("destinationStargatePoolAddress or precomputed composeGas is required");
|
|
101
|
-
}
|
|
102
|
-
composeGas = await (0, estimateComposeGas_1.estimateMultichainDepositComposeGas)(rpcs.destinationRpc, {
|
|
103
|
-
chainId,
|
|
104
|
-
account,
|
|
105
|
-
srcChainId,
|
|
106
|
-
tokenAddress,
|
|
107
|
-
destinationStargatePoolAddress: request.destinationStargatePoolAddress,
|
|
108
|
-
innerData,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
const sendParams = (0, sendParams_1.getMultichainTransferSendParams)({
|
|
112
|
-
dstChainId: chainId,
|
|
113
|
-
account,
|
|
114
|
-
srcChainId,
|
|
115
|
-
amountLD: amount,
|
|
116
|
-
composeGas,
|
|
117
|
-
isToGmx: true,
|
|
118
|
-
isManualGas: true,
|
|
119
|
-
nativeDropAmount,
|
|
120
|
-
innerData,
|
|
121
|
-
});
|
|
122
|
-
let nativeFee = request.nativeFee;
|
|
123
|
-
if (nativeFee === undefined) {
|
|
124
|
-
if (!rpcs.sourceRpc) {
|
|
125
|
-
throw new Error("sourceRpc or precomputed nativeFee is required");
|
|
126
|
-
}
|
|
127
|
-
const quote = await (0, quotes_1.quoteStargateSend)(rpcs.sourceRpc, {
|
|
128
|
-
stargateAddress: sourceStargatePoolAddress,
|
|
129
|
-
sendParams,
|
|
130
|
-
});
|
|
131
|
-
nativeFee = quote.nativeFee;
|
|
132
|
-
}
|
|
133
|
-
const functionName = tokenKind === "platform" ? "send" : "sendToken";
|
|
134
|
-
const data = (0, viem_1.encodeFunctionData)({
|
|
135
|
-
abi: abis_1.abis.IStargate,
|
|
136
|
-
functionName,
|
|
137
|
-
args: [sendParams, (0, sendParams_1.quoteFromNativeFee)(nativeFee), account],
|
|
138
|
-
});
|
|
139
|
-
const value = nativeFee + (isNativeOnSource ? amount : 0n);
|
|
140
|
-
return {
|
|
141
|
-
to: sourceStargatePoolAddress,
|
|
142
|
-
data,
|
|
143
|
-
value,
|
|
144
|
-
sendParams,
|
|
145
|
-
nativeFee,
|
|
146
|
-
composeGas,
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
exports.buildCrossChainDepositTxn = buildCrossChainDepositTxn;
|
|
150
80
|
/**
|
|
151
81
|
* `BridgeOutParams.data` for cross-chain delivery is a single `uint32 dstEid`
|
|
152
82
|
* pointing at the destination LayerZero endpoint; the on-chain provider takes
|
|
@@ -156,6 +86,9 @@ exports.buildCrossChainDepositTxn = buildCrossChainDepositTxn;
|
|
|
156
86
|
*/
|
|
157
87
|
function buildCrossChainWithdrawBridgeOutParams(params) {
|
|
158
88
|
const { tokenAddress, amount, dstChainId, stargateAddress, slippageBps = 0 } = params;
|
|
89
|
+
if (!Number.isInteger(slippageBps) || slippageBps < 0 || slippageBps >= 10000) {
|
|
90
|
+
throw new Error(`slippageBps must be an integer in range [0, 10000), got ${slippageBps}`);
|
|
91
|
+
}
|
|
159
92
|
const dstEid = (0, multichain_1.getLayerZeroEndpointId)(dstChainId);
|
|
160
93
|
if (dstEid === undefined) {
|
|
161
94
|
throw new Error(`No LayerZero endpoint id for destination chain ${dstChainId}`);
|
|
@@ -185,14 +118,3 @@ async function executeSameChainWithdraw(signer, request) {
|
|
|
185
118
|
return sendBuiltTxn(signer, buildSameChainWithdrawTxn(request));
|
|
186
119
|
}
|
|
187
120
|
exports.executeSameChainWithdraw = executeSameChainWithdraw;
|
|
188
|
-
async function executeCrossChainDeposit(signer, request, rpcs) {
|
|
189
|
-
const built = await buildCrossChainDepositTxn(request, rpcs);
|
|
190
|
-
const txnHash = await sendBuiltTxn(signer, { to: built.to, data: built.data, value: built.value });
|
|
191
|
-
return {
|
|
192
|
-
txnHash,
|
|
193
|
-
composeGas: built.composeGas,
|
|
194
|
-
nativeFee: built.nativeFee,
|
|
195
|
-
sendParams: built.sendParams,
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
exports.executeCrossChainDeposit = executeCrossChainDeposit;
|
|
@@ -20,7 +20,7 @@ async function estimateMultichainDepositComposeGas(rpc, { chainId, account, srcC
|
|
|
20
20
|
}
|
|
21
21
|
const fakeAmount = fakeAmountOverride ??
|
|
22
22
|
((0, tokens_1.isValidTokenSafe)(chainId, unwrappedTokenAddress)
|
|
23
|
-
? (0, numbers_1.expandDecimals)(
|
|
23
|
+
? (0, numbers_1.expandDecimals)(10n, (0, tokens_1.getToken)(chainId, unwrappedTokenAddress).decimals)
|
|
24
24
|
: DEFAULT_FAKE_AMOUNT);
|
|
25
25
|
const message = (0, codecs_1.encodeOftComposeMsg)(0n, sourceChainEndpointId, fakeAmount, composeFromWithMsg);
|
|
26
26
|
const layerZeroProvider = (0, contracts_1.getContract)(chainId, "LayerZeroProvider");
|
|
@@ -5,21 +5,16 @@ const validateTypedData_1 = require("./validateTypedData");
|
|
|
5
5
|
function parseEstimates(raw) {
|
|
6
6
|
if (!raw)
|
|
7
7
|
return undefined;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
catch (e) {
|
|
21
|
-
throw new Error(`Failed to parse order estimates: ${e instanceof Error ? e.message : e}`);
|
|
22
|
-
}
|
|
8
|
+
return {
|
|
9
|
+
positionPriceImpactDeltaUsd: BigInt(raw.positionPriceImpactDeltaUsd ?? "0"),
|
|
10
|
+
swapPriceImpactDeltaUsd: BigInt(raw.swapPriceImpactDeltaUsd ?? "0"),
|
|
11
|
+
executionFeeAmount: BigInt(raw.executionFeeAmount ?? "0"),
|
|
12
|
+
acceptablePrice: BigInt(raw.acceptablePrice ?? "0"),
|
|
13
|
+
sizeDeltaUsd: BigInt(raw.sizeDeltaUsd ?? "0"),
|
|
14
|
+
positionFeeUsd: BigInt(raw.positionFeeUsd ?? "0"),
|
|
15
|
+
borrowingFeeUsd: BigInt(raw.borrowingFeeUsd ?? "0"),
|
|
16
|
+
fundingFeeUsd: BigInt(raw.fundingFeeUsd ?? "0"),
|
|
17
|
+
};
|
|
23
18
|
}
|
|
24
19
|
function parsePrepareResponse(raw) {
|
|
25
20
|
return { ...raw, estimates: parseEstimates(raw.estimates) };
|
|
@@ -3,13 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.searchApiTrades = exports.fetchApiTrades = void 0;
|
|
4
4
|
const numbers_1 = require("../numbers");
|
|
5
5
|
function parseTradesResponse(raw) {
|
|
6
|
-
const trades = Array.isArray(raw?.trades)
|
|
7
|
-
? raw.trades.map((t) => (0, numbers_1.deserializeBigIntsInObject)(t, { handleInts: true }))
|
|
8
|
-
: [];
|
|
9
6
|
return {
|
|
10
|
-
trades,
|
|
11
|
-
nextCursor: raw
|
|
12
|
-
hasMore:
|
|
7
|
+
trades: raw.trades.map((t) => (0, numbers_1.deserializeBigIntsInObject)(t, { handleInts: true })),
|
|
8
|
+
nextCursor: raw.nextCursor,
|
|
9
|
+
hasMore: raw.hasMore,
|
|
13
10
|
};
|
|
14
11
|
}
|
|
15
12
|
async function fetchApiTrades(ctx, params) {
|
|
@@ -391,7 +391,7 @@ describe("decrease orders", () => {
|
|
|
391
391
|
const txHash = await signer.sendTransaction({
|
|
392
392
|
to: prepared.payload.to,
|
|
393
393
|
data: prepared.payload.data,
|
|
394
|
-
value: BigInt(prepared.payload.value
|
|
394
|
+
value: BigInt(prepared.payload.value),
|
|
395
395
|
});
|
|
396
396
|
expect(txHash).toBeDefined();
|
|
397
397
|
expect(txHash.startsWith("0x")).toBe(true);
|
|
@@ -607,7 +607,7 @@ describe("increase orders", () => {
|
|
|
607
607
|
const txHash = await signer.sendTransaction({
|
|
608
608
|
to: prepared.payload.to,
|
|
609
609
|
data: prepared.payload.data,
|
|
610
|
-
value: BigInt(prepared.payload.value
|
|
610
|
+
value: BigInt(prepared.payload.value),
|
|
611
611
|
});
|
|
612
612
|
expect(txHash).toBeDefined();
|
|
613
613
|
expect(txHash.startsWith("0x")).toBe(true);
|
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* E2E suite for multichain bridge in / bridge out (Arbitrum settlement, Base source).
|
|
3
|
-
*
|
|
4
|
-
* Required env (mirrors order tests):
|
|
5
|
-
* GMX_TEST_PRIVATE_KEY — wallet pk owning the GMX account (0x-prefixed 32 bytes)
|
|
6
|
-
* GMX_TEST_API_URL — points at the API instance with multichain endpoints,
|
|
7
|
-
* e.g. http://localhost:3004/v1 for a local gmx-api
|
|
8
|
-
*
|
|
9
|
-
* Optional env (gates extra scenarios):
|
|
10
|
-
* GMX_TEST_RPC_URL — Arbitrum settlement-chain RPC (for classic submit flows)
|
|
11
|
-
* GMX_TEST_SOURCE_RPC_URL — Base source-chain RPC (for Stargate quote + sendTransaction)
|
|
12
|
-
* GMX_TEST_SEND=1 — gates the actually-spends-funds execute* tests
|
|
13
|
-
*/
|
|
14
1
|
import { isAddress } from "viem";
|
|
15
2
|
import { beforeAll, describe, expect, it } from "vitest";
|
|
16
|
-
import { ensureSourceErc20Allowance, expectArbitrumSettlement,
|
|
3
|
+
import { ensureSourceErc20Allowance, expectArbitrumSettlement, getSourceSigner, shouldSendOnChain, STARGATE_USDC_ARBITRUM, STARGATE_USDC_BASE, TEST_SOURCE_CHAIN_ID, USDC_ARBITRUM, USDC_BASE, waitForWithdrawStatus, } from "./multichainTestUtil";
|
|
17
4
|
import { getTestSdk, getTestSigner } from "./testUtil";
|
|
18
5
|
beforeAll(() => {
|
|
19
6
|
expectArbitrumSettlement();
|
|
@@ -24,8 +11,8 @@ const account = signer?.address ?? "";
|
|
|
24
11
|
const hasSigner = signer !== undefined;
|
|
25
12
|
const DEPOSIT_AMOUNT = 1000000n; // 1 USDC
|
|
26
13
|
const WITHDRAW_AMOUNT = 500000n; // 0.5 USDC
|
|
27
|
-
describe.skipIf(!hasSigner)("multichain
|
|
28
|
-
it("
|
|
14
|
+
describe.skipIf(!hasSigner)("multichain same-chain (build only)", () => {
|
|
15
|
+
it("deposit: multicall(sendTokens + bridgeIn)", () => {
|
|
29
16
|
const built = sdk.buildSameChainDepositTxn({
|
|
30
17
|
tokenAddress: USDC_ARBITRUM,
|
|
31
18
|
amount: DEPOSIT_AMOUNT,
|
|
@@ -35,234 +22,92 @@ describe.skipIf(!hasSigner)("multichain bridge: same-chain deposit (build only)"
|
|
|
35
22
|
expect(built.data.startsWith("0x")).toBe(true);
|
|
36
23
|
expect(built.value).toBe(0n);
|
|
37
24
|
});
|
|
38
|
-
|
|
39
|
-
describe.skipIf(!hasSigner)("multichain bridge: same-chain withdraw (build only)", () => {
|
|
40
|
-
it("buildSameChainWithdrawTxn encodes transferOut(BridgeOutParams)", () => {
|
|
25
|
+
it("withdraw: transferOut(BridgeOutParams)", () => {
|
|
41
26
|
const params = sdk.buildSameChainWithdrawBridgeOutParams({
|
|
42
27
|
tokenAddress: USDC_ARBITRUM,
|
|
43
28
|
amount: WITHDRAW_AMOUNT,
|
|
44
29
|
});
|
|
45
|
-
expect(params.token.toLowerCase()).toBe(USDC_ARBITRUM.toLowerCase());
|
|
46
30
|
expect(params.amount).toBe(WITHDRAW_AMOUNT);
|
|
47
|
-
expect(params.minAmountOut).toBe(WITHDRAW_AMOUNT);
|
|
48
31
|
const built = sdk.buildSameChainWithdrawTxn({ bridgeOutParams: params });
|
|
49
32
|
expect(isAddress(built.to)).toBe(true);
|
|
50
|
-
expect(built.data.startsWith("0x")).toBe(true);
|
|
51
33
|
expect(built.value).toBe(0n);
|
|
52
34
|
});
|
|
53
35
|
});
|
|
54
|
-
describe.skipIf(!hasSigner)("multichain
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
sourceStargatePoolAddress: STARGATE_USDC_BASE,
|
|
62
|
-
destinationStargatePoolAddress: STARGATE_USDC_ARBITRUM,
|
|
63
|
-
isNativeOnSource: false,
|
|
64
|
-
amount: DEPOSIT_AMOUNT,
|
|
65
|
-
// Pre-computed compose gas avoids needing the on-chain registered MultichainProvider
|
|
66
|
-
// for lzCompose simulation. Real flows can omit this once the API resolves it from DataStore.
|
|
67
|
-
composeGas: 500000n,
|
|
68
|
-
// Caller still supplies a non-zero nativeFee placeholder — the API does not quote source-chain fees yet.
|
|
69
|
-
nativeFee: 200000000000000n, // ~0.0002 ETH; only used by the API's calldata builder
|
|
70
|
-
});
|
|
71
|
-
expect(prepared.payloadType).toBe("transaction");
|
|
72
|
-
expect(isAddress(prepared.payload.to)).toBe(true);
|
|
73
|
-
expect(prepared.payload.data.startsWith("0x")).toBe(true);
|
|
74
|
-
expect(typeof prepared.payload.value).toBe("bigint");
|
|
75
|
-
expect(prepared.composeGas).toBeGreaterThan(0n);
|
|
76
|
-
expect(prepared.nativeFee).toBeGreaterThan(0n);
|
|
77
|
-
expect(prepared.expiresAt).toBeGreaterThan(0);
|
|
36
|
+
describe.skipIf(!hasSigner)("multichain cross-chain deposit (Base → Arbitrum, API-driven)", () => {
|
|
37
|
+
it("prepare: server resolves pools + quotes nativeFee + composeGas (tokenSymbol)", async () => {
|
|
38
|
+
const prepared = await sdk.prepareCrossChainDeposit({
|
|
39
|
+
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
40
|
+
account,
|
|
41
|
+
tokenSymbol: "USDC",
|
|
42
|
+
amount: DEPOSIT_AMOUNT,
|
|
78
43
|
});
|
|
44
|
+
expect(prepared.payloadType).toBe("transaction");
|
|
45
|
+
expect(isAddress(prepared.payload.to)).toBe(true);
|
|
46
|
+
expect(prepared.payload.data.startsWith("0x")).toBe(true);
|
|
47
|
+
expect(prepared.composeGas).toBeGreaterThan(0n);
|
|
48
|
+
expect(prepared.nativeFee).toBeGreaterThan(0n);
|
|
49
|
+
expect(prepared.payload.value).toBeGreaterThanOrEqual(prepared.nativeFee);
|
|
79
50
|
});
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
tokenAddress: USDC_ARBITRUM,
|
|
87
|
-
sourceStargatePoolAddress: STARGATE_USDC_BASE,
|
|
88
|
-
isNativeOnSource: false,
|
|
89
|
-
amount: DEPOSIT_AMOUNT,
|
|
90
|
-
// Pre-computed compose gas avoids needing a settlement-chain RPC for this branch.
|
|
91
|
-
composeGas: 500000n,
|
|
92
|
-
}, { sourceRpc });
|
|
93
|
-
expect(isAddress(built.to)).toBe(true);
|
|
94
|
-
expect(built.data.startsWith("0x")).toBe(true);
|
|
95
|
-
// For ERC20 source, msg.value covers only LayerZero native fee.
|
|
96
|
-
expect(built.value).toBeGreaterThan(0n);
|
|
97
|
-
expect(built.composeGas).toBeGreaterThan(0n);
|
|
98
|
-
expect(built.nativeFee).toBeGreaterThan(0n);
|
|
99
|
-
});
|
|
100
|
-
it.skipIf(!shouldSendOnChain())("executeCrossChainDeposit submits the bridge tx on Base", async () => {
|
|
101
|
-
const sourceRpc = getSourceRpc();
|
|
102
|
-
const sourceSigner = getSourceSigner();
|
|
103
|
-
if (!sourceSigner) {
|
|
104
|
-
throw new Error("GMX_TEST_PRIVATE_KEY + GMX_TEST_SOURCE_RPC_URL are required for executeCrossChainDeposit");
|
|
105
|
-
}
|
|
106
|
-
await ensureSourceErc20Allowance({
|
|
107
|
-
sdk,
|
|
108
|
-
signer: sourceSigner,
|
|
109
|
-
tokenAddress: USDC_BASE,
|
|
110
|
-
spender: STARGATE_USDC_BASE,
|
|
111
|
-
minAmount: DEPOSIT_AMOUNT,
|
|
112
|
-
});
|
|
113
|
-
let result;
|
|
114
|
-
try {
|
|
115
|
-
result = await sdk.executeCrossChainDeposit(sourceSigner, {
|
|
116
|
-
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
117
|
-
account,
|
|
118
|
-
tokenAddress: USDC_ARBITRUM,
|
|
119
|
-
sourceStargatePoolAddress: STARGATE_USDC_BASE,
|
|
120
|
-
destinationStargatePoolAddress: STARGATE_USDC_ARBITRUM,
|
|
121
|
-
isNativeOnSource: false,
|
|
122
|
-
amount: DEPOSIT_AMOUNT,
|
|
123
|
-
// Pre-computed compose gas avoids needing a settlement-chain RPC just for estimation.
|
|
124
|
-
composeGas: 500000n,
|
|
125
|
-
}, { sourceRpc });
|
|
126
|
-
}
|
|
127
|
-
catch (err) {
|
|
128
|
-
// Soft-skip only on env-only failures the test cannot self-heal:
|
|
129
|
-
// - "insufficient funds" → wallet missing ETH for LayerZero nativeFee + gas
|
|
130
|
-
// - revert 0x7c75c3d2 = Stargate `Transfer_TransferFailed()` — usually low USDC balance
|
|
131
|
-
// (allowance is auto-handled by ensureSourceErc20Allowance above).
|
|
132
|
-
const msg = String(err?.shortMessage ?? err?.message ?? err);
|
|
133
|
-
let revertData = "";
|
|
134
|
-
for (let cur = err; cur && !revertData; cur = cur.cause) {
|
|
135
|
-
if (typeof cur.data === "string" && cur.data.startsWith("0x"))
|
|
136
|
-
revertData = cur.data;
|
|
137
|
-
}
|
|
138
|
-
if (/insufficient funds|exceeds the balance/i.test(msg) || revertData.startsWith("0x7c75c3d2")) {
|
|
139
|
-
// eslint-disable-next-line no-console
|
|
140
|
-
console.warn(`[multichain.deposit.execute] skipped (env): ${msg}`);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
throw err;
|
|
144
|
-
}
|
|
145
|
-
expect(result.txnHash).toBeDefined();
|
|
146
|
-
expect(result.txnHash.startsWith("0x")).toBe(true);
|
|
51
|
+
it("prepare: server resolves pools when tokenAddress is provided", async () => {
|
|
52
|
+
const prepared = await sdk.prepareCrossChainDeposit({
|
|
53
|
+
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
54
|
+
account,
|
|
55
|
+
tokenAddress: USDC_ARBITRUM,
|
|
56
|
+
amount: DEPOSIT_AMOUNT,
|
|
147
57
|
});
|
|
58
|
+
expect(prepared.nativeFee).toBeGreaterThan(0n);
|
|
59
|
+
expect(prepared.composeGas).toBeGreaterThan(0n);
|
|
148
60
|
});
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
stargateAddress: STARGATE_USDC_BASE,
|
|
158
|
-
});
|
|
159
|
-
const prepared = await sdk.prepareCrossChainWithdraw({
|
|
160
|
-
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
161
|
-
account,
|
|
162
|
-
bridgeOutParams,
|
|
163
|
-
});
|
|
164
|
-
expect(prepared.payloadType).toBe("typed-data");
|
|
165
|
-
expect(typeof prepared.requestId).toBe("string");
|
|
166
|
-
expect(prepared.requestId.length).toBeGreaterThan(0);
|
|
167
|
-
expect(prepared.payload.typedData.primaryType).toBe("BridgeOut");
|
|
168
|
-
expect(isAddress(prepared.payload.relayRouterAddress)).toBe(true);
|
|
169
|
-
expect(prepared.payload.gasPaymentParams.relayerFeeAmount).toBeGreaterThan(0n);
|
|
170
|
-
expect(isAddress(prepared.payload.gasPaymentParams.relayerFeeTokenAddress)).toBe(true);
|
|
171
|
-
expect(isAddress(prepared.payload.gasPaymentParams.gasPaymentTokenAddress)).toBe(true);
|
|
61
|
+
it.skipIf(!shouldSendOnChain())("execute: submits bridge tx on Base", async () => {
|
|
62
|
+
const sourceSigner = getSourceSigner();
|
|
63
|
+
await ensureSourceErc20Allowance({
|
|
64
|
+
sdk,
|
|
65
|
+
signer: sourceSigner,
|
|
66
|
+
tokenAddress: USDC_BASE,
|
|
67
|
+
spender: STARGATE_USDC_BASE,
|
|
68
|
+
minAmount: DEPOSIT_AMOUNT,
|
|
172
69
|
});
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
stargateAddress: STARGATE_USDC_BASE,
|
|
179
|
-
});
|
|
180
|
-
const prepared = await sdk.prepareCrossChainWithdraw({
|
|
181
|
-
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
182
|
-
account,
|
|
183
|
-
bridgeOutParams,
|
|
184
|
-
gasPaymentToken: USDC_ARBITRUM,
|
|
185
|
-
});
|
|
186
|
-
expect(prepared.payload.gasPaymentParams.gasPaymentTokenAddress.toLowerCase()).toBe(USDC_ARBITRUM.toLowerCase());
|
|
70
|
+
const result = await sdk.executeCrossChainDeposit(sourceSigner, {
|
|
71
|
+
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
72
|
+
account,
|
|
73
|
+
tokenSymbol: "USDC",
|
|
74
|
+
amount: DEPOSIT_AMOUNT,
|
|
187
75
|
});
|
|
76
|
+
expect(result.txnHash.startsWith("0x")).toBe(true);
|
|
77
|
+
expect(result.nativeFee).toBeGreaterThan(0n);
|
|
188
78
|
});
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const prepared = await sdk.prepareCrossChainWithdraw({
|
|
198
|
-
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
199
|
-
account,
|
|
200
|
-
bridgeOutParams,
|
|
201
|
-
});
|
|
202
|
-
const signature = await sdk.signCrossChainWithdraw(prepared, signer);
|
|
203
|
-
expect(signature.startsWith("0x")).toBe(true);
|
|
204
|
-
let submitted;
|
|
205
|
-
try {
|
|
206
|
-
submitted = await sdk.submitCrossChainWithdraw({
|
|
207
|
-
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
208
|
-
account,
|
|
209
|
-
signature,
|
|
210
|
-
bridgeOutParams,
|
|
211
|
-
relayParamsPayload: prepared.payload.relayParams,
|
|
212
|
-
relayerFeeTokenAddress: prepared.payload.gasPaymentParams.relayerFeeTokenAddress,
|
|
213
|
-
relayerFeeAmount: prepared.payload.gasPaymentParams.relayerFeeAmount,
|
|
214
|
-
requestId: prepared.requestId,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
catch (err) {
|
|
218
|
-
// Insufficient GMX-account balance / relayer-fee shortfall surfaces as a 400 (GELATO_RELAY_FAILED).
|
|
219
|
-
// The API persists relay_failed; we still verify status is queryable.
|
|
220
|
-
// eslint-disable-next-line no-console
|
|
221
|
-
console.warn(`[multichain.withdraw.submit] failed: ${err?.message ?? err}`);
|
|
222
|
-
const status = await sdk.getCrossChainWithdrawStatus(prepared.requestId);
|
|
223
|
-
expect(status.requestId).toBe(prepared.requestId);
|
|
224
|
-
expect(["relay_failed", "prepared"]).toContain(status.status);
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
expect(submitted.requestId).toBe(prepared.requestId);
|
|
228
|
-
expect(submitted.status).toBe("relay_accepted");
|
|
229
|
-
expect(typeof submitted.taskId).toBe("string");
|
|
230
|
-
const status = await sdk.getCrossChainWithdrawStatus(prepared.requestId);
|
|
231
|
-
expect(status.requestId).toBe(prepared.requestId);
|
|
232
|
-
expect(status.taskId).toBe(submitted.taskId);
|
|
79
|
+
});
|
|
80
|
+
describe.skipIf(!hasSigner)("multichain cross-chain withdraw (Arbitrum → Base, via Gelato)", () => {
|
|
81
|
+
it("prepare: returns typed-data + gasPaymentParams", async () => {
|
|
82
|
+
const bridgeOutParams = sdk.buildCrossChainWithdrawBridgeOutParams({
|
|
83
|
+
tokenAddress: USDC_ARBITRUM,
|
|
84
|
+
amount: WITHDRAW_AMOUNT,
|
|
85
|
+
dstChainId: TEST_SOURCE_CHAIN_ID,
|
|
86
|
+
stargateAddress: STARGATE_USDC_ARBITRUM,
|
|
233
87
|
});
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
dstChainId: TEST_SOURCE_CHAIN_ID,
|
|
239
|
-
stargateAddress: STARGATE_USDC_BASE,
|
|
240
|
-
});
|
|
241
|
-
let submitted;
|
|
242
|
-
try {
|
|
243
|
-
submitted = await sdk.executeCrossChainWithdraw({
|
|
244
|
-
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
245
|
-
account,
|
|
246
|
-
bridgeOutParams,
|
|
247
|
-
}, signer);
|
|
248
|
-
}
|
|
249
|
-
catch (err) {
|
|
250
|
-
// Gelato may reject the relay (insufficient GMX-account balance / fee shortfall).
|
|
251
|
-
// The API still records `relay_failed`; we treat that as a valid terminal state for the smoke run.
|
|
252
|
-
// eslint-disable-next-line no-console
|
|
253
|
-
console.warn(`[multichain.withdraw.execute] gelato rejected: ${err?.message ?? err}`);
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
expect(submitted.requestId).toBeDefined();
|
|
257
|
-
const final = await waitForWithdrawStatus(sdk, submitted.requestId, 120000);
|
|
258
|
-
// eslint-disable-next-line no-console
|
|
259
|
-
console.info(`[multichain.withdraw.terminal] id=${submitted.requestId} status=${final.status} txHash=${final.txHash ?? "-"}`);
|
|
260
|
-
expect(["created", "executed", "relay_failed", "relay_reverted"]).toContain(final.status);
|
|
88
|
+
const prepared = await sdk.prepareCrossChainWithdraw({
|
|
89
|
+
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
90
|
+
account,
|
|
91
|
+
bridgeOutParams,
|
|
261
92
|
});
|
|
93
|
+
expect(prepared.payloadType).toBe("typed-data");
|
|
94
|
+
expect(prepared.payload.typedData.primaryType).toBe("BridgeOut");
|
|
95
|
+
expect(prepared.payload.gasPaymentParams.relayerFeeAmount).toBeGreaterThan(0n);
|
|
262
96
|
});
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
97
|
+
it.skipIf(!shouldSendOnChain())("execute: resolves to terminal Gelato status", async () => {
|
|
98
|
+
const bridgeOutParams = sdk.buildCrossChainWithdrawBridgeOutParams({
|
|
99
|
+
tokenAddress: USDC_ARBITRUM,
|
|
100
|
+
amount: WITHDRAW_AMOUNT,
|
|
101
|
+
dstChainId: TEST_SOURCE_CHAIN_ID,
|
|
102
|
+
stargateAddress: STARGATE_USDC_ARBITRUM,
|
|
103
|
+
});
|
|
104
|
+
const submitted = await sdk.executeCrossChainWithdraw(signer, {
|
|
105
|
+
srcChainId: TEST_SOURCE_CHAIN_ID,
|
|
106
|
+
account,
|
|
107
|
+
bridgeOutParams,
|
|
266
108
|
});
|
|
109
|
+
expect(submitted.requestId).toBeDefined();
|
|
110
|
+
const final = await waitForWithdrawStatus(sdk, submitted.requestId, 120000);
|
|
111
|
+
expect(["executed", "cancelled", "relay_failed", "relay_reverted"]).toContain(final.status);
|
|
267
112
|
});
|
|
268
113
|
});
|
|
@@ -7,16 +7,11 @@ import { PrivateKeySigner } from "../../../utils/signer";
|
|
|
7
7
|
import { TEST_CHAIN_ID } from "./testUtil";
|
|
8
8
|
export { TEST_CHAIN_ID };
|
|
9
9
|
export const TEST_SOURCE_CHAIN_ID = SOURCE_BASE_MAINNET;
|
|
10
|
-
//
|
|
11
|
-
// USDC on Arbitrum (settlement chain destination token):
|
|
10
|
+
// Pinned to Arbitrum settlement / Base source.
|
|
12
11
|
export const USDC_ARBITRUM = "0xaf88d065e77c8cC2239327C5EDb3A432268e5831";
|
|
13
|
-
// Native USDC on Base (source chain) — what the wallet actually approves & spends in the bridge.
|
|
14
12
|
export const USDC_BASE = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
|
|
15
|
-
// Stargate v2 USDC pool on Base (source chain) — used as `to:` of source-chain bridge tx.
|
|
16
13
|
export const STARGATE_USDC_BASE = "0x27a16dc786820B16E5c9028b75B99F6f604b5d26";
|
|
17
|
-
|
|
18
|
-
// during destination-chain compose-gas estimation.
|
|
19
|
-
export const STARGATE_USDC_ARBITRUM = "0x70d95587d40A2caf56bd97485aB3Eec10Bee6336";
|
|
14
|
+
export const STARGATE_USDC_ARBITRUM = "0xe8CDF27AcD73a434D661C84887215F7598e7d0d3";
|
|
20
15
|
const VIEM_CHAIN_BY_SOURCE = {
|
|
21
16
|
[SOURCE_BASE_MAINNET]: base,
|
|
22
17
|
};
|
|
@@ -58,12 +53,6 @@ export function getSourceSigner() {
|
|
|
58
53
|
chain: requireSourceViemChain(),
|
|
59
54
|
});
|
|
60
55
|
}
|
|
61
|
-
/**
|
|
62
|
-
* Reads ERC20 allowance via the source-chain RPC and tops it up with `executeErc20Approve`
|
|
63
|
-
* if it's below `minAmount`. Returns the on-chain allowance after the (optional) approve.
|
|
64
|
-
*
|
|
65
|
-
* Used by the bridge-in tests so the suite is self-sufficient on a freshly-funded wallet.
|
|
66
|
-
*/
|
|
67
56
|
export async function ensureSourceErc20Allowance(params) {
|
|
68
57
|
// eslint-disable-next-line no-restricted-globals
|
|
69
58
|
const rpcUrl = process.env.GMX_TEST_SOURCE_RPC_URL;
|
|
@@ -89,16 +78,12 @@ export async function ensureSourceErc20Allowance(params) {
|
|
|
89
78
|
args: [owner, params.spender],
|
|
90
79
|
}));
|
|
91
80
|
}
|
|
92
|
-
const TERMINAL_WITHDRAW_STATUSES = new Set(["
|
|
81
|
+
const TERMINAL_WITHDRAW_STATUSES = new Set(["executed", "cancelled", "relay_failed", "relay_reverted"]);
|
|
93
82
|
export async function waitForWithdrawStatus(sdk, requestId, timeoutMs = 90000) {
|
|
94
83
|
const start = Date.now();
|
|
95
84
|
while (Date.now() - start < timeoutMs) {
|
|
96
85
|
const status = await sdk.getCrossChainWithdrawStatus(requestId);
|
|
97
86
|
if (TERMINAL_WITHDRAW_STATUSES.has(status.status)) {
|
|
98
|
-
if (status.error) {
|
|
99
|
-
// eslint-disable-next-line no-console
|
|
100
|
-
console.warn(`[withdraw:${status.status}] id=${requestId} error=${status.error.code}: ${status.error.message}`);
|
|
101
|
-
}
|
|
102
87
|
return status;
|
|
103
88
|
}
|
|
104
89
|
await sleep(2000);
|
|
@@ -96,7 +96,7 @@ describe("swap orders", () => {
|
|
|
96
96
|
const txHash = await signer.sendTransaction({
|
|
97
97
|
to: prepared.payload.to,
|
|
98
98
|
data: prepared.payload.data,
|
|
99
|
-
value: BigInt(prepared.payload.value
|
|
99
|
+
value: BigInt(prepared.payload.value),
|
|
100
100
|
});
|
|
101
101
|
expect(txHash).toBeDefined();
|
|
102
102
|
expect(txHash.startsWith("0x")).toBe(true);
|