@gmx-io/sdk 0.0.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/LICENSE +40 -0
- package/README.md +79 -0
- package/build/abis/CustomErrors.json +3254 -0
- package/build/abis/DataStore.json +1440 -0
- package/build/abis/ERC721.json +442 -0
- package/build/abis/EventEmitter.json +2065 -0
- package/build/abis/ExchangeRouter.json +1598 -0
- package/build/abis/GMT.json +522 -0
- package/build/abis/GlpManager.json +592 -0
- package/build/abis/GlvReader.json +1425 -0
- package/build/abis/GlvRouter.json +613 -0
- package/build/abis/GmxMigrator.json +657 -0
- package/build/abis/GovToken.json +1121 -0
- package/build/abis/MintableBaseToken.json +696 -0
- package/build/abis/Multicall.json +333 -0
- package/build/abis/OrderBook.json +1997 -0
- package/build/abis/OrderBookReader.json +111 -0
- package/build/abis/OrderExecutor.json +122 -0
- package/build/abis/PositionManager.json +1165 -0
- package/build/abis/PositionRouter.json +2030 -0
- package/build/abis/Reader.json +355 -0
- package/build/abis/ReaderV2.json +630 -0
- package/build/abis/ReferralStorage.json +507 -0
- package/build/abis/RewardReader.json +86 -0
- package/build/abis/RewardRouter.json +769 -0
- package/build/abis/RewardTracker.json +918 -0
- package/build/abis/Router-v2.json +605 -0
- package/build/abis/Router.json +605 -0
- package/build/abis/SubaccountRouter.json +648 -0
- package/build/abis/SyntheticsReader.json +4577 -0
- package/build/abis/SyntheticsRouter.json +77 -0
- package/build/abis/Timelock.json +1737 -0
- package/build/abis/Token.json +345 -0
- package/build/abis/Treasury.json +411 -0
- package/build/abis/UniPool.json +89 -0
- package/build/abis/UniswapV2.json +1 -0
- package/build/abis/Vault.json +3220 -0
- package/build/abis/VaultReader.json +87 -0
- package/build/abis/VaultV2.json +3019 -0
- package/build/abis/VaultV2b.json +3220 -0
- package/build/abis/Vester.json +1031 -0
- package/build/abis/WETH.json +320 -0
- package/build/abis/YieldFarm.json +599 -0
- package/build/abis/YieldToken.json +560 -0
- package/build/prebuilt/hashedMarketConfigKeys.json +3658 -0
- package/build/prebuilt/hashedMarketValuesKeys.json +1103 -0
- package/build/prebuilt/index.ts +28 -0
- package/build/src/abis/CustomErrors.json +3254 -0
- package/build/src/abis/DataStore.json +1440 -0
- package/build/src/abis/ERC721.json +442 -0
- package/build/src/abis/EventEmitter.json +2065 -0
- package/build/src/abis/ExchangeRouter.json +1598 -0
- package/build/src/abis/GMT.json +522 -0
- package/build/src/abis/GlpManager.json +591 -0
- package/build/src/abis/GlvReader.json +1424 -0
- package/build/src/abis/GlvRouter.json +612 -0
- package/build/src/abis/GmxMigrator.json +657 -0
- package/build/src/abis/GovToken.json +1121 -0
- package/build/src/abis/MintableBaseToken.json +696 -0
- package/build/src/abis/Multicall.json +333 -0
- package/build/src/abis/OrderBook.json +1997 -0
- package/build/src/abis/OrderBookReader.json +111 -0
- package/build/src/abis/OrderExecutor.json +122 -0
- package/build/src/abis/PositionManager.json +1165 -0
- package/build/src/abis/PositionRouter.json +2030 -0
- package/build/src/abis/Reader.json +355 -0
- package/build/src/abis/ReaderV2.json +630 -0
- package/build/src/abis/ReferralStorage.json +507 -0
- package/build/src/abis/RewardReader.json +86 -0
- package/build/src/abis/RewardRouter.json +769 -0
- package/build/src/abis/RewardTracker.json +918 -0
- package/build/src/abis/Router-v2.json +605 -0
- package/build/src/abis/Router.json +605 -0
- package/build/src/abis/SubaccountRouter.json +648 -0
- package/build/src/abis/SyntheticsReader.json +4577 -0
- package/build/src/abis/SyntheticsRouter.json +77 -0
- package/build/src/abis/Timelock.json +1737 -0
- package/build/src/abis/Token.json +345 -0
- package/build/src/abis/Treasury.json +411 -0
- package/build/src/abis/UniPool.json +89 -0
- package/build/src/abis/UniswapV2.json +1 -0
- package/build/src/abis/Vault.json +3220 -0
- package/build/src/abis/VaultReader.json +87 -0
- package/build/src/abis/VaultV2.json +3019 -0
- package/build/src/abis/VaultV2b.json +3220 -0
- package/build/src/abis/Vester.json +1031 -0
- package/build/src/abis/WETH.json +320 -0
- package/build/src/abis/YieldFarm.json +599 -0
- package/build/src/abis/YieldToken.json +560 -0
- package/build/src/configs/batch.d.ts +6 -0
- package/build/src/configs/batch.js +51 -0
- package/build/src/configs/chains.d.ts +37 -0
- package/build/src/configs/chains.js +90 -0
- package/build/src/configs/contracts.d.ts +2 -0
- package/build/src/configs/contracts.js +218 -0
- package/build/src/configs/dataStore.d.ts +149 -0
- package/build/src/configs/dataStore.js +276 -0
- package/build/src/configs/factors.d.ts +20 -0
- package/build/src/configs/factors.js +21 -0
- package/build/src/configs/markets.d.ts +8 -0
- package/build/src/configs/markets.js +528 -0
- package/build/src/configs/subgraph.d.ts +3 -0
- package/build/src/configs/subgraph.js +3 -0
- package/build/src/configs/tokens.d.ts +89 -0
- package/build/src/configs/tokens.js +1269 -0
- package/build/src/index.d.ts +44 -0
- package/build/src/index.js +245 -0
- package/build/src/modules/accounts/accounts.d.ts +5 -0
- package/build/src/modules/accounts/accounts.js +36 -0
- package/build/src/modules/accounts/accounts.spec.d.ts +1 -0
- package/build/src/modules/accounts/accounts.spec.js +7 -0
- package/build/src/modules/base.d.ts +9 -0
- package/build/src/modules/base.js +15 -0
- package/build/src/modules/markets/index.d.ts +26 -0
- package/build/src/modules/markets/index.js +339 -0
- package/build/src/modules/markets/markets.spec.d.ts +1 -0
- package/build/src/modules/markets/markets.spec.js +38 -0
- package/build/src/modules/markets/query-builders.d.ts +40 -0
- package/build/src/modules/markets/query-builders.js +379 -0
- package/build/src/modules/markets/types.d.ts +42 -0
- package/build/src/modules/markets/types.js +1 -0
- package/build/src/modules/oracle.d.ts +31 -0
- package/build/src/modules/oracle.js +53 -0
- package/build/src/modules/orders/orders.d.ts +60 -0
- package/build/src/modules/orders/orders.js +270 -0
- package/build/src/modules/orders/orders.spec.d.ts +1 -0
- package/build/src/modules/orders/orders.spec.js +16 -0
- package/build/src/modules/orders/transactions/cancelOrdersTxn.d.ts +6 -0
- package/build/src/modules/orders/transactions/cancelOrdersTxn.js +15 -0
- package/build/src/modules/orders/transactions/createDecreaseOrderTxn.d.ts +32 -0
- package/build/src/modules/orders/transactions/createDecreaseOrderTxn.js +98 -0
- package/build/src/modules/orders/transactions/createIncreaseOrderTxn.d.ts +77 -0
- package/build/src/modules/orders/transactions/createIncreaseOrderTxn.js +164 -0
- package/build/src/modules/orders/transactions/createSwapOrderTxn.d.ts +16 -0
- package/build/src/modules/orders/transactions/createSwapOrderTxn.js +97 -0
- package/build/src/modules/orders/transactions/createWrapOrUnwrapTxn.d.ts +6 -0
- package/build/src/modules/orders/transactions/createWrapOrUnwrapTxn.js +13 -0
- package/build/src/modules/orders/transactions/updateOrderTxn.d.ts +23 -0
- package/build/src/modules/orders/transactions/updateOrderTxn.js +44 -0
- package/build/src/modules/orders/utils.d.ts +248 -0
- package/build/src/modules/orders/utils.js +151 -0
- package/build/src/modules/positions/positions.d.ts +48 -0
- package/build/src/modules/positions/positions.js +563 -0
- package/build/src/modules/positions/positions.spec.d.ts +1 -0
- package/build/src/modules/positions/positions.spec.js +25 -0
- package/build/src/modules/tokens/tokens.d.ts +26 -0
- package/build/src/modules/tokens/tokens.js +132 -0
- package/build/src/modules/tokens/tokens.spec.d.ts +1 -0
- package/build/src/modules/tokens/tokens.spec.js +19 -0
- package/build/src/modules/trades/trades.d.ts +48 -0
- package/build/src/modules/trades/trades.js +390 -0
- package/build/src/modules/trades/trades.spec.d.ts +1 -0
- package/build/src/modules/trades/trades.spec.js +14 -0
- package/build/src/prebuilt/hashedMarketConfigKeys.json +3658 -0
- package/build/src/prebuilt/hashedMarketValuesKeys.json +1103 -0
- package/build/src/prebuilt/index.d.ts +14 -0
- package/build/src/prebuilt/index.js +9 -0
- package/build/src/types/amounts.d.ts +223 -0
- package/build/src/types/amounts.js +17 -0
- package/build/src/types/fees.d.ts +34 -0
- package/build/src/types/fees.js +1 -0
- package/build/src/types/markets.d.ts +143 -0
- package/build/src/types/markets.js +1 -0
- package/build/src/types/orders.d.ts +74 -0
- package/build/src/types/orders.js +33 -0
- package/build/src/types/positions.d.ts +53 -0
- package/build/src/types/positions.js +1 -0
- package/build/src/types/referrals.d.ts +124 -0
- package/build/src/types/referrals.js +9 -0
- package/build/src/types/sdk.d.ts +32 -0
- package/build/src/types/sdk.js +1 -0
- package/build/src/types/sidecarOrders.d.ts +54 -0
- package/build/src/types/sidecarOrders.js +1 -0
- package/build/src/types/syntheticsEvents.d.ts +88 -0
- package/build/src/types/syntheticsEvents.js +1 -0
- package/build/src/types/tokens.d.ts +89 -0
- package/build/src/types/tokens.js +1 -0
- package/build/src/types/trade.d.ts +223 -0
- package/build/src/types/trade.js +17 -0
- package/build/src/types/tradeHistory.d.ts +107 -0
- package/build/src/types/tradeHistory.js +8 -0
- package/build/src/utils/LruCache.d.ts +10 -0
- package/build/src/utils/LruCache.js +45 -0
- package/build/src/utils/bigmath.d.ts +7 -0
- package/build/src/utils/bigmath.js +23 -0
- package/build/src/utils/buildUrl.d.ts +1 -0
- package/build/src/utils/buildUrl.js +5 -0
- package/build/src/utils/callContract.d.ts +17 -0
- package/build/src/utils/callContract.js +133 -0
- package/build/src/utils/common.d.ts +2 -0
- package/build/src/utils/common.js +2 -0
- package/build/src/utils/fees/estimateOraclePriceCount.d.ts +6 -0
- package/build/src/utils/fees/estimateOraclePriceCount.js +19 -0
- package/build/src/utils/fees/executionFee.d.ts +59 -0
- package/build/src/utils/fees/executionFee.js +108 -0
- package/build/src/utils/fees/index.d.ts +29 -0
- package/build/src/utils/fees/index.js +89 -0
- package/build/src/utils/fees/priceImpact.d.ts +75 -0
- package/build/src/utils/fees/priceImpact.js +287 -0
- package/build/src/utils/graphqlFetcher.d.ts +1 -0
- package/build/src/utils/graphqlFetcher.js +18 -0
- package/build/src/utils/hash.d.ts +5 -0
- package/build/src/utils/hash.js +35 -0
- package/build/src/utils/marketKeysAndConfigs.d.ts +66 -0
- package/build/src/utils/marketKeysAndConfigs.js +257 -0
- package/build/src/utils/markets.d.ts +40 -0
- package/build/src/utils/markets.js +129 -0
- package/build/src/utils/multicall.d.ts +64 -0
- package/build/src/utils/multicall.js +116 -0
- package/build/src/utils/numbers.d.ts +9 -0
- package/build/src/utils/numbers.js +30 -0
- package/build/src/utils/objects.d.ts +18 -0
- package/build/src/utils/objects.js +30 -0
- package/build/src/utils/orders.d.ts +28 -0
- package/build/src/utils/orders.js +178 -0
- package/build/src/utils/positions.d.ts +60 -0
- package/build/src/utils/positions.js +133 -0
- package/build/src/utils/prices.d.ts +15 -0
- package/build/src/utils/prices.js +34 -0
- package/build/src/utils/referrals.d.ts +2 -0
- package/build/src/utils/referrals.js +27 -0
- package/build/src/utils/simulateExecuteOrder.d.ts +16 -0
- package/build/src/utils/simulateExecuteOrder.js +143 -0
- package/build/src/utils/slidingWindowFallbackSwitcher.d.ts +24 -0
- package/build/src/utils/slidingWindowFallbackSwitcher.js +40 -0
- package/build/src/utils/subgraph.d.ts +21 -0
- package/build/src/utils/subgraph.js +72 -0
- package/build/src/utils/swapStats.d.ts +35 -0
- package/build/src/utils/swapStats.js +197 -0
- package/build/src/utils/test.d.ts +2 -0
- package/build/src/utils/test.js +24 -0
- package/build/src/utils/tokens.d.ts +18 -0
- package/build/src/utils/tokens.js +64 -0
- package/build/src/utils/trade.d.ts +10 -0
- package/build/src/utils/trade.js +32 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/package.json +96 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Abi, Address, PublicClient, WalletClient } from "viem";
|
|
2
|
+
import { Accounts } from "modules/accounts/accounts";
|
|
3
|
+
import { Markets } from "modules/markets";
|
|
4
|
+
import { Orders } from "modules/orders/orders";
|
|
5
|
+
import { Positions } from "modules/positions/positions";
|
|
6
|
+
import { Tokens } from "modules/tokens/tokens";
|
|
7
|
+
import { Trades } from "modules/trades/trades";
|
|
8
|
+
import { Oracle } from "modules/oracle";
|
|
9
|
+
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
|
+
import { CallContractOpts } from "utils/callContract";
|
|
14
|
+
import { MulticallRequestConfig } from "utils/multicall";
|
|
15
|
+
export declare class GmxSdk {
|
|
16
|
+
config: GmxSdkConfig;
|
|
17
|
+
readonly markets: Markets;
|
|
18
|
+
readonly tokens: Tokens;
|
|
19
|
+
readonly positions: Positions;
|
|
20
|
+
readonly orders: Orders;
|
|
21
|
+
readonly trades: Trades;
|
|
22
|
+
readonly accounts: Accounts;
|
|
23
|
+
readonly oracle: Oracle;
|
|
24
|
+
publicClient: PublicClient;
|
|
25
|
+
walletClient: WalletClient;
|
|
26
|
+
constructor(config: GmxSdkConfig);
|
|
27
|
+
setAccount(account: Address): void;
|
|
28
|
+
executeMulticall<T = any>(request: MulticallRequestConfig<any>): Promise<T>;
|
|
29
|
+
callContract(address: Address, abi: Abi, method: string, params: any[], opts?: CallContractOpts): Promise<`0x${string}`>;
|
|
30
|
+
get chainId(): number;
|
|
31
|
+
_gasLimits: GasLimitsConfig | null;
|
|
32
|
+
getGasLimits(): Promise<GasLimitsConfig>;
|
|
33
|
+
getEstimatedGasFee(tradeFeesType: TradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }: {
|
|
34
|
+
swapAmounts?: SwapAmounts;
|
|
35
|
+
decreaseAmounts?: DecreasePositionAmounts;
|
|
36
|
+
increaseAmounts?: IncreasePositionAmounts;
|
|
37
|
+
}): Promise<bigint | null>;
|
|
38
|
+
getExecutionFee(tradeFeesType: TradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }: {
|
|
39
|
+
swapAmounts?: SwapAmounts;
|
|
40
|
+
decreaseAmounts?: DecreasePositionAmounts;
|
|
41
|
+
increaseAmounts?: IncreasePositionAmounts;
|
|
42
|
+
}): Promise<import("types/fees").ExecutionFee | undefined>;
|
|
43
|
+
getGasPrice(): Promise<bigint | undefined>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { createPublicClient, createWalletClient, http, withRetry, } from "viem";
|
|
2
|
+
import { Accounts } from "modules/accounts/accounts";
|
|
3
|
+
import { Markets } from "modules/markets";
|
|
4
|
+
import { Orders } from "modules/orders/orders";
|
|
5
|
+
import { Positions } from "modules/positions/positions";
|
|
6
|
+
import { Tokens } from "modules/tokens/tokens";
|
|
7
|
+
import { Trades } from "modules/trades/trades";
|
|
8
|
+
import { Oracle } from "modules/oracle";
|
|
9
|
+
import { BATCH_CONFIGS } from "configs/batch";
|
|
10
|
+
import { getContract } from "configs/contracts";
|
|
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";
|
|
15
|
+
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
|
+
import { MAX_TIMEOUT, Multicall } from "utils/multicall";
|
|
20
|
+
export class GmxSdk {
|
|
21
|
+
constructor(config) {
|
|
22
|
+
this.config = config;
|
|
23
|
+
this.markets = new Markets(this);
|
|
24
|
+
this.tokens = new Tokens(this);
|
|
25
|
+
this.positions = new Positions(this);
|
|
26
|
+
this.orders = new Orders(this);
|
|
27
|
+
this.trades = new Trades(this);
|
|
28
|
+
this.accounts = new Accounts(this);
|
|
29
|
+
this._gasLimits = null;
|
|
30
|
+
this.oracle = new Oracle(this);
|
|
31
|
+
this.publicClient =
|
|
32
|
+
config.publicClient ??
|
|
33
|
+
createPublicClient({
|
|
34
|
+
transport: http(this.config.rpcUrl, {
|
|
35
|
+
// retries works strangely in viem, so we disable them
|
|
36
|
+
retryCount: 0,
|
|
37
|
+
retryDelay: 10000000,
|
|
38
|
+
batch: BATCH_CONFIGS[this.config.chainId].http,
|
|
39
|
+
timeout: MAX_TIMEOUT,
|
|
40
|
+
}),
|
|
41
|
+
pollingInterval: undefined,
|
|
42
|
+
batch: BATCH_CONFIGS[this.config.chainId].client,
|
|
43
|
+
chain: getChain(this.config.chainId),
|
|
44
|
+
});
|
|
45
|
+
this.walletClient =
|
|
46
|
+
config.walletClient ??
|
|
47
|
+
createWalletClient({
|
|
48
|
+
account: config.account,
|
|
49
|
+
chain: getChain(config.chainId),
|
|
50
|
+
transport: http(config.rpcUrl, {
|
|
51
|
+
retryCount: 0,
|
|
52
|
+
retryDelay: 10000000,
|
|
53
|
+
batch: BATCH_CONFIGS[config.chainId].http,
|
|
54
|
+
timeout: MAX_TIMEOUT,
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
setAccount(account) {
|
|
59
|
+
this.config.account = account;
|
|
60
|
+
}
|
|
61
|
+
async executeMulticall(request) {
|
|
62
|
+
const multicall = await Multicall.getInstance(this);
|
|
63
|
+
const promise = multicall?.call(request, MAX_TIMEOUT);
|
|
64
|
+
// debug mode
|
|
65
|
+
promise.request = request;
|
|
66
|
+
return promise;
|
|
67
|
+
}
|
|
68
|
+
async callContract(address, abi, method, params, opts) {
|
|
69
|
+
return callContract(this, address, abi, method, params, opts);
|
|
70
|
+
}
|
|
71
|
+
get chainId() {
|
|
72
|
+
return this.config.chainId;
|
|
73
|
+
}
|
|
74
|
+
async getGasLimits() {
|
|
75
|
+
if (this._gasLimits) {
|
|
76
|
+
return this._gasLimits;
|
|
77
|
+
}
|
|
78
|
+
const gasLimits = await this.executeMulticall({
|
|
79
|
+
dataStore: {
|
|
80
|
+
contractAddress: getContract(this.chainId, "DataStore"),
|
|
81
|
+
abi: DataStore.abi,
|
|
82
|
+
calls: {
|
|
83
|
+
depositSingleToken: {
|
|
84
|
+
methodName: "getUint",
|
|
85
|
+
params: [depositGasLimitKey(true)],
|
|
86
|
+
},
|
|
87
|
+
depositMultiToken: {
|
|
88
|
+
methodName: "getUint",
|
|
89
|
+
params: [depositGasLimitKey(false)],
|
|
90
|
+
},
|
|
91
|
+
withdrawalMultiToken: {
|
|
92
|
+
methodName: "getUint",
|
|
93
|
+
params: [withdrawalGasLimitKey()],
|
|
94
|
+
},
|
|
95
|
+
shift: {
|
|
96
|
+
methodName: "getUint",
|
|
97
|
+
params: [shiftGasLimitKey()],
|
|
98
|
+
},
|
|
99
|
+
singleSwap: {
|
|
100
|
+
methodName: "getUint",
|
|
101
|
+
params: [singleSwapGasLimitKey()],
|
|
102
|
+
},
|
|
103
|
+
swapOrder: {
|
|
104
|
+
methodName: "getUint",
|
|
105
|
+
params: [swapOrderGasLimitKey()],
|
|
106
|
+
},
|
|
107
|
+
increaseOrder: {
|
|
108
|
+
methodName: "getUint",
|
|
109
|
+
params: [increaseOrderGasLimitKey()],
|
|
110
|
+
},
|
|
111
|
+
decreaseOrder: {
|
|
112
|
+
methodName: "getUint",
|
|
113
|
+
params: [decreaseOrderGasLimitKey()],
|
|
114
|
+
},
|
|
115
|
+
estimatedGasFeeBaseAmount: {
|
|
116
|
+
methodName: "getUint",
|
|
117
|
+
params: [ESTIMATED_GAS_FEE_BASE_AMOUNT_V2_1],
|
|
118
|
+
},
|
|
119
|
+
estimatedGasFeePerOraclePrice: {
|
|
120
|
+
methodName: "getUint",
|
|
121
|
+
params: [ESTIMATED_GAS_FEE_PER_ORACLE_PRICE],
|
|
122
|
+
},
|
|
123
|
+
estimatedFeeMultiplierFactor: {
|
|
124
|
+
methodName: "getUint",
|
|
125
|
+
params: [ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR],
|
|
126
|
+
},
|
|
127
|
+
glvDepositGasLimit: {
|
|
128
|
+
methodName: "getUint",
|
|
129
|
+
params: [GLV_DEPOSIT_GAS_LIMIT],
|
|
130
|
+
},
|
|
131
|
+
glvWithdrawalGasLimit: {
|
|
132
|
+
methodName: "getUint",
|
|
133
|
+
params: [GLV_WITHDRAWAL_GAS_LIMIT],
|
|
134
|
+
},
|
|
135
|
+
glvPerMarketGasLimit: {
|
|
136
|
+
methodName: "getUint",
|
|
137
|
+
params: [GLV_PER_MARKET_GAS_LIMIT],
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
}).then((res) => {
|
|
142
|
+
const results = res.data.dataStore;
|
|
143
|
+
function getBigInt(key) {
|
|
144
|
+
return BigInt(results[key].returnValues[0]);
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
depositSingleToken: getBigInt("depositSingleToken"),
|
|
148
|
+
depositMultiToken: getBigInt("depositMultiToken"),
|
|
149
|
+
withdrawalMultiToken: getBigInt("withdrawalMultiToken"),
|
|
150
|
+
shift: getBigInt("shift"),
|
|
151
|
+
singleSwap: getBigInt("singleSwap"),
|
|
152
|
+
swapOrder: getBigInt("swapOrder"),
|
|
153
|
+
increaseOrder: getBigInt("increaseOrder"),
|
|
154
|
+
decreaseOrder: getBigInt("decreaseOrder"),
|
|
155
|
+
estimatedGasFeeBaseAmount: getBigInt("estimatedGasFeeBaseAmount"),
|
|
156
|
+
estimatedGasFeePerOraclePrice: getBigInt("estimatedGasFeePerOraclePrice"),
|
|
157
|
+
estimatedFeeMultiplierFactor: getBigInt("estimatedFeeMultiplierFactor"),
|
|
158
|
+
glvDepositGasLimit: getBigInt("glvDepositGasLimit"),
|
|
159
|
+
glvWithdrawalGasLimit: getBigInt("glvWithdrawalGasLimit"),
|
|
160
|
+
glvPerMarketGasLimit: getBigInt("glvPerMarketGasLimit"),
|
|
161
|
+
};
|
|
162
|
+
});
|
|
163
|
+
this._gasLimits = gasLimits;
|
|
164
|
+
return gasLimits;
|
|
165
|
+
}
|
|
166
|
+
async getEstimatedGasFee(tradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
167
|
+
const gasLimits = await this.getGasLimits();
|
|
168
|
+
switch (tradeFeesType) {
|
|
169
|
+
case "swap": {
|
|
170
|
+
if (!swapAmounts || !swapAmounts.swapPathStats)
|
|
171
|
+
return null;
|
|
172
|
+
return estimateExecuteSwapOrderGasLimit(gasLimits, {
|
|
173
|
+
swapsCount: swapAmounts.swapPathStats.swapPath.length,
|
|
174
|
+
callbackGasLimit: 0n,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
case "increase": {
|
|
178
|
+
if (!increaseAmounts)
|
|
179
|
+
return null;
|
|
180
|
+
return estimateExecuteIncreaseOrderGasLimit(gasLimits, {
|
|
181
|
+
swapsCount: increaseAmounts.swapPathStats?.swapPath.length,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
case "decrease": {
|
|
185
|
+
if (!decreaseAmounts)
|
|
186
|
+
return null;
|
|
187
|
+
return estimateExecuteDecreaseOrderGasLimit(gasLimits, {
|
|
188
|
+
callbackGasLimit: 0n,
|
|
189
|
+
decreaseSwapType: decreaseAmounts.decreaseSwapType,
|
|
190
|
+
swapsCount: 0,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
case "edit":
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async getExecutionFee(tradeFeesType, { increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
198
|
+
const gasLimits = await this.getGasLimits();
|
|
199
|
+
const { tokensData } = await this.tokens.getTokensData();
|
|
200
|
+
const gasPrice = await this.getGasPrice();
|
|
201
|
+
const estimatedGas = await this.getEstimatedGasFee(tradeFeesType, {
|
|
202
|
+
increaseAmounts,
|
|
203
|
+
decreaseAmounts,
|
|
204
|
+
swapAmounts,
|
|
205
|
+
});
|
|
206
|
+
if (estimatedGas === null || estimatedGas === undefined)
|
|
207
|
+
return undefined;
|
|
208
|
+
const swapsCount = getSwapCount({
|
|
209
|
+
isSwap: tradeFeesType === "swap",
|
|
210
|
+
isIncrease: tradeFeesType === "increase",
|
|
211
|
+
increaseAmounts,
|
|
212
|
+
decreaseAmounts,
|
|
213
|
+
swapAmounts,
|
|
214
|
+
});
|
|
215
|
+
if (swapsCount === undefined)
|
|
216
|
+
return undefined;
|
|
217
|
+
if (tokensData === undefined)
|
|
218
|
+
return undefined;
|
|
219
|
+
if (gasPrice === undefined)
|
|
220
|
+
return undefined;
|
|
221
|
+
const oraclePriceCount = estimateOrderOraclePriceCount(swapsCount);
|
|
222
|
+
return getExecutionFee(this, gasLimits, tokensData, estimatedGas, gasPrice, oraclePriceCount);
|
|
223
|
+
}
|
|
224
|
+
async getGasPrice() {
|
|
225
|
+
const executionFeeConfig = EXECUTION_FEE_CONFIG_V2[this.chainId];
|
|
226
|
+
const feeData = await withRetry(() => this.publicClient.estimateFeesPerGas({
|
|
227
|
+
chain: getChain(this.chainId),
|
|
228
|
+
type: "legacy",
|
|
229
|
+
}), {
|
|
230
|
+
retryCount: 2,
|
|
231
|
+
shouldRetry: ({ error }) => {
|
|
232
|
+
const isInvalidBlockError = error?.message?.includes("invalid value for value.hash");
|
|
233
|
+
return isInvalidBlockError;
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
let gasPrice = feeData.gasPrice ?? 0n;
|
|
237
|
+
if (executionFeeConfig.shouldUseMaxPriorityFeePerGas) {
|
|
238
|
+
const maxPriorityFeePerGas = bigMath.max(feeData?.maxPriorityFeePerGas ?? 0n, MAX_PRIORITY_FEE_PER_GAS_MAP[this.chainId] ?? 0n);
|
|
239
|
+
gasPrice = gasPrice + maxPriorityFeePerGas;
|
|
240
|
+
}
|
|
241
|
+
const premium = GAS_PRICE_PREMIUM_MAP[this.chainId] ?? 0n;
|
|
242
|
+
const price = gasPrice + premium;
|
|
243
|
+
return price === undefined ? undefined : BigInt(gasPrice);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { getContract } from "configs/contracts";
|
|
2
|
+
import { Module } from "../base";
|
|
3
|
+
import GovToken from "abis/GovToken.json";
|
|
4
|
+
export class Accounts extends Module {
|
|
5
|
+
get govTokenAddress() {
|
|
6
|
+
let govTokenAddress;
|
|
7
|
+
try {
|
|
8
|
+
govTokenAddress = getContract(this.chainId, "GovToken");
|
|
9
|
+
}
|
|
10
|
+
catch (e) {
|
|
11
|
+
govTokenAddress = null;
|
|
12
|
+
}
|
|
13
|
+
return govTokenAddress;
|
|
14
|
+
}
|
|
15
|
+
getGovTokenDelegates(account) {
|
|
16
|
+
if (!this.govTokenAddress) {
|
|
17
|
+
return Promise.resolve([]);
|
|
18
|
+
}
|
|
19
|
+
return this.sdk
|
|
20
|
+
.executeMulticall({
|
|
21
|
+
govToken: {
|
|
22
|
+
contractAddress: this.govTokenAddress,
|
|
23
|
+
abi: GovToken.abi,
|
|
24
|
+
calls: {
|
|
25
|
+
delegates: {
|
|
26
|
+
methodName: "delegates",
|
|
27
|
+
params: [account ?? this.account],
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
.then((res) => {
|
|
33
|
+
return res.data.govToken.delegates.returnValues[0];
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ClaimableFundingData, MarketsData } from "types/markets";
|
|
2
|
+
import { TokensData } from "types/tokens";
|
|
3
|
+
import { Module } from "../base";
|
|
4
|
+
import { MarketConfig, MarketsInfoResult, MarketsResult } from "./types";
|
|
5
|
+
export declare class Markets extends Module {
|
|
6
|
+
private _marketsData;
|
|
7
|
+
getMarkets(offset?: bigint, limit?: bigint): Promise<MarketsResult>;
|
|
8
|
+
getClaimableFundingData(): Promise<ClaimableFundingData>;
|
|
9
|
+
getMarketsValues({ marketsAddresses, marketsData, tokensData, }: {
|
|
10
|
+
account: string | undefined;
|
|
11
|
+
marketsAddresses: string[] | undefined;
|
|
12
|
+
marketsData: MarketsData | undefined;
|
|
13
|
+
tokensData: TokensData | undefined;
|
|
14
|
+
}): Promise<MarketsResult>;
|
|
15
|
+
/**
|
|
16
|
+
* Fetch market configs from the blockchain
|
|
17
|
+
*/
|
|
18
|
+
getMarketsConfigs({ marketsAddresses, marketsData, }: {
|
|
19
|
+
marketsAddresses: string[] | undefined;
|
|
20
|
+
marketsData: MarketsData | undefined;
|
|
21
|
+
}): Promise<{
|
|
22
|
+
[marketAddress: string]: MarketConfig;
|
|
23
|
+
}>;
|
|
24
|
+
getMarketsInfo(): Promise<MarketsInfoResult>;
|
|
25
|
+
getDailyVolumes(): Promise<{} | undefined>;
|
|
26
|
+
}
|