@gmx-io/sdk 1.0.0-alpha.1 → 1.0.0-alpha.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/cjs/src/modules/trades/trades.js +27 -156
- package/build/cjs/src/utils/__tests__/subgraph.spec.js +0 -4
- package/build/cjs/src/utils/fees/priceImpact.js +2 -2
- package/build/cjs/src/utils/subgraph.js +16 -6
- package/build/cjs/src/utils/testUtil.js +1 -2
- package/build/cjs/src/utils/tradeHistory.js +137 -0
- package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/build/esm/src/modules/trades/trades.js +25 -153
- package/build/esm/src/utils/__tests__/subgraph.spec.js +0 -4
- package/build/esm/src/utils/fees/priceImpact.js +1 -1
- package/build/esm/src/utils/subgraph.js +16 -6
- package/build/esm/src/utils/testUtil.js +1 -2
- package/build/esm/src/utils/tradeHistory.js +132 -0
- package/build/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/build/src/abis/index.d.ts +20 -1
- package/build/src/modules/markets/query-builders.d.ts +1 -1
- package/build/src/modules/tokens/tokens.d.ts +2 -2
- package/build/src/modules/trades/trades.d.ts +2 -3
- package/build/src/types/sdk.d.ts +0 -2
- package/build/src/types/tradeHistory.d.ts +3 -3
- package/build/src/utils/subgraph.d.ts +7 -5
- package/build/src/utils/tradeHistory.d.ts +7 -0
- package/package.json +1 -1
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import merge from "lodash/merge";
|
|
2
|
-
import { getAddress } from "viem";
|
|
3
2
|
import { getWrappedToken } from "../../configs/tokens";
|
|
4
3
|
import { OrderType } from "../../types/orders";
|
|
5
4
|
import { TradeActionType } from "../../types/tradeHistory";
|
|
6
5
|
import graphqlFetcher from "../../utils/graphqlFetcher";
|
|
7
|
-
import { getByKey } from "../../utils/objects";
|
|
8
6
|
import { isIncreaseOrderType, isLimitOrderType, isSwapOrderType, isTriggerDecreaseOrderType } from "../../utils/orders";
|
|
9
7
|
import { buildFiltersBody } from "../../utils/subgraph";
|
|
10
8
|
import { getSwapPathOutputAddresses } from "../../utils/swap/swapStats";
|
|
11
|
-
import {
|
|
9
|
+
import { createRawTradeActionTransformer } from "../../utils/tradeHistory";
|
|
12
10
|
import { Module } from "../base";
|
|
13
11
|
export class Trades extends Module {
|
|
14
12
|
async getTradeHistory(p) {
|
|
@@ -31,13 +29,13 @@ export class Trades extends Module {
|
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
31
|
export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFilter = [], forAllAccounts, account, fromTxTimestamp, toTxTimestamp, orderEventCombinations, marketsInfoData, tokensData, }) {
|
|
34
|
-
const endpoint = sdk.config.
|
|
32
|
+
const endpoint = sdk.config.subsquidUrl;
|
|
35
33
|
const chainId = sdk.chainId;
|
|
36
34
|
if (!endpoint) {
|
|
37
35
|
return [];
|
|
38
36
|
}
|
|
39
|
-
const
|
|
40
|
-
const
|
|
37
|
+
const offset = pageIndex * pageSize;
|
|
38
|
+
const limit = pageSize;
|
|
41
39
|
const nonSwapRelevantDefinedFiltersLowercased = marketsDirectionsFilter
|
|
42
40
|
.filter((filter) => filter.direction !== "swap" && filter.marketAddress !== "any")
|
|
43
41
|
.map((filter) => ({
|
|
@@ -58,40 +56,38 @@ export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirec
|
|
|
58
56
|
.map((filter) => filter.marketAddress.toLowerCase());
|
|
59
57
|
const hasSwapRelevantDefinedMarkets = swapRelevantDefinedMarketsLowercased.length > 0;
|
|
60
58
|
const filtersStr = buildFiltersBody({
|
|
61
|
-
|
|
59
|
+
AND: [
|
|
62
60
|
{
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
timestamp_lte: toTxTimestamp,
|
|
67
|
-
},
|
|
61
|
+
account_eq: forAllAccounts ? undefined : account,
|
|
62
|
+
timestamp_gte: fromTxTimestamp,
|
|
63
|
+
timestamp_lte: toTxTimestamp,
|
|
68
64
|
},
|
|
69
65
|
{
|
|
70
|
-
|
|
66
|
+
OR: !hasPureDirectionFilters
|
|
71
67
|
? undefined
|
|
72
68
|
: pureDirectionFilters.map((filter) => filter.direction === "swap"
|
|
73
69
|
? {
|
|
74
70
|
orderType_in: [OrderType.LimitSwap, OrderType.MarketSwap],
|
|
75
71
|
}
|
|
76
72
|
: {
|
|
77
|
-
|
|
73
|
+
isLong_eq: filter.direction === "long",
|
|
78
74
|
orderType_not_in: [OrderType.LimitSwap, OrderType.MarketSwap],
|
|
79
75
|
}),
|
|
80
76
|
},
|
|
81
77
|
{
|
|
82
|
-
|
|
78
|
+
OR: [
|
|
83
79
|
// For non-swap orders
|
|
84
80
|
{
|
|
85
|
-
|
|
81
|
+
AND: !hasNonSwapRelevantDefinedMarkets
|
|
86
82
|
? undefined
|
|
87
83
|
: [
|
|
88
84
|
{
|
|
89
85
|
orderType_not_in: [OrderType.LimitSwap, OrderType.MarketSwap],
|
|
90
86
|
},
|
|
91
87
|
{
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
88
|
+
OR: nonSwapRelevantDefinedFiltersLowercased.map((filter) => ({
|
|
89
|
+
marketAddress_eq: filter.marketAddress === "any" ? undefined : filter.marketAddress,
|
|
90
|
+
isLong_eq: filter.direction === "any" ? undefined : filter.direction === "long",
|
|
95
91
|
// Collateral filtering is done outside of graphql on the client
|
|
96
92
|
})),
|
|
97
93
|
},
|
|
@@ -99,14 +95,14 @@ export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirec
|
|
|
99
95
|
},
|
|
100
96
|
// For defined markets on swap orders
|
|
101
97
|
{
|
|
102
|
-
|
|
98
|
+
AND: !hasSwapRelevantDefinedMarkets
|
|
103
99
|
? undefined
|
|
104
100
|
: [
|
|
105
101
|
{
|
|
106
102
|
orderType_in: [OrderType.LimitSwap, OrderType.MarketSwap],
|
|
107
103
|
},
|
|
108
104
|
{
|
|
109
|
-
|
|
105
|
+
OR: [
|
|
110
106
|
// Source token is not in swap path so we add it to the or filter
|
|
111
107
|
{
|
|
112
108
|
marketAddress_in: swapRelevantDefinedMarketsLowercased,
|
|
@@ -120,7 +116,7 @@ export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirec
|
|
|
120
116
|
],
|
|
121
117
|
},
|
|
122
118
|
{
|
|
123
|
-
|
|
119
|
+
OR: orderEventCombinations?.map((combination) => {
|
|
124
120
|
let sizeDeltaUsdCondition = {};
|
|
125
121
|
if (combination.orderType !== undefined &&
|
|
126
122
|
[OrderType.MarketDecrease, OrderType.MarketIncrease].includes(combination.orderType)) {
|
|
@@ -132,25 +128,24 @@ export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirec
|
|
|
132
128
|
}
|
|
133
129
|
}
|
|
134
130
|
return merge({
|
|
135
|
-
|
|
136
|
-
|
|
131
|
+
eventName_eq: combination.eventName,
|
|
132
|
+
orderType_eq: combination.orderType,
|
|
137
133
|
}, sizeDeltaUsdCondition);
|
|
138
134
|
}),
|
|
139
135
|
},
|
|
140
136
|
{
|
|
141
137
|
// We do not show create liquidation orders in the trade history, thus we filter it out
|
|
142
138
|
// ... && not (liquidation && orderCreated) === ... && (not liquidation || not orderCreated)
|
|
143
|
-
|
|
139
|
+
OR: [{ orderType_not_eq: OrderType.Liquidation }, { eventName_not_eq: TradeActionType.OrderCreated }],
|
|
144
140
|
},
|
|
145
141
|
],
|
|
146
142
|
});
|
|
147
143
|
const whereClause = `where: ${filtersStr}`;
|
|
148
144
|
const query = `{
|
|
149
145
|
tradeActions(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
orderBy:
|
|
153
|
-
orderDirection: desc,
|
|
146
|
+
offset: ${offset},
|
|
147
|
+
limit: ${limit},
|
|
148
|
+
orderBy: transaction_timestamp_DESC,
|
|
154
149
|
${whereClause}
|
|
155
150
|
) {
|
|
156
151
|
id
|
|
@@ -190,6 +185,7 @@ export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirec
|
|
|
190
185
|
|
|
191
186
|
reason
|
|
192
187
|
reasonBytes
|
|
188
|
+
timestamp
|
|
193
189
|
|
|
194
190
|
transaction {
|
|
195
191
|
timestamp
|
|
@@ -260,127 +256,3 @@ export async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirec
|
|
|
260
256
|
}
|
|
261
257
|
return tradeActions;
|
|
262
258
|
}
|
|
263
|
-
function createRawTradeActionTransformer(marketsInfoData, wrappedToken, tokensData) {
|
|
264
|
-
return (rawAction) => {
|
|
265
|
-
const orderType = Number(rawAction.orderType);
|
|
266
|
-
if (isSwapOrderType(orderType)) {
|
|
267
|
-
const initialCollateralTokenAddress = getAddress(rawAction.initialCollateralTokenAddress);
|
|
268
|
-
const swapPath = rawAction.swapPath.map((address) => getAddress(address));
|
|
269
|
-
const swapPathOutputAddresses = getSwapPathOutputAddresses({
|
|
270
|
-
marketsInfoData,
|
|
271
|
-
swapPath,
|
|
272
|
-
initialCollateralAddress: initialCollateralTokenAddress,
|
|
273
|
-
wrappedNativeTokenAddress: wrappedToken.address,
|
|
274
|
-
shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
|
|
275
|
-
isIncrease: false,
|
|
276
|
-
});
|
|
277
|
-
const initialCollateralToken = getByKey(tokensData, initialCollateralTokenAddress);
|
|
278
|
-
const targetCollateralToken = getByKey(tokensData, swapPathOutputAddresses.outTokenAddress);
|
|
279
|
-
if (!initialCollateralToken || !targetCollateralToken) {
|
|
280
|
-
return undefined;
|
|
281
|
-
}
|
|
282
|
-
const tradeAction = {
|
|
283
|
-
id: rawAction.id,
|
|
284
|
-
eventName: rawAction.eventName,
|
|
285
|
-
account: rawAction.account,
|
|
286
|
-
swapPath,
|
|
287
|
-
orderType,
|
|
288
|
-
orderKey: rawAction.orderKey,
|
|
289
|
-
initialCollateralTokenAddress: rawAction.initialCollateralTokenAddress,
|
|
290
|
-
initialCollateralDeltaAmount: bigNumberify(rawAction.initialCollateralDeltaAmount),
|
|
291
|
-
minOutputAmount: bigNumberify(rawAction.minOutputAmount),
|
|
292
|
-
executionAmountOut: rawAction.executionAmountOut ? bigNumberify(rawAction.executionAmountOut) : undefined,
|
|
293
|
-
shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
|
|
294
|
-
targetCollateralToken,
|
|
295
|
-
initialCollateralToken,
|
|
296
|
-
transaction: rawAction.transaction,
|
|
297
|
-
reason: rawAction.reason,
|
|
298
|
-
reasonBytes: rawAction.reasonBytes,
|
|
299
|
-
};
|
|
300
|
-
return tradeAction;
|
|
301
|
-
}
|
|
302
|
-
else {
|
|
303
|
-
const marketAddress = getAddress(rawAction.marketAddress);
|
|
304
|
-
const marketInfo = getByKey(marketsInfoData, marketAddress);
|
|
305
|
-
const indexToken = marketInfo?.indexToken;
|
|
306
|
-
const initialCollateralTokenAddress = getAddress(rawAction.initialCollateralTokenAddress);
|
|
307
|
-
const swapPath = rawAction.swapPath.map((address) => getAddress(address));
|
|
308
|
-
const swapPathOutputAddresses = getSwapPathOutputAddresses({
|
|
309
|
-
marketsInfoData,
|
|
310
|
-
swapPath,
|
|
311
|
-
initialCollateralAddress: initialCollateralTokenAddress,
|
|
312
|
-
wrappedNativeTokenAddress: wrappedToken.address,
|
|
313
|
-
shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
|
|
314
|
-
isIncrease: isIncreaseOrderType(rawAction.orderType),
|
|
315
|
-
});
|
|
316
|
-
const initialCollateralToken = getByKey(tokensData, initialCollateralTokenAddress);
|
|
317
|
-
const targetCollateralToken = getByKey(tokensData, swapPathOutputAddresses.outTokenAddress);
|
|
318
|
-
if (!marketInfo || !indexToken || !initialCollateralToken || !targetCollateralToken) {
|
|
319
|
-
return undefined;
|
|
320
|
-
}
|
|
321
|
-
const tradeAction = {
|
|
322
|
-
id: rawAction.id,
|
|
323
|
-
eventName: rawAction.eventName,
|
|
324
|
-
account: rawAction.account,
|
|
325
|
-
marketAddress,
|
|
326
|
-
marketInfo,
|
|
327
|
-
indexToken,
|
|
328
|
-
swapPath,
|
|
329
|
-
initialCollateralTokenAddress,
|
|
330
|
-
initialCollateralToken,
|
|
331
|
-
targetCollateralToken,
|
|
332
|
-
initialCollateralDeltaAmount: bigNumberify(rawAction.initialCollateralDeltaAmount),
|
|
333
|
-
sizeDeltaUsd: bigNumberify(rawAction.sizeDeltaUsd),
|
|
334
|
-
triggerPrice: rawAction.triggerPrice
|
|
335
|
-
? parseContractPrice(bigNumberify(rawAction.triggerPrice), indexToken.decimals)
|
|
336
|
-
: undefined,
|
|
337
|
-
acceptablePrice: parseContractPrice(bigNumberify(rawAction.acceptablePrice), indexToken.decimals),
|
|
338
|
-
executionPrice: rawAction.executionPrice
|
|
339
|
-
? parseContractPrice(bigNumberify(rawAction.executionPrice), indexToken.decimals)
|
|
340
|
-
: undefined,
|
|
341
|
-
minOutputAmount: bigNumberify(rawAction.minOutputAmount),
|
|
342
|
-
collateralTokenPriceMax: rawAction.collateralTokenPriceMax
|
|
343
|
-
? parseContractPrice(bigNumberify(rawAction.collateralTokenPriceMax), initialCollateralToken.decimals)
|
|
344
|
-
: undefined,
|
|
345
|
-
collateralTokenPriceMin: rawAction.collateralTokenPriceMin
|
|
346
|
-
? parseContractPrice(bigNumberify(rawAction.collateralTokenPriceMin), initialCollateralToken.decimals)
|
|
347
|
-
: undefined,
|
|
348
|
-
indexTokenPriceMin: rawAction.indexTokenPriceMin
|
|
349
|
-
? parseContractPrice(BigInt(rawAction.indexTokenPriceMin), indexToken.decimals)
|
|
350
|
-
: undefined,
|
|
351
|
-
indexTokenPriceMax: rawAction.indexTokenPriceMax
|
|
352
|
-
? parseContractPrice(BigInt(rawAction.indexTokenPriceMax), indexToken.decimals)
|
|
353
|
-
: undefined,
|
|
354
|
-
orderType,
|
|
355
|
-
orderKey: rawAction.orderKey,
|
|
356
|
-
isLong: rawAction.isLong,
|
|
357
|
-
pnlUsd: rawAction.pnlUsd ? BigInt(rawAction.pnlUsd) : undefined,
|
|
358
|
-
basePnlUsd: rawAction.basePnlUsd ? BigInt(rawAction.basePnlUsd) : undefined,
|
|
359
|
-
priceImpactDiffUsd: rawAction.priceImpactDiffUsd ? BigInt(rawAction.priceImpactDiffUsd) : undefined,
|
|
360
|
-
priceImpactUsd: rawAction.priceImpactUsd ? BigInt(rawAction.priceImpactUsd) : undefined,
|
|
361
|
-
positionFeeAmount: rawAction.positionFeeAmount ? BigInt(rawAction.positionFeeAmount) : undefined,
|
|
362
|
-
borrowingFeeAmount: rawAction.borrowingFeeAmount ? BigInt(rawAction.borrowingFeeAmount) : undefined,
|
|
363
|
-
fundingFeeAmount: rawAction.fundingFeeAmount ? BigInt(rawAction.fundingFeeAmount) : undefined,
|
|
364
|
-
reason: rawAction.reason,
|
|
365
|
-
reasonBytes: rawAction.reasonBytes,
|
|
366
|
-
transaction: rawAction.transaction,
|
|
367
|
-
shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
|
|
368
|
-
};
|
|
369
|
-
return tradeAction;
|
|
370
|
-
}
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
|
-
export function bigNumberify(n) {
|
|
374
|
-
try {
|
|
375
|
-
if (n === undefined)
|
|
376
|
-
throw new Error("n is undefined");
|
|
377
|
-
if (n === null)
|
|
378
|
-
throw new Error("n is null");
|
|
379
|
-
return BigInt(n);
|
|
380
|
-
}
|
|
381
|
-
catch (e) {
|
|
382
|
-
// eslint-disable-next-line no-console
|
|
383
|
-
console.error("bigNumberify error", e);
|
|
384
|
-
return undefined;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
@@ -94,7 +94,6 @@ describe("buildFiltersBody", () => {
|
|
|
94
94
|
foo: "bar",
|
|
95
95
|
},
|
|
96
96
|
],
|
|
97
|
-
// @ts-expect-error
|
|
98
97
|
baz: "qux",
|
|
99
98
|
};
|
|
100
99
|
const getResult = () => buildFiltersBody(input);
|
|
@@ -107,7 +106,6 @@ describe("buildFiltersBody", () => {
|
|
|
107
106
|
foo: "bar",
|
|
108
107
|
},
|
|
109
108
|
],
|
|
110
|
-
// @ts-expect-error
|
|
111
109
|
baz: "qux",
|
|
112
110
|
};
|
|
113
111
|
const getResult = () => buildFiltersBody(input);
|
|
@@ -116,7 +114,6 @@ describe("buildFiltersBody", () => {
|
|
|
116
114
|
it("should throw not error if empty or is mixed with other filters", () => {
|
|
117
115
|
const input = {
|
|
118
116
|
or: [],
|
|
119
|
-
// @ts-expect-error
|
|
120
117
|
baz: "qux",
|
|
121
118
|
};
|
|
122
119
|
const getResult = () => buildFiltersBody(input);
|
|
@@ -125,7 +122,6 @@ describe("buildFiltersBody", () => {
|
|
|
125
122
|
it("should throw not error if empty and is mixed with other filters", () => {
|
|
126
123
|
const input = {
|
|
127
124
|
and: [],
|
|
128
|
-
// @ts-expect-error
|
|
129
125
|
baz: "qux",
|
|
130
126
|
};
|
|
131
127
|
const getResult = () => buildFiltersBody(input);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { bigNumberify } from "../../modules/trades/trades";
|
|
2
1
|
import { bigMath } from "../bigmath";
|
|
3
2
|
import { getTokenPoolType } from "../markets";
|
|
4
3
|
import { applyFactor, expandDecimals, getBasisPoints, roundUpMagnitudeDivision } from "../numbers";
|
|
5
4
|
import { convertToTokenAmount, convertToUsd, getMidPrice } from "../tokens";
|
|
5
|
+
import { bigNumberify } from "../tradeHistory";
|
|
6
6
|
export function getPriceImpactByAcceptablePrice(p) {
|
|
7
7
|
const { sizeDeltaUsd, acceptablePrice, indexPrice: markPrice, isLong, isIncrease } = p;
|
|
8
8
|
const shouldFlipPriceDiff = isIncrease ? !isLong : isLong;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function buildFiltersBody(filters) {
|
|
1
|
+
export function buildFiltersBody(filters, options) {
|
|
2
2
|
const res = {};
|
|
3
3
|
let hadOr = false;
|
|
4
4
|
let hadAnd = false;
|
|
@@ -7,7 +7,12 @@ export function buildFiltersBody(filters) {
|
|
|
7
7
|
continue;
|
|
8
8
|
}
|
|
9
9
|
if (typeof value === "string") {
|
|
10
|
-
|
|
10
|
+
if (options?.enums?.[value]) {
|
|
11
|
+
res[key] = value;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
res[key] = `"${value}"`;
|
|
15
|
+
}
|
|
11
16
|
}
|
|
12
17
|
else if (typeof value === "number") {
|
|
13
18
|
res[key] = `${value}`;
|
|
@@ -20,13 +25,18 @@ export function buildFiltersBody(filters) {
|
|
|
20
25
|
value
|
|
21
26
|
.map((el) => {
|
|
22
27
|
if (typeof el === "string") {
|
|
23
|
-
|
|
28
|
+
if (options?.enums?.[el]) {
|
|
29
|
+
return el;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return `"${el}"`;
|
|
33
|
+
}
|
|
24
34
|
}
|
|
25
35
|
else if (typeof el === "number") {
|
|
26
36
|
return `${el}`;
|
|
27
37
|
}
|
|
28
38
|
else {
|
|
29
|
-
const elemStr = buildFiltersBody(el);
|
|
39
|
+
const elemStr = buildFiltersBody(el, options);
|
|
30
40
|
if (elemStr === "{}") {
|
|
31
41
|
return "";
|
|
32
42
|
}
|
|
@@ -46,9 +56,9 @@ export function buildFiltersBody(filters) {
|
|
|
46
56
|
res[key] = null;
|
|
47
57
|
}
|
|
48
58
|
else {
|
|
49
|
-
const valueStr = buildFiltersBody(value);
|
|
59
|
+
const valueStr = buildFiltersBody(value, options);
|
|
50
60
|
if (valueStr !== "{}") {
|
|
51
|
-
res[key + "_"] = buildFiltersBody(value);
|
|
61
|
+
res[key + "_"] = buildFiltersBody(value, options);
|
|
52
62
|
}
|
|
53
63
|
}
|
|
54
64
|
if (hadOr) {
|
|
@@ -14,7 +14,6 @@ export const arbitrumSdkConfig = {
|
|
|
14
14
|
oracleUrl: "https://arbitrum-api.gmxinfra.io",
|
|
15
15
|
rpcUrl: "https://arb1.arbitrum.io/rpc",
|
|
16
16
|
walletClient: client,
|
|
17
|
-
subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum
|
|
18
|
-
subgraphUrl: "https://subgraph.satsuma-prod.com/3b2ced13c8d9/gmx/synthetics-arbitrum-stats/api",
|
|
17
|
+
subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum@e27d93/api/graphql",
|
|
19
18
|
};
|
|
20
19
|
export const arbitrumSdk = new GmxSdk(arbitrumSdkConfig);
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { getAddress } from "viem";
|
|
2
|
+
import { getByKey } from "./objects";
|
|
3
|
+
import { isIncreaseOrderType, isSwapOrderType } from "./orders";
|
|
4
|
+
import { getSwapPathOutputAddresses } from "./swap/swapStats";
|
|
5
|
+
import { parseContractPrice } from "./tokens";
|
|
6
|
+
export function createRawTradeActionTransformer(marketsInfoData, wrappedToken, tokensData) {
|
|
7
|
+
return (rawAction) => {
|
|
8
|
+
const orderType = Number(rawAction.orderType);
|
|
9
|
+
if (isSwapOrderType(orderType)) {
|
|
10
|
+
const initialCollateralTokenAddress = getAddress(rawAction.initialCollateralTokenAddress);
|
|
11
|
+
const swapPath = rawAction.swapPath.map((address) => getAddress(address));
|
|
12
|
+
const swapPathOutputAddresses = getSwapPathOutputAddresses({
|
|
13
|
+
marketsInfoData,
|
|
14
|
+
swapPath,
|
|
15
|
+
initialCollateralAddress: initialCollateralTokenAddress,
|
|
16
|
+
wrappedNativeTokenAddress: wrappedToken.address,
|
|
17
|
+
shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
|
|
18
|
+
isIncrease: false,
|
|
19
|
+
});
|
|
20
|
+
const initialCollateralToken = getByKey(tokensData, initialCollateralTokenAddress);
|
|
21
|
+
const targetCollateralToken = getByKey(tokensData, swapPathOutputAddresses.outTokenAddress);
|
|
22
|
+
if (!initialCollateralToken || !targetCollateralToken) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const tradeAction = {
|
|
26
|
+
id: rawAction.id,
|
|
27
|
+
eventName: rawAction.eventName,
|
|
28
|
+
account: rawAction.account,
|
|
29
|
+
swapPath,
|
|
30
|
+
orderType,
|
|
31
|
+
orderKey: rawAction.orderKey,
|
|
32
|
+
initialCollateralTokenAddress: rawAction.initialCollateralTokenAddress,
|
|
33
|
+
initialCollateralDeltaAmount: bigNumberify(rawAction.initialCollateralDeltaAmount),
|
|
34
|
+
minOutputAmount: bigNumberify(rawAction.minOutputAmount),
|
|
35
|
+
executionAmountOut: rawAction.executionAmountOut ? bigNumberify(rawAction.executionAmountOut) : undefined,
|
|
36
|
+
shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
|
|
37
|
+
targetCollateralToken,
|
|
38
|
+
initialCollateralToken,
|
|
39
|
+
timestamp: rawAction.timestamp,
|
|
40
|
+
transaction: rawAction.transaction,
|
|
41
|
+
reason: rawAction.reason,
|
|
42
|
+
reasonBytes: rawAction.reasonBytes,
|
|
43
|
+
};
|
|
44
|
+
return tradeAction;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
const marketAddress = getAddress(rawAction.marketAddress);
|
|
48
|
+
const marketInfo = getByKey(marketsInfoData, marketAddress);
|
|
49
|
+
const indexToken = marketInfo?.indexToken;
|
|
50
|
+
const initialCollateralTokenAddress = getAddress(rawAction.initialCollateralTokenAddress);
|
|
51
|
+
const swapPath = rawAction.swapPath.map((address) => getAddress(address));
|
|
52
|
+
const swapPathOutputAddresses = getSwapPathOutputAddresses({
|
|
53
|
+
marketsInfoData,
|
|
54
|
+
swapPath,
|
|
55
|
+
initialCollateralAddress: initialCollateralTokenAddress,
|
|
56
|
+
wrappedNativeTokenAddress: wrappedToken.address,
|
|
57
|
+
shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
|
|
58
|
+
isIncrease: isIncreaseOrderType(rawAction.orderType),
|
|
59
|
+
});
|
|
60
|
+
const initialCollateralToken = getByKey(tokensData, initialCollateralTokenAddress);
|
|
61
|
+
const targetCollateralToken = getByKey(tokensData, swapPathOutputAddresses.outTokenAddress);
|
|
62
|
+
if (!marketInfo || !indexToken || !initialCollateralToken || !targetCollateralToken) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const tradeAction = {
|
|
66
|
+
id: rawAction.id,
|
|
67
|
+
eventName: rawAction.eventName,
|
|
68
|
+
account: rawAction.account,
|
|
69
|
+
marketAddress,
|
|
70
|
+
marketInfo,
|
|
71
|
+
indexToken,
|
|
72
|
+
swapPath,
|
|
73
|
+
initialCollateralTokenAddress,
|
|
74
|
+
initialCollateralToken,
|
|
75
|
+
targetCollateralToken,
|
|
76
|
+
initialCollateralDeltaAmount: bigNumberify(rawAction.initialCollateralDeltaAmount),
|
|
77
|
+
sizeDeltaUsd: bigNumberify(rawAction.sizeDeltaUsd),
|
|
78
|
+
triggerPrice: rawAction.triggerPrice
|
|
79
|
+
? parseContractPrice(bigNumberify(rawAction.triggerPrice), indexToken.decimals)
|
|
80
|
+
: undefined,
|
|
81
|
+
acceptablePrice: parseContractPrice(bigNumberify(rawAction.acceptablePrice), indexToken.decimals),
|
|
82
|
+
executionPrice: rawAction.executionPrice
|
|
83
|
+
? parseContractPrice(bigNumberify(rawAction.executionPrice), indexToken.decimals)
|
|
84
|
+
: undefined,
|
|
85
|
+
minOutputAmount: bigNumberify(rawAction.minOutputAmount),
|
|
86
|
+
collateralTokenPriceMax: rawAction.collateralTokenPriceMax
|
|
87
|
+
? parseContractPrice(bigNumberify(rawAction.collateralTokenPriceMax), initialCollateralToken.decimals)
|
|
88
|
+
: undefined,
|
|
89
|
+
collateralTokenPriceMin: rawAction.collateralTokenPriceMin
|
|
90
|
+
? parseContractPrice(bigNumberify(rawAction.collateralTokenPriceMin), initialCollateralToken.decimals)
|
|
91
|
+
: undefined,
|
|
92
|
+
indexTokenPriceMin: rawAction.indexTokenPriceMin
|
|
93
|
+
? parseContractPrice(BigInt(rawAction.indexTokenPriceMin), indexToken.decimals)
|
|
94
|
+
: undefined,
|
|
95
|
+
indexTokenPriceMax: rawAction.indexTokenPriceMax
|
|
96
|
+
? parseContractPrice(BigInt(rawAction.indexTokenPriceMax), indexToken.decimals)
|
|
97
|
+
: undefined,
|
|
98
|
+
orderType,
|
|
99
|
+
orderKey: rawAction.orderKey,
|
|
100
|
+
isLong: rawAction.isLong,
|
|
101
|
+
pnlUsd: rawAction.pnlUsd ? BigInt(rawAction.pnlUsd) : undefined,
|
|
102
|
+
basePnlUsd: rawAction.basePnlUsd ? BigInt(rawAction.basePnlUsd) : undefined,
|
|
103
|
+
priceImpactDiffUsd: rawAction.priceImpactDiffUsd ? BigInt(rawAction.priceImpactDiffUsd) : undefined,
|
|
104
|
+
priceImpactUsd: rawAction.priceImpactUsd ? BigInt(rawAction.priceImpactUsd) : undefined,
|
|
105
|
+
positionFeeAmount: rawAction.positionFeeAmount ? BigInt(rawAction.positionFeeAmount) : undefined,
|
|
106
|
+
borrowingFeeAmount: rawAction.borrowingFeeAmount ? BigInt(rawAction.borrowingFeeAmount) : undefined,
|
|
107
|
+
fundingFeeAmount: rawAction.fundingFeeAmount ? BigInt(rawAction.fundingFeeAmount) : undefined,
|
|
108
|
+
liquidationFeeAmount: rawAction.liquidationFeeAmount ? BigInt(rawAction.liquidationFeeAmount) : undefined,
|
|
109
|
+
reason: rawAction.reason,
|
|
110
|
+
reasonBytes: rawAction.reasonBytes,
|
|
111
|
+
transaction: rawAction.transaction,
|
|
112
|
+
timestamp: rawAction.timestamp,
|
|
113
|
+
shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
|
|
114
|
+
};
|
|
115
|
+
return tradeAction;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export function bigNumberify(n) {
|
|
120
|
+
try {
|
|
121
|
+
if (n === undefined)
|
|
122
|
+
throw new Error("n is undefined");
|
|
123
|
+
if (n === null)
|
|
124
|
+
throw new Error("n is null");
|
|
125
|
+
return BigInt(n);
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
// eslint-disable-next-line no-console
|
|
129
|
+
console.error("bigNumberify error", e);
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|