@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,270 @@
|
|
|
1
|
+
import { getWrappedToken } from "configs/tokens";
|
|
2
|
+
import { OrderType } from "types/orders";
|
|
3
|
+
import { getByKey } from "utils/objects";
|
|
4
|
+
import { getOrderInfo, isOrderForPositionByData, isVisibleOrder } from "utils/orders";
|
|
5
|
+
import { createDecreaseOrderTxn } from "./transactions/createDecreaseOrderTxn";
|
|
6
|
+
import { createIncreaseOrderTxn } from "./transactions/createIncreaseOrderTxn";
|
|
7
|
+
import { buildGetOrdersMulticall, getExecutionFeeAmountForEntry, matchByMarket, parseGetOrdersResponse } from "./utils";
|
|
8
|
+
import { Module } from "../base";
|
|
9
|
+
import { createSwapOrderTxn } from "./transactions/createSwapOrderTxn";
|
|
10
|
+
import { createWrapOrUnwrapTxn } from "./transactions/createWrapOrUnwrapTxn";
|
|
11
|
+
import { cancelOrdersTxn } from "./transactions/cancelOrdersTxn";
|
|
12
|
+
export class Orders extends Module {
|
|
13
|
+
async getOrders({ marketsInfoData, tokensData, orderTypesFilter = [], marketsDirectionsFilter = [], }) {
|
|
14
|
+
if (!this.account) {
|
|
15
|
+
return {
|
|
16
|
+
count: 0,
|
|
17
|
+
ordersInfoData: {},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const nonSwapRelevantDefinedFiltersLowercased = marketsDirectionsFilter
|
|
21
|
+
.filter((filter) => filter.direction !== "swap" && filter.marketAddress !== "any")
|
|
22
|
+
.map((filter) => ({
|
|
23
|
+
marketAddress: filter.marketAddress.toLowerCase(),
|
|
24
|
+
direction: filter.direction,
|
|
25
|
+
collateralAddress: filter.collateralAddress?.toLowerCase(),
|
|
26
|
+
}));
|
|
27
|
+
const hasNonSwapRelevantDefinedMarkets = nonSwapRelevantDefinedFiltersLowercased.length > 0;
|
|
28
|
+
const pureDirectionFilters = marketsDirectionsFilter
|
|
29
|
+
.filter((filter) => filter.direction !== "any" && filter.marketAddress === "any")
|
|
30
|
+
.map((filter) => filter.direction);
|
|
31
|
+
const hasPureDirectionFilters = pureDirectionFilters.length > 0;
|
|
32
|
+
const swapRelevantDefinedMarketsLowercased = marketsDirectionsFilter
|
|
33
|
+
.filter((filter) => (filter.direction === "any" || filter.direction === "swap") && filter.marketAddress !== "any")
|
|
34
|
+
.map((filter) => filter.marketAddress.toLowerCase());
|
|
35
|
+
const hasSwapRelevantDefinedMarkets = swapRelevantDefinedMarketsLowercased.length > 0;
|
|
36
|
+
const orders = await this.sdk
|
|
37
|
+
.executeMulticall(buildGetOrdersMulticall(this.chainId, this.account))
|
|
38
|
+
.then(parseGetOrdersResponse);
|
|
39
|
+
const filteredOrders = orders.orders.filter((order) => {
|
|
40
|
+
if (!isVisibleOrder(order.orderType)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
const matchByMarketResult = matchByMarket({
|
|
44
|
+
order,
|
|
45
|
+
nonSwapRelevantDefinedFiltersLowercased,
|
|
46
|
+
hasNonSwapRelevantDefinedMarkets,
|
|
47
|
+
pureDirectionFilters,
|
|
48
|
+
hasPureDirectionFilters,
|
|
49
|
+
swapRelevantDefinedMarketsLowercased,
|
|
50
|
+
hasSwapRelevantDefinedMarkets,
|
|
51
|
+
chainId: this.chainId,
|
|
52
|
+
marketsInfoData,
|
|
53
|
+
});
|
|
54
|
+
let matchByOrderType = true;
|
|
55
|
+
if (orderTypesFilter.length > 0) {
|
|
56
|
+
matchByOrderType = orderTypesFilter.includes(order.orderType);
|
|
57
|
+
}
|
|
58
|
+
return matchByMarketResult && matchByOrderType;
|
|
59
|
+
});
|
|
60
|
+
const ordersData = filteredOrders?.reduce((acc, order) => {
|
|
61
|
+
acc[order.key] = order;
|
|
62
|
+
return acc;
|
|
63
|
+
}, {});
|
|
64
|
+
const wrappedToken = getWrappedToken(this.chainId);
|
|
65
|
+
const ordersInfoData = Object.keys(ordersData).reduce((acc, orderKey) => {
|
|
66
|
+
const order = getByKey(ordersData, orderKey);
|
|
67
|
+
const orderInfo = getOrderInfo({
|
|
68
|
+
marketsInfoData,
|
|
69
|
+
tokensData,
|
|
70
|
+
wrappedNativeToken: wrappedToken,
|
|
71
|
+
order,
|
|
72
|
+
});
|
|
73
|
+
if (!orderInfo) {
|
|
74
|
+
// eslint-disable-next-line no-console
|
|
75
|
+
console.warn(`OrderInfo parsing error`, order);
|
|
76
|
+
return acc;
|
|
77
|
+
}
|
|
78
|
+
acc[orderKey] = orderInfo;
|
|
79
|
+
return acc;
|
|
80
|
+
}, {});
|
|
81
|
+
return {
|
|
82
|
+
count: orders.count,
|
|
83
|
+
ordersInfoData,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async createIncreaseOrder({ isLimit, marketAddress, allowedSlippage, collateralTokenAddress, receiveTokenAddress, fromToken, triggerPrice, referralCodeForTxn, increaseAmounts, collateralToken, createSltpEntries, cancelSltpEntries, updateSltpEntries, marketInfo, isLong, indexToken, marketsInfoData, tokensData, }) {
|
|
87
|
+
const account = this.account;
|
|
88
|
+
if (!account) {
|
|
89
|
+
throw new Error("Account is not defined");
|
|
90
|
+
}
|
|
91
|
+
const gasLimits = await this.sdk.getGasLimits();
|
|
92
|
+
const gasPrice = await this.sdk.getGasPrice();
|
|
93
|
+
const executionFee = await this.sdk.getExecutionFee("increase", {
|
|
94
|
+
increaseAmounts,
|
|
95
|
+
});
|
|
96
|
+
if (!executionFee) {
|
|
97
|
+
throw new Error("Execution fee is not available");
|
|
98
|
+
}
|
|
99
|
+
const { ordersInfoData } = await this.sdk.orders.getOrders({
|
|
100
|
+
marketsInfoData,
|
|
101
|
+
tokensData,
|
|
102
|
+
});
|
|
103
|
+
const orders = Object.values(ordersInfoData || {});
|
|
104
|
+
const positionOrders = orders.filter((order) => isOrderForPositionByData(order, {
|
|
105
|
+
isLong,
|
|
106
|
+
marketAddress,
|
|
107
|
+
account,
|
|
108
|
+
collateralAddress: collateralToken.address,
|
|
109
|
+
}));
|
|
110
|
+
const { autoCancelOrdersLimit } = await this.sdk.positions.getMaxAutoCancelOrders({
|
|
111
|
+
positionOrders,
|
|
112
|
+
});
|
|
113
|
+
const commonSecondaryOrderParams = {
|
|
114
|
+
account,
|
|
115
|
+
marketAddress,
|
|
116
|
+
swapPath: [],
|
|
117
|
+
allowedSlippage,
|
|
118
|
+
initialCollateralAddress: collateralTokenAddress,
|
|
119
|
+
receiveTokenAddress,
|
|
120
|
+
isLong,
|
|
121
|
+
indexToken,
|
|
122
|
+
};
|
|
123
|
+
return createIncreaseOrderTxn({
|
|
124
|
+
sdk: this.sdk,
|
|
125
|
+
createIncreaseOrderParams: {
|
|
126
|
+
account,
|
|
127
|
+
marketAddress: marketInfo.marketTokenAddress,
|
|
128
|
+
initialCollateralAddress: fromToken?.address,
|
|
129
|
+
initialCollateralAmount: increaseAmounts.initialCollateralAmount,
|
|
130
|
+
targetCollateralAddress: collateralToken.address,
|
|
131
|
+
collateralDeltaAmount: increaseAmounts.collateralDeltaAmount,
|
|
132
|
+
swapPath: increaseAmounts.swapPathStats?.swapPath || [],
|
|
133
|
+
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
|
|
134
|
+
sizeDeltaInTokens: increaseAmounts.sizeDeltaInTokens,
|
|
135
|
+
triggerPrice: isLimit ? triggerPrice : undefined,
|
|
136
|
+
acceptablePrice: increaseAmounts.acceptablePrice,
|
|
137
|
+
isLong,
|
|
138
|
+
orderType: isLimit ? OrderType.LimitIncrease : OrderType.MarketIncrease,
|
|
139
|
+
executionFee: executionFee.feeTokenAmount,
|
|
140
|
+
allowedSlippage,
|
|
141
|
+
referralCode: referralCodeForTxn,
|
|
142
|
+
indexToken: marketInfo.indexToken,
|
|
143
|
+
tokensData,
|
|
144
|
+
skipSimulation: isLimit,
|
|
145
|
+
},
|
|
146
|
+
createDecreaseOrderParams: createSltpEntries?.map((entry, i) => {
|
|
147
|
+
return {
|
|
148
|
+
...commonSecondaryOrderParams,
|
|
149
|
+
initialCollateralDeltaAmount: entry.decreaseAmounts.collateralDeltaAmount ?? 0n,
|
|
150
|
+
sizeDeltaUsd: entry.decreaseAmounts.sizeDeltaUsd,
|
|
151
|
+
sizeDeltaInTokens: entry.decreaseAmounts.sizeDeltaInTokens,
|
|
152
|
+
acceptablePrice: entry.decreaseAmounts.acceptablePrice,
|
|
153
|
+
triggerPrice: entry.decreaseAmounts.triggerPrice,
|
|
154
|
+
minOutputUsd: 0n,
|
|
155
|
+
decreasePositionSwapType: entry.decreaseAmounts.decreaseSwapType,
|
|
156
|
+
orderType: entry.decreaseAmounts.triggerOrderType,
|
|
157
|
+
referralCode: referralCodeForTxn,
|
|
158
|
+
executionFee: getExecutionFeeAmountForEntry(this.sdk, entry, gasLimits, tokensData, gasPrice) ?? 0n,
|
|
159
|
+
tokensData,
|
|
160
|
+
txnType: entry.txnType,
|
|
161
|
+
skipSimulation: isLimit,
|
|
162
|
+
autoCancel: i < autoCancelOrdersLimit,
|
|
163
|
+
};
|
|
164
|
+
}),
|
|
165
|
+
cancelOrderParams: cancelSltpEntries?.map((entry) => ({
|
|
166
|
+
...commonSecondaryOrderParams,
|
|
167
|
+
orderKey: entry.order.key,
|
|
168
|
+
orderType: entry.order.orderType,
|
|
169
|
+
minOutputAmount: 0n,
|
|
170
|
+
sizeDeltaUsd: entry.order.sizeDeltaUsd,
|
|
171
|
+
txnType: entry.txnType,
|
|
172
|
+
initialCollateralDeltaAmount: entry.order?.initialCollateralDeltaAmount ?? 0n,
|
|
173
|
+
})),
|
|
174
|
+
updateOrderParams: updateSltpEntries?.map((entry) => ({
|
|
175
|
+
...commonSecondaryOrderParams,
|
|
176
|
+
orderKey: entry.order.key,
|
|
177
|
+
orderType: entry.order.orderType,
|
|
178
|
+
sizeDeltaUsd: (entry.increaseAmounts?.sizeDeltaUsd || entry.decreaseAmounts?.sizeDeltaUsd),
|
|
179
|
+
acceptablePrice: (entry.increaseAmounts?.acceptablePrice || entry.decreaseAmounts?.acceptablePrice),
|
|
180
|
+
triggerPrice: (entry.increaseAmounts?.triggerPrice || entry.decreaseAmounts?.triggerPrice),
|
|
181
|
+
executionFee: getExecutionFeeAmountForEntry(this.sdk, entry, gasLimits, tokensData, gasPrice) ?? 0n,
|
|
182
|
+
minOutputAmount: 0n,
|
|
183
|
+
txnType: entry.txnType,
|
|
184
|
+
initialCollateralDeltaAmount: entry.order?.initialCollateralDeltaAmount ?? 0n,
|
|
185
|
+
autoCancel: entry.order.autoCancel,
|
|
186
|
+
})),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async createDecreaseOrder({ marketsInfoData, tokensData, marketInfo, decreaseAmounts, collateralToken, allowedSlippage, isLong, referralCode, }) {
|
|
190
|
+
const account = this.account;
|
|
191
|
+
if (!account) {
|
|
192
|
+
throw new Error("Account is not defined");
|
|
193
|
+
}
|
|
194
|
+
const executionFee = await this.sdk.getExecutionFee("decrease", {
|
|
195
|
+
decreaseAmounts,
|
|
196
|
+
});
|
|
197
|
+
if (!executionFee) {
|
|
198
|
+
throw new Error("Execution fee is not available");
|
|
199
|
+
}
|
|
200
|
+
if (decreaseAmounts?.triggerOrderType === undefined) {
|
|
201
|
+
throw new Error("Trigger order type is not defined");
|
|
202
|
+
}
|
|
203
|
+
const { ordersInfoData } = await this.sdk.orders.getOrders({
|
|
204
|
+
marketsInfoData,
|
|
205
|
+
tokensData,
|
|
206
|
+
});
|
|
207
|
+
const orders = Object.values(ordersInfoData || {});
|
|
208
|
+
const positionOrders = orders.filter((order) => isOrderForPositionByData(order, {
|
|
209
|
+
isLong,
|
|
210
|
+
marketAddress: marketInfo.marketTokenAddress,
|
|
211
|
+
account,
|
|
212
|
+
collateralAddress: collateralToken.address,
|
|
213
|
+
}));
|
|
214
|
+
const { autoCancelOrdersLimit } = await this.sdk.positions.getMaxAutoCancelOrders({
|
|
215
|
+
positionOrders,
|
|
216
|
+
});
|
|
217
|
+
return createDecreaseOrderTxn(this.sdk, {
|
|
218
|
+
account,
|
|
219
|
+
marketAddress: marketInfo.marketTokenAddress,
|
|
220
|
+
swapPath: [],
|
|
221
|
+
initialCollateralDeltaAmount: decreaseAmounts.collateralDeltaAmount,
|
|
222
|
+
initialCollateralAddress: collateralToken.address,
|
|
223
|
+
receiveTokenAddress: collateralToken.address,
|
|
224
|
+
triggerPrice: decreaseAmounts.triggerPrice,
|
|
225
|
+
acceptablePrice: decreaseAmounts.acceptablePrice,
|
|
226
|
+
sizeDeltaUsd: decreaseAmounts.sizeDeltaUsd,
|
|
227
|
+
sizeDeltaInTokens: decreaseAmounts.sizeDeltaInTokens,
|
|
228
|
+
minOutputUsd: BigInt(0),
|
|
229
|
+
isLong,
|
|
230
|
+
decreasePositionSwapType: decreaseAmounts.decreaseSwapType,
|
|
231
|
+
orderType: decreaseAmounts?.triggerOrderType,
|
|
232
|
+
executionFee: executionFee.feeTokenAmount,
|
|
233
|
+
allowedSlippage,
|
|
234
|
+
referralCode,
|
|
235
|
+
skipSimulation: true,
|
|
236
|
+
indexToken: marketInfo.indexToken,
|
|
237
|
+
tokensData,
|
|
238
|
+
autoCancel: autoCancelOrdersLimit > 0,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
async createSwapOrder({ isLimit, swapAmounts, allowedSlippage, fromToken, toToken, referralCodeForTxn, tokensData, }) {
|
|
242
|
+
const orderType = isLimit ? OrderType.LimitSwap : OrderType.MarketSwap;
|
|
243
|
+
const executionFee = await this.sdk.getExecutionFee("swap", {
|
|
244
|
+
swapAmounts,
|
|
245
|
+
});
|
|
246
|
+
if (!swapAmounts?.swapPathStats || !executionFee) {
|
|
247
|
+
throw new Error("Swap data is not defined");
|
|
248
|
+
}
|
|
249
|
+
return createSwapOrderTxn(this.sdk, {
|
|
250
|
+
fromTokenAddress: fromToken.address,
|
|
251
|
+
fromTokenAmount: swapAmounts.amountIn,
|
|
252
|
+
swapPath: swapAmounts.swapPathStats?.swapPath,
|
|
253
|
+
toTokenAddress: toToken.address,
|
|
254
|
+
orderType,
|
|
255
|
+
minOutputAmount: swapAmounts.minOutputAmount,
|
|
256
|
+
referralCode: referralCodeForTxn,
|
|
257
|
+
executionFee: executionFee.feeTokenAmount,
|
|
258
|
+
allowedSlippage,
|
|
259
|
+
tokensData,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
async cancelOrders(orderKeys) {
|
|
263
|
+
return cancelOrdersTxn(this.sdk, {
|
|
264
|
+
orderKeys: orderKeys,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
async createWrapOrUnwrapOrder(p) {
|
|
268
|
+
return createWrapOrUnwrapTxn(this.sdk, p);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { arbitrumSdk } from "utils/test";
|
|
2
|
+
describe("Positions", () => {
|
|
3
|
+
describe("read", () => {
|
|
4
|
+
it("should be able to get orders", async () => {
|
|
5
|
+
const { marketsInfoData, tokensData } = (await arbitrumSdk.markets.getMarketsInfo()) ?? {};
|
|
6
|
+
if (!tokensData || !marketsInfoData) {
|
|
7
|
+
throw new Error("Tokens data or markets info is not available");
|
|
8
|
+
}
|
|
9
|
+
const orders = await arbitrumSdk.orders.getOrders({
|
|
10
|
+
marketsInfoData,
|
|
11
|
+
tokensData,
|
|
12
|
+
});
|
|
13
|
+
expect(orders).toBeDefined();
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { GmxSdk } from "../../../index";
|
|
2
|
+
export type CancelOrderParams = {
|
|
3
|
+
orderKeys: string[];
|
|
4
|
+
};
|
|
5
|
+
export declare function cancelOrdersTxn(sdk: GmxSdk, p: CancelOrderParams): Promise<`0x${string}`>;
|
|
6
|
+
export declare function createCancelEncodedPayload(orderKeys?: (string | null)[]): `0x${string}`[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ExchangeRouter from "abis/ExchangeRouter.json";
|
|
2
|
+
import { getContract } from "configs/contracts";
|
|
3
|
+
import { encodeFunctionData } from "viem";
|
|
4
|
+
export async function cancelOrdersTxn(sdk, p) {
|
|
5
|
+
const multicall = createCancelEncodedPayload(p.orderKeys);
|
|
6
|
+
const exchangeRouter = getContract(sdk.chainId, "ExchangeRouter");
|
|
7
|
+
return sdk.callContract(exchangeRouter, ExchangeRouter.abi, "multicall", [multicall]);
|
|
8
|
+
}
|
|
9
|
+
export function createCancelEncodedPayload(orderKeys = []) {
|
|
10
|
+
return orderKeys.filter(Boolean).map((orderKey) => encodeFunctionData({
|
|
11
|
+
abi: ExchangeRouter.abi,
|
|
12
|
+
functionName: "cancelOrder",
|
|
13
|
+
args: [orderKey],
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { GmxSdk } from "../../../index";
|
|
2
|
+
import type { DecreasePositionSwapType, OrderType } from "types/orders";
|
|
3
|
+
import type { Token, TokensData } from "types/tokens";
|
|
4
|
+
export type DecreaseOrderParams = {
|
|
5
|
+
account: string;
|
|
6
|
+
marketAddress: string;
|
|
7
|
+
initialCollateralAddress: string;
|
|
8
|
+
initialCollateralDeltaAmount: bigint;
|
|
9
|
+
swapPath: string[];
|
|
10
|
+
receiveTokenAddress: string;
|
|
11
|
+
sizeDeltaUsd: bigint;
|
|
12
|
+
sizeDeltaInTokens: bigint;
|
|
13
|
+
acceptablePrice: bigint;
|
|
14
|
+
triggerPrice: bigint | undefined;
|
|
15
|
+
minOutputUsd: bigint;
|
|
16
|
+
isLong: boolean;
|
|
17
|
+
decreasePositionSwapType: DecreasePositionSwapType;
|
|
18
|
+
orderType: OrderType.MarketDecrease | OrderType.LimitDecrease | OrderType.StopLossDecrease;
|
|
19
|
+
executionFee: bigint;
|
|
20
|
+
allowedSlippage: number;
|
|
21
|
+
skipSimulation?: boolean;
|
|
22
|
+
referralCode?: string;
|
|
23
|
+
indexToken: Token;
|
|
24
|
+
tokensData: TokensData;
|
|
25
|
+
autoCancel: boolean;
|
|
26
|
+
};
|
|
27
|
+
export declare function createDecreaseOrderTxn(sdk: GmxSdk, params: DecreaseOrderParams | DecreaseOrderParams[]): Promise<void>;
|
|
28
|
+
export declare function createDecreaseEncodedPayload({ sdk, orderVaultAddress, ps, }: {
|
|
29
|
+
sdk: GmxSdk;
|
|
30
|
+
orderVaultAddress: string;
|
|
31
|
+
ps: DecreaseOrderParams[];
|
|
32
|
+
}): `0x${string}`[];
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { getContract } from "configs/contracts";
|
|
2
|
+
import ExchangeRouter from "abis/ExchangeRouter.json";
|
|
3
|
+
import { convertTokenAddress, NATIVE_TOKEN_ADDRESS } from "configs/tokens";
|
|
4
|
+
import { isMarketOrderType } from "utils/orders";
|
|
5
|
+
import { convertToContractPrice } from "utils/tokens";
|
|
6
|
+
import { applySlippageToMinOut, applySlippageToPrice } from "utils/trade";
|
|
7
|
+
import { encodeFunctionData, zeroAddress, zeroHash } from "viem";
|
|
8
|
+
import { simulateExecuteOrder } from "utils/simulateExecuteOrder";
|
|
9
|
+
export async function createDecreaseOrderTxn(sdk, params) {
|
|
10
|
+
const chainId = sdk.chainId;
|
|
11
|
+
const ps = Array.isArray(params) ? params : [params];
|
|
12
|
+
const orderVaultAddress = getContract(chainId, "OrderVault");
|
|
13
|
+
const totalWntAmount = ps.reduce((acc, p) => acc + p.executionFee, 0n);
|
|
14
|
+
const encodedPayload = createDecreaseEncodedPayload({
|
|
15
|
+
sdk,
|
|
16
|
+
orderVaultAddress,
|
|
17
|
+
ps,
|
|
18
|
+
});
|
|
19
|
+
const simulationEncodedPayload = createDecreaseEncodedPayload({
|
|
20
|
+
sdk,
|
|
21
|
+
orderVaultAddress,
|
|
22
|
+
ps,
|
|
23
|
+
});
|
|
24
|
+
await Promise.all(ps.map(async (p) => {
|
|
25
|
+
if (!p.skipSimulation) {
|
|
26
|
+
const primaryPriceOverrides = {};
|
|
27
|
+
if (p.triggerPrice != undefined) {
|
|
28
|
+
primaryPriceOverrides[p.indexToken.address] = {
|
|
29
|
+
minPrice: p.triggerPrice,
|
|
30
|
+
maxPrice: p.triggerPrice,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
await simulateExecuteOrder(sdk, {
|
|
34
|
+
primaryPriceOverrides,
|
|
35
|
+
createMulticallPayload: simulationEncodedPayload,
|
|
36
|
+
value: totalWntAmount,
|
|
37
|
+
tokensData: p.tokensData,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}));
|
|
41
|
+
const routerAddress = getContract(chainId, "ExchangeRouter");
|
|
42
|
+
await sdk.callContract(routerAddress, ExchangeRouter.abi, "multicall", [encodedPayload], {
|
|
43
|
+
value: totalWntAmount,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export function createDecreaseEncodedPayload({ sdk, orderVaultAddress, ps, }) {
|
|
47
|
+
const multicall = [
|
|
48
|
+
...ps.flatMap((p) => {
|
|
49
|
+
const isNativeReceive = p.receiveTokenAddress === NATIVE_TOKEN_ADDRESS;
|
|
50
|
+
const initialCollateralTokenAddress = convertTokenAddress(sdk.chainId, p.initialCollateralAddress, "wrapped");
|
|
51
|
+
const shouldApplySlippage = isMarketOrderType(p.orderType);
|
|
52
|
+
const acceptablePrice = shouldApplySlippage
|
|
53
|
+
? applySlippageToPrice(p.allowedSlippage, p.acceptablePrice, false, p.isLong)
|
|
54
|
+
: p.acceptablePrice;
|
|
55
|
+
const minOutputAmount = shouldApplySlippage
|
|
56
|
+
? applySlippageToMinOut(p.allowedSlippage, p.minOutputUsd)
|
|
57
|
+
: p.minOutputUsd;
|
|
58
|
+
const orderParams = {
|
|
59
|
+
addresses: {
|
|
60
|
+
cancellationReceiver: zeroAddress,
|
|
61
|
+
receiver: p.account,
|
|
62
|
+
initialCollateralToken: initialCollateralTokenAddress,
|
|
63
|
+
callbackContract: zeroAddress,
|
|
64
|
+
market: p.marketAddress,
|
|
65
|
+
swapPath: p.swapPath,
|
|
66
|
+
uiFeeReceiver: zeroAddress,
|
|
67
|
+
},
|
|
68
|
+
numbers: {
|
|
69
|
+
sizeDeltaUsd: p.sizeDeltaUsd,
|
|
70
|
+
initialCollateralDeltaAmount: p.initialCollateralDeltaAmount,
|
|
71
|
+
triggerPrice: convertToContractPrice(p.triggerPrice ?? 0n, p.indexToken.decimals),
|
|
72
|
+
acceptablePrice: convertToContractPrice(acceptablePrice, p.indexToken.decimals),
|
|
73
|
+
executionFee: p.executionFee,
|
|
74
|
+
callbackGasLimit: 0n,
|
|
75
|
+
minOutputAmount,
|
|
76
|
+
},
|
|
77
|
+
orderType: p.orderType,
|
|
78
|
+
decreasePositionSwapType: p.decreasePositionSwapType,
|
|
79
|
+
isLong: p.isLong,
|
|
80
|
+
shouldUnwrapNativeToken: isNativeReceive,
|
|
81
|
+
autoCancel: p.autoCancel,
|
|
82
|
+
referralCode: p.referralCode || zeroHash,
|
|
83
|
+
};
|
|
84
|
+
return [
|
|
85
|
+
{ method: "sendWnt", params: [orderVaultAddress, p.executionFee] },
|
|
86
|
+
{
|
|
87
|
+
method: "createOrder",
|
|
88
|
+
params: [orderParams],
|
|
89
|
+
},
|
|
90
|
+
];
|
|
91
|
+
}),
|
|
92
|
+
];
|
|
93
|
+
return multicall.filter(Boolean).map((call) => encodeFunctionData({
|
|
94
|
+
abi: ExchangeRouter.abi,
|
|
95
|
+
functionName: call.method,
|
|
96
|
+
args: call.params,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { GmxSdk } from "index";
|
|
2
|
+
import { OrderTxnType, OrderType } from "types/orders";
|
|
3
|
+
import { TokenData, TokenPrices, TokensData } from "types/tokens";
|
|
4
|
+
import { DecreaseOrderParams } from "./createDecreaseOrderTxn";
|
|
5
|
+
export type PriceOverrides = {
|
|
6
|
+
[address: string]: TokenPrices | undefined;
|
|
7
|
+
};
|
|
8
|
+
type IncreaseOrderParams = {
|
|
9
|
+
account: string;
|
|
10
|
+
marketAddress: string;
|
|
11
|
+
initialCollateralAddress: string;
|
|
12
|
+
targetCollateralAddress: string;
|
|
13
|
+
initialCollateralAmount: bigint;
|
|
14
|
+
collateralDeltaAmount: bigint;
|
|
15
|
+
swapPath: string[];
|
|
16
|
+
sizeDeltaUsd: bigint;
|
|
17
|
+
sizeDeltaInTokens: bigint;
|
|
18
|
+
acceptablePrice: bigint;
|
|
19
|
+
triggerPrice: bigint | undefined;
|
|
20
|
+
isLong: boolean;
|
|
21
|
+
orderType: OrderType.MarketIncrease | OrderType.LimitIncrease;
|
|
22
|
+
executionFee: bigint;
|
|
23
|
+
allowedSlippage: number;
|
|
24
|
+
skipSimulation?: boolean;
|
|
25
|
+
referralCode: string | undefined;
|
|
26
|
+
indexToken: TokenData;
|
|
27
|
+
tokensData: TokensData;
|
|
28
|
+
};
|
|
29
|
+
type SecondaryOrderCommonParams = {
|
|
30
|
+
account: string;
|
|
31
|
+
marketAddress: string;
|
|
32
|
+
swapPath: string[];
|
|
33
|
+
allowedSlippage: number;
|
|
34
|
+
initialCollateralAddress: string;
|
|
35
|
+
receiveTokenAddress: string;
|
|
36
|
+
isLong: boolean;
|
|
37
|
+
indexToken: TokenData;
|
|
38
|
+
txnType: OrderTxnType;
|
|
39
|
+
orderType: OrderType;
|
|
40
|
+
sizeDeltaUsd: bigint;
|
|
41
|
+
initialCollateralDeltaAmount: bigint;
|
|
42
|
+
};
|
|
43
|
+
export type SecondaryDecreaseOrderParams = DecreaseOrderParams & SecondaryOrderCommonParams;
|
|
44
|
+
export type SecondaryCancelOrderParams = SecondaryOrderCommonParams & {
|
|
45
|
+
orderKey: string | null;
|
|
46
|
+
};
|
|
47
|
+
export type SecondaryUpdateOrderParams = SecondaryOrderCommonParams & {
|
|
48
|
+
orderKey: string;
|
|
49
|
+
sizeDeltaUsd: bigint;
|
|
50
|
+
acceptablePrice: bigint;
|
|
51
|
+
triggerPrice: bigint;
|
|
52
|
+
executionFee: bigint;
|
|
53
|
+
indexToken: TokenData;
|
|
54
|
+
minOutputAmount: bigint;
|
|
55
|
+
};
|
|
56
|
+
export declare function createIncreaseOrderTxn({ sdk, createIncreaseOrderParams: p, createDecreaseOrderParams, cancelOrderParams, updateOrderParams, }: {
|
|
57
|
+
sdk: GmxSdk;
|
|
58
|
+
createIncreaseOrderParams: IncreaseOrderParams;
|
|
59
|
+
createDecreaseOrderParams?: SecondaryDecreaseOrderParams[];
|
|
60
|
+
cancelOrderParams?: SecondaryCancelOrderParams[];
|
|
61
|
+
updateOrderParams?: SecondaryUpdateOrderParams[];
|
|
62
|
+
}): Promise<void>;
|
|
63
|
+
export declare function getPendingOrderFromParams(chainId: number, txnType: OrderTxnType, p: DecreaseOrderParams | SecondaryUpdateOrderParams | SecondaryCancelOrderParams): {
|
|
64
|
+
txnType: OrderTxnType;
|
|
65
|
+
account: string;
|
|
66
|
+
marketAddress: string;
|
|
67
|
+
initialCollateralTokenAddress: string;
|
|
68
|
+
initialCollateralDeltaAmount: bigint;
|
|
69
|
+
swapPath: string[];
|
|
70
|
+
sizeDeltaUsd: bigint;
|
|
71
|
+
minOutputAmount: bigint;
|
|
72
|
+
isLong: boolean;
|
|
73
|
+
orderType: OrderType;
|
|
74
|
+
shouldUnwrapNativeToken: boolean;
|
|
75
|
+
orderKey: string | undefined;
|
|
76
|
+
};
|
|
77
|
+
export {};
|