@gmx-io/sdk 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -5
- package/build/src/configs/batch.d.ts +2 -3
- package/build/src/configs/chains.d.ts +5 -1
- package/build/src/configs/chains.js +9 -2
- package/build/src/configs/contracts.d.ts +220 -1
- package/build/src/configs/contracts.js +67 -8
- package/build/src/configs/markets.js +70 -0
- package/build/src/configs/tokens.js +128 -5
- package/build/src/modules/accounts/accounts.spec.js +1 -0
- package/build/src/modules/markets/index.js +2 -8
- package/build/src/modules/markets/markets.spec.js +1 -0
- package/build/src/modules/markets/query-builders.d.ts +1 -1
- package/build/src/modules/markets/query-builders.js +1 -1
- package/build/src/modules/markets/types.d.ts +4 -1
- package/build/src/modules/orders/orders.spec.js +1 -0
- package/build/src/modules/orders/utils.js +1 -1
- package/build/src/modules/positions/positions.d.ts +2 -2
- package/build/src/modules/positions/positions.js +21 -8
- package/build/src/modules/positions/positions.spec.js +2 -1
- package/build/src/modules/tokens/tokens.spec.js +1 -0
- package/build/src/modules/trades/trades.spec.js +1 -0
- package/build/src/modules/utils/utils.d.ts +1 -1
- package/build/src/modules/utils/utils.js +1 -1
- package/build/src/prebuilt/hashedKinkModelMarketRatesKeys.json +80 -0
- package/build/src/prebuilt/hashedMarketConfigKeys.json +500 -0
- package/build/src/prebuilt/hashedMarketValuesKeys.json +150 -0
- package/build/src/types/common.d.ts +1 -0
- package/build/src/types/fees.d.ts +2 -1
- package/build/src/types/markets.d.ts +7 -19
- package/build/src/types/orders.d.ts +3 -5
- package/build/src/types/positions.d.ts +11 -4
- package/build/src/types/trade.d.ts +1 -0
- package/build/src/types/tradeHistory.d.ts +4 -1
- package/build/src/utils/__tests__/LruCache.spec.d.ts +1 -0
- package/build/src/utils/__tests__/LruCache.spec.js +43 -0
- package/build/src/utils/__tests__/bigmath.spec.d.ts +1 -0
- package/build/src/utils/__tests__/bigmath.spec.js +38 -0
- package/build/src/utils/__tests__/fees.spec.d.ts +1 -0
- package/build/src/utils/__tests__/fees.spec.js +62 -0
- package/build/src/utils/__tests__/hash.spec.d.ts +1 -0
- package/build/src/utils/__tests__/hash.spec.js +41 -0
- package/build/src/utils/__tests__/markets.spec.d.ts +1 -0
- package/build/src/utils/__tests__/markets.spec.js +265 -0
- package/build/src/utils/__tests__/numbers.spec.d.ts +1 -0
- package/build/src/utils/__tests__/numbers.spec.js +81 -0
- package/build/src/utils/__tests__/objects.spec.d.ts +1 -0
- package/build/src/utils/__tests__/objects.spec.js +59 -0
- package/build/src/utils/__tests__/positions.spec.d.ts +1 -0
- package/build/src/utils/__tests__/positions.spec.js +178 -0
- package/build/src/utils/__tests__/prices.spec.d.ts +1 -0
- package/build/src/utils/__tests__/prices.spec.js +59 -0
- package/build/src/utils/__tests__/referrals.spec.d.ts +1 -0
- package/build/src/utils/__tests__/referrals.spec.js +18 -0
- package/build/src/utils/__tests__/subgraph.spec.d.ts +1 -0
- package/build/src/utils/__tests__/subgraph.spec.js +163 -0
- package/build/src/utils/__tests__/tokens.spec.d.ts +1 -0
- package/build/src/utils/__tests__/tokens.spec.js +83 -0
- package/build/src/utils/__tests__/trade.spec.d.ts +1 -0
- package/build/src/utils/__tests__/trade.spec.js +36 -0
- package/build/src/utils/bigmath.d.ts +1 -1
- package/build/src/utils/bigmath.js +6 -1
- package/build/src/utils/callContract.js +5 -1
- package/build/src/utils/common.d.ts +6 -1
- package/build/src/utils/common.js +9 -1
- package/build/src/utils/contracts.d.ts +4 -0
- package/build/src/utils/contracts.js +2 -0
- package/build/src/utils/fees/executionFee.d.ts +22 -21
- package/build/src/utils/fees/executionFee.js +26 -26
- package/build/src/utils/fees/executionFee.spec.d.ts +1 -0
- package/build/src/utils/fees/executionFee.spec.js +64 -0
- package/build/src/utils/fees/index.d.ts +2 -0
- package/build/src/utils/fees/index.js +21 -9
- package/build/src/utils/markets.d.ts +9 -2
- package/build/src/utils/markets.js +33 -10
- package/build/src/utils/multicall.js +4 -3
- package/build/src/utils/numbers.d.ts +2 -0
- package/build/src/utils/numbers.js +28 -0
- package/build/src/utils/orders.js +0 -8
- package/build/src/utils/positions.js +3 -3
- package/build/src/utils/prices.d.ts +1 -6
- package/build/src/utils/prices.js +0 -9
- package/build/src/utils/referrals.d.ts +2 -0
- package/build/src/utils/referrals.js +17 -15
- package/build/src/utils/swapStats.d.ts +1 -0
- package/build/src/utils/swapStats.js +12 -1
- package/build/src/utils/testUtil.d.ts +14314 -326
- package/build/src/utils/tokens.d.ts +4 -0
- package/build/src/utils/tokens.js +4 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -29
- package/build/src/types/amounts.d.ts +0 -223
- package/build/src/types/amounts.js +0 -17
- package/build/src/types/syntheticsEvents.d.ts +0 -88
- /package/build/src/types/{syntheticsEvents.js → common.js} +0 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { buildFiltersBody } from "../subgraph";
|
|
3
|
+
describe("buildFiltersBody", () => {
|
|
4
|
+
it("should return empty object if no filters", () => {
|
|
5
|
+
const input = {};
|
|
6
|
+
const result = buildFiltersBody(input);
|
|
7
|
+
expect(result).toEqual("{}");
|
|
8
|
+
});
|
|
9
|
+
it("should return correct filter string with single filter", () => {
|
|
10
|
+
const input = {
|
|
11
|
+
foo: "bar",
|
|
12
|
+
};
|
|
13
|
+
const result = buildFiltersBody(input);
|
|
14
|
+
expect(result).toEqual('{foo:"bar"}');
|
|
15
|
+
});
|
|
16
|
+
it("should return correct filter string with multiple filters", () => {
|
|
17
|
+
const input = {
|
|
18
|
+
foo: "bar",
|
|
19
|
+
baz: "qux",
|
|
20
|
+
};
|
|
21
|
+
const result = buildFiltersBody(input);
|
|
22
|
+
expect(result).toEqual('{foo:"bar",baz:"qux"}');
|
|
23
|
+
});
|
|
24
|
+
it("should return correct filter string with nested filter", () => {
|
|
25
|
+
const input = {
|
|
26
|
+
foo: {
|
|
27
|
+
bar: {
|
|
28
|
+
baz: "qux",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const result = buildFiltersBody(input);
|
|
33
|
+
expect(result).toEqual('{foo_:{bar_:{baz:"qux"}}}');
|
|
34
|
+
});
|
|
35
|
+
it("should return correct filter string with or filter", () => {
|
|
36
|
+
const input = {
|
|
37
|
+
or: [
|
|
38
|
+
{
|
|
39
|
+
foo: "bar",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
baz: "qux",
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
};
|
|
46
|
+
const result = buildFiltersBody(input);
|
|
47
|
+
expect(result).toEqual('{or:[{foo:"bar"},{baz:"qux"}]}');
|
|
48
|
+
});
|
|
49
|
+
it("should return correct filter string with and filter", () => {
|
|
50
|
+
const input = {
|
|
51
|
+
and: [
|
|
52
|
+
{
|
|
53
|
+
foo: "bar",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
baz: "qux",
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
const result = buildFiltersBody(input);
|
|
61
|
+
expect(result).toEqual('{and:[{foo:"bar"},{baz:"qux"}]}');
|
|
62
|
+
});
|
|
63
|
+
it("should strip out undefined filters", () => {
|
|
64
|
+
const input = {
|
|
65
|
+
foo: "bar",
|
|
66
|
+
baz: undefined,
|
|
67
|
+
};
|
|
68
|
+
const result = buildFiltersBody(input);
|
|
69
|
+
expect(result).toEqual('{foo:"bar"}');
|
|
70
|
+
});
|
|
71
|
+
it("should strip out empty or", () => {
|
|
72
|
+
const input = {
|
|
73
|
+
or: [],
|
|
74
|
+
};
|
|
75
|
+
const result = buildFiltersBody(input);
|
|
76
|
+
expect(result).toEqual("{}");
|
|
77
|
+
});
|
|
78
|
+
it("should strip out or with empty", () => {
|
|
79
|
+
const input = {
|
|
80
|
+
or: [
|
|
81
|
+
{},
|
|
82
|
+
{
|
|
83
|
+
foo: undefined,
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
};
|
|
87
|
+
const result = buildFiltersBody(input);
|
|
88
|
+
expect(result).toEqual("{}");
|
|
89
|
+
});
|
|
90
|
+
it("should throw error if or is mixed with other filters", () => {
|
|
91
|
+
const input = {
|
|
92
|
+
or: [
|
|
93
|
+
{
|
|
94
|
+
foo: "bar",
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
// @ts-expect-error
|
|
98
|
+
baz: "qux",
|
|
99
|
+
};
|
|
100
|
+
const getResult = () => buildFiltersBody(input);
|
|
101
|
+
expect(getResult).toThrowError();
|
|
102
|
+
});
|
|
103
|
+
it("should throw error if and is mixed with other filters", () => {
|
|
104
|
+
const input = {
|
|
105
|
+
and: [
|
|
106
|
+
{
|
|
107
|
+
foo: "bar",
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
// @ts-expect-error
|
|
111
|
+
baz: "qux",
|
|
112
|
+
};
|
|
113
|
+
const getResult = () => buildFiltersBody(input);
|
|
114
|
+
expect(getResult).toThrowError();
|
|
115
|
+
});
|
|
116
|
+
it("should throw not error if empty or is mixed with other filters", () => {
|
|
117
|
+
const input = {
|
|
118
|
+
or: [],
|
|
119
|
+
// @ts-expect-error
|
|
120
|
+
baz: "qux",
|
|
121
|
+
};
|
|
122
|
+
const getResult = () => buildFiltersBody(input);
|
|
123
|
+
expect(getResult).not.toThrowError();
|
|
124
|
+
});
|
|
125
|
+
it("should throw not error if empty and is mixed with other filters", () => {
|
|
126
|
+
const input = {
|
|
127
|
+
and: [],
|
|
128
|
+
// @ts-expect-error
|
|
129
|
+
baz: "qux",
|
|
130
|
+
};
|
|
131
|
+
const getResult = () => buildFiltersBody(input);
|
|
132
|
+
expect(getResult).not.toThrowError();
|
|
133
|
+
});
|
|
134
|
+
it("should format string values correctly", () => {
|
|
135
|
+
const input = {
|
|
136
|
+
foo: "bar",
|
|
137
|
+
};
|
|
138
|
+
const result = buildFiltersBody(input);
|
|
139
|
+
expect(result).toEqual('{foo:"bar"}');
|
|
140
|
+
});
|
|
141
|
+
it("should format number values correctly", () => {
|
|
142
|
+
const input = {
|
|
143
|
+
foo: 123,
|
|
144
|
+
};
|
|
145
|
+
const result = buildFiltersBody(input);
|
|
146
|
+
expect(result).toEqual("{foo:123}");
|
|
147
|
+
});
|
|
148
|
+
it("should format boolean values correctly", () => {
|
|
149
|
+
const input = {
|
|
150
|
+
foo: true,
|
|
151
|
+
bar: false,
|
|
152
|
+
};
|
|
153
|
+
const result = buildFiltersBody(input);
|
|
154
|
+
expect(result).toEqual("{foo:true,bar:false}");
|
|
155
|
+
});
|
|
156
|
+
it("should format null values correctly", () => {
|
|
157
|
+
const input = {
|
|
158
|
+
foo: null,
|
|
159
|
+
};
|
|
160
|
+
const result = buildFiltersBody(input);
|
|
161
|
+
expect(result).toEqual("{foo:null}");
|
|
162
|
+
});
|
|
163
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { parseContractPrice, convertToContractPrice, convertToContractTokenPrices, convertToTokenAmount, convertToUsd, getMidPrice, getIsEquivalentTokens, getTokenData, getTokensRatioByAmounts, } from "../tokens";
|
|
3
|
+
import { expandDecimals, PRECISION } from "../numbers";
|
|
4
|
+
import { TOKENS } from "../../configs/tokens";
|
|
5
|
+
import { ARBITRUM } from "../../configs/chains";
|
|
6
|
+
function getToken(symbol) {
|
|
7
|
+
return TOKENS[ARBITRUM].find((token) => token.symbol === symbol);
|
|
8
|
+
}
|
|
9
|
+
describe("parseContractPrice", () => {
|
|
10
|
+
it("multiplies price by 10^decimals", () => {
|
|
11
|
+
expect(parseContractPrice(100n, 2)).toBe(100n * expandDecimals(1, 2));
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
describe("convertToContractPrice", () => {
|
|
15
|
+
it("divides price by 10^decimals", () => {
|
|
16
|
+
expect(convertToContractPrice(10000n, 2)).toBe(10000n / expandDecimals(1, 2));
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
describe("convertToContractTokenPrices", () => {
|
|
20
|
+
it("returns min and max contract prices", () => {
|
|
21
|
+
const result = convertToContractTokenPrices({ minPrice: 1000n, maxPrice: 2000n }, 2);
|
|
22
|
+
expect(result.min).toBe(1000n / expandDecimals(1, 2));
|
|
23
|
+
expect(result.max).toBe(2000n / expandDecimals(1, 2));
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe("convertToTokenAmount", () => {
|
|
27
|
+
it("returns undefined if inputs are invalid", () => {
|
|
28
|
+
expect(convertToTokenAmount(undefined, 18, 100n)).toBeUndefined();
|
|
29
|
+
expect(convertToTokenAmount(1000n, undefined, 100n)).toBeUndefined();
|
|
30
|
+
expect(convertToTokenAmount(1000n, 18, 0n)).toBeUndefined();
|
|
31
|
+
});
|
|
32
|
+
it("converts usd to token amount", () => {
|
|
33
|
+
expect(convertToTokenAmount(1000n, 2, 100n)).toBe((1000n * expandDecimals(1, 2)) / 100n);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
describe("convertToUsd", () => {
|
|
37
|
+
it("returns undefined if inputs are invalid", () => {
|
|
38
|
+
expect(convertToUsd(undefined, 18, 100n)).toBeUndefined();
|
|
39
|
+
expect(convertToUsd(1000n, undefined, 100n)).toBeUndefined();
|
|
40
|
+
});
|
|
41
|
+
it("converts token amount to usd", () => {
|
|
42
|
+
expect(convertToUsd(1000n, 2, 100n)).toBe((1000n * 100n) / expandDecimals(1, 2));
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe("getMidPrice", () => {
|
|
46
|
+
it("returns the average of min and max price", () => {
|
|
47
|
+
expect(getMidPrice({ minPrice: 10n, maxPrice: 20n })).toBe(15n);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe("getIsEquivalentTokens", () => {
|
|
51
|
+
it("checks address, wrappedAddress, synthetic, and symbol", () => {
|
|
52
|
+
expect(getIsEquivalentTokens(getToken("ETH"), getToken("WETH"))).toBe(true);
|
|
53
|
+
expect(getIsEquivalentTokens(getToken("ETH"), getToken("ETH"))).toBe(true);
|
|
54
|
+
expect(getIsEquivalentTokens({ address: "0xA", isSynthetic: true, symbol: "SYN" }, { address: "0xB", isSynthetic: true, symbol: "SYN" })).toBe(true);
|
|
55
|
+
expect(getIsEquivalentTokens(getToken("ETH"), getToken("BTC"))).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe("getTokenData", () => {
|
|
59
|
+
it("returns undefined if no token data", () => {
|
|
60
|
+
expect(getTokenData()).toBeUndefined();
|
|
61
|
+
});
|
|
62
|
+
it("returns wrapped if convertTo=wrapped and token isNative", () => {
|
|
63
|
+
const tokensData = {
|
|
64
|
+
"0xnative": { address: "0xnative", isNative: true, wrappedAddress: "0xwrap" },
|
|
65
|
+
"0xwrap": { address: "0xwrap", isWrapped: true },
|
|
66
|
+
};
|
|
67
|
+
expect(getTokenData(tokensData, "0xnative", "wrapped")).toEqual(tokensData["0xwrap"]);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
describe("getTokensRatioByAmounts", () => {
|
|
71
|
+
it("returns ratio of two token amounts", () => {
|
|
72
|
+
const fromToken = { decimals: 2 };
|
|
73
|
+
const toToken = { decimals: 2 };
|
|
74
|
+
const result = getTokensRatioByAmounts({
|
|
75
|
+
fromToken,
|
|
76
|
+
toToken,
|
|
77
|
+
fromTokenAmount: 1000n,
|
|
78
|
+
toTokenAmount: 500n,
|
|
79
|
+
});
|
|
80
|
+
expect(result.largestToken).toEqual(fromToken);
|
|
81
|
+
expect(result.ratio).toBe((((1000n * PRECISION) / expandDecimals(1, 2)) * PRECISION) / ((500n * PRECISION) / expandDecimals(1, 2)));
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { applySlippageToMinOut, applySlippageToPrice } from "../trade";
|
|
3
|
+
describe("applySlippageToPrice", () => {
|
|
4
|
+
it("applies positive slippage if getShouldUseMaxPrice is true", () => {
|
|
5
|
+
// isIncrease=true and isLong=true => getShouldUseMaxPrice => true
|
|
6
|
+
const allowedSlippage = 100; // 1%
|
|
7
|
+
const price = 1000n;
|
|
8
|
+
const result = applySlippageToPrice(allowedSlippage, price, true, true);
|
|
9
|
+
// expected: price * (10000+100)/10000 = 1000n * 10100n / 10000n = 1010n
|
|
10
|
+
expect(result).toBe(1010n);
|
|
11
|
+
});
|
|
12
|
+
it("applies negative slippage if getShouldUseMaxPrice is false", () => {
|
|
13
|
+
// isIncrease=true or isLong=false => getShouldUseMaxPrice => false
|
|
14
|
+
const allowedSlippage = 100; // 1%
|
|
15
|
+
const price = 1000n;
|
|
16
|
+
const result = applySlippageToPrice(allowedSlippage, price, true, false);
|
|
17
|
+
// expected: price * (10000-100)/10000 = 1000n * 9900n / 10000n = 990n
|
|
18
|
+
expect(result).toBe(990n);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
describe("applySlippageToMinOut", () => {
|
|
22
|
+
it("reduces minOutputAmount by allowed slippage", () => {
|
|
23
|
+
const allowedSlippage = 100; // 1%
|
|
24
|
+
const minOutputAmount = 10000n;
|
|
25
|
+
const result = applySlippageToMinOut(allowedSlippage, minOutputAmount);
|
|
26
|
+
// expected: minOut * (10000 - 100) / 10000 = 10_000n * 9900n / 10000n = 9900n
|
|
27
|
+
expect(result).toBe(9900n);
|
|
28
|
+
});
|
|
29
|
+
it("does nothing if slippage is zero", () => {
|
|
30
|
+
const allowedSlippage = 0;
|
|
31
|
+
const minOutputAmount = 10000n;
|
|
32
|
+
const result = applySlippageToMinOut(allowedSlippage, minOutputAmount);
|
|
33
|
+
// expected: 10_000n * (10000 - 0) / 10000n = 10_000n
|
|
34
|
+
expect(result).toBe(10000n);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -3,5 +3,5 @@ export declare const bigMath: {
|
|
|
3
3
|
mulDiv(x: bigint, y: bigint, z: bigint): bigint;
|
|
4
4
|
max(max: bigint, ...rest: bigint[]): bigint;
|
|
5
5
|
min(min: bigint, ...rest: bigint[]): bigint;
|
|
6
|
-
avg(...values: (bigint | undefined)[]): bigint;
|
|
6
|
+
avg(...values: (bigint | undefined)[]): bigint | undefined;
|
|
7
7
|
};
|
|
@@ -13,11 +13,16 @@ export const bigMath = {
|
|
|
13
13
|
},
|
|
14
14
|
avg(...values) {
|
|
15
15
|
let sum = 0n;
|
|
16
|
+
let count = 0n;
|
|
16
17
|
for (const value of values) {
|
|
17
18
|
if (value !== undefined) {
|
|
18
19
|
sum += value;
|
|
20
|
+
count += 1n;
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
|
-
|
|
23
|
+
if (count === 0n) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
return sum / count;
|
|
22
27
|
},
|
|
23
28
|
};
|
|
@@ -122,12 +122,16 @@ export async function callContract(sdk, contractAddress, abi, method, params, op
|
|
|
122
122
|
...txnInstance,
|
|
123
123
|
});
|
|
124
124
|
});
|
|
125
|
-
const res = await Promise.any(txnCalls)
|
|
125
|
+
const res = await Promise.any(txnCalls)
|
|
126
|
+
.catch((error) => {
|
|
126
127
|
if (error.errors && error.errors.length > 1) {
|
|
127
128
|
// eslint-disable-next-line no-console
|
|
128
129
|
console.error("All transactions failed", ...error.errors);
|
|
129
130
|
}
|
|
130
131
|
throw error.errors ? error.errors[0] : error;
|
|
132
|
+
})
|
|
133
|
+
.catch((error) => {
|
|
134
|
+
throw error;
|
|
131
135
|
});
|
|
132
136
|
return res;
|
|
133
137
|
}
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
declare global {
|
|
2
|
+
interface AbortSignal {
|
|
3
|
+
addEventListener(type: "abort", listener: () => void): void;
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export declare const sleep: (ms: number, abortSignal?: AbortSignal) => Promise<unknown>;
|
|
2
7
|
export declare const TIMEZONE_OFFSET_SEC: number;
|
|
@@ -1,2 +1,10 @@
|
|
|
1
|
-
export const sleep = (ms) => new Promise((resolve) =>
|
|
1
|
+
export const sleep = (ms, abortSignal) => new Promise((resolve) => {
|
|
2
|
+
const timeout = setTimeout(resolve, ms);
|
|
3
|
+
if (abortSignal) {
|
|
4
|
+
abortSignal.addEventListener("abort", () => {
|
|
5
|
+
clearTimeout(timeout);
|
|
6
|
+
resolve(undefined);
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
});
|
|
2
10
|
export const TIMEZONE_OFFSET_SEC = -new Date().getTimezoneOffset() * 60;
|
|
@@ -17,6 +17,8 @@ const TX_ERROR_PATTERNS = {
|
|
|
17
17
|
{ msg: "User denied transaction signature" },
|
|
18
18
|
{ msg: "User rejected" },
|
|
19
19
|
{ msg: "user rejected action" },
|
|
20
|
+
{ msg: "ethers-user-denied" },
|
|
21
|
+
{ msg: "User canceled" },
|
|
20
22
|
{ msg: "Signing aborted by user" },
|
|
21
23
|
],
|
|
22
24
|
[TxErrorType.Slippage]: [
|
|
@@ -1,8 +1,26 @@
|
|
|
1
1
|
import { ExecutionFee, GasLimitsConfig } from "../../types/fees";
|
|
2
2
|
import { DecreasePositionSwapType } from "../../types/orders";
|
|
3
3
|
import { TokensData } from "../../types/tokens";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
export declare function getExecutionFee(chainId: number, gasLimits: GasLimitsConfig, tokensData: TokensData, estimatedGasLimit: bigint, gasPrice: bigint, oraclePriceCount: bigint): ExecutionFee | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Copy from contract: `estimateExecuteIncreaseOrderGasLimit`
|
|
7
|
+
*/
|
|
8
|
+
export declare function estimateExecuteIncreaseOrderGasLimit(gasLimits: GasLimitsConfig, order: {
|
|
9
|
+
swapsCount?: number;
|
|
10
|
+
callbackGasLimit?: bigint;
|
|
11
|
+
}): bigint;
|
|
12
|
+
/**
|
|
13
|
+
* Copy from contract: `estimateExecuteDecreaseOrderGasLimit`
|
|
14
|
+
*/
|
|
15
|
+
export declare function estimateExecuteDecreaseOrderGasLimit(gasLimits: GasLimitsConfig, order: {
|
|
16
|
+
swapsCount: number;
|
|
17
|
+
callbackGasLimit?: bigint;
|
|
18
|
+
decreaseSwapType?: DecreasePositionSwapType;
|
|
19
|
+
}): bigint;
|
|
20
|
+
export declare function estimateExecuteSwapOrderGasLimit(gasLimits: GasLimitsConfig, order: {
|
|
21
|
+
swapsCount: number;
|
|
22
|
+
callbackGasLimit?: bigint;
|
|
23
|
+
}): bigint;
|
|
6
24
|
/**
|
|
7
25
|
* Only GM deposits. Do not confuse with increase with zero delta size.
|
|
8
26
|
*
|
|
@@ -16,6 +34,8 @@ export declare function estimateExecuteDepositGasLimit(gasLimits: GasLimitsConfi
|
|
|
16
34
|
export declare function estimateExecuteGlvDepositGasLimit(gasLimits: GasLimitsConfig, { marketsCount, isMarketTokenDeposit, }: {
|
|
17
35
|
isMarketTokenDeposit: any;
|
|
18
36
|
marketsCount: bigint;
|
|
37
|
+
initialLongTokenAmount: bigint;
|
|
38
|
+
initialShortTokenAmount: bigint;
|
|
19
39
|
}): bigint;
|
|
20
40
|
export declare function estimateExecuteGlvWithdrawalGasLimit(gasLimits: GasLimitsConfig, { marketsCount, }: {
|
|
21
41
|
marketsCount: bigint;
|
|
@@ -34,22 +54,3 @@ export declare function estimateExecuteWithdrawalGasLimit(gasLimits: GasLimitsCo
|
|
|
34
54
|
export declare function estimateExecuteShiftGasLimit(gasLimits: GasLimitsConfig, shift: {
|
|
35
55
|
callbackGasLimit?: bigint;
|
|
36
56
|
}): bigint;
|
|
37
|
-
/**
|
|
38
|
-
* Copy from contract: `estimateExecuteIncreaseOrderGasLimit`
|
|
39
|
-
*/
|
|
40
|
-
export declare function estimateExecuteIncreaseOrderGasLimit(gasLimits: GasLimitsConfig, order: {
|
|
41
|
-
swapsCount?: number;
|
|
42
|
-
callbackGasLimit?: bigint;
|
|
43
|
-
}): bigint;
|
|
44
|
-
/**
|
|
45
|
-
* Copy from contract: `estimateExecuteDecreaseOrderGasLimit`
|
|
46
|
-
*/
|
|
47
|
-
export declare function estimateExecuteDecreaseOrderGasLimit(gasLimits: GasLimitsConfig, order: {
|
|
48
|
-
swapsCount: number;
|
|
49
|
-
callbackGasLimit?: bigint;
|
|
50
|
-
decreaseSwapType?: DecreasePositionSwapType;
|
|
51
|
-
}): bigint;
|
|
52
|
-
export declare function estimateExecuteSwapOrderGasLimit(gasLimits: GasLimitsConfig, order: {
|
|
53
|
-
swapsCount: number;
|
|
54
|
-
callbackGasLimit?: bigint;
|
|
55
|
-
}): bigint;
|
|
@@ -4,9 +4,8 @@ import { NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
|
4
4
|
import { DecreasePositionSwapType } from "../../types/orders";
|
|
5
5
|
import { applyFactor, expandDecimals } from "../numbers";
|
|
6
6
|
import { convertToUsd, getTokenData } from "../tokens";
|
|
7
|
-
export function getExecutionFee(
|
|
7
|
+
export function getExecutionFee(chainId, gasLimits, tokensData, estimatedGasLimit, gasPrice, oraclePriceCount) {
|
|
8
8
|
const nativeToken = getTokenData(tokensData, NATIVE_TOKEN_ADDRESS);
|
|
9
|
-
const chainId = sdk.chainId;
|
|
10
9
|
if (!nativeToken)
|
|
11
10
|
return undefined;
|
|
12
11
|
// #region adjustGasLimitForEstimate. Copy from contract.
|
|
@@ -23,10 +22,35 @@ export function getExecutionFee(sdk, gasLimits, tokensData, estimatedGasLimit, g
|
|
|
23
22
|
feeUsd,
|
|
24
23
|
feeTokenAmount,
|
|
25
24
|
feeToken: nativeToken,
|
|
25
|
+
gasLimit,
|
|
26
26
|
isFeeHigh,
|
|
27
27
|
isFeeVeryHigh,
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Copy from contract: `estimateExecuteIncreaseOrderGasLimit`
|
|
32
|
+
*/
|
|
33
|
+
export function estimateExecuteIncreaseOrderGasLimit(gasLimits, order) {
|
|
34
|
+
const gasPerSwap = gasLimits.singleSwap;
|
|
35
|
+
const swapsCount = BigInt(order.swapsCount ?? 0);
|
|
36
|
+
return gasLimits.increaseOrder + gasPerSwap * swapsCount + (order.callbackGasLimit ?? 0n);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Copy from contract: `estimateExecuteDecreaseOrderGasLimit`
|
|
40
|
+
*/
|
|
41
|
+
export function estimateExecuteDecreaseOrderGasLimit(gasLimits, order) {
|
|
42
|
+
const gasPerSwap = gasLimits.singleSwap;
|
|
43
|
+
let swapsCount = BigInt(order.swapsCount);
|
|
44
|
+
if (order.decreaseSwapType !== DecreasePositionSwapType.NoSwap) {
|
|
45
|
+
swapsCount += 1n;
|
|
46
|
+
}
|
|
47
|
+
return gasLimits.decreaseOrder + gasPerSwap * swapsCount + (order.callbackGasLimit ?? 0n);
|
|
48
|
+
}
|
|
49
|
+
export function estimateExecuteSwapOrderGasLimit(gasLimits, order) {
|
|
50
|
+
const gasPerSwap = gasLimits.singleSwap;
|
|
51
|
+
const swapsCount = BigInt(order.swapsCount);
|
|
52
|
+
return gasLimits.swapOrder + gasPerSwap * swapsCount + (order.callbackGasLimit ?? 0n);
|
|
53
|
+
}
|
|
30
54
|
/**
|
|
31
55
|
* Only GM deposits. Do not confuse with increase with zero delta size.
|
|
32
56
|
*
|
|
@@ -73,27 +97,3 @@ export function estimateExecuteWithdrawalGasLimit(gasLimits, withdrawal) {
|
|
|
73
97
|
export function estimateExecuteShiftGasLimit(gasLimits, shift) {
|
|
74
98
|
return gasLimits.shift + (shift.callbackGasLimit ?? 0n);
|
|
75
99
|
}
|
|
76
|
-
/**
|
|
77
|
-
* Copy from contract: `estimateExecuteIncreaseOrderGasLimit`
|
|
78
|
-
*/
|
|
79
|
-
export function estimateExecuteIncreaseOrderGasLimit(gasLimits, order) {
|
|
80
|
-
const gasPerSwap = gasLimits.singleSwap;
|
|
81
|
-
const swapsCount = BigInt(order.swapsCount ?? 0);
|
|
82
|
-
return gasLimits.increaseOrder + gasPerSwap * swapsCount + (order.callbackGasLimit ?? 0n);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Copy from contract: `estimateExecuteDecreaseOrderGasLimit`
|
|
86
|
-
*/
|
|
87
|
-
export function estimateExecuteDecreaseOrderGasLimit(gasLimits, order) {
|
|
88
|
-
const gasPerSwap = gasLimits.singleSwap;
|
|
89
|
-
let swapsCount = BigInt(order.swapsCount);
|
|
90
|
-
if (order.decreaseSwapType !== DecreasePositionSwapType.NoSwap) {
|
|
91
|
-
swapsCount += 1n;
|
|
92
|
-
}
|
|
93
|
-
return gasLimits.decreaseOrder + gasPerSwap * swapsCount + (order.callbackGasLimit ?? 0n);
|
|
94
|
-
}
|
|
95
|
-
export function estimateExecuteSwapOrderGasLimit(gasLimits, order) {
|
|
96
|
-
const gasPerSwap = gasLimits.singleSwap;
|
|
97
|
-
const swapsCount = BigInt(order.swapsCount);
|
|
98
|
-
return gasLimits.swapOrder + gasPerSwap * swapsCount + (order.callbackGasLimit ?? 0n);
|
|
99
|
-
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
3
|
+
import { getExecutionFee } from "./executionFee";
|
|
4
|
+
import { expandDecimals } from "../numbers";
|
|
5
|
+
import { ARBITRUM } from "../../configs/chains";
|
|
6
|
+
describe("getExecutionFee", () => {
|
|
7
|
+
const chainId = ARBITRUM;
|
|
8
|
+
const gasLimits = {
|
|
9
|
+
estimatedGasFeeBaseAmount: 600000n,
|
|
10
|
+
estimatedGasFeePerOraclePrice: 250000n,
|
|
11
|
+
estimatedFeeMultiplierFactor: 1000000000000000000000000000000n,
|
|
12
|
+
};
|
|
13
|
+
const tokensData = {
|
|
14
|
+
"0xAddress": {
|
|
15
|
+
decimals: 18,
|
|
16
|
+
prices: {
|
|
17
|
+
minPrice: expandDecimals(5, 18),
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
[NATIVE_TOKEN_ADDRESS]: {
|
|
21
|
+
decimals: 18,
|
|
22
|
+
prices: {
|
|
23
|
+
minPrice: expandDecimals(2, 18),
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
it("should return undefined if native token is not found", () => {
|
|
28
|
+
const result = getExecutionFee(chainId, gasLimits, {}, 0n, 0n, 0n);
|
|
29
|
+
expect(result).toBeUndefined();
|
|
30
|
+
});
|
|
31
|
+
it("should return feeUsd for native token 1-2 price", () => {
|
|
32
|
+
const result = getExecutionFee(chainId, gasLimits, tokensData, 5000000n, 2750000001n, 4n);
|
|
33
|
+
expect(result).toEqual({
|
|
34
|
+
feeUsd: 36300000013200000n,
|
|
35
|
+
feeTokenAmount: 18150000006600000n,
|
|
36
|
+
gasLimit: 6600000n,
|
|
37
|
+
feeToken: tokensData[NATIVE_TOKEN_ADDRESS],
|
|
38
|
+
isFeeHigh: false,
|
|
39
|
+
isFeeVeryHigh: false,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
it("should return isFeeHigh", () => {
|
|
43
|
+
const result = getExecutionFee(chainId, gasLimits, tokensData, 5000000n, expandDecimals(5, 23), 4n);
|
|
44
|
+
expect(result).toEqual({
|
|
45
|
+
feeUsd: 6600000000000000000000000000000n,
|
|
46
|
+
gasLimit: 6600000n,
|
|
47
|
+
feeTokenAmount: 3300000000000000000000000000000n,
|
|
48
|
+
feeToken: tokensData[NATIVE_TOKEN_ADDRESS],
|
|
49
|
+
isFeeHigh: true,
|
|
50
|
+
isFeeVeryHigh: false,
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
it("should return isFeeHigh", () => {
|
|
54
|
+
const result = getExecutionFee(chainId, gasLimits, tokensData, 5000000n, expandDecimals(1, 25), 4n);
|
|
55
|
+
expect(result).toEqual({
|
|
56
|
+
feeUsd: 132000000000000000000000000000000n,
|
|
57
|
+
feeTokenAmount: 66000000000000000000000000000000n,
|
|
58
|
+
gasLimit: 6600000n,
|
|
59
|
+
feeToken: tokensData[NATIVE_TOKEN_ADDRESS],
|
|
60
|
+
isFeeHigh: true,
|
|
61
|
+
isFeeVeryHigh: true,
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { FeeItem } from "../../types/fees";
|
|
2
2
|
import { MarketInfo } from "../../types/markets";
|
|
3
3
|
import { SwapStats } from "../../types/trade";
|
|
4
|
+
export * from "./estimateOraclePriceCount";
|
|
5
|
+
export * from "./executionFee";
|
|
4
6
|
export * from "./priceImpact";
|
|
5
7
|
export declare function getSwapFee(marketInfo: MarketInfo, swapAmount: bigint, forPositiveImpact: boolean): bigint;
|
|
6
8
|
export declare function getPositionFee(marketInfo: MarketInfo, sizeDeltaUsd: bigint, forPositiveImpact: boolean, referralInfo: {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { HIGH_PRICE_IMPACT_BPS } from "../../configs/factors";
|
|
2
2
|
import { bigMath } from "../bigmath";
|
|
3
3
|
import { applyFactor, getBasisPoints, PRECISION } from "../numbers";
|
|
4
|
+
export * from "./estimateOraclePriceCount";
|
|
5
|
+
export * from "./executionFee";
|
|
4
6
|
export * from "./priceImpact";
|
|
5
7
|
export function getSwapFee(marketInfo, swapAmount, forPositiveImpact) {
|
|
6
8
|
const factor = forPositiveImpact
|
|
@@ -29,18 +31,25 @@ export function getPositionFee(marketInfo, sizeDeltaUsd, forPositiveImpact, refe
|
|
|
29
31
|
}
|
|
30
32
|
export function getFundingFactorPerPeriod(marketInfo, isLong, periodInSeconds) {
|
|
31
33
|
const { fundingFactorPerSecond, longsPayShorts, longInterestUsd, shortInterestUsd } = marketInfo;
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
const largerInterestUsd = bigMath.max(longInterestUsd, shortInterestUsd);
|
|
35
|
+
const payingInterestUsd = longsPayShorts ? longInterestUsd : shortInterestUsd;
|
|
36
|
+
const receivingInterestUsd = longsPayShorts ? shortInterestUsd : longInterestUsd;
|
|
37
|
+
let fundingForPayingSide = 0n;
|
|
38
|
+
if (payingInterestUsd !== 0n) {
|
|
39
|
+
fundingForPayingSide = bigMath.mulDiv(fundingFactorPerSecond, largerInterestUsd, payingInterestUsd);
|
|
40
|
+
}
|
|
41
|
+
let fundingForReceivingSide = 0n;
|
|
42
|
+
if (receivingInterestUsd !== 0n) {
|
|
43
|
+
fundingForReceivingSide = bigMath.mulDiv(fundingForPayingSide, payingInterestUsd, receivingInterestUsd);
|
|
44
|
+
}
|
|
45
|
+
if ((longsPayShorts && isLong) || (!longsPayShorts && !isLong)) {
|
|
46
|
+
// paying side
|
|
47
|
+
return fundingForPayingSide * BigInt(periodInSeconds) * -1n;
|
|
36
48
|
}
|
|
37
49
|
else {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const ratio = smallerInterestUsd > 0 ? bigMath.mulDiv(largerInterestUsd, PRECISION, smallerInterestUsd) : 0n;
|
|
41
|
-
factorPerSecond = applyFactor(ratio, fundingFactorPerSecond);
|
|
50
|
+
// receiving side
|
|
51
|
+
return fundingForReceivingSide * BigInt(periodInSeconds);
|
|
42
52
|
}
|
|
43
|
-
return factorPerSecond * BigInt(periodInSeconds);
|
|
44
53
|
}
|
|
45
54
|
export function getFundingFeeRateUsd(marketInfo, isLong, sizeInUsd, periodInSeconds) {
|
|
46
55
|
const factor = getFundingFactorPerPeriod(marketInfo, isLong, periodInSeconds);
|
|
@@ -67,16 +76,19 @@ export function getFeeItem(feeDeltaUsd, basis, opts = {}) {
|
|
|
67
76
|
return {
|
|
68
77
|
deltaUsd: feeDeltaUsd,
|
|
69
78
|
bps: basis !== undefined && basis > 0 ? getBasisPoints(feeDeltaUsd, basis, shouldRoundUp) : 0n,
|
|
79
|
+
precisePercentage: basis !== undefined && basis > 0 ? bigMath.mulDiv(feeDeltaUsd, PRECISION, basis) : 0n,
|
|
70
80
|
};
|
|
71
81
|
}
|
|
72
82
|
export function getTotalFeeItem(feeItems) {
|
|
73
83
|
const totalFeeItem = {
|
|
74
84
|
deltaUsd: 0n,
|
|
75
85
|
bps: 0n,
|
|
86
|
+
precisePercentage: 0n,
|
|
76
87
|
};
|
|
77
88
|
feeItems.filter(Boolean).forEach((feeItem) => {
|
|
78
89
|
totalFeeItem.deltaUsd = totalFeeItem.deltaUsd + feeItem.deltaUsd;
|
|
79
90
|
totalFeeItem.bps = totalFeeItem.bps + feeItem.bps;
|
|
91
|
+
totalFeeItem.precisePercentage = totalFeeItem.precisePercentage + feeItem.precisePercentage;
|
|
80
92
|
});
|
|
81
93
|
return totalFeeItem;
|
|
82
94
|
}
|