@gmx-io/sdk 2.3.2 → 2.6.0-next.0
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/scripts/generate-synthetics-abis.js +17 -2
- package/build/cjs/src/abis/CustomErrors.js +98 -0
- package/build/cjs/src/abis/CustomErrorsV2_2_1.js +5565 -0
- package/build/cjs/src/abis/ExchangeRouterV2_2_1.js +1462 -0
- package/build/cjs/src/abis/GelatoRelayRouterV2_2_1.js +1945 -0
- package/build/cjs/src/abis/GlvReaderV2_2_1.js +1681 -0
- package/build/cjs/src/abis/MultichainClaimsRouterV2_2_1.js +1281 -0
- package/build/cjs/src/abis/MultichainGlvRouterV2_2_1.js +1176 -0
- package/build/cjs/src/abis/MultichainGmRouterV2_2_1.js +1480 -0
- package/build/cjs/src/abis/MultichainOrderRouterV2_2_1.js +2065 -0
- package/build/cjs/src/abis/MultichainSubaccountRouterV2_2_1.js +2160 -0
- package/build/cjs/src/abis/MultichainTransferRouterV2_2_1.js +1033 -0
- package/build/cjs/src/abis/RelayParamsV2_2_1.js +56 -0
- package/build/cjs/src/abis/RewardRouterV3.js +675 -0
- package/build/cjs/src/abis/SubaccountGelatoRelayRouterV2_2_1.js +2502 -0
- package/build/cjs/src/abis/SyntheticsReaderV2_2_1.js +5279 -0
- package/build/cjs/src/abis/__tests__/fixtures/arbitrumSepoliaReaders.json +8 -0
- package/build/cjs/src/abis/__tests__/getAbi.spec.js +84 -0
- package/build/cjs/src/abis/getAbi.js +42 -0
- package/build/cjs/src/abis/index.js +4 -1
- package/build/cjs/src/clients/v1/modules/orders/orders.js +5 -3
- package/build/cjs/src/clients/v1/modules/orders/transactions/createIncreaseOrderTxn.js +3 -1
- package/build/cjs/src/clients/v1/modules/orders/transactions/updateOrderTxn.js +21 -25
- package/build/cjs/src/clients/v1/modules/orders/utils.js +6 -7
- package/build/cjs/src/clients/v1/modules/trades/trades.js +4 -3
- package/build/cjs/src/clients/v1/multicall.js +2 -1
- package/build/cjs/src/clients/v2/__tests__/decrease.spec.js +0 -2
- package/build/cjs/src/clients/v2/__tests__/editCancel.spec.js +0 -3
- package/build/cjs/src/clients/v2/__tests__/globalCleanup.js +0 -1
- package/build/cjs/src/clients/v2/__tests__/increase.spec.js +0 -2
- package/build/cjs/src/clients/v2/__tests__/subaccountLifecycle.spec.js +0 -1
- package/build/cjs/src/clients/v2/__tests__/testUtil.js +0 -1
- package/build/cjs/src/configs/__tests__/markets.spec.js +8 -1
- package/build/cjs/src/configs/chains.js +16 -1
- package/build/cjs/src/configs/contracts.js +27 -18
- package/build/cjs/src/configs/dataStore.js +13 -3
- package/build/cjs/src/configs/markets.js +12 -7
- package/build/cjs/src/configs/tokens.js +12 -7
- package/build/cjs/src/utils/__tests__/address.spec.js +50 -0
- package/build/cjs/src/utils/__tests__/numbers.spec.js +24 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +41 -4
- package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +4 -0
- package/build/cjs/src/utils/__tests__/parseError.spec.js +61 -0
- package/build/cjs/src/utils/__tests__/referrals.spec.js +36 -4
- package/build/cjs/src/utils/address/index.js +17 -0
- package/build/cjs/src/utils/address/utils.js +32 -0
- package/build/cjs/src/utils/errors/parseError.js +10 -0
- package/build/cjs/src/utils/errors/transactionsErrors.js +23 -0
- package/build/cjs/src/utils/express/utils/batchOrderUtils.js +14 -9
- package/build/cjs/src/utils/express/utils/bridgeOutUtils.js +52 -17
- package/build/cjs/src/utils/express/utils/referralUtils.js +38 -25
- package/build/cjs/src/utils/express/utils/relayParamsUtils.js +11 -3
- package/build/cjs/src/utils/markets/__tests__/multicall.spec.js +27 -0
- package/build/cjs/src/utils/markets/multicall.js +5 -7
- package/build/cjs/src/utils/multichain/api.js +17 -8
- package/build/cjs/src/utils/numbers/utils.js +7 -1
- package/build/cjs/src/utils/orderTransactions/api.js +3 -4
- package/build/cjs/src/utils/orderTransactions/utils.js +8 -7
- package/build/cjs/src/utils/orders/utils.js +8 -1
- package/build/cjs/src/utils/paxos/__tests__/api.spec.js +23 -3
- package/build/cjs/src/utils/paxos/api.js +6 -0
- package/build/cjs/src/utils/referrals/utils.js +24 -15
- package/build/cjs/src/utils/subaccount/getEmptySubaccountApproval.js +2 -0
- package/build/cjs/src/utils/subaccount/getSubaccountApprovalTypedData.js +37 -15
- package/build/cjs/src/utils/subaccount/hashSubaccountApproval.js +32 -18
- package/build/cjs/src/utils/subaccount/index.js +4 -1
- package/build/cjs/src/utils/subaccount/sdkClient.js +1 -1
- package/build/cjs/src/utils/subaccount/state.js +10 -1
- package/build/cjs/src/utils/swap/preparedSwapData.js +6 -4
- package/build/cjs/src/utils/swap/swapPath.js +1 -1
- package/build/cjs/src/utils/trade/__tests__/decreaseMarginCheck.spec.js +224 -0
- package/build/cjs/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +128 -9
- package/build/cjs/src/utils/trade/decrease.js +198 -33
- package/build/cjs/src/utils/trade/decreaseMarginCheck.js +157 -0
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +27 -15
- package/build/cjs/src/utils/trade/index.js +1 -0
- package/build/cjs/src/utils/tradeHistory/utils.js +6 -6
- package/build/esm/scripts/generate-synthetics-abis.js +17 -2
- package/build/esm/src/abis/CustomErrors.js +98 -0
- package/build/esm/src/abis/CustomErrorsV2_2_1.js +5563 -0
- package/build/esm/src/abis/ExchangeRouterV2_2_1.js +1460 -0
- package/build/esm/src/abis/GelatoRelayRouterV2_2_1.js +1943 -0
- package/build/esm/src/abis/GlvReaderV2_2_1.js +1679 -0
- package/build/esm/src/abis/MultichainClaimsRouterV2_2_1.js +1279 -0
- package/build/esm/src/abis/MultichainGlvRouterV2_2_1.js +1174 -0
- package/build/esm/src/abis/MultichainGmRouterV2_2_1.js +1478 -0
- package/build/esm/src/abis/MultichainOrderRouterV2_2_1.js +2063 -0
- package/build/esm/src/abis/MultichainSubaccountRouterV2_2_1.js +2158 -0
- package/build/esm/src/abis/MultichainTransferRouterV2_2_1.js +1031 -0
- package/build/esm/src/abis/RelayParamsV2_2_1.js +54 -0
- package/build/esm/src/abis/RewardRouterV3.js +673 -0
- package/build/esm/src/abis/SubaccountGelatoRelayRouterV2_2_1.js +2500 -0
- package/build/esm/src/abis/SyntheticsReaderV2_2_1.js +5277 -0
- package/build/esm/src/abis/__tests__/fixtures/arbitrumSepoliaReaders.json +8 -0
- package/build/esm/src/abis/__tests__/getAbi.spec.js +79 -0
- package/build/esm/src/abis/getAbi.js +35 -0
- package/build/esm/src/abis/index.js +4 -1
- package/build/esm/src/clients/v1/modules/orders/orders.js +5 -3
- package/build/esm/src/clients/v1/modules/orders/transactions/createIncreaseOrderTxn.js +3 -1
- package/build/esm/src/clients/v1/modules/orders/transactions/updateOrderTxn.js +21 -25
- package/build/esm/src/clients/v1/modules/orders/utils.js +6 -7
- package/build/esm/src/clients/v1/modules/trades/trades.js +4 -3
- package/build/esm/src/clients/v1/multicall.js +2 -1
- package/build/esm/src/clients/v2/__tests__/decrease.spec.js +0 -2
- package/build/esm/src/clients/v2/__tests__/editCancel.spec.js +0 -3
- package/build/esm/src/clients/v2/__tests__/globalCleanup.js +0 -1
- package/build/esm/src/clients/v2/__tests__/increase.spec.js +0 -2
- package/build/esm/src/clients/v2/__tests__/subaccountLifecycle.spec.js +0 -1
- package/build/esm/src/clients/v2/__tests__/testUtil.js +0 -1
- package/build/esm/src/configs/__tests__/markets.spec.js +9 -2
- package/build/esm/src/configs/chains.js +14 -0
- package/build/esm/src/configs/contracts.js +25 -17
- package/build/esm/src/configs/dataStore.js +8 -0
- package/build/esm/src/configs/markets.js +13 -8
- package/build/esm/src/configs/tokens.js +13 -8
- package/build/esm/src/utils/__tests__/address.spec.js +48 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +25 -1
- package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +43 -6
- package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +5 -1
- package/build/esm/src/utils/__tests__/parseError.spec.js +62 -1
- package/build/esm/src/utils/__tests__/referrals.spec.js +37 -5
- package/build/esm/src/utils/address/index.js +1 -0
- package/build/esm/src/utils/address/utils.js +25 -0
- package/build/esm/src/utils/errors/parseError.js +11 -1
- package/build/esm/src/utils/errors/transactionsErrors.js +23 -0
- package/build/esm/src/utils/express/utils/batchOrderUtils.js +14 -9
- package/build/esm/src/utils/express/utils/bridgeOutUtils.js +53 -18
- package/build/esm/src/utils/express/utils/referralUtils.js +37 -25
- package/build/esm/src/utils/express/utils/relayParamsUtils.js +10 -5
- package/build/esm/src/utils/markets/__tests__/multicall.spec.js +25 -0
- package/build/esm/src/utils/markets/multicall.js +5 -7
- package/build/esm/src/utils/multichain/api.js +15 -7
- package/build/esm/src/utils/numbers/utils.js +5 -0
- package/build/esm/src/utils/orderTransactions/api.js +3 -4
- package/build/esm/src/utils/orderTransactions/utils.js +9 -8
- package/build/esm/src/utils/orders/utils.js +6 -0
- package/build/esm/src/utils/paxos/__tests__/api.spec.js +23 -3
- package/build/esm/src/utils/paxos/api.js +6 -0
- package/build/esm/src/utils/referrals/utils.js +23 -15
- package/build/esm/src/utils/subaccount/getEmptySubaccountApproval.js +2 -0
- package/build/esm/src/utils/subaccount/getSubaccountApprovalTypedData.js +36 -14
- package/build/esm/src/utils/subaccount/hashSubaccountApproval.js +32 -18
- package/build/esm/src/utils/subaccount/index.js +2 -2
- package/build/esm/src/utils/subaccount/sdkClient.js +2 -2
- package/build/esm/src/utils/subaccount/state.js +8 -0
- package/build/esm/src/utils/swap/preparedSwapData.js +6 -4
- package/build/esm/src/utils/swap/swapPath.js +2 -2
- package/build/esm/src/utils/trade/__tests__/decreaseMarginCheck.spec.js +222 -0
- package/build/esm/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +129 -10
- package/build/esm/src/utils/trade/decrease.js +195 -32
- package/build/esm/src/utils/trade/decreaseMarginCheck.js +152 -0
- package/build/esm/src/utils/trade/increaseMarginCheck.js +25 -14
- package/build/esm/src/utils/trade/index.js +1 -0
- package/build/esm/src/utils/tradeHistory/utils.js +6 -6
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/scripts/generate-synthetics-abis.d.ts +6 -0
- package/build/types/src/abis/CustomErrors.d.ts +160 -0
- package/build/types/src/abis/CustomErrorsV2_2_1.d.ts +4174 -0
- package/build/types/src/abis/ExchangeRouterV2_2_1.d.ts +1128 -0
- package/build/types/src/abis/GelatoRelayRouterV2_2_1.d.ts +1508 -0
- package/build/types/src/abis/GlvReaderV2_2_1.d.ts +1293 -0
- package/build/types/src/abis/MultichainClaimsRouterV2_2_1.d.ts +985 -0
- package/build/types/src/abis/MultichainGlvRouterV2_2_1.d.ts +906 -0
- package/build/types/src/abis/MultichainGmRouterV2_2_1.d.ts +1142 -0
- package/build/types/src/abis/MultichainOrderRouterV2_2_1.d.ts +1601 -0
- package/build/types/src/abis/MultichainSubaccountRouterV2_2_1.d.ts +1678 -0
- package/build/types/src/abis/MultichainTransferRouterV2_2_1.d.ts +793 -0
- package/build/types/src/abis/RelayParamsV2_2_1.d.ts +118 -0
- package/build/types/src/abis/RewardRouterV3.d.ts +1129 -0
- package/build/types/src/abis/SubaccountGelatoRelayRouterV2_2_1.d.ts +1946 -0
- package/build/types/src/abis/SyntheticsReaderV2_2_1.d.ts +4075 -0
- package/build/types/src/abis/__tests__/getAbi.spec.d.ts +1 -0
- package/build/types/src/abis/getAbi.d.ts +17051 -0
- package/build/types/src/abis/index.d.ts +6700 -4744
- package/build/types/src/clients/v1/modules/orders/transactions/createIncreaseOrderTxn.d.ts +2 -1
- package/build/types/src/clients/v1/modules/orders/transactions/updateOrderTxn.d.ts +8 -3
- package/build/types/src/configs/chains.d.ts +6 -2
- package/build/types/src/configs/contracts.d.ts +6 -0
- package/build/types/src/configs/dataStore.d.ts +4 -0
- package/build/types/src/configs/markets.d.ts +4 -4
- package/build/types/src/utils/__tests__/address.spec.d.ts +1 -0
- package/build/types/src/utils/address/index.d.ts +1 -0
- package/build/types/src/utils/address/utils.d.ts +11 -0
- package/build/types/src/utils/errors/transactionsErrors.d.ts +18 -1
- package/build/types/src/utils/express/types.d.ts +1 -0
- package/build/types/src/utils/express/utils/batchOrderUtils.d.ts +4 -3
- package/build/types/src/utils/express/utils/bridgeOutUtils.d.ts +8 -2
- package/build/types/src/utils/express/utils/referralUtils.d.ts +21 -0
- package/build/types/src/utils/markets/__tests__/multicall.spec.d.ts +1 -0
- package/build/types/src/utils/multichain/api.d.ts +8 -0
- package/build/types/src/utils/numbers/utils.d.ts +1 -0
- package/build/types/src/utils/orderTransactions/api.d.ts +1 -3
- package/build/types/src/utils/orderTransactions/utils.d.ts +5 -2
- package/build/types/src/utils/orders/utils.d.ts +1 -0
- package/build/types/src/utils/paxos/types.d.ts +2 -0
- package/build/types/src/utils/referrals/utils.d.ts +7 -1
- package/build/types/src/utils/subaccount/api.d.ts +1 -0
- package/build/types/src/utils/subaccount/getSubaccountApprovalTypedData.d.ts +4 -0
- package/build/types/src/utils/subaccount/hashSubaccountApproval.d.ts +2 -1
- package/build/types/src/utils/subaccount/index.d.ts +2 -2
- package/build/types/src/utils/subaccount/state.d.ts +2 -1
- package/build/types/src/utils/subaccount/types.d.ts +3 -1
- package/build/types/src/utils/trade/__tests__/decreaseMarginCheck.spec.d.ts +1 -0
- package/build/types/src/utils/trade/decrease.d.ts +18 -0
- package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +52 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +11 -1
- package/build/types/src/utils/trade/index.d.ts +1 -0
- package/build/types/src/utils/trade/types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import uniq from "lodash/uniq";
|
|
2
2
|
import { encodeFunctionData, zeroAddress, zeroHash } from "viem";
|
|
3
|
-
import
|
|
4
|
-
import { abis } from "../../abis/index";
|
|
3
|
+
import { getAbi } from "../../abis/getAbi";
|
|
5
4
|
import ERC20ABI from "../../abis/Token";
|
|
6
5
|
import { getExcessiveExecutionFee, getExecutionFeePriorityFeeAllowance, getHighExecutionFee, } from "../../configs/chains";
|
|
7
|
-
import { getContract } from "../../configs/contracts";
|
|
6
|
+
import { getContract, isContractsV2_2_1 } from "../../configs/contracts";
|
|
8
7
|
import { convertTokenAddress, getToken, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
9
8
|
import { bigMath } from "../bigmath";
|
|
10
9
|
import { expandDecimals, MaxUint256, USD_DECIMALS } from "../numbers";
|
|
@@ -283,6 +282,7 @@ export function buildUpdateOrderPayload(p) {
|
|
|
283
282
|
minOutputAmount: p.minOutputAmount,
|
|
284
283
|
autoCancel: p.autoCancel,
|
|
285
284
|
validFromTime: 0n,
|
|
285
|
+
decreasePositionSwapType: p.decreasePositionSwapType,
|
|
286
286
|
executionFeeTopUp: p.executionFeeTopUp,
|
|
287
287
|
},
|
|
288
288
|
};
|
|
@@ -544,7 +544,7 @@ function combineTransfers(tokenTransfers) {
|
|
|
544
544
|
}
|
|
545
545
|
return { tokenTransfers: Object.values(transfersMap), value };
|
|
546
546
|
}
|
|
547
|
-
export function getBatchOrderMulticallPayload({ params }) {
|
|
547
|
+
export function getBatchOrderMulticallPayload({ chainId, params, }) {
|
|
548
548
|
const { createOrderParams, updateOrderParams, cancelOrderParams } = params;
|
|
549
549
|
const multicall = [];
|
|
550
550
|
let value = 0n;
|
|
@@ -563,7 +563,7 @@ export function getBatchOrderMulticallPayload({ params }) {
|
|
|
563
563
|
multicall.push(...cancelMulticall);
|
|
564
564
|
value += cancelValue;
|
|
565
565
|
}
|
|
566
|
-
const { encodedMulticall, callData } = encodeExchangeRouterMulticall(multicall);
|
|
566
|
+
const { encodedMulticall, callData } = encodeExchangeRouterMulticall(chainId, multicall);
|
|
567
567
|
return { multicall, value, encodedMulticall, callData };
|
|
568
568
|
}
|
|
569
569
|
export function buildCreateOrderMulticall(params) {
|
|
@@ -615,6 +615,7 @@ export function buildUpdateOrderMulticall(updateTxn) {
|
|
|
615
615
|
updatePayload.triggerPrice,
|
|
616
616
|
updatePayload.minOutputAmount,
|
|
617
617
|
0n,
|
|
618
|
+
...(isContractsV2_2_1(chainId) ? [updatePayload.decreasePositionSwapType] : []),
|
|
618
619
|
updatePayload.autoCancel,
|
|
619
620
|
],
|
|
620
621
|
});
|
|
@@ -635,14 +636,14 @@ export function buildCancelOrderMulticall({ params }) {
|
|
|
635
636
|
value: 0n,
|
|
636
637
|
};
|
|
637
638
|
}
|
|
638
|
-
export function encodeExchangeRouterMulticall(multicall) {
|
|
639
|
+
export function encodeExchangeRouterMulticall(chainId, multicall) {
|
|
639
640
|
const encodedMulticall = multicall.map((call) => encodeFunctionData({
|
|
640
|
-
abi:
|
|
641
|
+
abi: getAbi(chainId, "ExchangeRouter"),
|
|
641
642
|
functionName: call.method,
|
|
642
643
|
args: call.params,
|
|
643
644
|
}));
|
|
644
645
|
const callData = encodeFunctionData({
|
|
645
|
-
abi:
|
|
646
|
+
abi: getAbi(chainId, "ExchangeRouter"),
|
|
646
647
|
functionName: "multicall",
|
|
647
648
|
args: [encodedMulticall],
|
|
648
649
|
});
|
|
@@ -220,6 +220,12 @@ export function isOrderForPositionByData(order, { account, marketAddress, collat
|
|
|
220
220
|
}
|
|
221
221
|
return isMatch;
|
|
222
222
|
}
|
|
223
|
+
export function getOrderPositionKey(order) {
|
|
224
|
+
const collateralAddress = isDecreaseOrderType(order.orderType)
|
|
225
|
+
? order.initialCollateralTokenAddress
|
|
226
|
+
: order.targetCollateralToken.address;
|
|
227
|
+
return getPositionKey(order.account, order.marketAddress, collateralAddress, order.isLong);
|
|
228
|
+
}
|
|
223
229
|
export function getOrderTradeboxKey(order) {
|
|
224
230
|
if (isPositionOrder(order) || isTwapPositionOrder(order)) {
|
|
225
231
|
return `POSITION-${getPositionKey(order.account, order.marketAddress, order.initialCollateralTokenAddress, order.isLong)}`;
|
|
@@ -72,12 +72,32 @@ describe("paxos transit api", () => {
|
|
|
72
72
|
await fetchApiTransitRoutes({ api }, { feeTier: "zeroFee" });
|
|
73
73
|
expect(calls).toEqual([{ path: "/v1/paxos/transit/routes", query: { filter: undefined, feeTier: "zeroFee" } }]);
|
|
74
74
|
});
|
|
75
|
-
it("parses fee tier capacity as bigint", async () => {
|
|
75
|
+
it("parses the fee tier with capacity as bigint and the reopen time in seconds", async () => {
|
|
76
76
|
const calls = [];
|
|
77
|
-
const api = createApi(calls, {
|
|
77
|
+
const api = createApi(calls, {
|
|
78
|
+
feeTier: "zeroFee",
|
|
79
|
+
zeroFeeCapacity: "250000000000",
|
|
80
|
+
isZeroFeeAvailable: false,
|
|
81
|
+
zeroFeeExpectedReopenAt: "2026-10-26T13:00:00Z",
|
|
82
|
+
});
|
|
78
83
|
const result = await fetchApiTransitFeeTier({ api }, { userAddress: USER });
|
|
79
84
|
expect(calls).toEqual([{ path: "/v1/paxos/transit/fee-tier", query: { userAddress: USER } }]);
|
|
80
|
-
expect(result).toEqual({
|
|
85
|
+
expect(result).toEqual({
|
|
86
|
+
feeTier: "zeroFee",
|
|
87
|
+
zeroFeeCapacity: 250000000000n,
|
|
88
|
+
isZeroFeeAvailable: false,
|
|
89
|
+
zeroFeeExpectedReopenAt: Date.UTC(2026, 9, 26, 13) / 1000,
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
it.each([undefined, null, "soon"])("drops a reopen time of %s", async (zeroFeeExpectedReopenAt) => {
|
|
93
|
+
const api = createApi([], {
|
|
94
|
+
feeTier: "zeroFee",
|
|
95
|
+
zeroFeeCapacity: "0",
|
|
96
|
+
isZeroFeeAvailable: false,
|
|
97
|
+
zeroFeeExpectedReopenAt,
|
|
98
|
+
});
|
|
99
|
+
const result = await fetchApiTransitFeeTier({ api }, { userAddress: USER });
|
|
100
|
+
expect(result.zeroFeeExpectedReopenAt).toBeUndefined();
|
|
81
101
|
});
|
|
82
102
|
it("sends quote amounts as strings and parses fees", async () => {
|
|
83
103
|
const calls = [];
|
|
@@ -8,9 +8,15 @@ export async function fetchApiTransitFeeTier(ctx, params) {
|
|
|
8
8
|
const raw = await ctx.api.fetchJson("/v1/paxos/transit/fee-tier", {
|
|
9
9
|
query: { userAddress: params.userAddress },
|
|
10
10
|
});
|
|
11
|
+
const zeroFeeExpectedReopenAtMs = Date.parse(raw.zeroFeeExpectedReopenAt);
|
|
12
|
+
const zeroFeeExpectedReopenAt = Number.isFinite(zeroFeeExpectedReopenAtMs)
|
|
13
|
+
? Math.floor(zeroFeeExpectedReopenAtMs / 1000)
|
|
14
|
+
: undefined;
|
|
11
15
|
return {
|
|
12
16
|
feeTier: raw.feeTier,
|
|
13
17
|
zeroFeeCapacity: BigInt(raw.zeroFeeCapacity),
|
|
18
|
+
isZeroFeeAvailable: raw.isZeroFeeAvailable,
|
|
19
|
+
zeroFeeExpectedReopenAt,
|
|
14
20
|
};
|
|
15
21
|
}
|
|
16
22
|
export async function fetchApiTransitAuthorization(ctx, params) {
|
|
@@ -1,26 +1,34 @@
|
|
|
1
1
|
export const MAX_REFERRAL_CODE_LENGTH = 20;
|
|
2
2
|
import { padHex, stringToHex, zeroHash } from "viem";
|
|
3
|
-
import {
|
|
3
|
+
import { hexToBytes } from "viem/utils";
|
|
4
|
+
export const REFERRAL_CODE_REGEX = /^\w+$/;
|
|
5
|
+
const BYTES32_HEX_REGEX = /^0x[0-9a-fA-F]{64}$/;
|
|
6
|
+
export function isReferralCodeHex(value) {
|
|
7
|
+
return typeof value === "string" && BYTES32_HEX_REGEX.test(value);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Codes registered through the UI are letters, digits and `_` right-padded with zeros. The contract accepts any
|
|
11
|
+
* bytes32, so anything else (e.g. left-padded addresses, hashes, other text) is returned as the original hex.
|
|
12
|
+
*/
|
|
4
13
|
export function decodeReferralCode(hexCode) {
|
|
5
|
-
if (!hexCode || hexCode === zeroHash) {
|
|
14
|
+
if (!hexCode || !isReferralCodeHex(hexCode) || hexCode === zeroHash) {
|
|
6
15
|
return "";
|
|
7
16
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
let code = "";
|
|
16
|
-
const cleaned = hexCode.substring(2);
|
|
17
|
-
for (let i = 0; i < 32; i++) {
|
|
18
|
-
code += String.fromCharCode(parseInt(cleaned.substring(i * 2, i * 2 + 2), 16));
|
|
19
|
-
}
|
|
20
|
-
return code.trim();
|
|
17
|
+
const bytes = hexToBytes(hexCode);
|
|
18
|
+
const firstZeroIndex = bytes.indexOf(0);
|
|
19
|
+
const textLength = firstZeroIndex === -1 ? bytes.length : firstZeroIndex;
|
|
20
|
+
const text = String.fromCharCode(...bytes.subarray(0, textLength));
|
|
21
|
+
// only the characters `encodeReferralCode` keeps, so the text never points to a different code
|
|
22
|
+
if (!REFERRAL_CODE_REGEX.test(text) || bytes.subarray(textLength).some((byte) => byte !== 0)) {
|
|
23
|
+
return hexCode;
|
|
21
24
|
}
|
|
25
|
+
return text;
|
|
22
26
|
}
|
|
23
27
|
export function encodeReferralCode(code) {
|
|
28
|
+
if (isReferralCodeHex(code)) {
|
|
29
|
+
// not an address, so no checksum casing; lowercase matches what chain and subgraph return
|
|
30
|
+
return code.toLowerCase();
|
|
31
|
+
}
|
|
24
32
|
let final = code.replace(/[^\w_]/g, ""); // replace everything other than numbers, string and underscor to ''
|
|
25
33
|
if (final.length > MAX_REFERRAL_CODE_LENGTH) {
|
|
26
34
|
return zeroHash;
|
|
@@ -1,27 +1,46 @@
|
|
|
1
1
|
import { zeroHash } from "viem";
|
|
2
|
+
import { isContractsV2_2_1 } from "../../configs/contracts";
|
|
2
3
|
import { SUBACCOUNT_ORDER_ACTION } from "../../configs/dataStore";
|
|
3
4
|
import { getExpressContractAddress, getGelatoRelayRouterDomain } from "../express";
|
|
5
|
+
export const SUBACCOUNT_APPROVAL_TYPES = {
|
|
6
|
+
SubaccountApproval: [
|
|
7
|
+
{ name: "subaccount", type: "address" },
|
|
8
|
+
{ name: "shouldAdd", type: "bool" },
|
|
9
|
+
{ name: "expiresAt", type: "uint256" },
|
|
10
|
+
{ name: "maxAllowedCount", type: "uint256" },
|
|
11
|
+
{ name: "actionType", type: "bytes32" },
|
|
12
|
+
{ name: "nonce", type: "uint256" },
|
|
13
|
+
{ name: "desChainId", type: "uint256" },
|
|
14
|
+
{ name: "deadline", type: "uint256" },
|
|
15
|
+
{ name: "integrationId", type: "bytes32" },
|
|
16
|
+
],
|
|
17
|
+
};
|
|
18
|
+
export const SUBACCOUNT_APPROVAL_TYPES_V2_2_1 = {
|
|
19
|
+
SubaccountApproval: [
|
|
20
|
+
{ name: "account", type: "address" },
|
|
21
|
+
{ name: "subaccount", type: "address" },
|
|
22
|
+
{ name: "shouldAdd", type: "bool" },
|
|
23
|
+
{ name: "expiresAt", type: "uint256" },
|
|
24
|
+
{ name: "maxAllowedCount", type: "uint256" },
|
|
25
|
+
{ name: "actionType", type: "bytes32" },
|
|
26
|
+
{ name: "nonce", type: "uint256" },
|
|
27
|
+
{ name: "desChainId", type: "uint256" },
|
|
28
|
+
{ name: "deadline", type: "uint256" },
|
|
29
|
+
{ name: "integrationId", type: "bytes32" },
|
|
30
|
+
{ name: "revocationCounter", type: "uint256" },
|
|
31
|
+
{ name: "eip6492SignatureWrapperHash", type: "bytes32" },
|
|
32
|
+
],
|
|
33
|
+
};
|
|
4
34
|
export function getSubaccountApprovalTypedData(params) {
|
|
5
|
-
const { chainId, subaccountAddress, shouldAdd, expiresAt, maxAllowedCount, nonce, signingChainId } = params;
|
|
35
|
+
const { chainId, subaccountAddress, shouldAdd, expiresAt, maxAllowedCount, nonce, account, revocationCounter, signingChainId, } = params;
|
|
36
|
+
const isV2_2_1 = isContractsV2_2_1(chainId);
|
|
6
37
|
const relayRouterAddress = getExpressContractAddress(chainId, {
|
|
7
38
|
isSubaccount: true,
|
|
8
39
|
isMultichain: false,
|
|
9
40
|
scope: "subaccount",
|
|
10
41
|
});
|
|
11
42
|
const domain = getGelatoRelayRouterDomain((signingChainId ?? chainId), relayRouterAddress);
|
|
12
|
-
const types =
|
|
13
|
-
SubaccountApproval: [
|
|
14
|
-
{ name: "subaccount", type: "address" },
|
|
15
|
-
{ name: "shouldAdd", type: "bool" },
|
|
16
|
-
{ name: "expiresAt", type: "uint256" },
|
|
17
|
-
{ name: "maxAllowedCount", type: "uint256" },
|
|
18
|
-
{ name: "actionType", type: "bytes32" },
|
|
19
|
-
{ name: "nonce", type: "uint256" },
|
|
20
|
-
{ name: "desChainId", type: "uint256" },
|
|
21
|
-
{ name: "deadline", type: "uint256" },
|
|
22
|
-
{ name: "integrationId", type: "bytes32" },
|
|
23
|
-
],
|
|
24
|
-
};
|
|
43
|
+
const types = isV2_2_1 ? SUBACCOUNT_APPROVAL_TYPES_V2_2_1 : SUBACCOUNT_APPROVAL_TYPES;
|
|
25
44
|
const message = {
|
|
26
45
|
subaccount: subaccountAddress,
|
|
27
46
|
shouldAdd,
|
|
@@ -32,6 +51,9 @@ export function getSubaccountApprovalTypedData(params) {
|
|
|
32
51
|
desChainId: chainId.toString(),
|
|
33
52
|
deadline: expiresAt.toString(),
|
|
34
53
|
integrationId: zeroHash,
|
|
54
|
+
...(isV2_2_1
|
|
55
|
+
? { account, revocationCounter: revocationCounter.toString(), eip6492SignatureWrapperHash: zeroHash }
|
|
56
|
+
: {}),
|
|
35
57
|
};
|
|
36
58
|
return {
|
|
37
59
|
domain,
|
|
@@ -1,24 +1,38 @@
|
|
|
1
1
|
import { encodeAbiParameters, keccak256, zeroHash } from "viem";
|
|
2
|
-
|
|
2
|
+
import { isContractsV2_2_1 } from "../../configs/contracts";
|
|
3
|
+
const SUBACCOUNT_APPROVAL_COMPONENTS = [
|
|
4
|
+
{ name: "subaccount", type: "address" },
|
|
5
|
+
{ name: "shouldAdd", type: "bool" },
|
|
6
|
+
{ name: "expiresAt", type: "uint256" },
|
|
7
|
+
{ name: "maxAllowedCount", type: "uint256" },
|
|
8
|
+
{ name: "actionType", type: "bytes32" },
|
|
9
|
+
{ name: "nonce", type: "uint256" },
|
|
10
|
+
{ name: "desChainId", type: "uint256" },
|
|
11
|
+
{ name: "deadline", type: "uint256" },
|
|
12
|
+
{ name: "integrationId", type: "bytes32" },
|
|
13
|
+
{ name: "signature", type: "bytes" },
|
|
14
|
+
];
|
|
15
|
+
const SUBACCOUNT_APPROVAL_COMPONENTS_V2_2_1 = [
|
|
16
|
+
{ name: "subaccount", type: "address" },
|
|
17
|
+
{ name: "shouldAdd", type: "bool" },
|
|
18
|
+
{ name: "expiresAt", type: "uint256" },
|
|
19
|
+
{ name: "maxAllowedCount", type: "uint256" },
|
|
20
|
+
{ name: "actionType", type: "bytes32" },
|
|
21
|
+
{ name: "nonce", type: "uint256" },
|
|
22
|
+
{ name: "desChainId", type: "uint256" },
|
|
23
|
+
{ name: "deadline", type: "uint256" },
|
|
24
|
+
{ name: "integrationId", type: "bytes32" },
|
|
25
|
+
{ name: "revocationCounter", type: "uint256" },
|
|
26
|
+
{ name: "eip6492SignatureWrapperHash", type: "bytes32" },
|
|
27
|
+
{ name: "signature", type: "bytes" },
|
|
28
|
+
];
|
|
29
|
+
export function hashSubaccountApproval(chainId, approval) {
|
|
3
30
|
if (!approval || !approval.subaccount) {
|
|
4
31
|
return zeroHash;
|
|
5
32
|
}
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
{ name: "subaccount", type: "address" },
|
|
11
|
-
{ name: "shouldAdd", type: "bool" },
|
|
12
|
-
{ name: "expiresAt", type: "uint256" },
|
|
13
|
-
{ name: "maxAllowedCount", type: "uint256" },
|
|
14
|
-
{ name: "actionType", type: "bytes32" },
|
|
15
|
-
{ name: "nonce", type: "uint256" },
|
|
16
|
-
{ name: "desChainId", type: "uint256" },
|
|
17
|
-
{ name: "deadline", type: "uint256" },
|
|
18
|
-
{ name: "integrationId", type: "bytes32" },
|
|
19
|
-
{ name: "signature", type: "bytes" },
|
|
20
|
-
],
|
|
21
|
-
},
|
|
22
|
-
], [approval]);
|
|
33
|
+
const components = isContractsV2_2_1(chainId)
|
|
34
|
+
? SUBACCOUNT_APPROVAL_COMPONENTS_V2_2_1
|
|
35
|
+
: SUBACCOUNT_APPROVAL_COMPONENTS;
|
|
36
|
+
const encodedData = encodeAbiParameters([{ type: "tuple", components }], [approval]);
|
|
23
37
|
return keccak256(encodedData);
|
|
24
38
|
}
|
|
@@ -2,6 +2,6 @@ export * from "./types";
|
|
|
2
2
|
export { generateSubaccount, decryptSubaccountPrivateKey, createSubaccountSignerFromConfig, } from "./generateSubaccount";
|
|
3
3
|
export { hashSubaccountApproval } from "./hashSubaccountApproval";
|
|
4
4
|
export { getEmptySubaccountApproval } from "./getEmptySubaccountApproval";
|
|
5
|
-
export { getSubaccountApprovalTypedData } from "./getSubaccountApprovalTypedData";
|
|
6
|
-
export { isEmptySubaccountApproval, isSameSubaccountApproval, isSubaccountApprovalExpired, isSubaccountApprovalNonceExpired, isSubaccountApprovalSynced, isSubaccountStatusUsable, parseSubaccountStatus, shouldRefreshExplicitSubaccountApproval, toSignedSubaccountApproval, } from "./state";
|
|
5
|
+
export { getSubaccountApprovalTypedData, SUBACCOUNT_APPROVAL_TYPES, SUBACCOUNT_APPROVAL_TYPES_V2_2_1, } from "./getSubaccountApprovalTypedData";
|
|
6
|
+
export { isEmptySubaccountApproval, isSameSubaccountApproval, isSubaccountApprovalExpired, isSubaccountApprovalNonceExpired, isSubaccountApprovalRevoked, isSubaccountApprovalSynced, isSubaccountStatusUsable, parseSubaccountStatus, shouldRefreshExplicitSubaccountApproval, toSignedSubaccountApproval, } from "./state";
|
|
7
7
|
export { fetchSubaccountStatus, prepareSubaccountApproval, signSubaccountApproval } from "./api";
|
|
@@ -4,7 +4,7 @@ import { nowInSeconds } from "../time";
|
|
|
4
4
|
import { fetchSubaccountStatus, prepareSubaccountApproval, signSubaccountApproval } from "./api";
|
|
5
5
|
import { generateSubaccount } from "./generateSubaccount";
|
|
6
6
|
import { getEmptySubaccountApproval } from "./getEmptySubaccountApproval";
|
|
7
|
-
import { isEmptySubaccountApproval, isSameSubaccountApproval, isSubaccountApprovalExpired, isSubaccountApprovalNonceExpired, isSubaccountApprovalSynced, isSubaccountStatusUsable, parseSubaccountStatus, shouldRefreshExplicitSubaccountApproval, toSignedSubaccountApproval, } from "./state";
|
|
7
|
+
import { isEmptySubaccountApproval, isSameSubaccountApproval, isSubaccountApprovalExpired, isSubaccountApprovalNonceExpired, isSubaccountApprovalRevoked, isSubaccountApprovalSynced, isSubaccountStatusUsable, parseSubaccountStatus, shouldRefreshExplicitSubaccountApproval, toSignedSubaccountApproval, } from "./state";
|
|
8
8
|
const sdkSubaccountSigners = new WeakMap();
|
|
9
9
|
const MAX_PREPARED_SUBACCOUNT_APPROVALS = 100;
|
|
10
10
|
const SUBACCOUNT_STATUS_CACHE_TTL_MS = 15000;
|
|
@@ -133,6 +133,7 @@ export function reconcileSdkSubaccountApproval(subaccount) {
|
|
|
133
133
|
}
|
|
134
134
|
if (isSubaccountApprovalSynced(approval, status) ||
|
|
135
135
|
isSubaccountApprovalNonceExpired(approval, status) ||
|
|
136
|
+
isSubaccountApprovalRevoked(approval, status.revocationCounter) ||
|
|
136
137
|
isSubaccountApprovalExpired(approval)) {
|
|
137
138
|
subaccount.approval = undefined;
|
|
138
139
|
}
|
|
@@ -339,7 +340,6 @@ export async function executeExpressOrderWithSubaccount(client, request, signer)
|
|
|
339
340
|
requestId: prepared.requestId,
|
|
340
341
|
signature,
|
|
341
342
|
from: isSubaccountOrder ? request.from : signer.address,
|
|
342
|
-
idempotencyKey: prepared.idempotencyKey,
|
|
343
343
|
eip712Data: {
|
|
344
344
|
batchParams: prepared.payload.batchParams,
|
|
345
345
|
relayParams: prepared.payload.relayParams,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { zeroHash } from "viem";
|
|
1
2
|
import { toBigInt } from "../numbers";
|
|
2
3
|
import { nowInSeconds } from "../time";
|
|
3
4
|
export function parseSubaccountStatus(status) {
|
|
@@ -10,6 +11,7 @@ export function parseSubaccountStatus(status) {
|
|
|
10
11
|
approvalNonce: toBigInt(status.approvalNonce) ?? 0n,
|
|
11
12
|
multichainApprovalNonce: toBigInt(status.multichainApprovalNonce) ?? 0n,
|
|
12
13
|
integrationId: status.integrationId ?? undefined,
|
|
14
|
+
revocationCounter: toBigInt(status.revocationCounter) ?? 0n,
|
|
13
15
|
};
|
|
14
16
|
}
|
|
15
17
|
export function isSubaccountStatusUsable(status) {
|
|
@@ -31,11 +33,15 @@ export function shouldRefreshExplicitSubaccountApproval(approval, status, pendin
|
|
|
31
33
|
return (status !== undefined &&
|
|
32
34
|
(isSubaccountApprovalSynced(approval, status) ||
|
|
33
35
|
isSubaccountApprovalNonceExpired(approval, status) ||
|
|
36
|
+
isSubaccountApprovalRevoked(approval, status.revocationCounter) ||
|
|
34
37
|
isSubaccountApprovalExpired(approval)));
|
|
35
38
|
}
|
|
36
39
|
export function isSubaccountApprovalNonceExpired(approval, status) {
|
|
37
40
|
return toBigInt(approval.nonce) !== status.approvalNonce;
|
|
38
41
|
}
|
|
42
|
+
export function isSubaccountApprovalRevoked(approval, revocationCounter) {
|
|
43
|
+
return (toBigInt(approval.revocationCounter) ?? 0n) < revocationCounter;
|
|
44
|
+
}
|
|
39
45
|
export function isSubaccountApprovalSynced(approval, status) {
|
|
40
46
|
return (status.active &&
|
|
41
47
|
status.maxAllowedCount === toBigInt(approval.maxAllowedCount) &&
|
|
@@ -56,6 +62,8 @@ export function toSignedSubaccountApproval(prepared, signature) {
|
|
|
56
62
|
desChainId: toBigInt(prepared.message.desChainId) ?? 0n,
|
|
57
63
|
deadline: toBigInt(prepared.message.deadline) ?? 0n,
|
|
58
64
|
integrationId: prepared.message.integrationId,
|
|
65
|
+
revocationCounter: toBigInt(prepared.message.revocationCounter) ?? 0n,
|
|
66
|
+
eip6492SignatureWrapperHash: prepared.message.eip6492SignatureWrapperHash ?? zeroHash,
|
|
59
67
|
signature,
|
|
60
68
|
signedAt: Date.now(),
|
|
61
69
|
signatureChainId: prepared.domain.chainId,
|
|
@@ -1,21 +1,23 @@
|
|
|
1
|
+
import { SOLANA_NETWORK_ID } from "../../configs/chains";
|
|
1
2
|
import { MARKETS } from "../../configs/markets";
|
|
2
3
|
import { buildMarketsAdjacencyGraph } from "./buildMarketsAdjacencyGraph";
|
|
3
4
|
import { findReachableTokens } from "./findReachableTokens";
|
|
4
5
|
import { findSwapPathsBetweenTokens } from "./findSwapPathsBetweenTokens";
|
|
6
|
+
const SWAP_MARKETS = { ...MARKETS, [SOLANA_NETWORK_ID]: {} };
|
|
5
7
|
const MARKETS_ADJACENCY_GRAPH = {};
|
|
6
|
-
for (const chainId in
|
|
7
|
-
const markets =
|
|
8
|
+
for (const chainId in SWAP_MARKETS) {
|
|
9
|
+
const markets = SWAP_MARKETS[chainId];
|
|
8
10
|
const chainGraph = buildMarketsAdjacencyGraph(markets);
|
|
9
11
|
MARKETS_ADJACENCY_GRAPH[chainId] = chainGraph;
|
|
10
12
|
}
|
|
11
13
|
const TOKEN_SWAP_PATHS = {};
|
|
12
|
-
for (const chainId in
|
|
14
|
+
for (const chainId in SWAP_MARKETS) {
|
|
13
15
|
const chainGraph = MARKETS_ADJACENCY_GRAPH[chainId];
|
|
14
16
|
const chainSwapPaths = findSwapPathsBetweenTokens(chainGraph);
|
|
15
17
|
TOKEN_SWAP_PATHS[chainId] = chainSwapPaths;
|
|
16
18
|
}
|
|
17
19
|
const REACHABLE_TOKENS = {};
|
|
18
|
-
for (const chainId in
|
|
20
|
+
for (const chainId in SWAP_MARKETS) {
|
|
19
21
|
const chainGraph = MARKETS_ADJACENCY_GRAPH[chainId];
|
|
20
22
|
const chainReachableTokens = findReachableTokens(chainGraph);
|
|
21
23
|
REACHABLE_TOKENS[chainId] = chainReachableTokens;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getMarketsByChainId } from "../../configs/markets";
|
|
2
2
|
import { convertTokenAddress, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
3
3
|
import { LRUCache } from "../LruCache";
|
|
4
4
|
import { getIsMarketAvailableForExpressSwaps } from "../markets";
|
|
@@ -20,7 +20,7 @@ function buildMarketAdjacencyGraph(chainId, disabledMarkets) {
|
|
|
20
20
|
return cachedGraph;
|
|
21
21
|
}
|
|
22
22
|
const disabledMarketAddresses = disabledMarkets;
|
|
23
|
-
const strippedMarkets = Object.fromEntries(Object.entries(
|
|
23
|
+
const strippedMarkets = Object.fromEntries(Object.entries(getMarketsByChainId(chainId)).filter(([marketAddress]) => !disabledMarketAddresses.includes(marketAddress)));
|
|
24
24
|
const graph = buildMarketsAdjacencyGraph(strippedMarkets);
|
|
25
25
|
DEBUG_MARKET_ADJACENCY_GRAPH_CACHE.set(cacheKey, graph);
|
|
26
26
|
return graph;
|