@gmx-io/sdk 1.0.5 → 1.0.6
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/abis/ArbitrumNodeInterface.json +105 -0
- package/build/cjs/src/abis/CustomErrors.json +613 -16
- package/build/cjs/src/abis/ERC20PermitInterface.json +118 -0
- package/build/cjs/src/abis/GelatoRelayRouter.json +1426 -0
- package/build/cjs/src/abis/RelayParams.json +110 -0
- package/build/cjs/src/abis/SubaccountApproval.json +17 -0
- package/build/cjs/src/abis/SubaccountGelatoRelayRouter.json +1885 -0
- package/build/cjs/src/abis/SyntheticsReader.json +1 -131
- package/build/cjs/src/abis/__tests__/abis.spec.js +40 -0
- package/build/cjs/src/abis/index.js +8 -0
- package/build/cjs/src/configs/__tests__/permits.spec.js +109 -0
- package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -1
- package/build/cjs/src/configs/chains.js +36 -5
- package/build/cjs/src/configs/contracts.js +15 -3
- package/build/cjs/src/configs/dataStore.js +26 -4
- package/build/cjs/src/configs/express.js +35 -0
- package/build/cjs/src/configs/factors.js +4 -3
- package/build/cjs/src/configs/tokens.js +50 -0
- package/build/cjs/src/configs/twap.js +10 -0
- package/build/cjs/src/index.js +3 -3
- package/build/cjs/src/modules/markets/index.js +1 -0
- package/build/cjs/src/modules/markets/query-builders.js +4 -0
- package/build/cjs/src/modules/orders/helpers.js +2 -0
- package/build/cjs/src/modules/orders/transactions/createDecreaseOrderTxn.js +1 -1
- package/build/cjs/src/modules/orders/transactions/createIncreaseOrderTxn.js +6 -3
- package/build/cjs/src/modules/orders/transactions/createSwapOrderTxn.js +1 -1
- package/build/cjs/src/modules/utils/utils.js +11 -1
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +119 -0
- package/build/cjs/src/test/mock.js +30 -1
- package/build/cjs/src/types/twap.js +2 -0
- package/build/cjs/src/utils/__tests__/numbers.spec.js +217 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/batchUtils.spec.js +241 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +595 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +296 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/externalCalls.spec.js +118 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +762 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +469 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +80 -0
- package/build/cjs/src/utils/__tests__/parseError.spec.js +263 -0
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +45 -0
- package/build/cjs/src/utils/bigmath.js +3 -0
- package/build/cjs/src/utils/callContract.js +2 -2
- package/build/cjs/src/utils/errors/index.js +18 -0
- package/build/cjs/src/utils/errors/parseError.js +150 -0
- package/build/cjs/src/utils/{contracts.js → errors/transactionsErrors.js} +35 -3
- package/build/cjs/src/utils/fees/executionFee.js +52 -1
- package/build/cjs/src/utils/fees/index.js +8 -4
- package/build/cjs/src/utils/gelatoRelay.js +5 -0
- package/build/cjs/src/utils/hash.js +6 -1
- package/build/cjs/src/utils/marketKeysAndConfigs.js +4 -0
- package/build/cjs/src/utils/markets.js +5 -1
- package/build/cjs/src/utils/numbers.js +559 -5
- package/build/cjs/src/utils/orderTransactions.js +638 -0
- package/build/cjs/src/utils/orders.js +7 -1
- package/build/cjs/src/utils/simulateExecuteOrder.js +6 -2
- package/build/cjs/src/utils/swap/__tests__/mockRouting.spec.js +11 -6
- package/build/cjs/src/utils/swap/__tests__/swapRouting.spec.js +4 -4
- package/build/cjs/src/utils/swap/__tests__/swapStats.spec.js +9 -0
- package/build/cjs/src/utils/swap/swapPath.js +30 -13
- package/build/cjs/src/utils/swap/swapRouting.js +4 -2
- package/build/cjs/src/utils/swap/swapStats.js +6 -5
- package/build/cjs/src/utils/testUtil.js +1 -1
- package/build/cjs/src/utils/time.js +30 -0
- package/build/cjs/src/utils/tokens.js +16 -2
- package/build/cjs/src/utils/twap/index.js +53 -0
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +31 -0
- package/build/cjs/src/utils/types.js +13 -0
- package/build/esm/src/abis/ArbitrumNodeInterface.json +105 -0
- package/build/esm/src/abis/CustomErrors.json +613 -16
- package/build/esm/src/abis/ERC20PermitInterface.json +118 -0
- package/build/esm/src/abis/GelatoRelayRouter.json +1426 -0
- package/build/esm/src/abis/RelayParams.json +110 -0
- package/build/esm/src/abis/SubaccountApproval.json +17 -0
- package/build/esm/src/abis/SubaccountGelatoRelayRouter.json +1885 -0
- package/build/esm/src/abis/SyntheticsReader.json +1 -131
- package/build/esm/src/abis/__tests__/abis.spec.js +35 -0
- package/build/esm/src/abis/index.js +8 -0
- package/build/esm/src/configs/__tests__/permits.spec.js +107 -0
- package/build/esm/src/configs/__tests__/tokens.spec.js +3 -3
- package/build/esm/src/configs/chains.js +33 -3
- package/build/esm/src/configs/contracts.js +15 -3
- package/build/esm/src/configs/dataStore.js +19 -1
- package/build/esm/src/configs/express.js +29 -0
- package/build/esm/src/configs/factors.js +1 -3
- package/build/esm/src/configs/tokens.js +50 -0
- package/build/esm/src/configs/twap.js +7 -0
- package/build/esm/src/index.js +4 -4
- package/build/esm/src/modules/markets/index.js +1 -0
- package/build/esm/src/modules/markets/query-builders.js +4 -0
- package/build/esm/src/modules/orders/helpers.js +2 -0
- package/build/esm/src/modules/orders/transactions/createDecreaseOrderTxn.js +1 -1
- package/build/esm/src/modules/orders/transactions/createIncreaseOrderTxn.js +6 -3
- package/build/esm/src/modules/orders/transactions/createSwapOrderTxn.js +1 -1
- package/build/esm/src/modules/utils/utils.js +13 -3
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +119 -0
- package/build/esm/src/test/mock.js +28 -0
- package/build/esm/src/types/twap.js +1 -0
- package/build/esm/src/utils/__tests__/numbers.spec.js +219 -2
- package/build/esm/src/utils/__tests__/orderTransactions/batchUtils.spec.js +239 -0
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +593 -0
- package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +294 -0
- package/build/esm/src/utils/__tests__/orderTransactions/externalCalls.spec.js +116 -0
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +760 -0
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +467 -0
- package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +78 -0
- package/build/esm/src/utils/__tests__/parseError.spec.js +261 -0
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +43 -0
- package/build/esm/src/utils/bigmath.js +3 -0
- package/build/esm/src/utils/callContract.js +3 -3
- package/build/esm/src/utils/errors/index.js +2 -0
- package/build/esm/src/utils/errors/parseError.js +141 -0
- package/build/esm/src/utils/{contracts.js → errors/transactionsErrors.js} +30 -1
- package/build/esm/src/utils/fees/executionFee.js +48 -1
- package/build/esm/src/utils/fees/index.js +8 -4
- package/build/esm/src/utils/gelatoRelay.js +2 -0
- package/build/esm/src/utils/hash.js +5 -1
- package/build/esm/src/utils/marketKeysAndConfigs.js +5 -1
- package/build/esm/src/utils/markets.js +3 -0
- package/build/esm/src/utils/numbers.js +516 -1
- package/build/esm/src/utils/orderTransactions.js +607 -0
- package/build/esm/src/utils/orders.js +5 -0
- package/build/esm/src/utils/simulateExecuteOrder.js +6 -2
- package/build/esm/src/utils/swap/__tests__/mockRouting.spec.js +11 -6
- package/build/esm/src/utils/swap/__tests__/swapRouting.spec.js +4 -4
- package/build/esm/src/utils/swap/__tests__/swapStats.spec.js +9 -0
- package/build/esm/src/utils/swap/swapPath.js +30 -13
- package/build/esm/src/utils/swap/swapRouting.js +4 -2
- package/build/esm/src/utils/swap/swapStats.js +6 -5
- package/build/esm/src/utils/testUtil.js +2 -2
- package/build/esm/src/utils/time.js +23 -0
- package/build/esm/src/utils/tokens.js +14 -1
- package/build/esm/src/utils/twap/index.js +44 -0
- package/build/esm/src/utils/twap/uiFeeReceiver.js +24 -0
- package/build/esm/src/utils/types.js +8 -0
- package/build/types/src/abis/__tests__/abis.spec.d.ts +1 -0
- package/build/types/src/abis/index.d.ts +1 -1
- package/build/types/src/configs/__tests__/permits.spec.d.ts +1 -0
- package/build/types/src/configs/chains.d.ts +29 -1
- package/build/types/src/configs/contracts.d.ts +238 -1
- package/build/types/src/configs/dataStore.d.ts +10 -0
- package/build/types/src/configs/express.d.ts +12 -0
- package/build/types/src/configs/factors.d.ts +1 -3
- package/build/types/src/configs/tokens.d.ts +2 -2
- package/build/types/src/configs/twap.d.ts +7 -0
- package/build/types/src/modules/markets/query-builders.d.ts +1 -1
- package/build/types/src/modules/markets/types.d.ts +2 -2
- package/build/types/src/modules/orders/transactions/createIncreaseOrderTxn.d.ts +1 -1
- package/build/types/src/test/mock.d.ts +16 -1
- package/build/types/src/types/fees.d.ts +9 -0
- package/build/types/src/types/markets.d.ts +1 -0
- package/build/types/src/types/orders.d.ts +3 -0
- package/build/types/src/types/tokens.d.ts +27 -0
- package/build/types/src/types/trade.d.ts +2 -19
- package/build/types/src/types/twap.d.ts +11 -0
- package/build/types/src/utils/__tests__/orderTransactions/batchUtils.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/decreaseOrders.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/externalCalls.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/increaseOrders.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/swapOrders.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/orderTransactions/updateOrder.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/parseError.spec.d.ts +1 -0
- package/build/types/src/utils/__tests__/uiFeeReceiver.spec.d.ts +1 -0
- package/build/types/src/utils/bigmath.d.ts +1 -0
- package/build/types/src/utils/errors/index.d.ts +2 -0
- package/build/types/src/utils/errors/parseError.d.ts +50 -0
- package/build/types/src/utils/errors/transactionsErrors.d.ts +34 -0
- package/build/types/src/utils/fees/executionFee.d.ts +35 -2
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/gelatoRelay.d.ts +2 -0
- package/build/types/src/utils/hash.d.ts +2 -0
- package/build/types/src/utils/marketKeysAndConfigs.d.ts +1 -0
- package/build/types/src/utils/markets.d.ts +1 -0
- package/build/types/src/utils/numbers.d.ts +110 -1
- package/build/types/src/utils/orderTransactions.d.ts +231 -0
- package/build/types/src/utils/orders.d.ts +2 -1
- package/build/types/src/utils/simulateExecuteOrder.d.ts +4 -0
- package/build/types/src/utils/swap/swapPath.d.ts +5 -5
- package/build/types/src/utils/swap/swapRouting.d.ts +2 -2
- package/build/types/src/utils/swap/swapStats.d.ts +2 -0
- package/build/types/src/utils/time.d.ts +14 -0
- package/build/types/src/utils/tokens.d.ts +6 -5
- package/build/types/src/utils/twap/index.d.ts +17 -0
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +9 -0
- package/build/types/src/utils/types.d.ts +5 -0
- package/package.json +3 -1
- package/build/types/src/utils/contracts.d.ts +0 -19
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
import uniq from "lodash/uniq";
|
|
2
|
+
import { encodeFunctionData, zeroAddress, zeroHash } from "viem";
|
|
3
|
+
import ExchangeRouterAbi from "../abis/ExchangeRouter.json";
|
|
4
|
+
import ERC20ABI from "../abis/Token.json";
|
|
5
|
+
import { getExcessiveExecutionFee, getHighExecutionFee } from "../configs/chains";
|
|
6
|
+
import { getContract } from "../configs/contracts";
|
|
7
|
+
import { convertTokenAddress, getToken, getWrappedToken, NATIVE_TOKEN_ADDRESS } from "../configs/tokens";
|
|
8
|
+
import { DecreasePositionSwapType, OrderType } from "../types/orders";
|
|
9
|
+
import { expandDecimals, MaxUint256, USD_DECIMALS } from "./numbers";
|
|
10
|
+
import { getByKey } from "./objects";
|
|
11
|
+
import { isIncreaseOrderType, isSwapOrderType } from "./orders";
|
|
12
|
+
import { convertToContractPrice, convertToUsd } from "./tokens";
|
|
13
|
+
import { applySlippageToMinOut, applySlippageToPrice } from "./trade";
|
|
14
|
+
import { getTwapValidFromTime } from "./twap";
|
|
15
|
+
import { createTwapUiFeeReceiver } from "./twap/uiFeeReceiver";
|
|
16
|
+
export function buildSwapOrderPayload(p) {
|
|
17
|
+
const tokenTransfersParams = buildTokenTransfersParamsForIncreaseOrSwap(p);
|
|
18
|
+
const orderPayload = {
|
|
19
|
+
addresses: {
|
|
20
|
+
receiver: p.receiver,
|
|
21
|
+
cancellationReceiver: zeroAddress,
|
|
22
|
+
callbackContract: zeroAddress,
|
|
23
|
+
uiFeeReceiver: p.uiFeeReceiver ?? zeroAddress,
|
|
24
|
+
market: zeroAddress,
|
|
25
|
+
initialCollateralToken: tokenTransfersParams.initialCollateralTokenAddress,
|
|
26
|
+
swapPath: tokenTransfersParams.swapPath,
|
|
27
|
+
},
|
|
28
|
+
numbers: {
|
|
29
|
+
sizeDeltaUsd: 0n,
|
|
30
|
+
initialCollateralDeltaAmount: tokenTransfersParams.initialCollateralDeltaAmount,
|
|
31
|
+
// triggerRatio of limit swaps is used in trade history
|
|
32
|
+
triggerPrice: p.triggerRatio ?? 0n,
|
|
33
|
+
acceptablePrice: 0n,
|
|
34
|
+
executionFee: p.executionFeeAmount,
|
|
35
|
+
callbackGasLimit: 0n,
|
|
36
|
+
minOutputAmount: applySlippageToMinOut(p.allowedSlippage, tokenTransfersParams.minOutputAmount),
|
|
37
|
+
validFromTime: p.validFromTime ?? 0n,
|
|
38
|
+
},
|
|
39
|
+
orderType: p.orderType,
|
|
40
|
+
decreasePositionSwapType: DecreasePositionSwapType.NoSwap,
|
|
41
|
+
isLong: false,
|
|
42
|
+
shouldUnwrapNativeToken: tokenTransfersParams.isNativePayment || tokenTransfersParams.isNativeReceive,
|
|
43
|
+
autoCancel: p.autoCancel,
|
|
44
|
+
referralCode: p.referralCode ?? zeroHash,
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
params: p,
|
|
48
|
+
orderPayload,
|
|
49
|
+
tokenTransfersParams,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export function buildIncreaseOrderPayload(p) {
|
|
53
|
+
const tokenTransfersParams = buildTokenTransfersParamsForIncreaseOrSwap({
|
|
54
|
+
...p,
|
|
55
|
+
minOutputAmount: 0n,
|
|
56
|
+
receiveTokenAddress: undefined,
|
|
57
|
+
});
|
|
58
|
+
const indexToken = getToken(p.chainId, p.indexTokenAddress);
|
|
59
|
+
let acceptablePrice;
|
|
60
|
+
if (p.acceptablePrice === MaxUint256) {
|
|
61
|
+
acceptablePrice = MaxUint256;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
acceptablePrice = convertToContractPrice(applySlippageToPrice(p.allowedSlippage, p.acceptablePrice, true, p.isLong), indexToken.decimals);
|
|
65
|
+
}
|
|
66
|
+
let triggerPrice;
|
|
67
|
+
if (p.triggerPrice === MaxUint256) {
|
|
68
|
+
triggerPrice = MaxUint256;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
triggerPrice = convertToContractPrice(p.triggerPrice ?? 0n, indexToken.decimals);
|
|
72
|
+
}
|
|
73
|
+
const orderPayload = {
|
|
74
|
+
addresses: {
|
|
75
|
+
receiver: p.receiver,
|
|
76
|
+
cancellationReceiver: zeroAddress,
|
|
77
|
+
callbackContract: zeroAddress,
|
|
78
|
+
uiFeeReceiver: p.uiFeeReceiver ?? zeroAddress,
|
|
79
|
+
market: p.marketAddress,
|
|
80
|
+
initialCollateralToken: tokenTransfersParams.initialCollateralTokenAddress,
|
|
81
|
+
swapPath: tokenTransfersParams.swapPath,
|
|
82
|
+
},
|
|
83
|
+
numbers: {
|
|
84
|
+
sizeDeltaUsd: p.sizeDeltaUsd,
|
|
85
|
+
initialCollateralDeltaAmount: tokenTransfersParams.initialCollateralDeltaAmount,
|
|
86
|
+
triggerPrice,
|
|
87
|
+
acceptablePrice,
|
|
88
|
+
executionFee: p.executionFeeAmount,
|
|
89
|
+
callbackGasLimit: 0n,
|
|
90
|
+
minOutputAmount: applySlippageToMinOut(p.allowedSlippage, tokenTransfersParams.minOutputAmount),
|
|
91
|
+
validFromTime: p.validFromTime ?? 0n,
|
|
92
|
+
},
|
|
93
|
+
orderType: p.orderType,
|
|
94
|
+
decreasePositionSwapType: DecreasePositionSwapType.NoSwap,
|
|
95
|
+
isLong: p.isLong,
|
|
96
|
+
shouldUnwrapNativeToken: tokenTransfersParams.isNativePayment,
|
|
97
|
+
autoCancel: p.autoCancel,
|
|
98
|
+
referralCode: p.referralCode ?? zeroHash,
|
|
99
|
+
};
|
|
100
|
+
return {
|
|
101
|
+
params: p,
|
|
102
|
+
orderPayload,
|
|
103
|
+
tokenTransfersParams,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export function buildDecreaseOrderPayload(p) {
|
|
107
|
+
const indexToken = getToken(p.chainId, p.indexTokenAddress);
|
|
108
|
+
const tokenTransfersParams = buildTokenTransfersParamsForDecrease(p);
|
|
109
|
+
let acceptablePrice;
|
|
110
|
+
if (p.acceptablePrice === MaxUint256) {
|
|
111
|
+
acceptablePrice = MaxUint256;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
acceptablePrice = convertToContractPrice(applySlippageToPrice(p.allowedSlippage, p.acceptablePrice, false, p.isLong), indexToken.decimals);
|
|
115
|
+
}
|
|
116
|
+
let triggerPrice;
|
|
117
|
+
if (p.triggerPrice === MaxUint256) {
|
|
118
|
+
triggerPrice = MaxUint256;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
triggerPrice = convertToContractPrice(p.triggerPrice ?? 0n, indexToken.decimals);
|
|
122
|
+
}
|
|
123
|
+
const orderPayload = {
|
|
124
|
+
addresses: {
|
|
125
|
+
receiver: p.receiver,
|
|
126
|
+
cancellationReceiver: zeroAddress,
|
|
127
|
+
callbackContract: zeroAddress,
|
|
128
|
+
uiFeeReceiver: p.uiFeeReceiver ?? zeroAddress,
|
|
129
|
+
market: p.marketAddress,
|
|
130
|
+
initialCollateralToken: tokenTransfersParams.initialCollateralTokenAddress,
|
|
131
|
+
swapPath: tokenTransfersParams.swapPath,
|
|
132
|
+
},
|
|
133
|
+
numbers: {
|
|
134
|
+
sizeDeltaUsd: p.sizeDeltaUsd,
|
|
135
|
+
initialCollateralDeltaAmount: tokenTransfersParams.initialCollateralDeltaAmount,
|
|
136
|
+
triggerPrice,
|
|
137
|
+
acceptablePrice,
|
|
138
|
+
executionFee: p.executionFeeAmount,
|
|
139
|
+
callbackGasLimit: 0n,
|
|
140
|
+
minOutputAmount: applySlippageToMinOut(p.allowedSlippage, tokenTransfersParams.minOutputAmount),
|
|
141
|
+
validFromTime: p.validFromTime ?? 0n,
|
|
142
|
+
},
|
|
143
|
+
orderType: p.orderType,
|
|
144
|
+
decreasePositionSwapType: p.decreasePositionSwapType,
|
|
145
|
+
isLong: p.isLong,
|
|
146
|
+
shouldUnwrapNativeToken: p.receiveTokenAddress === NATIVE_TOKEN_ADDRESS,
|
|
147
|
+
autoCancel: p.autoCancel,
|
|
148
|
+
referralCode: p.referralCode ?? zeroHash,
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
params: p,
|
|
152
|
+
orderPayload,
|
|
153
|
+
tokenTransfersParams,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
export function buildTwapOrdersPayloads(p, twapParams) {
|
|
157
|
+
const uiFeeReceiver = createTwapUiFeeReceiver({ numberOfParts: twapParams.numberOfParts });
|
|
158
|
+
if (isSwapOrderType(p.orderType)) {
|
|
159
|
+
return Array.from({ length: twapParams.numberOfParts }, (_, i) => {
|
|
160
|
+
const params = p;
|
|
161
|
+
return buildSwapOrderPayload({
|
|
162
|
+
chainId: params.chainId,
|
|
163
|
+
receiver: params.receiver,
|
|
164
|
+
executionGasLimit: params.executionGasLimit,
|
|
165
|
+
payTokenAddress: params.payTokenAddress,
|
|
166
|
+
receiveTokenAddress: params.receiveTokenAddress,
|
|
167
|
+
swapPath: params.swapPath,
|
|
168
|
+
externalSwapQuote: undefined,
|
|
169
|
+
minOutputAmount: 0n,
|
|
170
|
+
triggerRatio: params.triggerRatio,
|
|
171
|
+
referralCode: params.referralCode,
|
|
172
|
+
autoCancel: params.autoCancel,
|
|
173
|
+
allowedSlippage: 0,
|
|
174
|
+
payTokenAmount: params.payTokenAmount / BigInt(twapParams.numberOfParts),
|
|
175
|
+
executionFeeAmount: params.executionFeeAmount / BigInt(twapParams.numberOfParts),
|
|
176
|
+
validFromTime: getTwapValidFromTime(twapParams.duration, twapParams.numberOfParts, i),
|
|
177
|
+
orderType: OrderType.LimitSwap,
|
|
178
|
+
uiFeeReceiver,
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (isIncreaseOrderType(p.orderType)) {
|
|
183
|
+
return Array.from({ length: twapParams.numberOfParts }, (_, i) => {
|
|
184
|
+
const params = p;
|
|
185
|
+
const acceptablePrice = params.isLong ? MaxUint256 : 0n;
|
|
186
|
+
const triggerPrice = acceptablePrice;
|
|
187
|
+
return buildIncreaseOrderPayload({
|
|
188
|
+
chainId: params.chainId,
|
|
189
|
+
receiver: params.receiver,
|
|
190
|
+
executionGasLimit: params.executionGasLimit,
|
|
191
|
+
referralCode: params.referralCode,
|
|
192
|
+
autoCancel: params.autoCancel,
|
|
193
|
+
swapPath: params.swapPath,
|
|
194
|
+
externalSwapQuote: undefined,
|
|
195
|
+
marketAddress: params.marketAddress,
|
|
196
|
+
indexTokenAddress: params.indexTokenAddress,
|
|
197
|
+
isLong: params.isLong,
|
|
198
|
+
sizeDeltaUsd: params.sizeDeltaUsd / BigInt(twapParams.numberOfParts),
|
|
199
|
+
sizeDeltaInTokens: params.sizeDeltaInTokens / BigInt(twapParams.numberOfParts),
|
|
200
|
+
payTokenAddress: params.payTokenAddress,
|
|
201
|
+
allowedSlippage: 0,
|
|
202
|
+
payTokenAmount: params.payTokenAmount / BigInt(twapParams.numberOfParts),
|
|
203
|
+
collateralTokenAddress: params.collateralTokenAddress,
|
|
204
|
+
collateralDeltaAmount: params.collateralDeltaAmount / BigInt(twapParams.numberOfParts),
|
|
205
|
+
executionFeeAmount: params.executionFeeAmount / BigInt(twapParams.numberOfParts),
|
|
206
|
+
validFromTime: getTwapValidFromTime(twapParams.duration, twapParams.numberOfParts, i),
|
|
207
|
+
orderType: OrderType.LimitIncrease,
|
|
208
|
+
acceptablePrice,
|
|
209
|
+
triggerPrice,
|
|
210
|
+
uiFeeReceiver,
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return Array.from({ length: twapParams.numberOfParts }, (_, i) => {
|
|
215
|
+
const params = p;
|
|
216
|
+
const acceptablePrice = !params.isLong ? MaxUint256 : 0n;
|
|
217
|
+
const triggerPrice = acceptablePrice;
|
|
218
|
+
return buildDecreaseOrderPayload({
|
|
219
|
+
chainId: params.chainId,
|
|
220
|
+
receiver: params.receiver,
|
|
221
|
+
executionGasLimit: params.executionGasLimit,
|
|
222
|
+
referralCode: params.referralCode,
|
|
223
|
+
autoCancel: params.autoCancel,
|
|
224
|
+
swapPath: params.swapPath,
|
|
225
|
+
externalSwapQuote: undefined,
|
|
226
|
+
marketAddress: params.marketAddress,
|
|
227
|
+
indexTokenAddress: params.indexTokenAddress,
|
|
228
|
+
isLong: params.isLong,
|
|
229
|
+
collateralTokenAddress: params.collateralTokenAddress,
|
|
230
|
+
collateralDeltaAmount: params.collateralDeltaAmount / BigInt(twapParams.numberOfParts),
|
|
231
|
+
sizeDeltaUsd: params.sizeDeltaUsd / BigInt(twapParams.numberOfParts),
|
|
232
|
+
sizeDeltaInTokens: params.sizeDeltaInTokens / BigInt(twapParams.numberOfParts),
|
|
233
|
+
executionFeeAmount: params.executionFeeAmount / BigInt(twapParams.numberOfParts),
|
|
234
|
+
validFromTime: getTwapValidFromTime(twapParams.duration, twapParams.numberOfParts, i),
|
|
235
|
+
orderType: OrderType.LimitDecrease,
|
|
236
|
+
acceptablePrice,
|
|
237
|
+
triggerPrice,
|
|
238
|
+
allowedSlippage: 0,
|
|
239
|
+
uiFeeReceiver,
|
|
240
|
+
minOutputUsd: params.minOutputUsd / BigInt(twapParams.numberOfParts),
|
|
241
|
+
receiveTokenAddress: params.receiveTokenAddress,
|
|
242
|
+
decreasePositionSwapType: params.decreasePositionSwapType,
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
export function getIsTwapOrderPayload(p) {
|
|
247
|
+
return p.numbers.validFromTime !== 0n;
|
|
248
|
+
}
|
|
249
|
+
export function buildUpdateOrderPayload(p) {
|
|
250
|
+
const indexToken = getToken(p.chainId, p.indexTokenAddress);
|
|
251
|
+
return {
|
|
252
|
+
params: p,
|
|
253
|
+
updatePayload: {
|
|
254
|
+
orderKey: p.orderKey,
|
|
255
|
+
sizeDeltaUsd: p.sizeDeltaUsd,
|
|
256
|
+
triggerPrice: isSwapOrderType(p.orderType)
|
|
257
|
+
? p.triggerPrice
|
|
258
|
+
: convertToContractPrice(p.triggerPrice, indexToken.decimals),
|
|
259
|
+
acceptablePrice: convertToContractPrice(p.acceptablePrice, indexToken.decimals),
|
|
260
|
+
minOutputAmount: p.minOutputAmount,
|
|
261
|
+
autoCancel: p.autoCancel,
|
|
262
|
+
validFromTime: 0n,
|
|
263
|
+
executionFeeTopUp: p.executionFeeTopUp,
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
export function getBatchTotalExecutionFee({ batchParams: { createOrderParams, updateOrderParams }, tokensData, chainId, }) {
|
|
268
|
+
let feeTokenAmount = 0n;
|
|
269
|
+
let gasLimit = 0n;
|
|
270
|
+
const wnt = getByKey(tokensData, getWrappedToken(chainId).address);
|
|
271
|
+
if (!wnt) {
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
for (const co of createOrderParams) {
|
|
275
|
+
feeTokenAmount += co.orderPayload.numbers.executionFee;
|
|
276
|
+
gasLimit += co.params.executionGasLimit;
|
|
277
|
+
}
|
|
278
|
+
for (const uo of updateOrderParams) {
|
|
279
|
+
feeTokenAmount += uo.updatePayload.executionFeeTopUp;
|
|
280
|
+
}
|
|
281
|
+
const feeUsd = convertToUsd(feeTokenAmount, wnt.decimals, wnt.prices.maxPrice);
|
|
282
|
+
const isFeeHigh = feeUsd > expandDecimals(getHighExecutionFee(chainId), USD_DECIMALS);
|
|
283
|
+
const isFeeVeryHigh = feeUsd > expandDecimals(getExcessiveExecutionFee(chainId), USD_DECIMALS);
|
|
284
|
+
return {
|
|
285
|
+
feeTokenAmount,
|
|
286
|
+
gasLimit,
|
|
287
|
+
feeUsd,
|
|
288
|
+
feeToken: wnt,
|
|
289
|
+
isFeeHigh,
|
|
290
|
+
isFeeVeryHigh,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
export function getBatchTotalPayCollateralAmount(batchParams) {
|
|
294
|
+
const payAmounts = {};
|
|
295
|
+
for (const co of batchParams.createOrderParams) {
|
|
296
|
+
const payTokenAddress = co.tokenTransfersParams?.payTokenAddress;
|
|
297
|
+
const payTokenAmount = co.tokenTransfersParams?.payTokenAmount;
|
|
298
|
+
if (payTokenAddress && payTokenAmount) {
|
|
299
|
+
payAmounts[payTokenAddress] = (payAmounts[payTokenAddress] ?? 0n) + payTokenAmount;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return payAmounts;
|
|
303
|
+
}
|
|
304
|
+
export function getBatchExternalSwapGasLimit(batchParams) {
|
|
305
|
+
return batchParams.createOrderParams.reduce((acc, co) => {
|
|
306
|
+
const externalSwapQuote = co.params.externalSwapQuote;
|
|
307
|
+
if (externalSwapQuote) {
|
|
308
|
+
return acc + externalSwapQuote.txnData.estimatedGas;
|
|
309
|
+
}
|
|
310
|
+
return acc;
|
|
311
|
+
}, 0n);
|
|
312
|
+
}
|
|
313
|
+
export function buildTokenTransfersParamsForDecrease({ chainId, executionFeeAmount, collateralTokenAddress, collateralDeltaAmount, swapPath, minOutputUsd, receiveTokenAddress, }) {
|
|
314
|
+
const orderVaultAddress = getContract(chainId, "OrderVault");
|
|
315
|
+
const { tokenTransfers, value } = combineTransfers([
|
|
316
|
+
{
|
|
317
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
318
|
+
destination: orderVaultAddress,
|
|
319
|
+
amount: executionFeeAmount,
|
|
320
|
+
},
|
|
321
|
+
]);
|
|
322
|
+
return {
|
|
323
|
+
isNativePayment: false,
|
|
324
|
+
isNativeReceive: receiveTokenAddress === NATIVE_TOKEN_ADDRESS,
|
|
325
|
+
initialCollateralTokenAddress: convertTokenAddress(chainId, collateralTokenAddress, "wrapped"),
|
|
326
|
+
initialCollateralDeltaAmount: collateralDeltaAmount,
|
|
327
|
+
tokenTransfers,
|
|
328
|
+
payTokenAddress: zeroAddress,
|
|
329
|
+
payTokenAmount: 0n,
|
|
330
|
+
minOutputAmount: minOutputUsd,
|
|
331
|
+
swapPath,
|
|
332
|
+
value,
|
|
333
|
+
externalCalls: undefined,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
export function buildTokenTransfersParamsForIncreaseOrSwap({ chainId, receiver, payTokenAddress, payTokenAmount, receiveTokenAddress, executionFeeAmount, externalSwapQuote, minOutputAmount, swapPath, }) {
|
|
337
|
+
const isNativePayment = payTokenAddress === NATIVE_TOKEN_ADDRESS;
|
|
338
|
+
const isNativeReceive = receiveTokenAddress === NATIVE_TOKEN_ADDRESS;
|
|
339
|
+
const orderVaultAddress = getContract(chainId, "OrderVault");
|
|
340
|
+
const externalHandlerAddress = getContract(chainId, "ExternalHandler");
|
|
341
|
+
let finalPayTokenAmount = payTokenAmount;
|
|
342
|
+
const { tokenTransfers, value } = combineTransfers([
|
|
343
|
+
{
|
|
344
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
345
|
+
destination: orderVaultAddress,
|
|
346
|
+
amount: executionFeeAmount,
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
tokenAddress: payTokenAddress,
|
|
350
|
+
destination: externalSwapQuote ? externalHandlerAddress : orderVaultAddress,
|
|
351
|
+
amount: payTokenAmount,
|
|
352
|
+
},
|
|
353
|
+
]);
|
|
354
|
+
let initialCollateralTokenAddress = convertTokenAddress(chainId, payTokenAddress, "wrapped");
|
|
355
|
+
let initialCollateralDeltaAmount = payTokenAmount;
|
|
356
|
+
let externalCalls;
|
|
357
|
+
if (externalSwapQuote) {
|
|
358
|
+
/**
|
|
359
|
+
* External swap will be executed before order creation logic,
|
|
360
|
+
* so the final order has no swap parameters and must treat the outToken address as an initial collateral
|
|
361
|
+
* */
|
|
362
|
+
initialCollateralTokenAddress = convertTokenAddress(chainId, externalSwapQuote.outTokenAddress, "wrapped");
|
|
363
|
+
initialCollateralDeltaAmount = 0n;
|
|
364
|
+
swapPath = [];
|
|
365
|
+
externalCalls = getExternalCallsPayload({
|
|
366
|
+
chainId,
|
|
367
|
+
account: receiver,
|
|
368
|
+
quote: externalSwapQuote,
|
|
369
|
+
});
|
|
370
|
+
finalPayTokenAmount = externalSwapQuote.amountIn;
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
isNativePayment,
|
|
374
|
+
isNativeReceive,
|
|
375
|
+
initialCollateralTokenAddress,
|
|
376
|
+
initialCollateralDeltaAmount,
|
|
377
|
+
tokenTransfers,
|
|
378
|
+
payTokenAddress,
|
|
379
|
+
payTokenAmount: finalPayTokenAmount,
|
|
380
|
+
minOutputAmount,
|
|
381
|
+
swapPath,
|
|
382
|
+
value,
|
|
383
|
+
externalCalls,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
export function getBatchExternalCalls(batchParams) {
|
|
387
|
+
const externalCalls = [];
|
|
388
|
+
for (const createOrderParams of batchParams.createOrderParams) {
|
|
389
|
+
if (createOrderParams.tokenTransfersParams?.externalCalls) {
|
|
390
|
+
externalCalls.push(createOrderParams.tokenTransfersParams.externalCalls);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return combineExternalCalls(externalCalls);
|
|
394
|
+
}
|
|
395
|
+
export function combineExternalCalls(externalCalls) {
|
|
396
|
+
const sendTokensMap = {};
|
|
397
|
+
const refundTokensMap = {};
|
|
398
|
+
const externalCallTargets = [];
|
|
399
|
+
const externalCallDataList = [];
|
|
400
|
+
for (const call of externalCalls) {
|
|
401
|
+
for (const [index, tokenAddress] of call.sendTokens.entries()) {
|
|
402
|
+
sendTokensMap[tokenAddress] = (sendTokensMap[tokenAddress] ?? 0n) + call.sendAmounts[index];
|
|
403
|
+
}
|
|
404
|
+
for (const [index, tokenAddress] of call.refundTokens.entries()) {
|
|
405
|
+
refundTokensMap[tokenAddress] = call.refundReceivers[index];
|
|
406
|
+
}
|
|
407
|
+
externalCallTargets.push(...call.externalCallTargets);
|
|
408
|
+
externalCallDataList.push(...call.externalCallDataList);
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
sendTokens: Object.keys(sendTokensMap),
|
|
412
|
+
sendAmounts: Object.values(sendTokensMap),
|
|
413
|
+
externalCallTargets,
|
|
414
|
+
externalCallDataList,
|
|
415
|
+
refundReceivers: Object.values(refundTokensMap),
|
|
416
|
+
refundTokens: Object.keys(refundTokensMap),
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
export function getEmptyExternalCallsPayload() {
|
|
420
|
+
return {
|
|
421
|
+
sendTokens: [],
|
|
422
|
+
sendAmounts: [],
|
|
423
|
+
externalCallTargets: [],
|
|
424
|
+
externalCallDataList: [],
|
|
425
|
+
refundReceivers: [],
|
|
426
|
+
refundTokens: [],
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
export function getExternalCallsPayload({ chainId, account, quote, }) {
|
|
430
|
+
const inTokenAddress = convertTokenAddress(chainId, quote.inTokenAddress, "wrapped");
|
|
431
|
+
const outTokenAddress = convertTokenAddress(chainId, quote.outTokenAddress, "wrapped");
|
|
432
|
+
const wntAddress = getWrappedToken(chainId).address;
|
|
433
|
+
const refundTokens = uniq([inTokenAddress, outTokenAddress, wntAddress]);
|
|
434
|
+
const payload = {
|
|
435
|
+
sendTokens: [inTokenAddress],
|
|
436
|
+
sendAmounts: [quote.amountIn],
|
|
437
|
+
externalCallTargets: [],
|
|
438
|
+
externalCallDataList: [],
|
|
439
|
+
refundTokens,
|
|
440
|
+
refundReceivers: Array.from({ length: refundTokens.length }, () => account),
|
|
441
|
+
};
|
|
442
|
+
if (quote.needSpenderApproval) {
|
|
443
|
+
payload.externalCallTargets.push(inTokenAddress);
|
|
444
|
+
payload.externalCallDataList.push(encodeFunctionData({
|
|
445
|
+
abi: ERC20ABI.abi,
|
|
446
|
+
functionName: "approve",
|
|
447
|
+
args: [quote.txnData.to, MaxUint256],
|
|
448
|
+
}));
|
|
449
|
+
}
|
|
450
|
+
payload.externalCallTargets.push(quote.txnData.to);
|
|
451
|
+
payload.externalCallDataList.push(quote.txnData.data);
|
|
452
|
+
return payload;
|
|
453
|
+
}
|
|
454
|
+
function combineTransfers(tokenTransfers) {
|
|
455
|
+
const transfersMap = {};
|
|
456
|
+
let value = 0n;
|
|
457
|
+
for (const transfer of tokenTransfers) {
|
|
458
|
+
const key = `${transfer.tokenAddress}:${transfer.destination}`;
|
|
459
|
+
if (!transfersMap[key]) {
|
|
460
|
+
transfersMap[key] = { ...transfer };
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
transfersMap[key].amount += transfer.amount;
|
|
464
|
+
}
|
|
465
|
+
if (transfer.tokenAddress === NATIVE_TOKEN_ADDRESS) {
|
|
466
|
+
value += transfer.amount;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return { tokenTransfers: Object.values(transfersMap), value };
|
|
470
|
+
}
|
|
471
|
+
export function getBatchOrderMulticallPayload({ params }) {
|
|
472
|
+
const { createOrderParams, updateOrderParams, cancelOrderParams } = params;
|
|
473
|
+
const multicall = [];
|
|
474
|
+
let value = 0n;
|
|
475
|
+
for (const params of createOrderParams) {
|
|
476
|
+
const { multicall: createMulticall, value: createValue } = buildCreateOrderMulticall(params);
|
|
477
|
+
multicall.push(...createMulticall);
|
|
478
|
+
value += createValue;
|
|
479
|
+
}
|
|
480
|
+
for (const update of updateOrderParams) {
|
|
481
|
+
const { multicall: updateMulticall, value: updateValue } = buildUpdateOrderMulticall(update);
|
|
482
|
+
multicall.push(...updateMulticall);
|
|
483
|
+
value += updateValue;
|
|
484
|
+
}
|
|
485
|
+
for (const cancel of cancelOrderParams) {
|
|
486
|
+
const { multicall: cancelMulticall, value: cancelValue } = buildCancelOrderMulticall({ params: cancel });
|
|
487
|
+
multicall.push(...cancelMulticall);
|
|
488
|
+
value += cancelValue;
|
|
489
|
+
}
|
|
490
|
+
const { encodedMulticall, callData } = encodeExchangeRouterMulticall(multicall);
|
|
491
|
+
return { multicall, value, encodedMulticall, callData };
|
|
492
|
+
}
|
|
493
|
+
export function buildCreateOrderMulticall(params) {
|
|
494
|
+
const { tokenTransfersParams, orderPayload } = params;
|
|
495
|
+
const { tokenTransfers = [], value = 0n, externalCalls = undefined } = tokenTransfersParams ?? {};
|
|
496
|
+
const multicall = [];
|
|
497
|
+
for (const transfer of tokenTransfers) {
|
|
498
|
+
if (transfer.tokenAddress === NATIVE_TOKEN_ADDRESS) {
|
|
499
|
+
multicall.push({ method: "sendWnt", params: [transfer.destination, transfer.amount] });
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
multicall.push({ method: "sendTokens", params: [transfer.tokenAddress, transfer.destination, transfer.amount] });
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (externalCalls) {
|
|
506
|
+
multicall.push({
|
|
507
|
+
method: "makeExternalCalls",
|
|
508
|
+
params: [
|
|
509
|
+
externalCalls.externalCallTargets,
|
|
510
|
+
externalCalls.externalCallDataList,
|
|
511
|
+
externalCalls.refundTokens,
|
|
512
|
+
externalCalls.refundReceivers,
|
|
513
|
+
],
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
multicall.push({
|
|
517
|
+
method: "createOrder",
|
|
518
|
+
params: [orderPayload],
|
|
519
|
+
});
|
|
520
|
+
return {
|
|
521
|
+
multicall,
|
|
522
|
+
value,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
export function buildUpdateOrderMulticall(updateTxn) {
|
|
526
|
+
const { updatePayload, params: updateParams } = updateTxn;
|
|
527
|
+
const { chainId } = updateParams;
|
|
528
|
+
const orderVaultAddress = getContract(chainId, "OrderVault");
|
|
529
|
+
const multicall = [];
|
|
530
|
+
if (updatePayload.executionFeeTopUp > 0n) {
|
|
531
|
+
multicall.push({ method: "sendWnt", params: [orderVaultAddress, updatePayload.executionFeeTopUp] });
|
|
532
|
+
}
|
|
533
|
+
multicall.push({
|
|
534
|
+
method: "updateOrder",
|
|
535
|
+
params: [
|
|
536
|
+
updatePayload.orderKey,
|
|
537
|
+
updatePayload.sizeDeltaUsd,
|
|
538
|
+
updatePayload.acceptablePrice,
|
|
539
|
+
updatePayload.triggerPrice,
|
|
540
|
+
updatePayload.minOutputAmount,
|
|
541
|
+
0n,
|
|
542
|
+
updatePayload.autoCancel,
|
|
543
|
+
],
|
|
544
|
+
});
|
|
545
|
+
return {
|
|
546
|
+
multicall,
|
|
547
|
+
value: updatePayload.executionFeeTopUp,
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
export function buildCancelOrderMulticall({ params }) {
|
|
551
|
+
const { orderKey } = params;
|
|
552
|
+
const multicall = [];
|
|
553
|
+
multicall.push({
|
|
554
|
+
method: "cancelOrder",
|
|
555
|
+
params: [orderKey],
|
|
556
|
+
});
|
|
557
|
+
return {
|
|
558
|
+
multicall,
|
|
559
|
+
value: 0n,
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
export function encodeExchangeRouterMulticall(multicall) {
|
|
563
|
+
const encodedMulticall = multicall.map((call) => encodeFunctionData({
|
|
564
|
+
abi: ExchangeRouterAbi.abi,
|
|
565
|
+
functionName: call.method,
|
|
566
|
+
args: call.params,
|
|
567
|
+
}));
|
|
568
|
+
const callData = encodeFunctionData({
|
|
569
|
+
abi: ExchangeRouterAbi.abi,
|
|
570
|
+
functionName: "multicall",
|
|
571
|
+
args: [encodedMulticall],
|
|
572
|
+
});
|
|
573
|
+
return {
|
|
574
|
+
encodedMulticall,
|
|
575
|
+
callData,
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
export function getBatchRequiredActions(orderParams) {
|
|
579
|
+
if (!orderParams) {
|
|
580
|
+
return 0;
|
|
581
|
+
}
|
|
582
|
+
return (orderParams.createOrderParams.length + orderParams.updateOrderParams.length + orderParams.cancelOrderParams.length);
|
|
583
|
+
}
|
|
584
|
+
export function getBatchSwapsCount(orderParams) {
|
|
585
|
+
if (!orderParams) {
|
|
586
|
+
return 0;
|
|
587
|
+
}
|
|
588
|
+
return orderParams.createOrderParams.reduce((acc, co) => {
|
|
589
|
+
return acc + co.orderPayload.addresses.swapPath.length;
|
|
590
|
+
}, 0);
|
|
591
|
+
}
|
|
592
|
+
export function getIsEmptyBatch(orderParams) {
|
|
593
|
+
if (!orderParams) {
|
|
594
|
+
return true;
|
|
595
|
+
}
|
|
596
|
+
if (getBatchRequiredActions(orderParams) === 0) {
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
const hasEmptyOrder = orderParams.createOrderParams.some((o) => o.orderPayload.numbers.sizeDeltaUsd === 0n && o.orderPayload.numbers.initialCollateralDeltaAmount === 0n);
|
|
600
|
+
return hasEmptyOrder;
|
|
601
|
+
}
|
|
602
|
+
export function getBatchIsNativePayment(orderParams) {
|
|
603
|
+
return orderParams.createOrderParams.some((o) => o.tokenTransfersParams?.isNativePayment);
|
|
604
|
+
}
|
|
605
|
+
export function getIsInvalidBatchReceiver(batchParams, signerAddress) {
|
|
606
|
+
return batchParams.createOrderParams.some((co) => co.orderPayload.addresses.receiver !== signerAddress);
|
|
607
|
+
}
|
|
@@ -57,6 +57,9 @@ export function isSwapOrder(orderInfo) {
|
|
|
57
57
|
export function isPositionOrder(orderInfo) {
|
|
58
58
|
return !orderInfo.isSwap;
|
|
59
59
|
}
|
|
60
|
+
export function getOrderKeys(order) {
|
|
61
|
+
return isTwapOrder(order) ? order.orders.map((o) => o.key) : [order.key];
|
|
62
|
+
}
|
|
60
63
|
export function getOrderInfo(p) {
|
|
61
64
|
const { marketsInfoData, tokensData, wrappedNativeToken, order } = p;
|
|
62
65
|
if (isSwapOrderType(order.orderType)) {
|
|
@@ -81,6 +84,7 @@ export function getOrderInfo(p) {
|
|
|
81
84
|
usdIn: convertToUsd(order.initialCollateralDeltaAmount, initialCollateralToken.decimals, initialCollateralToken.prices.minPrice),
|
|
82
85
|
shouldUnwrapNativeToken: order.shouldUnwrapNativeToken,
|
|
83
86
|
shouldApplyPriceImpact: true,
|
|
87
|
+
isAtomicSwap: false,
|
|
84
88
|
});
|
|
85
89
|
const priceImpactAmount = convertToTokenAmount(swapPathStats?.totalSwapPriceImpactDeltaUsd, targetCollateralToken.decimals, targetCollateralToken.prices.minPrice);
|
|
86
90
|
const swapFeeAmount = convertToTokenAmount(swapPathStats?.totalSwapFeeUsd, targetCollateralToken.decimals, targetCollateralToken.prices.minPrice);
|
|
@@ -152,6 +156,7 @@ export function getOrderInfo(p) {
|
|
|
152
156
|
usdIn: convertToUsd(order.initialCollateralDeltaAmount, initialCollateralToken.decimals, initialCollateralToken.prices.minPrice),
|
|
153
157
|
shouldUnwrapNativeToken: order.shouldUnwrapNativeToken,
|
|
154
158
|
shouldApplyPriceImpact: true,
|
|
159
|
+
isAtomicSwap: false,
|
|
155
160
|
});
|
|
156
161
|
const triggerThresholdType = getOrderThresholdType(order.orderType, order.isLong);
|
|
157
162
|
const orderInfo = {
|
|
@@ -2,7 +2,7 @@ import { decodeErrorResult, encodeFunctionData, withRetry } from "viem";
|
|
|
2
2
|
import { abis } from "../abis";
|
|
3
3
|
import { getContract } from "../configs/contracts";
|
|
4
4
|
import { convertTokenAddress } from "../configs/tokens";
|
|
5
|
-
import {
|
|
5
|
+
import { extractTxnError } from "./errors";
|
|
6
6
|
import { convertToContractPrice, getTokenData } from "./tokens";
|
|
7
7
|
class SimulateExecuteOrderError extends Error {
|
|
8
8
|
constructor(message, cause) {
|
|
@@ -10,6 +10,10 @@ class SimulateExecuteOrderError extends Error {
|
|
|
10
10
|
this.cause = cause;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @deprecated use simulateExecution instead
|
|
16
|
+
*/
|
|
13
17
|
export async function simulateExecuteOrder(sdk, p) {
|
|
14
18
|
const chainId = sdk.chainId;
|
|
15
19
|
const client = sdk.publicClient;
|
|
@@ -59,7 +63,7 @@ export async function simulateExecuteOrder(sdk, p) {
|
|
|
59
63
|
retryCount: 2,
|
|
60
64
|
delay: 200,
|
|
61
65
|
shouldRetry: (error) => {
|
|
62
|
-
const [message] =
|
|
66
|
+
const [message] = extractTxnError(error);
|
|
63
67
|
return message?.toLocaleLowerCase()?.includes("unsupported block number") ?? false;
|
|
64
68
|
},
|
|
65
69
|
});
|