@gmx-io/sdk 0.0.8 → 0.1.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/src/index.d.ts +3 -17
- package/build/src/index.js +5 -182
- package/build/src/modules/markets/index.d.ts +1 -1
- package/build/src/modules/markets/markets.spec.js +1 -1
- package/build/src/modules/orders/orders.js +5 -5
- package/build/src/modules/utils/utils.d.ts +20 -0
- package/build/src/modules/utils/utils.js +188 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/build/src/index.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { Abi, Address, PublicClient, WalletClient } from "viem";
|
|
2
2
|
import { Accounts } from "./modules/accounts/accounts";
|
|
3
3
|
import { Markets } from "./modules/markets";
|
|
4
|
+
import { Oracle } from "./modules/oracle";
|
|
4
5
|
import { Orders } from "./modules/orders/orders";
|
|
5
6
|
import { Positions } from "./modules/positions/positions";
|
|
6
7
|
import { Tokens } from "./modules/tokens/tokens";
|
|
7
8
|
import { Trades } from "./modules/trades/trades";
|
|
8
|
-
import {
|
|
9
|
+
import { Utils } from "./modules/utils/utils";
|
|
9
10
|
import type { GmxSdkConfig } from "./types/sdk";
|
|
10
|
-
import type { IncreasePositionAmounts } from "./types/amounts";
|
|
11
|
-
import type { GasLimitsConfig } from "./types/fees";
|
|
12
|
-
import type { DecreasePositionAmounts, SwapAmounts, TradeFeesType } from "./types/trade";
|
|
13
11
|
import { CallContractOpts } from "./utils/callContract";
|
|
14
12
|
import { MulticallRequestConfig } from "./utils/multicall";
|
|
15
13
|
export declare class GmxSdk {
|
|
@@ -20,6 +18,7 @@ export declare class GmxSdk {
|
|
|
20
18
|
readonly orders: Orders;
|
|
21
19
|
readonly trades: Trades;
|
|
22
20
|
readonly accounts: Accounts;
|
|
21
|
+
readonly utils: Utils;
|
|
23
22
|
readonly oracle: Oracle;
|
|
24
23
|
publicClient: PublicClient;
|
|
25
24
|
walletClient: WalletClient;
|
|
@@ -30,17 +29,4 @@ export declare class GmxSdk {
|
|
|
30
29
|
get chainId(): number;
|
|
31
30
|
get chain(): import("viem").Chain;
|
|
32
31
|
get account(): `0x${string}`;
|
|
33
|
-
_gasLimits: GasLimitsConfig | null;
|
|
34
|
-
getGasLimits(): Promise<GasLimitsConfig>;
|
|
35
|
-
getEstimatedGasFee(tradeFeesType: TradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }: {
|
|
36
|
-
swapAmounts?: SwapAmounts;
|
|
37
|
-
decreaseAmounts?: DecreasePositionAmounts;
|
|
38
|
-
increaseAmounts?: IncreasePositionAmounts;
|
|
39
|
-
}): Promise<bigint | null>;
|
|
40
|
-
getExecutionFee(tradeFeesType: TradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }: {
|
|
41
|
-
swapAmounts?: SwapAmounts;
|
|
42
|
-
decreaseAmounts?: DecreasePositionAmounts;
|
|
43
|
-
increaseAmounts?: IncreasePositionAmounts;
|
|
44
|
-
}): Promise<import("./types/fees").ExecutionFee | undefined>;
|
|
45
|
-
getGasPrice(): Promise<bigint | undefined>;
|
|
46
32
|
}
|
package/build/src/index.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
import { createPublicClient, createWalletClient, http
|
|
1
|
+
import { createPublicClient, createWalletClient, http } from "viem";
|
|
2
2
|
import { Accounts } from "./modules/accounts/accounts";
|
|
3
3
|
import { Markets } from "./modules/markets";
|
|
4
|
+
import { Oracle } from "./modules/oracle";
|
|
4
5
|
import { Orders } from "./modules/orders/orders";
|
|
5
6
|
import { Positions } from "./modules/positions/positions";
|
|
6
7
|
import { Tokens } from "./modules/tokens/tokens";
|
|
7
8
|
import { Trades } from "./modules/trades/trades";
|
|
8
|
-
import {
|
|
9
|
+
import { Utils } from "./modules/utils/utils";
|
|
9
10
|
import { BATCH_CONFIGS } from "./configs/batch";
|
|
10
|
-
import {
|
|
11
|
-
import { EXECUTION_FEE_CONFIG_V2, GAS_PRICE_PREMIUM_MAP, getChain, MAX_PRIORITY_FEE_PER_GAS_MAP } from "./configs/chains";
|
|
12
|
-
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";
|
|
13
|
-
import DataStore from "./abis/DataStore.json";
|
|
14
|
-
import { bigMath } from "./utils/bigmath";
|
|
11
|
+
import { getChain } from "./configs/chains";
|
|
15
12
|
import { callContract } from "./utils/callContract";
|
|
16
|
-
import { estimateOrderOraclePriceCount } from "./utils/fees/estimateOraclePriceCount";
|
|
17
|
-
import { estimateExecuteDecreaseOrderGasLimit, estimateExecuteIncreaseOrderGasLimit, estimateExecuteSwapOrderGasLimit, getExecutionFee, } from "./utils/fees/executionFee";
|
|
18
|
-
import { getSwapCount } from "./utils/trade";
|
|
19
13
|
import { MAX_TIMEOUT, Multicall } from "./utils/multicall";
|
|
20
14
|
export class GmxSdk {
|
|
21
15
|
constructor(config) {
|
|
@@ -26,7 +20,7 @@ export class GmxSdk {
|
|
|
26
20
|
this.orders = new Orders(this);
|
|
27
21
|
this.trades = new Trades(this);
|
|
28
22
|
this.accounts = new Accounts(this);
|
|
29
|
-
this.
|
|
23
|
+
this.utils = new Utils(this);
|
|
30
24
|
this.oracle = new Oracle(this);
|
|
31
25
|
this.publicClient =
|
|
32
26
|
config.publicClient ??
|
|
@@ -74,175 +68,4 @@ export class GmxSdk {
|
|
|
74
68
|
get account() {
|
|
75
69
|
return this.config.account;
|
|
76
70
|
}
|
|
77
|
-
async getGasLimits() {
|
|
78
|
-
if (this._gasLimits) {
|
|
79
|
-
return this._gasLimits;
|
|
80
|
-
}
|
|
81
|
-
const gasLimits = await this.executeMulticall({
|
|
82
|
-
dataStore: {
|
|
83
|
-
contractAddress: getContract(this.chainId, "DataStore"),
|
|
84
|
-
abi: DataStore.abi,
|
|
85
|
-
calls: {
|
|
86
|
-
depositSingleToken: {
|
|
87
|
-
methodName: "getUint",
|
|
88
|
-
params: [depositGasLimitKey(true)],
|
|
89
|
-
},
|
|
90
|
-
depositMultiToken: {
|
|
91
|
-
methodName: "getUint",
|
|
92
|
-
params: [depositGasLimitKey(false)],
|
|
93
|
-
},
|
|
94
|
-
withdrawalMultiToken: {
|
|
95
|
-
methodName: "getUint",
|
|
96
|
-
params: [withdrawalGasLimitKey()],
|
|
97
|
-
},
|
|
98
|
-
shift: {
|
|
99
|
-
methodName: "getUint",
|
|
100
|
-
params: [shiftGasLimitKey()],
|
|
101
|
-
},
|
|
102
|
-
singleSwap: {
|
|
103
|
-
methodName: "getUint",
|
|
104
|
-
params: [singleSwapGasLimitKey()],
|
|
105
|
-
},
|
|
106
|
-
swapOrder: {
|
|
107
|
-
methodName: "getUint",
|
|
108
|
-
params: [swapOrderGasLimitKey()],
|
|
109
|
-
},
|
|
110
|
-
increaseOrder: {
|
|
111
|
-
methodName: "getUint",
|
|
112
|
-
params: [increaseOrderGasLimitKey()],
|
|
113
|
-
},
|
|
114
|
-
decreaseOrder: {
|
|
115
|
-
methodName: "getUint",
|
|
116
|
-
params: [decreaseOrderGasLimitKey()],
|
|
117
|
-
},
|
|
118
|
-
estimatedGasFeeBaseAmount: {
|
|
119
|
-
methodName: "getUint",
|
|
120
|
-
params: [ESTIMATED_GAS_FEE_BASE_AMOUNT_V2_1],
|
|
121
|
-
},
|
|
122
|
-
estimatedGasFeePerOraclePrice: {
|
|
123
|
-
methodName: "getUint",
|
|
124
|
-
params: [ESTIMATED_GAS_FEE_PER_ORACLE_PRICE],
|
|
125
|
-
},
|
|
126
|
-
estimatedFeeMultiplierFactor: {
|
|
127
|
-
methodName: "getUint",
|
|
128
|
-
params: [ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR],
|
|
129
|
-
},
|
|
130
|
-
glvDepositGasLimit: {
|
|
131
|
-
methodName: "getUint",
|
|
132
|
-
params: [GLV_DEPOSIT_GAS_LIMIT],
|
|
133
|
-
},
|
|
134
|
-
glvWithdrawalGasLimit: {
|
|
135
|
-
methodName: "getUint",
|
|
136
|
-
params: [GLV_WITHDRAWAL_GAS_LIMIT],
|
|
137
|
-
},
|
|
138
|
-
glvPerMarketGasLimit: {
|
|
139
|
-
methodName: "getUint",
|
|
140
|
-
params: [GLV_PER_MARKET_GAS_LIMIT],
|
|
141
|
-
},
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
}).then((res) => {
|
|
145
|
-
const results = res.data.dataStore;
|
|
146
|
-
function getBigInt(key) {
|
|
147
|
-
return BigInt(results[key].returnValues[0]);
|
|
148
|
-
}
|
|
149
|
-
return {
|
|
150
|
-
depositSingleToken: getBigInt("depositSingleToken"),
|
|
151
|
-
depositMultiToken: getBigInt("depositMultiToken"),
|
|
152
|
-
withdrawalMultiToken: getBigInt("withdrawalMultiToken"),
|
|
153
|
-
shift: getBigInt("shift"),
|
|
154
|
-
singleSwap: getBigInt("singleSwap"),
|
|
155
|
-
swapOrder: getBigInt("swapOrder"),
|
|
156
|
-
increaseOrder: getBigInt("increaseOrder"),
|
|
157
|
-
decreaseOrder: getBigInt("decreaseOrder"),
|
|
158
|
-
estimatedGasFeeBaseAmount: getBigInt("estimatedGasFeeBaseAmount"),
|
|
159
|
-
estimatedGasFeePerOraclePrice: getBigInt("estimatedGasFeePerOraclePrice"),
|
|
160
|
-
estimatedFeeMultiplierFactor: getBigInt("estimatedFeeMultiplierFactor"),
|
|
161
|
-
glvDepositGasLimit: getBigInt("glvDepositGasLimit"),
|
|
162
|
-
glvWithdrawalGasLimit: getBigInt("glvWithdrawalGasLimit"),
|
|
163
|
-
glvPerMarketGasLimit: getBigInt("glvPerMarketGasLimit"),
|
|
164
|
-
};
|
|
165
|
-
});
|
|
166
|
-
this._gasLimits = gasLimits;
|
|
167
|
-
return gasLimits;
|
|
168
|
-
}
|
|
169
|
-
async getEstimatedGasFee(tradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
170
|
-
const gasLimits = await this.getGasLimits();
|
|
171
|
-
switch (tradeFeesType) {
|
|
172
|
-
case "swap": {
|
|
173
|
-
if (!swapAmounts || !swapAmounts.swapPathStats)
|
|
174
|
-
return null;
|
|
175
|
-
return estimateExecuteSwapOrderGasLimit(gasLimits, {
|
|
176
|
-
swapsCount: swapAmounts.swapPathStats.swapPath.length,
|
|
177
|
-
callbackGasLimit: 0n,
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
case "increase": {
|
|
181
|
-
if (!increaseAmounts)
|
|
182
|
-
return null;
|
|
183
|
-
return estimateExecuteIncreaseOrderGasLimit(gasLimits, {
|
|
184
|
-
swapsCount: increaseAmounts.swapPathStats?.swapPath.length,
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
case "decrease": {
|
|
188
|
-
if (!decreaseAmounts)
|
|
189
|
-
return null;
|
|
190
|
-
return estimateExecuteDecreaseOrderGasLimit(gasLimits, {
|
|
191
|
-
callbackGasLimit: 0n,
|
|
192
|
-
decreaseSwapType: decreaseAmounts.decreaseSwapType,
|
|
193
|
-
swapsCount: 0,
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
case "edit":
|
|
197
|
-
return null;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
async getExecutionFee(tradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
201
|
-
const gasLimits = await this.getGasLimits();
|
|
202
|
-
const { tokensData } = await this.tokens.getTokensData();
|
|
203
|
-
const gasPrice = await this.getGasPrice();
|
|
204
|
-
const estimatedGas = await this.getEstimatedGasFee(tradeFeesType, {
|
|
205
|
-
increaseAmounts,
|
|
206
|
-
decreaseAmounts,
|
|
207
|
-
swapAmounts,
|
|
208
|
-
});
|
|
209
|
-
if (estimatedGas === null || estimatedGas === undefined)
|
|
210
|
-
return undefined;
|
|
211
|
-
const swapsCount = getSwapCount({
|
|
212
|
-
isSwap: tradeFeesType === "swap",
|
|
213
|
-
isIncrease: tradeFeesType === "increase",
|
|
214
|
-
increaseAmounts,
|
|
215
|
-
decreaseAmounts,
|
|
216
|
-
swapAmounts,
|
|
217
|
-
});
|
|
218
|
-
if (swapsCount === undefined)
|
|
219
|
-
return undefined;
|
|
220
|
-
if (tokensData === undefined)
|
|
221
|
-
return undefined;
|
|
222
|
-
if (gasPrice === undefined)
|
|
223
|
-
return undefined;
|
|
224
|
-
const oraclePriceCount = estimateOrderOraclePriceCount(swapsCount);
|
|
225
|
-
return getExecutionFee(this, gasLimits, tokensData, estimatedGas, gasPrice, oraclePriceCount);
|
|
226
|
-
}
|
|
227
|
-
async getGasPrice() {
|
|
228
|
-
const executionFeeConfig = EXECUTION_FEE_CONFIG_V2[this.chainId];
|
|
229
|
-
const feeData = await withRetry(() => this.publicClient.estimateFeesPerGas({
|
|
230
|
-
chain: getChain(this.chainId),
|
|
231
|
-
type: "legacy",
|
|
232
|
-
}), {
|
|
233
|
-
retryCount: 2,
|
|
234
|
-
shouldRetry: ({ error }) => {
|
|
235
|
-
const isInvalidBlockError = error?.message?.includes("invalid value for value.hash");
|
|
236
|
-
return isInvalidBlockError;
|
|
237
|
-
},
|
|
238
|
-
});
|
|
239
|
-
let gasPrice = feeData.gasPrice ?? 0n;
|
|
240
|
-
if (executionFeeConfig.shouldUseMaxPriorityFeePerGas) {
|
|
241
|
-
const maxPriorityFeePerGas = bigMath.max(feeData?.maxPriorityFeePerGas ?? 0n, MAX_PRIORITY_FEE_PER_GAS_MAP[this.chainId] ?? 0n);
|
|
242
|
-
gasPrice = gasPrice + maxPriorityFeePerGas;
|
|
243
|
-
}
|
|
244
|
-
const premium = GAS_PRICE_PREMIUM_MAP[this.chainId] ?? 0n;
|
|
245
|
-
const price = gasPrice + premium;
|
|
246
|
-
return price === undefined ? undefined : BigInt(gasPrice);
|
|
247
|
-
}
|
|
248
71
|
}
|
|
@@ -7,5 +7,5 @@ export declare class Markets extends Module {
|
|
|
7
7
|
private _marketsData;
|
|
8
8
|
getMarkets(offset?: bigint, limit?: bigint): Promise<MarketsResult>;
|
|
9
9
|
getMarketsInfo(): Promise<MarketsInfoResult>;
|
|
10
|
-
getDailyVolumes(): Promise<
|
|
10
|
+
getDailyVolumes(): Promise<Record<string, bigint> | undefined>;
|
|
11
11
|
}
|
|
@@ -88,9 +88,9 @@ export class Orders extends Module {
|
|
|
88
88
|
if (!account) {
|
|
89
89
|
throw new Error("Account is not defined");
|
|
90
90
|
}
|
|
91
|
-
const gasLimits = await this.sdk.getGasLimits();
|
|
92
|
-
const gasPrice = await this.sdk.getGasPrice();
|
|
93
|
-
const executionFee = await this.sdk.getExecutionFee("increase", {
|
|
91
|
+
const gasLimits = await this.sdk.utils.getGasLimits();
|
|
92
|
+
const gasPrice = await this.sdk.utils.getGasPrice();
|
|
93
|
+
const executionFee = await this.sdk.utils.getExecutionFee("increase", tokensData, {
|
|
94
94
|
increaseAmounts,
|
|
95
95
|
});
|
|
96
96
|
if (!executionFee) {
|
|
@@ -191,7 +191,7 @@ export class Orders extends Module {
|
|
|
191
191
|
if (!account) {
|
|
192
192
|
throw new Error("Account is not defined");
|
|
193
193
|
}
|
|
194
|
-
const executionFee = await this.sdk.getExecutionFee("decrease", {
|
|
194
|
+
const executionFee = await this.sdk.utils.getExecutionFee("decrease", tokensData, {
|
|
195
195
|
decreaseAmounts,
|
|
196
196
|
});
|
|
197
197
|
if (!executionFee) {
|
|
@@ -240,7 +240,7 @@ export class Orders extends Module {
|
|
|
240
240
|
}
|
|
241
241
|
async createSwapOrder({ isLimit, swapAmounts, allowedSlippage, fromToken, toToken, referralCodeForTxn, tokensData, }) {
|
|
242
242
|
const orderType = isLimit ? OrderType.LimitSwap : OrderType.MarketSwap;
|
|
243
|
-
const executionFee = await this.sdk.getExecutionFee("swap", {
|
|
243
|
+
const executionFee = await this.sdk.utils.getExecutionFee("swap", tokensData, {
|
|
244
244
|
swapAmounts,
|
|
245
245
|
});
|
|
246
246
|
if (!swapAmounts?.swapPathStats || !executionFee) {
|
|
@@ -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,188 @@
|
|
|
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
|
+
depositSingleToken: {
|
|
27
|
+
methodName: "getUint",
|
|
28
|
+
params: [depositGasLimitKey(true)],
|
|
29
|
+
},
|
|
30
|
+
depositMultiToken: {
|
|
31
|
+
methodName: "getUint",
|
|
32
|
+
params: [depositGasLimitKey(false)],
|
|
33
|
+
},
|
|
34
|
+
withdrawalMultiToken: {
|
|
35
|
+
methodName: "getUint",
|
|
36
|
+
params: [withdrawalGasLimitKey()],
|
|
37
|
+
},
|
|
38
|
+
shift: {
|
|
39
|
+
methodName: "getUint",
|
|
40
|
+
params: [shiftGasLimitKey()],
|
|
41
|
+
},
|
|
42
|
+
singleSwap: {
|
|
43
|
+
methodName: "getUint",
|
|
44
|
+
params: [singleSwapGasLimitKey()],
|
|
45
|
+
},
|
|
46
|
+
swapOrder: {
|
|
47
|
+
methodName: "getUint",
|
|
48
|
+
params: [swapOrderGasLimitKey()],
|
|
49
|
+
},
|
|
50
|
+
increaseOrder: {
|
|
51
|
+
methodName: "getUint",
|
|
52
|
+
params: [increaseOrderGasLimitKey()],
|
|
53
|
+
},
|
|
54
|
+
decreaseOrder: {
|
|
55
|
+
methodName: "getUint",
|
|
56
|
+
params: [decreaseOrderGasLimitKey()],
|
|
57
|
+
},
|
|
58
|
+
estimatedGasFeeBaseAmount: {
|
|
59
|
+
methodName: "getUint",
|
|
60
|
+
params: [ESTIMATED_GAS_FEE_BASE_AMOUNT_V2_1],
|
|
61
|
+
},
|
|
62
|
+
estimatedGasFeePerOraclePrice: {
|
|
63
|
+
methodName: "getUint",
|
|
64
|
+
params: [ESTIMATED_GAS_FEE_PER_ORACLE_PRICE],
|
|
65
|
+
},
|
|
66
|
+
estimatedFeeMultiplierFactor: {
|
|
67
|
+
methodName: "getUint",
|
|
68
|
+
params: [ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR],
|
|
69
|
+
},
|
|
70
|
+
glvDepositGasLimit: {
|
|
71
|
+
methodName: "getUint",
|
|
72
|
+
params: [GLV_DEPOSIT_GAS_LIMIT],
|
|
73
|
+
},
|
|
74
|
+
glvWithdrawalGasLimit: {
|
|
75
|
+
methodName: "getUint",
|
|
76
|
+
params: [GLV_WITHDRAWAL_GAS_LIMIT],
|
|
77
|
+
},
|
|
78
|
+
glvPerMarketGasLimit: {
|
|
79
|
+
methodName: "getUint",
|
|
80
|
+
params: [GLV_PER_MARKET_GAS_LIMIT],
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
.then((res) => {
|
|
86
|
+
const results = res.data.dataStore;
|
|
87
|
+
function getBigInt(key) {
|
|
88
|
+
return BigInt(results[key].returnValues[0]);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
depositSingleToken: getBigInt("depositSingleToken"),
|
|
92
|
+
depositMultiToken: getBigInt("depositMultiToken"),
|
|
93
|
+
withdrawalMultiToken: getBigInt("withdrawalMultiToken"),
|
|
94
|
+
shift: getBigInt("shift"),
|
|
95
|
+
singleSwap: getBigInt("singleSwap"),
|
|
96
|
+
swapOrder: getBigInt("swapOrder"),
|
|
97
|
+
increaseOrder: getBigInt("increaseOrder"),
|
|
98
|
+
decreaseOrder: getBigInt("decreaseOrder"),
|
|
99
|
+
estimatedGasFeeBaseAmount: getBigInt("estimatedGasFeeBaseAmount"),
|
|
100
|
+
estimatedGasFeePerOraclePrice: getBigInt("estimatedGasFeePerOraclePrice"),
|
|
101
|
+
estimatedFeeMultiplierFactor: getBigInt("estimatedFeeMultiplierFactor"),
|
|
102
|
+
glvDepositGasLimit: getBigInt("glvDepositGasLimit"),
|
|
103
|
+
glvWithdrawalGasLimit: getBigInt("glvWithdrawalGasLimit"),
|
|
104
|
+
glvPerMarketGasLimit: getBigInt("glvPerMarketGasLimit"),
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
this._gasLimits = gasLimits;
|
|
108
|
+
return gasLimits;
|
|
109
|
+
}
|
|
110
|
+
async getEstimatedGasFee(tradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
111
|
+
const gasLimits = await this.getGasLimits();
|
|
112
|
+
switch (tradeFeesType) {
|
|
113
|
+
case "swap": {
|
|
114
|
+
if (!swapAmounts || !swapAmounts.swapPathStats)
|
|
115
|
+
return null;
|
|
116
|
+
return estimateExecuteSwapOrderGasLimit(gasLimits, {
|
|
117
|
+
swapsCount: swapAmounts.swapPathStats.swapPath.length,
|
|
118
|
+
callbackGasLimit: 0n,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
case "increase": {
|
|
122
|
+
if (!increaseAmounts)
|
|
123
|
+
return null;
|
|
124
|
+
return estimateExecuteIncreaseOrderGasLimit(gasLimits, {
|
|
125
|
+
swapsCount: increaseAmounts.swapPathStats?.swapPath.length,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
case "decrease": {
|
|
129
|
+
if (!decreaseAmounts)
|
|
130
|
+
return null;
|
|
131
|
+
return estimateExecuteDecreaseOrderGasLimit(gasLimits, {
|
|
132
|
+
callbackGasLimit: 0n,
|
|
133
|
+
decreaseSwapType: decreaseAmounts.decreaseSwapType,
|
|
134
|
+
swapsCount: 0,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
case "edit":
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async getExecutionFee(tradeFeesType, tokensData, { increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
142
|
+
const gasLimits = await this.getGasLimits();
|
|
143
|
+
const gasPrice = await this.getGasPrice();
|
|
144
|
+
const estimatedGas = await this.getEstimatedGasFee(tradeFeesType, {
|
|
145
|
+
increaseAmounts,
|
|
146
|
+
decreaseAmounts,
|
|
147
|
+
swapAmounts,
|
|
148
|
+
});
|
|
149
|
+
if (estimatedGas === null || estimatedGas === undefined)
|
|
150
|
+
return undefined;
|
|
151
|
+
const swapsCount = getSwapCount({
|
|
152
|
+
isSwap: tradeFeesType === "swap",
|
|
153
|
+
isIncrease: tradeFeesType === "increase",
|
|
154
|
+
increaseAmounts,
|
|
155
|
+
decreaseAmounts,
|
|
156
|
+
swapAmounts,
|
|
157
|
+
});
|
|
158
|
+
if (swapsCount === undefined)
|
|
159
|
+
return undefined;
|
|
160
|
+
if (tokensData === undefined)
|
|
161
|
+
return undefined;
|
|
162
|
+
if (gasPrice === undefined)
|
|
163
|
+
return undefined;
|
|
164
|
+
const oraclePriceCount = estimateOrderOraclePriceCount(swapsCount);
|
|
165
|
+
return getExecutionFee(this.sdk, gasLimits, tokensData, estimatedGas, gasPrice, oraclePriceCount);
|
|
166
|
+
}
|
|
167
|
+
async getGasPrice() {
|
|
168
|
+
const executionFeeConfig = EXECUTION_FEE_CONFIG_V2[this.chainId];
|
|
169
|
+
const feeData = await withRetry(() => this.sdk.publicClient.estimateFeesPerGas({
|
|
170
|
+
chain: getChain(this.chainId),
|
|
171
|
+
type: "legacy",
|
|
172
|
+
}), {
|
|
173
|
+
retryCount: 2,
|
|
174
|
+
shouldRetry: ({ error }) => {
|
|
175
|
+
const isInvalidBlockError = error?.message?.includes("invalid value for value.hash");
|
|
176
|
+
return isInvalidBlockError;
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
let gasPrice = feeData.gasPrice ?? 0n;
|
|
180
|
+
if (executionFeeConfig.shouldUseMaxPriorityFeePerGas) {
|
|
181
|
+
const maxPriorityFeePerGas = bigMath.max(feeData?.maxPriorityFeePerGas ?? 0n, MAX_PRIORITY_FEE_PER_GAS_MAP[this.chainId] ?? 0n);
|
|
182
|
+
gasPrice = gasPrice + maxPriorityFeePerGas;
|
|
183
|
+
}
|
|
184
|
+
const premium = GAS_PRICE_PREMIUM_MAP[this.chainId] ?? 0n;
|
|
185
|
+
const price = gasPrice + premium;
|
|
186
|
+
return price === undefined ? undefined : BigInt(gasPrice);
|
|
187
|
+
}
|
|
188
|
+
}
|