@gmx-io/sdk 0.1.1-alpha-1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/configs/chains.d.ts +3 -0
- package/build/src/configs/chains.js +6 -0
- package/build/src/configs/contracts.d.ts +220 -1
- package/build/src/configs/contracts.js +67 -8
- package/build/src/configs/markets.js +14 -0
- package/build/src/configs/tokens.js +40 -5
- package/build/src/modules/markets/index.js +1 -7
- package/build/src/modules/markets/query-builders.d.ts +1 -1
- package/build/src/modules/markets/types.d.ts +4 -1
- package/build/src/modules/orders/utils.js +1 -1
- package/build/src/modules/positions/positions.js +17 -4
- 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 +16 -0
- package/build/src/prebuilt/hashedMarketConfigKeys.json +100 -0
- package/build/src/prebuilt/hashedMarketValuesKeys.json +30 -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 +42 -0
- package/build/src/utils/__tests__/bigmath.spec.d.ts +1 -0
- package/build/src/utils/__tests__/bigmath.spec.js +37 -0
- package/build/src/utils/__tests__/hash.spec.d.ts +1 -0
- package/build/src/utils/__tests__/hash.spec.js +40 -0
- package/build/src/utils/__tests__/markets.spec.d.ts +1 -0
- package/build/src/utils/__tests__/markets.spec.js +264 -0
- package/build/src/utils/__tests__/numbers.spec.d.ts +1 -0
- package/build/src/utils/__tests__/numbers.spec.js +56 -0
- package/build/src/utils/__tests__/objects.spec.d.ts +1 -0
- package/build/src/utils/__tests__/objects.spec.js +58 -0
- package/build/src/utils/__tests__/positions.spec.d.ts +1 -0
- package/build/src/utils/__tests__/positions.spec.js +177 -0
- package/build/src/utils/__tests__/prices.spec.d.ts +1 -0
- package/build/src/utils/__tests__/prices.spec.js +58 -0
- package/build/src/utils/__tests__/referrals.spec.d.ts +1 -0
- package/build/src/utils/__tests__/referrals.spec.js +17 -0
- package/build/src/utils/__tests__/subgraph.spec.d.ts +1 -0
- package/build/src/utils/__tests__/subgraph.spec.js +162 -0
- package/build/src/utils/__tests__/tokens.spec.d.ts +1 -0
- package/build/src/utils/__tests__/tokens.spec.js +82 -0
- package/build/src/utils/__tests__/trade.spec.d.ts +1 -0
- package/build/src/utils/__tests__/trade.spec.js +35 -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/contracts.d.ts +4 -0
- package/build/src/utils/contracts.js +2 -0
- package/build/src/utils/fees/executionFee.d.ts +1 -33
- package/build/src/utils/fees/executionFee.js +2 -48
- 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 +5 -0
- package/build/src/utils/markets.d.ts +9 -2
- package/build/src/utils/markets.js +33 -10
- 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 +5 -5
- 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 +2 -3
- 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,162 @@
|
|
|
1
|
+
import { buildFiltersBody } from "../subgraph";
|
|
2
|
+
describe("buildFiltersBody", () => {
|
|
3
|
+
it("should return empty object if no filters", () => {
|
|
4
|
+
const input = {};
|
|
5
|
+
const result = buildFiltersBody(input);
|
|
6
|
+
expect(result).toEqual("{}");
|
|
7
|
+
});
|
|
8
|
+
it("should return correct filter string with single filter", () => {
|
|
9
|
+
const input = {
|
|
10
|
+
foo: "bar",
|
|
11
|
+
};
|
|
12
|
+
const result = buildFiltersBody(input);
|
|
13
|
+
expect(result).toEqual('{foo:"bar"}');
|
|
14
|
+
});
|
|
15
|
+
it("should return correct filter string with multiple filters", () => {
|
|
16
|
+
const input = {
|
|
17
|
+
foo: "bar",
|
|
18
|
+
baz: "qux",
|
|
19
|
+
};
|
|
20
|
+
const result = buildFiltersBody(input);
|
|
21
|
+
expect(result).toEqual('{foo:"bar",baz:"qux"}');
|
|
22
|
+
});
|
|
23
|
+
it("should return correct filter string with nested filter", () => {
|
|
24
|
+
const input = {
|
|
25
|
+
foo: {
|
|
26
|
+
bar: {
|
|
27
|
+
baz: "qux",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const result = buildFiltersBody(input);
|
|
32
|
+
expect(result).toEqual('{foo_:{bar_:{baz:"qux"}}}');
|
|
33
|
+
});
|
|
34
|
+
it("should return correct filter string with or filter", () => {
|
|
35
|
+
const input = {
|
|
36
|
+
or: [
|
|
37
|
+
{
|
|
38
|
+
foo: "bar",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
baz: "qux",
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
const result = buildFiltersBody(input);
|
|
46
|
+
expect(result).toEqual('{or:[{foo:"bar"},{baz:"qux"}]}');
|
|
47
|
+
});
|
|
48
|
+
it("should return correct filter string with and filter", () => {
|
|
49
|
+
const input = {
|
|
50
|
+
and: [
|
|
51
|
+
{
|
|
52
|
+
foo: "bar",
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
baz: "qux",
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
const result = buildFiltersBody(input);
|
|
60
|
+
expect(result).toEqual('{and:[{foo:"bar"},{baz:"qux"}]}');
|
|
61
|
+
});
|
|
62
|
+
it("should strip out undefined filters", () => {
|
|
63
|
+
const input = {
|
|
64
|
+
foo: "bar",
|
|
65
|
+
baz: undefined,
|
|
66
|
+
};
|
|
67
|
+
const result = buildFiltersBody(input);
|
|
68
|
+
expect(result).toEqual('{foo:"bar"}');
|
|
69
|
+
});
|
|
70
|
+
it("should strip out empty or", () => {
|
|
71
|
+
const input = {
|
|
72
|
+
or: [],
|
|
73
|
+
};
|
|
74
|
+
const result = buildFiltersBody(input);
|
|
75
|
+
expect(result).toEqual("{}");
|
|
76
|
+
});
|
|
77
|
+
it("should strip out or with empty", () => {
|
|
78
|
+
const input = {
|
|
79
|
+
or: [
|
|
80
|
+
{},
|
|
81
|
+
{
|
|
82
|
+
foo: undefined,
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
const result = buildFiltersBody(input);
|
|
87
|
+
expect(result).toEqual("{}");
|
|
88
|
+
});
|
|
89
|
+
it("should throw error if or is mixed with other filters", () => {
|
|
90
|
+
const input = {
|
|
91
|
+
or: [
|
|
92
|
+
{
|
|
93
|
+
foo: "bar",
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
// @ts-expect-error
|
|
97
|
+
baz: "qux",
|
|
98
|
+
};
|
|
99
|
+
const getResult = () => buildFiltersBody(input);
|
|
100
|
+
expect(getResult).toThrowError();
|
|
101
|
+
});
|
|
102
|
+
it("should throw error if and is mixed with other filters", () => {
|
|
103
|
+
const input = {
|
|
104
|
+
and: [
|
|
105
|
+
{
|
|
106
|
+
foo: "bar",
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
// @ts-expect-error
|
|
110
|
+
baz: "qux",
|
|
111
|
+
};
|
|
112
|
+
const getResult = () => buildFiltersBody(input);
|
|
113
|
+
expect(getResult).toThrowError();
|
|
114
|
+
});
|
|
115
|
+
it("should throw not error if empty or is mixed with other filters", () => {
|
|
116
|
+
const input = {
|
|
117
|
+
or: [],
|
|
118
|
+
// @ts-expect-error
|
|
119
|
+
baz: "qux",
|
|
120
|
+
};
|
|
121
|
+
const getResult = () => buildFiltersBody(input);
|
|
122
|
+
expect(getResult).not.toThrowError();
|
|
123
|
+
});
|
|
124
|
+
it("should throw not error if empty and is mixed with other filters", () => {
|
|
125
|
+
const input = {
|
|
126
|
+
and: [],
|
|
127
|
+
// @ts-expect-error
|
|
128
|
+
baz: "qux",
|
|
129
|
+
};
|
|
130
|
+
const getResult = () => buildFiltersBody(input);
|
|
131
|
+
expect(getResult).not.toThrowError();
|
|
132
|
+
});
|
|
133
|
+
it("should format string values correctly", () => {
|
|
134
|
+
const input = {
|
|
135
|
+
foo: "bar",
|
|
136
|
+
};
|
|
137
|
+
const result = buildFiltersBody(input);
|
|
138
|
+
expect(result).toEqual('{foo:"bar"}');
|
|
139
|
+
});
|
|
140
|
+
it("should format number values correctly", () => {
|
|
141
|
+
const input = {
|
|
142
|
+
foo: 123,
|
|
143
|
+
};
|
|
144
|
+
const result = buildFiltersBody(input);
|
|
145
|
+
expect(result).toEqual("{foo:123}");
|
|
146
|
+
});
|
|
147
|
+
it("should format boolean values correctly", () => {
|
|
148
|
+
const input = {
|
|
149
|
+
foo: true,
|
|
150
|
+
bar: false,
|
|
151
|
+
};
|
|
152
|
+
const result = buildFiltersBody(input);
|
|
153
|
+
expect(result).toEqual("{foo:true,bar:false}");
|
|
154
|
+
});
|
|
155
|
+
it("should format null values correctly", () => {
|
|
156
|
+
const input = {
|
|
157
|
+
foo: null,
|
|
158
|
+
};
|
|
159
|
+
const result = buildFiltersBody(input);
|
|
160
|
+
expect(result).toEqual("{foo:null}");
|
|
161
|
+
});
|
|
162
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { parseContractPrice, convertToContractPrice, convertToContractTokenPrices, convertToTokenAmount, convertToUsd, getMidPrice, getIsEquivalentTokens, getTokenData, getTokensRatioByAmounts, } from "../tokens";
|
|
2
|
+
import { expandDecimals, PRECISION } from "../numbers";
|
|
3
|
+
import { TOKENS } from "../../configs/tokens";
|
|
4
|
+
import { ARBITRUM } from "../../configs/chains";
|
|
5
|
+
function getToken(symbol) {
|
|
6
|
+
return TOKENS[ARBITRUM].find((token) => token.symbol === symbol);
|
|
7
|
+
}
|
|
8
|
+
describe("parseContractPrice", () => {
|
|
9
|
+
it("multiplies price by 10^decimals", () => {
|
|
10
|
+
expect(parseContractPrice(100n, 2)).toBe(100n * expandDecimals(1, 2));
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
describe("convertToContractPrice", () => {
|
|
14
|
+
it("divides price by 10^decimals", () => {
|
|
15
|
+
expect(convertToContractPrice(10000n, 2)).toBe(10000n / expandDecimals(1, 2));
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
describe("convertToContractTokenPrices", () => {
|
|
19
|
+
it("returns min and max contract prices", () => {
|
|
20
|
+
const result = convertToContractTokenPrices({ minPrice: 1000n, maxPrice: 2000n }, 2);
|
|
21
|
+
expect(result.min).toBe(1000n / expandDecimals(1, 2));
|
|
22
|
+
expect(result.max).toBe(2000n / expandDecimals(1, 2));
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
describe("convertToTokenAmount", () => {
|
|
26
|
+
it("returns undefined if inputs are invalid", () => {
|
|
27
|
+
expect(convertToTokenAmount(undefined, 18, 100n)).toBeUndefined();
|
|
28
|
+
expect(convertToTokenAmount(1000n, undefined, 100n)).toBeUndefined();
|
|
29
|
+
expect(convertToTokenAmount(1000n, 18, 0n)).toBeUndefined();
|
|
30
|
+
});
|
|
31
|
+
it("converts usd to token amount", () => {
|
|
32
|
+
expect(convertToTokenAmount(1000n, 2, 100n)).toBe((1000n * expandDecimals(1, 2)) / 100n);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe("convertToUsd", () => {
|
|
36
|
+
it("returns undefined if inputs are invalid", () => {
|
|
37
|
+
expect(convertToUsd(undefined, 18, 100n)).toBeUndefined();
|
|
38
|
+
expect(convertToUsd(1000n, undefined, 100n)).toBeUndefined();
|
|
39
|
+
});
|
|
40
|
+
it("converts token amount to usd", () => {
|
|
41
|
+
expect(convertToUsd(1000n, 2, 100n)).toBe((1000n * 100n) / expandDecimals(1, 2));
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe("getMidPrice", () => {
|
|
45
|
+
it("returns the average of min and max price", () => {
|
|
46
|
+
expect(getMidPrice({ minPrice: 10n, maxPrice: 20n })).toBe(15n);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe("getIsEquivalentTokens", () => {
|
|
50
|
+
it("checks address, wrappedAddress, synthetic, and symbol", () => {
|
|
51
|
+
expect(getIsEquivalentTokens(getToken("ETH"), getToken("WETH"))).toBe(true);
|
|
52
|
+
expect(getIsEquivalentTokens(getToken("ETH"), getToken("ETH"))).toBe(true);
|
|
53
|
+
expect(getIsEquivalentTokens({ address: "0xA", isSynthetic: true, symbol: "SYN" }, { address: "0xB", isSynthetic: true, symbol: "SYN" })).toBe(true);
|
|
54
|
+
expect(getIsEquivalentTokens(getToken("ETH"), getToken("BTC"))).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe("getTokenData", () => {
|
|
58
|
+
it("returns undefined if no token data", () => {
|
|
59
|
+
expect(getTokenData()).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
it("returns wrapped if convertTo=wrapped and token isNative", () => {
|
|
62
|
+
const tokensData = {
|
|
63
|
+
"0xnative": { address: "0xnative", isNative: true, wrappedAddress: "0xwrap" },
|
|
64
|
+
"0xwrap": { address: "0xwrap", isWrapped: true },
|
|
65
|
+
};
|
|
66
|
+
expect(getTokenData(tokensData, "0xnative", "wrapped")).toEqual(tokensData["0xwrap"]);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
describe("getTokensRatioByAmounts", () => {
|
|
70
|
+
it("returns ratio of two token amounts", () => {
|
|
71
|
+
const fromToken = { decimals: 2 };
|
|
72
|
+
const toToken = { decimals: 2 };
|
|
73
|
+
const result = getTokensRatioByAmounts({
|
|
74
|
+
fromToken,
|
|
75
|
+
toToken,
|
|
76
|
+
fromTokenAmount: 1000n,
|
|
77
|
+
toTokenAmount: 500n,
|
|
78
|
+
});
|
|
79
|
+
expect(result.largestToken).toEqual(fromToken);
|
|
80
|
+
expect(result.ratio).toBe((((1000n * PRECISION) / expandDecimals(1, 2)) * PRECISION) / ((500n * PRECISION) / expandDecimals(1, 2)));
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { applySlippageToMinOut, applySlippageToPrice } from "../trade";
|
|
2
|
+
describe("applySlippageToPrice", () => {
|
|
3
|
+
it("applies positive slippage if getShouldUseMaxPrice is true", () => {
|
|
4
|
+
// isIncrease=true and isLong=true => getShouldUseMaxPrice => true
|
|
5
|
+
const allowedSlippage = 100; // 1%
|
|
6
|
+
const price = 1000n;
|
|
7
|
+
const result = applySlippageToPrice(allowedSlippage, price, true, true);
|
|
8
|
+
// expected: price * (10000+100)/10000 = 1000n * 10100n / 10000n = 1010n
|
|
9
|
+
expect(result).toBe(1010n);
|
|
10
|
+
});
|
|
11
|
+
it("applies negative slippage if getShouldUseMaxPrice is false", () => {
|
|
12
|
+
// isIncrease=true or isLong=false => getShouldUseMaxPrice => false
|
|
13
|
+
const allowedSlippage = 100; // 1%
|
|
14
|
+
const price = 1000n;
|
|
15
|
+
const result = applySlippageToPrice(allowedSlippage, price, true, false);
|
|
16
|
+
// expected: price * (10000-100)/10000 = 1000n * 9900n / 10000n = 990n
|
|
17
|
+
expect(result).toBe(990n);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
describe("applySlippageToMinOut", () => {
|
|
21
|
+
it("reduces minOutputAmount by allowed slippage", () => {
|
|
22
|
+
const allowedSlippage = 100; // 1%
|
|
23
|
+
const minOutputAmount = 10000n;
|
|
24
|
+
const result = applySlippageToMinOut(allowedSlippage, minOutputAmount);
|
|
25
|
+
// expected: minOut * (10000 - 100) / 10000 = 10_000n * 9900n / 10000n = 9900n
|
|
26
|
+
expect(result).toBe(9900n);
|
|
27
|
+
});
|
|
28
|
+
it("does nothing if slippage is zero", () => {
|
|
29
|
+
const allowedSlippage = 0;
|
|
30
|
+
const minOutputAmount = 10000n;
|
|
31
|
+
const result = applySlippageToMinOut(allowedSlippage, minOutputAmount);
|
|
32
|
+
// expected: 10_000n * (10000 - 0) / 10000n = 10_000n
|
|
33
|
+
expect(result).toBe(10000n);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -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
|
}
|
|
@@ -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,39 +1,7 @@
|
|
|
1
1
|
import { ExecutionFee, GasLimitsConfig } from "../../types/fees";
|
|
2
2
|
import { DecreasePositionSwapType } from "../../types/orders";
|
|
3
3
|
import { TokensData } from "../../types/tokens";
|
|
4
|
-
|
|
5
|
-
export declare function getExecutionFee(sdk: GmxSdk, gasLimits: GasLimitsConfig, tokensData: TokensData, estimatedGasLimit: bigint, gasPrice: bigint, oraclePriceCount: bigint): ExecutionFee | undefined;
|
|
6
|
-
/**
|
|
7
|
-
* Only GM deposits. Do not confuse with increase with zero delta size.
|
|
8
|
-
*
|
|
9
|
-
* Copy from contract: `estimateExecuteDepositGasLimit`
|
|
10
|
-
*/
|
|
11
|
-
export declare function estimateExecuteDepositGasLimit(gasLimits: GasLimitsConfig, deposit: {
|
|
12
|
-
longTokenSwapsCount?: number;
|
|
13
|
-
shortTokenSwapsCount?: number;
|
|
14
|
-
callbackGasLimit?: bigint;
|
|
15
|
-
}): bigint;
|
|
16
|
-
export declare function estimateExecuteGlvDepositGasLimit(gasLimits: GasLimitsConfig, { marketsCount, isMarketTokenDeposit, }: {
|
|
17
|
-
isMarketTokenDeposit: any;
|
|
18
|
-
marketsCount: bigint;
|
|
19
|
-
}): bigint;
|
|
20
|
-
export declare function estimateExecuteGlvWithdrawalGasLimit(gasLimits: GasLimitsConfig, { marketsCount, }: {
|
|
21
|
-
marketsCount: bigint;
|
|
22
|
-
}): bigint;
|
|
23
|
-
/**
|
|
24
|
-
* Only GM withdrawals. Do not confuse with decrease with zero delta size.
|
|
25
|
-
*
|
|
26
|
-
* Copy from contract: `estimateExecuteWithdrawalGasLimit`
|
|
27
|
-
*/
|
|
28
|
-
export declare function estimateExecuteWithdrawalGasLimit(gasLimits: GasLimitsConfig, withdrawal: {
|
|
29
|
-
callbackGasLimit?: bigint;
|
|
30
|
-
}): bigint;
|
|
31
|
-
/**
|
|
32
|
-
* Copy from contract: `estimateExecuteShiftGasLimit`
|
|
33
|
-
*/
|
|
34
|
-
export declare function estimateExecuteShiftGasLimit(gasLimits: GasLimitsConfig, shift: {
|
|
35
|
-
callbackGasLimit?: bigint;
|
|
36
|
-
}): bigint;
|
|
4
|
+
export declare function getExecutionFee(chainId: number, gasLimits: GasLimitsConfig, tokensData: TokensData, estimatedGasLimit: bigint, gasPrice: bigint, oraclePriceCount: bigint): ExecutionFee | undefined;
|
|
37
5
|
/**
|
|
38
6
|
* Copy from contract: `estimateExecuteIncreaseOrderGasLimit`
|
|
39
7
|
*/
|
|
@@ -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,56 +22,11 @@ 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
|
-
* Only GM deposits. Do not confuse with increase with zero delta size.
|
|
32
|
-
*
|
|
33
|
-
* Copy from contract: `estimateExecuteDepositGasLimit`
|
|
34
|
-
*/
|
|
35
|
-
export function estimateExecuteDepositGasLimit(gasLimits, deposit) {
|
|
36
|
-
const gasPerSwap = gasLimits.singleSwap;
|
|
37
|
-
const swapsCount = BigInt((deposit.longTokenSwapsCount ?? 0) + (deposit.shortTokenSwapsCount ?? 0));
|
|
38
|
-
const gasForSwaps = swapsCount * gasPerSwap;
|
|
39
|
-
return gasLimits.depositToken + (deposit.callbackGasLimit ?? 0n) + gasForSwaps;
|
|
40
|
-
}
|
|
41
|
-
export function estimateExecuteGlvDepositGasLimit(gasLimits, { marketsCount, isMarketTokenDeposit, }) {
|
|
42
|
-
const gasPerGlvPerMarket = gasLimits.glvPerMarketGasLimit;
|
|
43
|
-
const gasForGlvMarkets = gasPerGlvPerMarket * marketsCount;
|
|
44
|
-
const glvDepositGasLimit = gasLimits.glvDepositGasLimit;
|
|
45
|
-
const gasLimit = glvDepositGasLimit + gasForGlvMarkets;
|
|
46
|
-
if (isMarketTokenDeposit) {
|
|
47
|
-
return gasLimit;
|
|
48
|
-
}
|
|
49
|
-
return gasLimit + gasLimits.depositToken;
|
|
50
|
-
}
|
|
51
|
-
export function estimateExecuteGlvWithdrawalGasLimit(gasLimits, { marketsCount, }) {
|
|
52
|
-
const gasPerGlvPerMarket = gasLimits.glvPerMarketGasLimit;
|
|
53
|
-
const gasForGlvMarkets = gasPerGlvPerMarket * marketsCount;
|
|
54
|
-
const glvWithdrawalGasLimit = gasLimits.glvWithdrawalGasLimit;
|
|
55
|
-
const gasLimit = glvWithdrawalGasLimit + gasForGlvMarkets;
|
|
56
|
-
return gasLimit + gasLimits.withdrawalMultiToken;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Only GM withdrawals. Do not confuse with decrease with zero delta size.
|
|
60
|
-
*
|
|
61
|
-
* Copy from contract: `estimateExecuteWithdrawalGasLimit`
|
|
62
|
-
*/
|
|
63
|
-
export function estimateExecuteWithdrawalGasLimit(gasLimits, withdrawal) {
|
|
64
|
-
// Swap is not used but supported in the contract.
|
|
65
|
-
// const gasPerSwap = gasLimits.singleSwap;
|
|
66
|
-
// const swapsCount = 0n;
|
|
67
|
-
// const gasForSwaps = swapsCount * gasPerSwap;
|
|
68
|
-
return gasLimits.withdrawalMultiToken + (withdrawal.callbackGasLimit ?? 0n);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Copy from contract: `estimateExecuteShiftGasLimit`
|
|
72
|
-
*/
|
|
73
|
-
export function estimateExecuteShiftGasLimit(gasLimits, shift) {
|
|
74
|
-
return gasLimits.shift + (shift.callbackGasLimit ?? 0n);
|
|
75
|
-
}
|
|
76
30
|
/**
|
|
77
31
|
* Copy from contract: `estimateExecuteIncreaseOrderGasLimit`
|
|
78
32
|
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { NATIVE_TOKEN_ADDRESS } from "../../configs/tokens";
|
|
2
|
+
import { getExecutionFee } from "./executionFee";
|
|
3
|
+
import { expandDecimals } from "../numbers";
|
|
4
|
+
import { ARBITRUM } from "../../configs/chains";
|
|
5
|
+
const serializeBigInt = (e) => JSON.stringify(e, (t, e) => (typeof e === "bigint" ? e.toString() : e), 2);
|
|
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
|
+
});
|
|
@@ -2,6 +2,8 @@ import { FeeItem } from "../../types/fees";
|
|
|
2
2
|
import { MarketInfo } from "../../types/markets";
|
|
3
3
|
import { SwapStats } from "../../types/trade";
|
|
4
4
|
export * from "./priceImpact";
|
|
5
|
+
export * from "./estimateOraclePriceCount";
|
|
6
|
+
export * from "./executionFee";
|
|
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: {
|
|
7
9
|
totalRebateFactor: bigint;
|
|
@@ -2,6 +2,8 @@ import { HIGH_PRICE_IMPACT_BPS } from "../../configs/factors";
|
|
|
2
2
|
import { bigMath } from "../bigmath";
|
|
3
3
|
import { applyFactor, getBasisPoints, PRECISION } from "../numbers";
|
|
4
4
|
export * from "./priceImpact";
|
|
5
|
+
export * from "./estimateOraclePriceCount";
|
|
6
|
+
export * from "./executionFee";
|
|
5
7
|
export function getSwapFee(marketInfo, swapAmount, forPositiveImpact) {
|
|
6
8
|
const factor = forPositiveImpact
|
|
7
9
|
? marketInfo.swapFeeFactorForPositiveImpact
|
|
@@ -67,16 +69,19 @@ export function getFeeItem(feeDeltaUsd, basis, opts = {}) {
|
|
|
67
69
|
return {
|
|
68
70
|
deltaUsd: feeDeltaUsd,
|
|
69
71
|
bps: basis !== undefined && basis > 0 ? getBasisPoints(feeDeltaUsd, basis, shouldRoundUp) : 0n,
|
|
72
|
+
precisePercentage: basis !== undefined && basis > 0 ? bigMath.mulDiv(feeDeltaUsd, PRECISION, basis) : 0n,
|
|
70
73
|
};
|
|
71
74
|
}
|
|
72
75
|
export function getTotalFeeItem(feeItems) {
|
|
73
76
|
const totalFeeItem = {
|
|
74
77
|
deltaUsd: 0n,
|
|
75
78
|
bps: 0n,
|
|
79
|
+
precisePercentage: 0n,
|
|
76
80
|
};
|
|
77
81
|
feeItems.filter(Boolean).forEach((feeItem) => {
|
|
78
82
|
totalFeeItem.deltaUsd = totalFeeItem.deltaUsd + feeItem.deltaUsd;
|
|
79
83
|
totalFeeItem.bps = totalFeeItem.bps + feeItem.bps;
|
|
84
|
+
totalFeeItem.precisePercentage = totalFeeItem.precisePercentage + feeItem.precisePercentage;
|
|
80
85
|
});
|
|
81
86
|
return totalFeeItem;
|
|
82
87
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ContractMarketPrices, Market, MarketInfo } from "../types/markets";
|
|
2
|
-
import { Token, TokensData } from "../types/tokens";
|
|
2
|
+
import { Token, TokenPrices, TokensData } from "../types/tokens";
|
|
3
3
|
export declare function getMarketFullName(p: {
|
|
4
4
|
longToken: Token;
|
|
5
5
|
shortToken: Token;
|
|
@@ -18,6 +18,9 @@ export declare function getMarketPoolName(p: {
|
|
|
18
18
|
shortToken: Token;
|
|
19
19
|
}): string;
|
|
20
20
|
export declare function getContractMarketPrices(tokensData: TokensData, market: Market): ContractMarketPrices | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Apart from usual cases, returns `long` for single token backed markets.
|
|
23
|
+
*/
|
|
21
24
|
export declare function getTokenPoolType(marketInfo: {
|
|
22
25
|
longToken: Token;
|
|
23
26
|
shortToken: Token;
|
|
@@ -26,8 +29,8 @@ export declare function getPoolUsdWithoutPnl(marketInfo: MarketInfo, isLong: boo
|
|
|
26
29
|
export declare function getCappedPoolPnl(p: {
|
|
27
30
|
marketInfo: MarketInfo;
|
|
28
31
|
poolUsd: bigint;
|
|
32
|
+
poolPnl: bigint;
|
|
29
33
|
isLong: boolean;
|
|
30
|
-
maximize: boolean;
|
|
31
34
|
}): bigint;
|
|
32
35
|
export declare function getMaxLeverageByMinCollateralFactor(minCollateralFactor: bigint | undefined): number;
|
|
33
36
|
export declare function getMaxAllowedLeverageByMinCollateralFactor(minCollateralFactor: bigint | undefined): number;
|
|
@@ -38,3 +41,7 @@ export declare function getMarketDivisor({ longTokenAddress, shortTokenAddress,
|
|
|
38
41
|
longTokenAddress: string;
|
|
39
42
|
shortTokenAddress: string;
|
|
40
43
|
}): 1n | 2n;
|
|
44
|
+
export declare function getMarketPnl(marketInfo: MarketInfo, isLong: boolean, forMaxPoolValue: boolean): bigint;
|
|
45
|
+
export declare function getOpenInterestUsd(marketInfo: MarketInfo, isLong: boolean): bigint;
|
|
46
|
+
export declare function getOpenInterestInTokens(marketInfo: MarketInfo, isLong: boolean): bigint;
|
|
47
|
+
export declare function getPriceForPnl(prices: TokenPrices, isLong: boolean, maximize: boolean): bigint;
|