@gmx-io/sdk 0.0.9 → 0.1.1-alpha-1
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/README.md +25 -7
- package/build/scripts/prebuild/index.js +2 -0
- package/build/scripts/prebuild/prebuildKinkModelMarketRatesKeys.d.ts +1 -0
- package/build/scripts/prebuild/prebuildKinkModelMarketRatesKeys.js +44 -0
- package/build/src/abis/ExchangeRouter.json +203 -0
- package/build/src/abis/GlvReader.json +0 -10
- package/build/src/abis/GlvRouter.json +96 -2
- package/build/src/abis/RewardRouter.json +323 -68
- package/build/src/abis/SubaccountRouter.json +10 -0
- package/build/src/abis/SyntheticsReader.json +196 -64
- package/build/src/configs/batch.js +1 -13
- package/build/src/configs/chains.d.ts +0 -1
- package/build/src/configs/chains.js +1 -7
- package/build/src/configs/contracts.d.ts +2 -1
- package/build/src/configs/contracts.js +27 -66
- package/build/src/configs/dataStore.d.ts +4 -1
- package/build/src/configs/dataStore.js +5 -2
- package/build/src/configs/factors.d.ts +0 -12
- package/build/src/configs/factors.js +0 -12
- package/build/src/configs/markets.js +147 -0
- package/build/src/configs/tokens.d.ts +5 -2
- package/build/src/configs/tokens.js +297 -221
- package/build/src/index.d.ts +3 -17
- package/build/src/index.js +5 -182
- package/build/src/modules/markets/index.js +2 -3
- package/build/src/modules/markets/markets.spec.js +1 -7
- package/build/src/modules/markets/types.d.ts +8 -0
- package/build/src/modules/oracle.js +1 -16
- package/build/src/modules/orders/orders.d.ts +2 -1
- package/build/src/modules/orders/orders.js +9 -8
- package/build/src/modules/orders/transactions/cancelOrdersTxn.js +1 -1
- package/build/src/modules/orders/transactions/createDecreaseOrderTxn.js +1 -0
- package/build/src/modules/orders/transactions/createIncreaseOrderTxn.d.ts +1 -0
- package/build/src/modules/orders/transactions/createIncreaseOrderTxn.js +3 -1
- package/build/src/modules/orders/transactions/createSwapOrderTxn.js +1 -0
- package/build/src/modules/orders/transactions/createWrapOrUnwrapTxn.d.ts +1 -1
- package/build/src/modules/orders/transactions/updateOrderTxn.d.ts +3 -1
- package/build/src/modules/orders/transactions/updateOrderTxn.js +7 -5
- package/build/src/modules/orders/utils.js +1 -1
- package/build/src/modules/positions/positions.d.ts +1 -1
- package/build/src/modules/positions/positions.js +10 -86
- package/build/src/modules/trades/trades.js +1 -2
- package/build/src/modules/utils/utils.d.ts +20 -0
- package/build/src/modules/utils/utils.js +183 -0
- package/build/src/prebuilt/hashedKinkModelMarketRatesKeys.json +760 -0
- package/build/src/prebuilt/hashedMarketConfigKeys.json +1050 -0
- package/build/src/prebuilt/hashedMarketValuesKeys.json +315 -0
- package/build/src/prebuilt/index.d.ts +8 -2
- package/build/src/prebuilt/index.js +3 -1
- package/build/src/prebuilt/index.ts +19 -2
- package/build/src/types/amounts.d.ts +1 -1
- package/build/src/types/fees.d.ts +1 -2
- package/build/src/types/orders.d.ts +2 -2
- package/build/src/types/positions.d.ts +2 -2
- package/build/src/types/sdk.d.ts +4 -4
- package/build/src/types/syntheticsEvents.d.ts +3 -3
- package/build/src/types/tokens.d.ts +4 -0
- package/build/src/types/trade.d.ts +1 -1
- package/build/src/utils/fees/executionFee.d.ts +1 -5
- package/build/src/utils/fees/executionFee.js +3 -12
- package/build/src/utils/objects.d.ts +0 -5
- package/build/src/utils/objects.js +0 -17
- package/build/src/utils/orders.js +0 -8
- package/build/src/utils/simulateExecuteOrder.js +8 -21
- package/build/src/utils/testUtil.d.ts +2 -3
- package/build/src/utils/testUtil.js +2 -3
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -2
- package/build/src/configs/subgraph.d.ts +0 -3
- package/build/src/configs/subgraph.js +0 -3
- package/build/src/utils/slidingWindowFallbackSwitcher.d.ts +0 -24
- package/build/src/utils/slidingWindowFallbackSwitcher.js +0 -40
|
@@ -6,7 +6,7 @@ import { getContract } from "../../configs/contracts";
|
|
|
6
6
|
import { hashedPositionKey, MAX_AUTO_CANCEL_ORDERS_KEY, MIN_COLLATERAL_USD_KEY, MIN_POSITION_SIZE_USD_KEY, uiFeeFactorKey, } from "../../configs/dataStore";
|
|
7
7
|
import { getContractMarketPrices, getMaxAllowedLeverageByMinCollateralFactor } from "../../utils/markets";
|
|
8
8
|
import { getByKey } from "../../utils/objects";
|
|
9
|
-
import { getEntryPrice, getLeverage, getLiquidationPrice, getPositionKey, getPositionNetValue, getPositionPnlUsd,
|
|
9
|
+
import { getEntryPrice, getLeverage, getLiquidationPrice, getPositionKey, getPositionNetValue, getPositionPnlUsd, } from "../../utils/positions";
|
|
10
10
|
import { Module } from "../base";
|
|
11
11
|
import { getPositionFee, getPriceImpactForPosition } from "../../utils/fees";
|
|
12
12
|
import { basisPointsToFloat, getBasisPoints } from "../../utils/numbers";
|
|
@@ -50,51 +50,19 @@ export class Positions extends Module {
|
|
|
50
50
|
}
|
|
51
51
|
return values;
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
const { positionsData, allPositionsKeys
|
|
53
|
+
getPositionsData(p) {
|
|
54
|
+
const { positionsData, allPositionsKeys } = p;
|
|
55
55
|
if (!allPositionsKeys) {
|
|
56
56
|
return undefined;
|
|
57
57
|
}
|
|
58
|
-
if (!positionDecreaseEvents && !positionIncreaseEvents && !pendingPositionsUpdates) {
|
|
59
|
-
return positionsData;
|
|
60
|
-
}
|
|
61
58
|
return allPositionsKeys.reduce((acc, key) => {
|
|
62
|
-
const now = Date.now();
|
|
63
|
-
const lastIncreaseEvent = positionIncreaseEvents
|
|
64
|
-
? positionIncreaseEvents.filter((e) => e.positionKey === key).pop()
|
|
65
|
-
: undefined;
|
|
66
|
-
const lastDecreaseEvent = positionDecreaseEvents
|
|
67
|
-
? positionDecreaseEvents.filter((e) => e.positionKey === key).pop()
|
|
68
|
-
: undefined;
|
|
69
|
-
const pendingUpdate = pendingPositionsUpdates?.[key] &&
|
|
70
|
-
(pendingPositionsUpdates[key]?.updatedAt ?? 0) + Positions.MAX_PENDING_UPDATE_AGE > now
|
|
71
|
-
? pendingPositionsUpdates[key]
|
|
72
|
-
: undefined;
|
|
73
59
|
let position;
|
|
74
60
|
if (getByKey(positionsData, key)) {
|
|
75
61
|
position = { ...getByKey(positionsData, key) };
|
|
76
62
|
}
|
|
77
|
-
else if (pendingUpdate && pendingUpdate.isIncrease) {
|
|
78
|
-
position = getPendingMockPosition(pendingUpdate);
|
|
79
|
-
}
|
|
80
63
|
else {
|
|
81
64
|
return acc;
|
|
82
65
|
}
|
|
83
|
-
if (lastIncreaseEvent &&
|
|
84
|
-
lastIncreaseEvent.increasedAtBlock > position.increasedAtBlock &&
|
|
85
|
-
lastIncreaseEvent.increasedAtBlock > (lastDecreaseEvent?.decreasedAtBlock || 0)) {
|
|
86
|
-
position = applyEventChanges(position, lastIncreaseEvent);
|
|
87
|
-
}
|
|
88
|
-
else if (lastDecreaseEvent &&
|
|
89
|
-
lastDecreaseEvent.decreasedAtBlock > position.decreasedAtBlock &&
|
|
90
|
-
lastDecreaseEvent.decreasedAtBlock > (lastIncreaseEvent?.increasedAtBlock || 0)) {
|
|
91
|
-
position = applyEventChanges(position, lastDecreaseEvent);
|
|
92
|
-
}
|
|
93
|
-
if (pendingUpdate &&
|
|
94
|
-
((pendingUpdate.isIncrease && pendingUpdate.updatedAtBlock > position.increasedAtBlock) ||
|
|
95
|
-
(!pendingUpdate.isIncrease && pendingUpdate.updatedAtBlock > position.decreasedAtBlock))) {
|
|
96
|
-
position.pendingUpdate = pendingUpdate;
|
|
97
|
-
}
|
|
98
66
|
if (position.sizeInUsd > 0) {
|
|
99
67
|
acc[key] = position;
|
|
100
68
|
}
|
|
@@ -126,14 +94,14 @@ export class Positions extends Module {
|
|
|
126
94
|
},
|
|
127
95
|
},
|
|
128
96
|
};
|
|
129
|
-
const
|
|
97
|
+
const positions = await this.sdk.executeMulticall(request).then((res) => {
|
|
130
98
|
const positions = res.data.reader.positions.returnValues;
|
|
131
99
|
return positions.reduce((positionsMap, positionInfo) => {
|
|
132
100
|
const { position, fees } = positionInfo;
|
|
133
101
|
const { addresses, numbers, flags, data } = position;
|
|
134
102
|
const { account, market: marketAddress, collateralToken: collateralTokenAddress } = addresses;
|
|
135
103
|
// Empty position
|
|
136
|
-
if (BigInt(numbers.
|
|
104
|
+
if (BigInt(numbers.increasedAtTime) == 0n) {
|
|
137
105
|
return positionsMap;
|
|
138
106
|
}
|
|
139
107
|
const positionKey = getPositionKey(account, marketAddress, collateralTokenAddress, flags.isLong);
|
|
@@ -147,8 +115,8 @@ export class Positions extends Module {
|
|
|
147
115
|
sizeInUsd: numbers.sizeInUsd,
|
|
148
116
|
sizeInTokens: numbers.sizeInTokens,
|
|
149
117
|
collateralAmount: numbers.collateralAmount,
|
|
150
|
-
|
|
151
|
-
|
|
118
|
+
increasedAtTime: numbers.increasedAtTime,
|
|
119
|
+
decreasedAtTime: numbers.decreasedAtTime,
|
|
152
120
|
isLong: flags.isLong,
|
|
153
121
|
pendingBorrowingFeesUsd: fees.borrowing.borrowingFeeUsd,
|
|
154
122
|
fundingFeeAmount: fees.funding.fundingFeeAmount,
|
|
@@ -159,12 +127,12 @@ export class Positions extends Module {
|
|
|
159
127
|
return positionsMap;
|
|
160
128
|
}, {});
|
|
161
129
|
});
|
|
162
|
-
const
|
|
163
|
-
positionsData:
|
|
130
|
+
const positionsData = this.getPositionsData({
|
|
131
|
+
positionsData: positions,
|
|
164
132
|
allPositionsKeys: keysAndPrices?.allPositionsKeys,
|
|
165
133
|
});
|
|
166
134
|
return {
|
|
167
|
-
positionsData
|
|
135
|
+
positionsData,
|
|
168
136
|
};
|
|
169
137
|
}
|
|
170
138
|
getUiFeeFactorRequest() {
|
|
@@ -517,47 +485,3 @@ export class Positions extends Module {
|
|
|
517
485
|
}
|
|
518
486
|
}
|
|
519
487
|
Positions.MAX_PENDING_UPDATE_AGE = 600 * 1000; // 10 minutes
|
|
520
|
-
function getPendingMockPosition(pendingUpdate) {
|
|
521
|
-
const { account, marketAddress, collateralAddress, isLong } = parsePositionKey(pendingUpdate.positionKey);
|
|
522
|
-
return {
|
|
523
|
-
key: pendingUpdate.positionKey,
|
|
524
|
-
contractKey: hashedPositionKey(account, marketAddress, collateralAddress, isLong),
|
|
525
|
-
account,
|
|
526
|
-
marketAddress,
|
|
527
|
-
collateralTokenAddress: collateralAddress,
|
|
528
|
-
isLong,
|
|
529
|
-
sizeInUsd: pendingUpdate.sizeDeltaUsd ?? 0n,
|
|
530
|
-
collateralAmount: pendingUpdate.collateralDeltaAmount ?? 0n,
|
|
531
|
-
sizeInTokens: pendingUpdate.sizeDeltaInTokens ?? 0n,
|
|
532
|
-
increasedAtBlock: pendingUpdate.updatedAtBlock,
|
|
533
|
-
decreasedAtBlock: 0n,
|
|
534
|
-
pendingBorrowingFeesUsd: 0n,
|
|
535
|
-
fundingFeeAmount: 0n,
|
|
536
|
-
claimableLongTokenAmount: 0n,
|
|
537
|
-
claimableShortTokenAmount: 0n,
|
|
538
|
-
data: "0x",
|
|
539
|
-
isOpening: true,
|
|
540
|
-
pendingUpdate: pendingUpdate,
|
|
541
|
-
};
|
|
542
|
-
}
|
|
543
|
-
function applyEventChanges(position, event) {
|
|
544
|
-
const nextPosition = { ...position };
|
|
545
|
-
nextPosition.sizeInUsd = event.sizeInUsd;
|
|
546
|
-
nextPosition.sizeInTokens = event.sizeInTokens;
|
|
547
|
-
nextPosition.collateralAmount = event.collateralAmount;
|
|
548
|
-
nextPosition.pendingBorrowingFeesUsd = 0n;
|
|
549
|
-
nextPosition.fundingFeeAmount = 0n;
|
|
550
|
-
nextPosition.claimableLongTokenAmount = 0n;
|
|
551
|
-
nextPosition.claimableShortTokenAmount = 0n;
|
|
552
|
-
nextPosition.pendingUpdate = undefined;
|
|
553
|
-
nextPosition.isOpening = false;
|
|
554
|
-
// eslint-disable-next-line local-rules/no-logical-bigint
|
|
555
|
-
if (event.increasedAtBlock) {
|
|
556
|
-
nextPosition.increasedAtBlock = event.increasedAtBlock;
|
|
557
|
-
}
|
|
558
|
-
// eslint-disable-next-line local-rules/no-logical-bigint
|
|
559
|
-
if (event.decreasedAtBlock) {
|
|
560
|
-
nextPosition.decreasedAtBlock = event.decreasedAtBlock;
|
|
561
|
-
}
|
|
562
|
-
return nextPosition;
|
|
563
|
-
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { getSubgraphUrl } from "../../configs/subgraph";
|
|
2
1
|
import { getWrappedToken } from "../../configs/tokens";
|
|
3
2
|
import merge from "lodash/merge";
|
|
4
3
|
import { OrderType } from "../../types/orders";
|
|
@@ -32,7 +31,7 @@ export class Trades extends Module {
|
|
|
32
31
|
}
|
|
33
32
|
}
|
|
34
33
|
export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFilter = [], forAllAccounts, account, fromTxTimestamp, toTxTimestamp, orderEventCombinations, marketsInfoData, tokensData, }) {
|
|
35
|
-
const endpoint =
|
|
34
|
+
const endpoint = sdk.config.subgraphUrl;
|
|
36
35
|
const chainId = sdk.chainId;
|
|
37
36
|
if (!endpoint) {
|
|
38
37
|
return [];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { IncreasePositionAmounts } from "../../types/amounts";
|
|
2
|
+
import type { GasLimitsConfig } from "../../types/fees";
|
|
3
|
+
import type { DecreasePositionAmounts, SwapAmounts, TradeFeesType } from "../../types/trade";
|
|
4
|
+
import { TokensData } from "../../types/tokens";
|
|
5
|
+
import { Module } from "../base";
|
|
6
|
+
export declare class Utils extends Module {
|
|
7
|
+
private _gasLimits;
|
|
8
|
+
getGasLimits(): Promise<GasLimitsConfig>;
|
|
9
|
+
getEstimatedGasFee(tradeFeesType: TradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }: {
|
|
10
|
+
swapAmounts?: SwapAmounts;
|
|
11
|
+
decreaseAmounts?: DecreasePositionAmounts;
|
|
12
|
+
increaseAmounts?: IncreasePositionAmounts;
|
|
13
|
+
}): Promise<bigint | null>;
|
|
14
|
+
getExecutionFee(tradeFeesType: TradeFeesType, tokensData: TokensData, { increaseAmounts, decreaseAmounts, swapAmounts, }: {
|
|
15
|
+
swapAmounts?: SwapAmounts;
|
|
16
|
+
decreaseAmounts?: DecreasePositionAmounts;
|
|
17
|
+
increaseAmounts?: IncreasePositionAmounts;
|
|
18
|
+
}): Promise<import("../../types/fees").ExecutionFee | undefined>;
|
|
19
|
+
getGasPrice(): Promise<bigint | undefined>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { withRetry } from "viem";
|
|
2
|
+
import { EXECUTION_FEE_CONFIG_V2, GAS_PRICE_PREMIUM_MAP, getChain, MAX_PRIORITY_FEE_PER_GAS_MAP } from "../../configs/chains";
|
|
3
|
+
import { getContract } from "../../configs/contracts";
|
|
4
|
+
import { decreaseOrderGasLimitKey, depositGasLimitKey, ESTIMATED_GAS_FEE_BASE_AMOUNT_V2_1, ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR, ESTIMATED_GAS_FEE_PER_ORACLE_PRICE, GLV_DEPOSIT_GAS_LIMIT, GLV_PER_MARKET_GAS_LIMIT, GLV_WITHDRAWAL_GAS_LIMIT, increaseOrderGasLimitKey, shiftGasLimitKey, singleSwapGasLimitKey, swapOrderGasLimitKey, withdrawalGasLimitKey, } from "../../configs/dataStore";
|
|
5
|
+
import DataStore from "../../abis/DataStore.json";
|
|
6
|
+
import { bigMath } from "../../utils/bigmath";
|
|
7
|
+
import { estimateOrderOraclePriceCount } from "../../utils/fees/estimateOraclePriceCount";
|
|
8
|
+
import { estimateExecuteDecreaseOrderGasLimit, estimateExecuteIncreaseOrderGasLimit, estimateExecuteSwapOrderGasLimit, getExecutionFee, } from "../../utils/fees/executionFee";
|
|
9
|
+
import { getSwapCount } from "../../utils/trade";
|
|
10
|
+
import { Module } from "../base";
|
|
11
|
+
export class Utils extends Module {
|
|
12
|
+
constructor() {
|
|
13
|
+
super(...arguments);
|
|
14
|
+
this._gasLimits = null;
|
|
15
|
+
}
|
|
16
|
+
async getGasLimits() {
|
|
17
|
+
if (this._gasLimits) {
|
|
18
|
+
return this._gasLimits;
|
|
19
|
+
}
|
|
20
|
+
const gasLimits = await this.sdk
|
|
21
|
+
.executeMulticall({
|
|
22
|
+
dataStore: {
|
|
23
|
+
contractAddress: getContract(this.chainId, "DataStore"),
|
|
24
|
+
abi: DataStore.abi,
|
|
25
|
+
calls: {
|
|
26
|
+
depositToken: {
|
|
27
|
+
methodName: "getUint",
|
|
28
|
+
params: [depositGasLimitKey()],
|
|
29
|
+
},
|
|
30
|
+
withdrawalMultiToken: {
|
|
31
|
+
methodName: "getUint",
|
|
32
|
+
params: [withdrawalGasLimitKey()],
|
|
33
|
+
},
|
|
34
|
+
shift: {
|
|
35
|
+
methodName: "getUint",
|
|
36
|
+
params: [shiftGasLimitKey()],
|
|
37
|
+
},
|
|
38
|
+
singleSwap: {
|
|
39
|
+
methodName: "getUint",
|
|
40
|
+
params: [singleSwapGasLimitKey()],
|
|
41
|
+
},
|
|
42
|
+
swapOrder: {
|
|
43
|
+
methodName: "getUint",
|
|
44
|
+
params: [swapOrderGasLimitKey()],
|
|
45
|
+
},
|
|
46
|
+
increaseOrder: {
|
|
47
|
+
methodName: "getUint",
|
|
48
|
+
params: [increaseOrderGasLimitKey()],
|
|
49
|
+
},
|
|
50
|
+
decreaseOrder: {
|
|
51
|
+
methodName: "getUint",
|
|
52
|
+
params: [decreaseOrderGasLimitKey()],
|
|
53
|
+
},
|
|
54
|
+
estimatedGasFeeBaseAmount: {
|
|
55
|
+
methodName: "getUint",
|
|
56
|
+
params: [ESTIMATED_GAS_FEE_BASE_AMOUNT_V2_1],
|
|
57
|
+
},
|
|
58
|
+
estimatedGasFeePerOraclePrice: {
|
|
59
|
+
methodName: "getUint",
|
|
60
|
+
params: [ESTIMATED_GAS_FEE_PER_ORACLE_PRICE],
|
|
61
|
+
},
|
|
62
|
+
estimatedFeeMultiplierFactor: {
|
|
63
|
+
methodName: "getUint",
|
|
64
|
+
params: [ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR],
|
|
65
|
+
},
|
|
66
|
+
glvDepositGasLimit: {
|
|
67
|
+
methodName: "getUint",
|
|
68
|
+
params: [GLV_DEPOSIT_GAS_LIMIT],
|
|
69
|
+
},
|
|
70
|
+
glvWithdrawalGasLimit: {
|
|
71
|
+
methodName: "getUint",
|
|
72
|
+
params: [GLV_WITHDRAWAL_GAS_LIMIT],
|
|
73
|
+
},
|
|
74
|
+
glvPerMarketGasLimit: {
|
|
75
|
+
methodName: "getUint",
|
|
76
|
+
params: [GLV_PER_MARKET_GAS_LIMIT],
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
})
|
|
81
|
+
.then((res) => {
|
|
82
|
+
const results = res.data.dataStore;
|
|
83
|
+
function getBigInt(key) {
|
|
84
|
+
return BigInt(results[key].returnValues[0]);
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
depositToken: getBigInt("depositToken"),
|
|
88
|
+
withdrawalMultiToken: getBigInt("withdrawalMultiToken"),
|
|
89
|
+
shift: getBigInt("shift"),
|
|
90
|
+
singleSwap: getBigInt("singleSwap"),
|
|
91
|
+
swapOrder: getBigInt("swapOrder"),
|
|
92
|
+
increaseOrder: getBigInt("increaseOrder"),
|
|
93
|
+
decreaseOrder: getBigInt("decreaseOrder"),
|
|
94
|
+
estimatedGasFeeBaseAmount: getBigInt("estimatedGasFeeBaseAmount"),
|
|
95
|
+
estimatedGasFeePerOraclePrice: getBigInt("estimatedGasFeePerOraclePrice"),
|
|
96
|
+
estimatedFeeMultiplierFactor: getBigInt("estimatedFeeMultiplierFactor"),
|
|
97
|
+
glvDepositGasLimit: getBigInt("glvDepositGasLimit"),
|
|
98
|
+
glvWithdrawalGasLimit: getBigInt("glvWithdrawalGasLimit"),
|
|
99
|
+
glvPerMarketGasLimit: getBigInt("glvPerMarketGasLimit"),
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
this._gasLimits = gasLimits;
|
|
103
|
+
return gasLimits;
|
|
104
|
+
}
|
|
105
|
+
async getEstimatedGasFee(tradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
106
|
+
const gasLimits = await this.getGasLimits();
|
|
107
|
+
switch (tradeFeesType) {
|
|
108
|
+
case "swap": {
|
|
109
|
+
if (!swapAmounts || !swapAmounts.swapPathStats)
|
|
110
|
+
return null;
|
|
111
|
+
return estimateExecuteSwapOrderGasLimit(gasLimits, {
|
|
112
|
+
swapsCount: swapAmounts.swapPathStats.swapPath.length,
|
|
113
|
+
callbackGasLimit: 0n,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
case "increase": {
|
|
117
|
+
if (!increaseAmounts)
|
|
118
|
+
return null;
|
|
119
|
+
return estimateExecuteIncreaseOrderGasLimit(gasLimits, {
|
|
120
|
+
swapsCount: increaseAmounts.swapPathStats?.swapPath.length,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
case "decrease": {
|
|
124
|
+
if (!decreaseAmounts)
|
|
125
|
+
return null;
|
|
126
|
+
return estimateExecuteDecreaseOrderGasLimit(gasLimits, {
|
|
127
|
+
callbackGasLimit: 0n,
|
|
128
|
+
decreaseSwapType: decreaseAmounts.decreaseSwapType,
|
|
129
|
+
swapsCount: 0,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
case "edit":
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async getExecutionFee(tradeFeesType, tokensData, { increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
137
|
+
const gasLimits = await this.getGasLimits();
|
|
138
|
+
const gasPrice = await this.getGasPrice();
|
|
139
|
+
const estimatedGas = await this.getEstimatedGasFee(tradeFeesType, {
|
|
140
|
+
increaseAmounts,
|
|
141
|
+
decreaseAmounts,
|
|
142
|
+
swapAmounts,
|
|
143
|
+
});
|
|
144
|
+
if (estimatedGas === null || estimatedGas === undefined)
|
|
145
|
+
return undefined;
|
|
146
|
+
const swapsCount = getSwapCount({
|
|
147
|
+
isSwap: tradeFeesType === "swap",
|
|
148
|
+
isIncrease: tradeFeesType === "increase",
|
|
149
|
+
increaseAmounts,
|
|
150
|
+
decreaseAmounts,
|
|
151
|
+
swapAmounts,
|
|
152
|
+
});
|
|
153
|
+
if (swapsCount === undefined)
|
|
154
|
+
return undefined;
|
|
155
|
+
if (tokensData === undefined)
|
|
156
|
+
return undefined;
|
|
157
|
+
if (gasPrice === undefined)
|
|
158
|
+
return undefined;
|
|
159
|
+
const oraclePriceCount = estimateOrderOraclePriceCount(swapsCount);
|
|
160
|
+
return getExecutionFee(this.sdk, gasLimits, tokensData, estimatedGas, gasPrice, oraclePriceCount);
|
|
161
|
+
}
|
|
162
|
+
async getGasPrice() {
|
|
163
|
+
const executionFeeConfig = EXECUTION_FEE_CONFIG_V2[this.chainId];
|
|
164
|
+
const feeData = await withRetry(() => this.sdk.publicClient.estimateFeesPerGas({
|
|
165
|
+
chain: getChain(this.chainId),
|
|
166
|
+
type: "legacy",
|
|
167
|
+
}), {
|
|
168
|
+
retryCount: 2,
|
|
169
|
+
shouldRetry: ({ error }) => {
|
|
170
|
+
const isInvalidBlockError = error?.message?.includes("invalid value for value.hash");
|
|
171
|
+
return isInvalidBlockError;
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
let gasPrice = feeData.gasPrice ?? 0n;
|
|
175
|
+
if (executionFeeConfig.shouldUseMaxPriorityFeePerGas) {
|
|
176
|
+
const maxPriorityFeePerGas = bigMath.max(feeData?.maxPriorityFeePerGas ?? 0n, MAX_PRIORITY_FEE_PER_GAS_MAP[this.chainId] ?? 0n);
|
|
177
|
+
gasPrice = gasPrice + maxPriorityFeePerGas;
|
|
178
|
+
}
|
|
179
|
+
const premium = GAS_PRICE_PREMIUM_MAP[this.chainId] ?? 0n;
|
|
180
|
+
const price = gasPrice + premium;
|
|
181
|
+
return price === undefined ? undefined : BigInt(gasPrice);
|
|
182
|
+
}
|
|
183
|
+
}
|