@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,24 @@
|
|
|
1
|
+
type SlidingWindowFallbackSwitcherOptions = {
|
|
2
|
+
fallbackTimeout: number;
|
|
3
|
+
restoreTimeout: number;
|
|
4
|
+
eventsThreshold: number;
|
|
5
|
+
onFallback?: () => void;
|
|
6
|
+
onRestore?: () => void;
|
|
7
|
+
};
|
|
8
|
+
export declare class SlidingWindowFallbackSwitcher {
|
|
9
|
+
private fallbackTimeout;
|
|
10
|
+
private restoreTimeout;
|
|
11
|
+
private eventsThreshold;
|
|
12
|
+
private onFallback?;
|
|
13
|
+
private onRestore?;
|
|
14
|
+
private eventTimestamps;
|
|
15
|
+
isFallbackMode: boolean;
|
|
16
|
+
private fallbackTimerId;
|
|
17
|
+
private restoreTimerId;
|
|
18
|
+
constructor(options: SlidingWindowFallbackSwitcherOptions);
|
|
19
|
+
trigger(): void;
|
|
20
|
+
private cleanupOldEvents;
|
|
21
|
+
private switchToFallback;
|
|
22
|
+
private switchToMain;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class SlidingWindowFallbackSwitcher {
|
|
2
|
+
constructor(options) {
|
|
3
|
+
this.eventTimestamps = [];
|
|
4
|
+
this.isFallbackMode = false;
|
|
5
|
+
this.fallbackTimerId = null;
|
|
6
|
+
this.restoreTimerId = null;
|
|
7
|
+
this.fallbackTimeout = options.fallbackTimeout;
|
|
8
|
+
this.restoreTimeout = options.restoreTimeout;
|
|
9
|
+
this.eventsThreshold = options.eventsThreshold;
|
|
10
|
+
this.onFallback = options.onFallback;
|
|
11
|
+
this.onRestore = options.onRestore;
|
|
12
|
+
}
|
|
13
|
+
trigger() {
|
|
14
|
+
const now = Date.now();
|
|
15
|
+
this.eventTimestamps.push(now);
|
|
16
|
+
this.cleanupOldEvents();
|
|
17
|
+
if (!this.isFallbackMode && this.eventTimestamps.length >= this.eventsThreshold) {
|
|
18
|
+
this.switchToFallback();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
cleanupOldEvents() {
|
|
22
|
+
const windowStart = Date.now() - this.fallbackTimeout;
|
|
23
|
+
this.eventTimestamps = this.eventTimestamps.filter((timestamp) => timestamp >= windowStart);
|
|
24
|
+
}
|
|
25
|
+
switchToFallback() {
|
|
26
|
+
this.onFallback?.();
|
|
27
|
+
this.isFallbackMode = true;
|
|
28
|
+
if (this.restoreTimerId) {
|
|
29
|
+
clearTimeout(this.restoreTimerId);
|
|
30
|
+
}
|
|
31
|
+
this.fallbackTimerId = setTimeout(() => this.switchToMain(), this.restoreTimeout);
|
|
32
|
+
}
|
|
33
|
+
switchToMain() {
|
|
34
|
+
this.onRestore?.();
|
|
35
|
+
this.isFallbackMode = false;
|
|
36
|
+
if (this.fallbackTimerId) {
|
|
37
|
+
clearTimeout(this.fallbackTimerId);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type GraphQlFilters = {
|
|
2
|
+
or: GraphQlFilters[];
|
|
3
|
+
} | {
|
|
4
|
+
and: GraphQlFilters[];
|
|
5
|
+
} | {
|
|
6
|
+
/**
|
|
7
|
+
* `or` must be a single key-value pair in the object.
|
|
8
|
+
*/
|
|
9
|
+
or?: never;
|
|
10
|
+
/**
|
|
11
|
+
* `and` must be a single key-value pair in the object.
|
|
12
|
+
*/
|
|
13
|
+
and?: never;
|
|
14
|
+
/**
|
|
15
|
+
* Key must not start with an `_`. If you want to use nested filtering add `_` to the parent key itself if possible.
|
|
16
|
+
* Otherwise, if for some reason the field name itself starts with an `_`, change these types.
|
|
17
|
+
*/
|
|
18
|
+
[key: `_${string}`]: never;
|
|
19
|
+
[key: string]: string | number | boolean | undefined | GraphQlFilters | string[] | number[] | GraphQlFilters[] | null;
|
|
20
|
+
};
|
|
21
|
+
export declare function buildFiltersBody(filters: GraphQlFilters): string;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export function buildFiltersBody(filters) {
|
|
2
|
+
const res = {};
|
|
3
|
+
let hadOr = false;
|
|
4
|
+
let hadAnd = false;
|
|
5
|
+
for (const [key, value] of Object.entries(filters)) {
|
|
6
|
+
if (value === undefined) {
|
|
7
|
+
continue;
|
|
8
|
+
}
|
|
9
|
+
if (typeof value === "string") {
|
|
10
|
+
res[key] = `"${value}"`;
|
|
11
|
+
}
|
|
12
|
+
else if (typeof value === "number") {
|
|
13
|
+
res[key] = `${value}`;
|
|
14
|
+
}
|
|
15
|
+
else if (typeof value === "boolean") {
|
|
16
|
+
res[key] = `${value}`;
|
|
17
|
+
}
|
|
18
|
+
else if (Array.isArray(value)) {
|
|
19
|
+
const valueStr = "[" +
|
|
20
|
+
value
|
|
21
|
+
.map((el) => {
|
|
22
|
+
if (typeof el === "string") {
|
|
23
|
+
return `"${el}"`;
|
|
24
|
+
}
|
|
25
|
+
else if (typeof el === "number") {
|
|
26
|
+
return `${el}`;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const elemStr = buildFiltersBody(el);
|
|
30
|
+
if (elemStr === "{}") {
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
return elemStr;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
.filter((el) => el !== "")
|
|
39
|
+
.join(",") +
|
|
40
|
+
"]";
|
|
41
|
+
if (valueStr !== "[]") {
|
|
42
|
+
res[key] = valueStr;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else if (value === null) {
|
|
46
|
+
res[key] = null;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const valueStr = buildFiltersBody(value);
|
|
50
|
+
if (valueStr !== "{}") {
|
|
51
|
+
res[key + "_"] = buildFiltersBody(value);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (hadOr) {
|
|
55
|
+
throw new Error("Or must be a single key-value pair in the object.");
|
|
56
|
+
}
|
|
57
|
+
if (key === "or" && res[key] !== undefined) {
|
|
58
|
+
hadOr = true;
|
|
59
|
+
}
|
|
60
|
+
if (hadAnd) {
|
|
61
|
+
throw new Error("And must be a single key-value pair in the object.");
|
|
62
|
+
}
|
|
63
|
+
if (key === "and" && res[key] !== undefined) {
|
|
64
|
+
hadAnd = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const str = Object.entries(res).reduce((previous, [key, value], index) => {
|
|
68
|
+
const maybeComma = index === 0 ? "" : ",";
|
|
69
|
+
return `${previous}${maybeComma}${key}:${value}`;
|
|
70
|
+
}, "");
|
|
71
|
+
return `{${str}}`;
|
|
72
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MarketInfo, MarketsInfoData } from "types/markets";
|
|
2
|
+
import { SwapPathStats, SwapStats } from "types/trade";
|
|
3
|
+
export declare function getSwapPathOutputAddresses(p: {
|
|
4
|
+
marketsInfoData: MarketsInfoData;
|
|
5
|
+
initialCollateralAddress: string;
|
|
6
|
+
swapPath: string[];
|
|
7
|
+
wrappedNativeTokenAddress: string;
|
|
8
|
+
shouldUnwrapNativeToken: boolean;
|
|
9
|
+
isIncrease: boolean;
|
|
10
|
+
}): {
|
|
11
|
+
outTokenAddress: string;
|
|
12
|
+
outMarketAddress: undefined;
|
|
13
|
+
} | {
|
|
14
|
+
outTokenAddress: undefined;
|
|
15
|
+
outMarketAddress: undefined;
|
|
16
|
+
} | {
|
|
17
|
+
outTokenAddress: string;
|
|
18
|
+
outMarketAddress: string;
|
|
19
|
+
};
|
|
20
|
+
export declare function getSwapPathStats(p: {
|
|
21
|
+
marketsInfoData: MarketsInfoData;
|
|
22
|
+
swapPath: string[];
|
|
23
|
+
initialCollateralAddress: string;
|
|
24
|
+
wrappedNativeTokenAddress: string;
|
|
25
|
+
usdIn: bigint;
|
|
26
|
+
shouldUnwrapNativeToken: boolean;
|
|
27
|
+
shouldApplyPriceImpact: boolean;
|
|
28
|
+
}): SwapPathStats | undefined;
|
|
29
|
+
export declare function getSwapStats(p: {
|
|
30
|
+
marketInfo: MarketInfo;
|
|
31
|
+
tokenInAddress: string;
|
|
32
|
+
tokenOutAddress: string;
|
|
33
|
+
usdIn: bigint;
|
|
34
|
+
shouldApplyPriceImpact: boolean;
|
|
35
|
+
}): SwapStats;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { NATIVE_TOKEN_ADDRESS } from "configs/tokens";
|
|
2
|
+
import { getByKey } from "./objects";
|
|
3
|
+
import { getAvailableUsdLiquidityForCollateral, getOppositeCollateral, getTokenPoolType } from "./markets";
|
|
4
|
+
import { convertToTokenAmount, convertToUsd } from "./tokens";
|
|
5
|
+
import { applySwapImpactWithCap, getPriceImpactForSwap, getSwapFee } from "./fees";
|
|
6
|
+
export function getSwapPathOutputAddresses(p) {
|
|
7
|
+
const { marketsInfoData, initialCollateralAddress, swapPath, wrappedNativeTokenAddress, shouldUnwrapNativeToken, isIncrease, } = p;
|
|
8
|
+
if (swapPath.length === 0) {
|
|
9
|
+
// Increase
|
|
10
|
+
if (isIncrease) {
|
|
11
|
+
// During increase target collateral token is always ERC20 token, it can not be native token.
|
|
12
|
+
// Thus we do not need to check if initial collateral token is wrapped token to unwrap it.
|
|
13
|
+
// So we can safely return initial collateral token address as out token address, when there is no swap path.
|
|
14
|
+
return {
|
|
15
|
+
outTokenAddress: initialCollateralAddress,
|
|
16
|
+
outMarketAddress: undefined,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
// Decrease
|
|
20
|
+
if (shouldUnwrapNativeToken && initialCollateralAddress === wrappedNativeTokenAddress) {
|
|
21
|
+
return {
|
|
22
|
+
outTokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
23
|
+
outMarketAddress: undefined,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
outTokenAddress: initialCollateralAddress,
|
|
28
|
+
outMarketAddress: undefined,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const [firstMarketAddress, ...marketAddresses] = swapPath;
|
|
32
|
+
let outMarket = getByKey(marketsInfoData, firstMarketAddress);
|
|
33
|
+
if (!outMarket) {
|
|
34
|
+
return {
|
|
35
|
+
outTokenAddress: undefined,
|
|
36
|
+
outMarketAddress: undefined,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
let outTokenType = getTokenPoolType(outMarket, initialCollateralAddress);
|
|
40
|
+
let outToken = outTokenType === "long" ? outMarket.shortToken : outMarket.longToken;
|
|
41
|
+
for (const marketAddress of marketAddresses) {
|
|
42
|
+
outMarket = getByKey(marketsInfoData, marketAddress);
|
|
43
|
+
if (!outMarket) {
|
|
44
|
+
return {
|
|
45
|
+
outTokenAddress: undefined,
|
|
46
|
+
outMarketAddress: undefined,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
outTokenType = outMarket.longTokenAddress === outToken.address ? "short" : "long";
|
|
50
|
+
outToken = outTokenType === "long" ? outMarket.longToken : outMarket.shortToken;
|
|
51
|
+
}
|
|
52
|
+
let outTokenAddress;
|
|
53
|
+
if (isIncrease) {
|
|
54
|
+
// Here swap path is not empty, this means out token came from swapping tokens,
|
|
55
|
+
// thus it can not be native token by design.
|
|
56
|
+
outTokenAddress = outToken.address;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (shouldUnwrapNativeToken && outToken.address === wrappedNativeTokenAddress) {
|
|
60
|
+
outTokenAddress = NATIVE_TOKEN_ADDRESS;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
outTokenAddress = outToken.address;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
outTokenAddress,
|
|
68
|
+
outMarketAddress: outMarket.marketTokenAddress,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export function getSwapPathStats(p) {
|
|
72
|
+
const { marketsInfoData, swapPath, initialCollateralAddress, usdIn, shouldUnwrapNativeToken, shouldApplyPriceImpact, wrappedNativeTokenAddress, } = p;
|
|
73
|
+
if (swapPath.length === 0) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const swapSteps = [];
|
|
77
|
+
let usdOut = usdIn;
|
|
78
|
+
let tokenInAddress = initialCollateralAddress;
|
|
79
|
+
let tokenOutAddress = initialCollateralAddress;
|
|
80
|
+
let totalSwapPriceImpactDeltaUsd = 0n;
|
|
81
|
+
let totalSwapFeeUsd = 0n;
|
|
82
|
+
for (let i = 0; i < swapPath.length; i++) {
|
|
83
|
+
const marketAddress = swapPath[i];
|
|
84
|
+
const marketInfo = marketsInfoData[marketAddress];
|
|
85
|
+
tokenOutAddress = getOppositeCollateral(marketInfo, tokenInAddress).address;
|
|
86
|
+
if (i === swapPath.length - 1 && shouldUnwrapNativeToken && tokenOutAddress === wrappedNativeTokenAddress) {
|
|
87
|
+
tokenOutAddress = NATIVE_TOKEN_ADDRESS;
|
|
88
|
+
}
|
|
89
|
+
const swapStep = getSwapStats({
|
|
90
|
+
marketInfo,
|
|
91
|
+
tokenInAddress,
|
|
92
|
+
tokenOutAddress,
|
|
93
|
+
usdIn: usdOut,
|
|
94
|
+
shouldApplyPriceImpact,
|
|
95
|
+
});
|
|
96
|
+
tokenInAddress = swapStep.tokenOutAddress;
|
|
97
|
+
usdOut = swapStep.usdOut;
|
|
98
|
+
totalSwapPriceImpactDeltaUsd = totalSwapPriceImpactDeltaUsd + swapStep.priceImpactDeltaUsd;
|
|
99
|
+
totalSwapFeeUsd = totalSwapFeeUsd + swapStep.swapFeeUsd;
|
|
100
|
+
swapSteps.push(swapStep);
|
|
101
|
+
}
|
|
102
|
+
const lastStep = swapSteps[swapSteps.length - 1];
|
|
103
|
+
const targetMarketAddress = lastStep.marketAddress;
|
|
104
|
+
const amountOut = lastStep.amountOut;
|
|
105
|
+
const totalFeesDeltaUsd = 0n - totalSwapFeeUsd + totalSwapPriceImpactDeltaUsd;
|
|
106
|
+
return {
|
|
107
|
+
swapPath,
|
|
108
|
+
tokenInAddress,
|
|
109
|
+
tokenOutAddress,
|
|
110
|
+
targetMarketAddress,
|
|
111
|
+
swapSteps,
|
|
112
|
+
usdOut,
|
|
113
|
+
amountOut,
|
|
114
|
+
totalSwapFeeUsd,
|
|
115
|
+
totalSwapPriceImpactDeltaUsd,
|
|
116
|
+
totalFeesDeltaUsd,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export function getSwapStats(p) {
|
|
120
|
+
const { marketInfo, tokenInAddress, tokenOutAddress, usdIn, shouldApplyPriceImpact } = p;
|
|
121
|
+
const isWrap = tokenInAddress === NATIVE_TOKEN_ADDRESS;
|
|
122
|
+
const isUnwrap = tokenOutAddress === NATIVE_TOKEN_ADDRESS;
|
|
123
|
+
const tokenIn = getTokenPoolType(marketInfo, tokenInAddress) === "long" ? marketInfo.longToken : marketInfo.shortToken;
|
|
124
|
+
const tokenOut = getTokenPoolType(marketInfo, tokenOutAddress) === "long" ? marketInfo.longToken : marketInfo.shortToken;
|
|
125
|
+
const priceIn = tokenIn.prices.minPrice;
|
|
126
|
+
const priceOut = tokenOut.prices.maxPrice;
|
|
127
|
+
const amountIn = convertToTokenAmount(usdIn, tokenIn.decimals, priceIn);
|
|
128
|
+
let priceImpactDeltaUsd;
|
|
129
|
+
try {
|
|
130
|
+
priceImpactDeltaUsd = getPriceImpactForSwap(marketInfo, tokenIn, tokenOut, usdIn, usdIn * -1n);
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
return {
|
|
134
|
+
swapFeeUsd: 0n,
|
|
135
|
+
swapFeeAmount: 0n,
|
|
136
|
+
isWrap,
|
|
137
|
+
isUnwrap,
|
|
138
|
+
marketAddress: marketInfo.marketTokenAddress,
|
|
139
|
+
tokenInAddress,
|
|
140
|
+
tokenOutAddress,
|
|
141
|
+
priceImpactDeltaUsd: 0n,
|
|
142
|
+
amountIn,
|
|
143
|
+
amountInAfterFees: amountIn,
|
|
144
|
+
usdIn,
|
|
145
|
+
amountOut: 0n,
|
|
146
|
+
usdOut: 0n,
|
|
147
|
+
isOutLiquidity: true,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const swapFeeAmount = getSwapFee(marketInfo, amountIn, priceImpactDeltaUsd > 0);
|
|
151
|
+
const swapFeeUsd = getSwapFee(marketInfo, usdIn, priceImpactDeltaUsd > 0);
|
|
152
|
+
const amountInAfterFees = amountIn - swapFeeAmount;
|
|
153
|
+
const usdInAfterFees = usdIn - swapFeeUsd;
|
|
154
|
+
let usdOut = usdInAfterFees;
|
|
155
|
+
let amountOut = convertToTokenAmount(usdOut, tokenOut.decimals, priceOut);
|
|
156
|
+
let cappedImpactDeltaUsd;
|
|
157
|
+
if (priceImpactDeltaUsd > 0) {
|
|
158
|
+
const { impactDeltaAmount: positiveImpactAmountTokenOut, cappedDiffUsd } = applySwapImpactWithCap(marketInfo, tokenOut, priceImpactDeltaUsd);
|
|
159
|
+
cappedImpactDeltaUsd = convertToUsd(positiveImpactAmountTokenOut, tokenOut.decimals, priceOut);
|
|
160
|
+
// https://github.com/gmx-io/gmx-synthetics/blob/3df10f1eab2734cf1b5f0a5dff12b79cbb19907d/contracts/swap/SwapUtils.sol#L290-L291
|
|
161
|
+
if (cappedDiffUsd > 0) {
|
|
162
|
+
const { impactDeltaAmount: positiveImpactAmountTokenIn } = applySwapImpactWithCap(marketInfo, tokenIn, cappedDiffUsd);
|
|
163
|
+
if (positiveImpactAmountTokenIn > 0) {
|
|
164
|
+
cappedImpactDeltaUsd += convertToUsd(positiveImpactAmountTokenIn, tokenIn.decimals, priceIn);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const { impactDeltaAmount: negativeImpactAmount } = applySwapImpactWithCap(marketInfo, tokenIn, priceImpactDeltaUsd);
|
|
170
|
+
cappedImpactDeltaUsd = convertToUsd(negativeImpactAmount, tokenIn.decimals, priceIn);
|
|
171
|
+
}
|
|
172
|
+
if (shouldApplyPriceImpact) {
|
|
173
|
+
usdOut = usdOut + cappedImpactDeltaUsd;
|
|
174
|
+
}
|
|
175
|
+
if (usdOut < 0) {
|
|
176
|
+
usdOut = 0n;
|
|
177
|
+
}
|
|
178
|
+
amountOut = convertToTokenAmount(usdOut, tokenOut.decimals, priceOut);
|
|
179
|
+
const liquidity = getAvailableUsdLiquidityForCollateral(marketInfo, getTokenPoolType(marketInfo, tokenOutAddress) === "long");
|
|
180
|
+
const isOutLiquidity = liquidity < usdOut;
|
|
181
|
+
return {
|
|
182
|
+
swapFeeUsd,
|
|
183
|
+
swapFeeAmount,
|
|
184
|
+
isWrap,
|
|
185
|
+
isUnwrap,
|
|
186
|
+
marketAddress: marketInfo.marketTokenAddress,
|
|
187
|
+
tokenInAddress,
|
|
188
|
+
tokenOutAddress,
|
|
189
|
+
priceImpactDeltaUsd: cappedImpactDeltaUsd,
|
|
190
|
+
amountIn,
|
|
191
|
+
amountInAfterFees,
|
|
192
|
+
usdIn,
|
|
193
|
+
amountOut,
|
|
194
|
+
usdOut,
|
|
195
|
+
isOutLiquidity,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createTestClient, http, publicActions, walletActions } from "viem";
|
|
2
|
+
import { ARBITRUM, getChain } from "configs/chains";
|
|
3
|
+
import { GmxSdk } from "../index";
|
|
4
|
+
const client = createTestClient({
|
|
5
|
+
chain: getChain(ARBITRUM),
|
|
6
|
+
mode: "hardhat",
|
|
7
|
+
transport: http(),
|
|
8
|
+
})
|
|
9
|
+
.extend(publicActions)
|
|
10
|
+
.extend(walletActions);
|
|
11
|
+
export const arbitrumSdk = new GmxSdk({
|
|
12
|
+
chainId: ARBITRUM,
|
|
13
|
+
account: "0x9f7198eb1b9Ccc0Eb7A07eD228d8FbC12963ea33",
|
|
14
|
+
oracleUrl: "https://arbitrum-api.gmxinfra.io",
|
|
15
|
+
rpcUrl: "https://arb1.arbitrum.io/rpc",
|
|
16
|
+
walletClient: client,
|
|
17
|
+
subgraph: {
|
|
18
|
+
stats: "https://subgraph.satsuma-prod.com/3b2ced13c8d9/gmx/gmx-arbitrum-stats/api",
|
|
19
|
+
referrals: "https://subgraph.satsuma-prod.com/3b2ced13c8d9/gmx/gmx-arbitrum-referrals/api",
|
|
20
|
+
nissohVault: "https://api.thegraph.com/subgraphs/name/nissoh/gmx-vault",
|
|
21
|
+
syntheticsStats: "https://subgraph.satsuma-prod.com/3b2ced13c8d9/gmx/synthetics-arbitrum-stats/api",
|
|
22
|
+
subsquid: "https://gmx.squids.live/gmx-synthetics-arbitrum/v/v20/graphql",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Token, TokenPrices, TokensData, TokensRatio } from "types/tokens";
|
|
2
|
+
export declare function parseContractPrice(price: bigint, tokenDecimals: number): bigint;
|
|
3
|
+
export declare function convertToContractPrice(price: bigint, tokenDecimals: number): bigint;
|
|
4
|
+
export declare function convertToContractTokenPrices(prices: TokenPrices, tokenDecimals: number): {
|
|
5
|
+
min: bigint;
|
|
6
|
+
max: bigint;
|
|
7
|
+
};
|
|
8
|
+
export declare function convertToTokenAmount(usd: bigint | undefined, tokenDecimals: number | undefined, price: bigint | undefined): bigint | undefined;
|
|
9
|
+
export declare function convertToUsd(tokenAmount: bigint | undefined, tokenDecimals: number | undefined, price: bigint | undefined): bigint | undefined;
|
|
10
|
+
export declare function getMidPrice(prices: TokenPrices): bigint;
|
|
11
|
+
export declare function getIsEquivalentTokens(token1: Token, token2: Token): boolean;
|
|
12
|
+
export declare function getTokenData(tokensData?: TokensData, address?: string, convertTo?: "wrapped" | "native"): import("types/tokens").TokenData | undefined;
|
|
13
|
+
export declare function getTokensRatioByAmounts(p: {
|
|
14
|
+
fromToken: Token;
|
|
15
|
+
toToken: Token;
|
|
16
|
+
fromTokenAmount: bigint;
|
|
17
|
+
toTokenAmount: bigint;
|
|
18
|
+
}): TokensRatio;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { expandDecimals, PRECISION } from "./numbers";
|
|
2
|
+
import { NATIVE_TOKEN_ADDRESS } from "configs/tokens";
|
|
3
|
+
export function parseContractPrice(price, tokenDecimals) {
|
|
4
|
+
return price * expandDecimals(1, tokenDecimals);
|
|
5
|
+
}
|
|
6
|
+
export function convertToContractPrice(price, tokenDecimals) {
|
|
7
|
+
return price / expandDecimals(1, tokenDecimals);
|
|
8
|
+
}
|
|
9
|
+
export function convertToContractTokenPrices(prices, tokenDecimals) {
|
|
10
|
+
return {
|
|
11
|
+
min: convertToContractPrice(prices.minPrice, tokenDecimals),
|
|
12
|
+
max: convertToContractPrice(prices.maxPrice, tokenDecimals),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function convertToTokenAmount(usd, tokenDecimals, price) {
|
|
16
|
+
if (usd === undefined || typeof tokenDecimals !== "number" || price === undefined || price <= 0) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
return (usd * expandDecimals(1, tokenDecimals)) / price;
|
|
20
|
+
}
|
|
21
|
+
export function convertToUsd(tokenAmount, tokenDecimals, price) {
|
|
22
|
+
if (tokenAmount == undefined || typeof tokenDecimals !== "number" || price === undefined) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
return (tokenAmount * price) / expandDecimals(1, tokenDecimals);
|
|
26
|
+
}
|
|
27
|
+
export function getMidPrice(prices) {
|
|
28
|
+
return (prices.minPrice + prices.maxPrice) / 2n;
|
|
29
|
+
}
|
|
30
|
+
export function getIsEquivalentTokens(token1, token2) {
|
|
31
|
+
if (token1.address === token2.address) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (token1.wrappedAddress === token2.address || token2.wrappedAddress === token1.address) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
if ((token1.isSynthetic || token2.isSynthetic) && token1.symbol === token2.symbol) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
export function getTokenData(tokensData, address, convertTo) {
|
|
43
|
+
if (!address || !tokensData?.[address]) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
const token = tokensData[address];
|
|
47
|
+
if (convertTo === "wrapped" && token.isNative && token.wrappedAddress) {
|
|
48
|
+
return tokensData[token.wrappedAddress];
|
|
49
|
+
}
|
|
50
|
+
if (convertTo === "native" && token.isWrapped) {
|
|
51
|
+
return tokensData[NATIVE_TOKEN_ADDRESS];
|
|
52
|
+
}
|
|
53
|
+
return token;
|
|
54
|
+
}
|
|
55
|
+
export function getTokensRatioByAmounts(p) {
|
|
56
|
+
const { fromToken, toToken, fromTokenAmount, toTokenAmount } = p;
|
|
57
|
+
const adjustedFromAmount = (fromTokenAmount * PRECISION) / expandDecimals(1, fromToken.decimals);
|
|
58
|
+
const adjustedToAmount = (toTokenAmount * PRECISION) / expandDecimals(1, toToken.decimals);
|
|
59
|
+
const [smallestToken, largestToken, largestAmount, smallestAmount] = adjustedFromAmount > adjustedToAmount
|
|
60
|
+
? [fromToken, toToken, adjustedFromAmount, adjustedToAmount]
|
|
61
|
+
: [toToken, fromToken, adjustedToAmount, adjustedFromAmount];
|
|
62
|
+
const ratio = smallestAmount > 0 ? (largestAmount * PRECISION) / smallestAmount : 0n;
|
|
63
|
+
return { ratio, largestToken, smallestToken };
|
|
64
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DecreasePositionAmounts, IncreasePositionAmounts, SwapAmounts } from "types/trade";
|
|
2
|
+
export declare function applySlippageToPrice(allowedSlippage: number, price: bigint, isIncrease: boolean, isLong: boolean): bigint;
|
|
3
|
+
export declare function applySlippageToMinOut(allowedSlippage: number, minOutputAmount: bigint): bigint;
|
|
4
|
+
export declare function getSwapCount({ isSwap, isIncrease, increaseAmounts, decreaseAmounts, swapAmounts, }: {
|
|
5
|
+
isSwap: boolean;
|
|
6
|
+
isIncrease: boolean;
|
|
7
|
+
swapAmounts?: SwapAmounts;
|
|
8
|
+
increaseAmounts?: IncreasePositionAmounts;
|
|
9
|
+
decreaseAmounts?: DecreasePositionAmounts;
|
|
10
|
+
}): number | undefined;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BASIS_POINTS_DIVISOR, BASIS_POINTS_DIVISOR_BIGINT } from "configs/factors";
|
|
2
|
+
import { getShouldUseMaxPrice } from "./prices";
|
|
3
|
+
import { bigMath } from "./bigmath";
|
|
4
|
+
import { DecreasePositionSwapType } from "types/orders";
|
|
5
|
+
export function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong) {
|
|
6
|
+
const shouldIncreasePrice = getShouldUseMaxPrice(isIncrease, isLong);
|
|
7
|
+
const slippageBasisPoints = shouldIncreasePrice
|
|
8
|
+
? BASIS_POINTS_DIVISOR + allowedSlippage
|
|
9
|
+
: BASIS_POINTS_DIVISOR - allowedSlippage;
|
|
10
|
+
return bigMath.mulDiv(price, BigInt(slippageBasisPoints), BASIS_POINTS_DIVISOR_BIGINT);
|
|
11
|
+
}
|
|
12
|
+
export function applySlippageToMinOut(allowedSlippage, minOutputAmount) {
|
|
13
|
+
const slippageBasisPoints = BASIS_POINTS_DIVISOR - allowedSlippage;
|
|
14
|
+
return bigMath.mulDiv(minOutputAmount, BigInt(slippageBasisPoints), BASIS_POINTS_DIVISOR_BIGINT);
|
|
15
|
+
}
|
|
16
|
+
export function getSwapCount({ isSwap, isIncrease, increaseAmounts, decreaseAmounts, swapAmounts, }) {
|
|
17
|
+
if (isSwap) {
|
|
18
|
+
if (!swapAmounts)
|
|
19
|
+
return undefined;
|
|
20
|
+
return swapAmounts.swapPathStats?.swapPath.length ?? 0;
|
|
21
|
+
}
|
|
22
|
+
else if (isIncrease) {
|
|
23
|
+
if (!increaseAmounts)
|
|
24
|
+
return undefined;
|
|
25
|
+
return increaseAmounts.swapPathStats?.swapPath.length ?? 0;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
if (decreaseAmounts?.decreaseSwapType === undefined)
|
|
29
|
+
return undefined;
|
|
30
|
+
return decreaseAmounts.decreaseSwapType !== DecreasePositionSwapType.NoSwap ? 1 : 0;
|
|
31
|
+
}
|
|
32
|
+
}
|