@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.
@@ -3,18 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.bigNumberify = exports.fetchTradeActions = exports.Trades = void 0;
6
+ exports.fetchTradeActions = exports.Trades = void 0;
7
7
  const merge_1 = __importDefault(require("lodash/merge"));
8
- const viem_1 = require("viem");
9
8
  const tokens_1 = require("../../configs/tokens");
10
9
  const orders_1 = require("../../types/orders");
11
10
  const tradeHistory_1 = require("../../types/tradeHistory");
12
11
  const graphqlFetcher_1 = __importDefault(require("../../utils/graphqlFetcher"));
13
- const objects_1 = require("../../utils/objects");
14
12
  const orders_2 = require("../../utils/orders");
15
13
  const subgraph_1 = require("../../utils/subgraph");
16
14
  const swapStats_1 = require("../../utils/swap/swapStats");
17
- const tokens_2 = require("../../utils/tokens");
15
+ const tradeHistory_2 = require("../../utils/tradeHistory");
18
16
  const base_1 = require("../base");
19
17
  class Trades extends base_1.Module {
20
18
  async getTradeHistory(p) {
@@ -38,13 +36,13 @@ class Trades extends base_1.Module {
38
36
  }
39
37
  exports.Trades = Trades;
40
38
  async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFilter = [], forAllAccounts, account, fromTxTimestamp, toTxTimestamp, orderEventCombinations, marketsInfoData, tokensData, }) {
41
- const endpoint = sdk.config.subgraphUrl;
39
+ const endpoint = sdk.config.subsquidUrl;
42
40
  const chainId = sdk.chainId;
43
41
  if (!endpoint) {
44
42
  return [];
45
43
  }
46
- const skip = pageIndex * pageSize;
47
- const first = pageSize;
44
+ const offset = pageIndex * pageSize;
45
+ const limit = pageSize;
48
46
  const nonSwapRelevantDefinedFiltersLowercased = marketsDirectionsFilter
49
47
  .filter((filter) => filter.direction !== "swap" && filter.marketAddress !== "any")
50
48
  .map((filter) => ({
@@ -65,40 +63,38 @@ async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFi
65
63
  .map((filter) => filter.marketAddress.toLowerCase());
66
64
  const hasSwapRelevantDefinedMarkets = swapRelevantDefinedMarketsLowercased.length > 0;
67
65
  const filtersStr = (0, subgraph_1.buildFiltersBody)({
68
- and: [
66
+ AND: [
69
67
  {
70
- account: forAllAccounts ? undefined : account.toLowerCase(),
71
- transaction: {
72
- timestamp_gte: fromTxTimestamp,
73
- timestamp_lte: toTxTimestamp,
74
- },
68
+ account_eq: forAllAccounts ? undefined : account,
69
+ timestamp_gte: fromTxTimestamp,
70
+ timestamp_lte: toTxTimestamp,
75
71
  },
76
72
  {
77
- or: !hasPureDirectionFilters
73
+ OR: !hasPureDirectionFilters
78
74
  ? undefined
79
75
  : pureDirectionFilters.map((filter) => filter.direction === "swap"
80
76
  ? {
81
77
  orderType_in: [orders_1.OrderType.LimitSwap, orders_1.OrderType.MarketSwap],
82
78
  }
83
79
  : {
84
- isLong: filter.direction === "long",
80
+ isLong_eq: filter.direction === "long",
85
81
  orderType_not_in: [orders_1.OrderType.LimitSwap, orders_1.OrderType.MarketSwap],
86
82
  }),
87
83
  },
88
84
  {
89
- or: [
85
+ OR: [
90
86
  // For non-swap orders
91
87
  {
92
- and: !hasNonSwapRelevantDefinedMarkets
88
+ AND: !hasNonSwapRelevantDefinedMarkets
93
89
  ? undefined
94
90
  : [
95
91
  {
96
92
  orderType_not_in: [orders_1.OrderType.LimitSwap, orders_1.OrderType.MarketSwap],
97
93
  },
98
94
  {
99
- or: nonSwapRelevantDefinedFiltersLowercased.map((filter) => ({
100
- marketAddress: filter.marketAddress === "any" ? undefined : filter.marketAddress,
101
- isLong: filter.direction === "any" ? undefined : filter.direction === "long",
95
+ OR: nonSwapRelevantDefinedFiltersLowercased.map((filter) => ({
96
+ marketAddress_eq: filter.marketAddress === "any" ? undefined : filter.marketAddress,
97
+ isLong_eq: filter.direction === "any" ? undefined : filter.direction === "long",
102
98
  // Collateral filtering is done outside of graphql on the client
103
99
  })),
104
100
  },
@@ -106,14 +102,14 @@ async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFi
106
102
  },
107
103
  // For defined markets on swap orders
108
104
  {
109
- and: !hasSwapRelevantDefinedMarkets
105
+ AND: !hasSwapRelevantDefinedMarkets
110
106
  ? undefined
111
107
  : [
112
108
  {
113
109
  orderType_in: [orders_1.OrderType.LimitSwap, orders_1.OrderType.MarketSwap],
114
110
  },
115
111
  {
116
- or: [
112
+ OR: [
117
113
  // Source token is not in swap path so we add it to the or filter
118
114
  {
119
115
  marketAddress_in: swapRelevantDefinedMarketsLowercased,
@@ -127,7 +123,7 @@ async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFi
127
123
  ],
128
124
  },
129
125
  {
130
- or: orderEventCombinations?.map((combination) => {
126
+ OR: orderEventCombinations?.map((combination) => {
131
127
  let sizeDeltaUsdCondition = {};
132
128
  if (combination.orderType !== undefined &&
133
129
  [orders_1.OrderType.MarketDecrease, orders_1.OrderType.MarketIncrease].includes(combination.orderType)) {
@@ -139,25 +135,24 @@ async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFi
139
135
  }
140
136
  }
141
137
  return (0, merge_1.default)({
142
- eventName: combination.eventName,
143
- orderType: combination.orderType,
138
+ eventName_eq: combination.eventName,
139
+ orderType_eq: combination.orderType,
144
140
  }, sizeDeltaUsdCondition);
145
141
  }),
146
142
  },
147
143
  {
148
144
  // We do not show create liquidation orders in the trade history, thus we filter it out
149
145
  // ... && not (liquidation && orderCreated) === ... && (not liquidation || not orderCreated)
150
- or: [{ orderType_not: orders_1.OrderType.Liquidation }, { eventName_not: tradeHistory_1.TradeActionType.OrderCreated }],
146
+ OR: [{ orderType_not_eq: orders_1.OrderType.Liquidation }, { eventName_not_eq: tradeHistory_1.TradeActionType.OrderCreated }],
151
147
  },
152
148
  ],
153
149
  });
154
150
  const whereClause = `where: ${filtersStr}`;
155
151
  const query = `{
156
152
  tradeActions(
157
- skip: ${skip},
158
- first: ${first},
159
- orderBy: transaction__timestamp,
160
- orderDirection: desc,
153
+ offset: ${offset},
154
+ limit: ${limit},
155
+ orderBy: transaction_timestamp_DESC,
161
156
  ${whereClause}
162
157
  ) {
163
158
  id
@@ -197,6 +192,7 @@ async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFi
197
192
 
198
193
  reason
199
194
  reasonBytes
195
+ timestamp
200
196
 
201
197
  transaction {
202
198
  timestamp
@@ -210,7 +206,7 @@ async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFi
210
206
  return [];
211
207
  }
212
208
  const wrappedToken = (0, tokens_1.getWrappedToken)(chainId);
213
- const transformer = createRawTradeActionTransformer(marketsInfoData, wrappedToken, tokensData);
209
+ const transformer = (0, tradeHistory_2.createRawTradeActionTransformer)(marketsInfoData, wrappedToken, tokensData);
214
210
  let tradeActions = rawTradeActions.map(transformer).filter(Boolean);
215
211
  const collateralFilterTree = {
216
212
  long: {},
@@ -268,128 +264,3 @@ async function fetchTradeActions({ sdk, pageIndex, pageSize, marketsDirectionsFi
268
264
  return tradeActions;
269
265
  }
270
266
  exports.fetchTradeActions = fetchTradeActions;
271
- function createRawTradeActionTransformer(marketsInfoData, wrappedToken, tokensData) {
272
- return (rawAction) => {
273
- const orderType = Number(rawAction.orderType);
274
- if ((0, orders_2.isSwapOrderType)(orderType)) {
275
- const initialCollateralTokenAddress = (0, viem_1.getAddress)(rawAction.initialCollateralTokenAddress);
276
- const swapPath = rawAction.swapPath.map((address) => (0, viem_1.getAddress)(address));
277
- const swapPathOutputAddresses = (0, swapStats_1.getSwapPathOutputAddresses)({
278
- marketsInfoData,
279
- swapPath,
280
- initialCollateralAddress: initialCollateralTokenAddress,
281
- wrappedNativeTokenAddress: wrappedToken.address,
282
- shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
283
- isIncrease: false,
284
- });
285
- const initialCollateralToken = (0, objects_1.getByKey)(tokensData, initialCollateralTokenAddress);
286
- const targetCollateralToken = (0, objects_1.getByKey)(tokensData, swapPathOutputAddresses.outTokenAddress);
287
- if (!initialCollateralToken || !targetCollateralToken) {
288
- return undefined;
289
- }
290
- const tradeAction = {
291
- id: rawAction.id,
292
- eventName: rawAction.eventName,
293
- account: rawAction.account,
294
- swapPath,
295
- orderType,
296
- orderKey: rawAction.orderKey,
297
- initialCollateralTokenAddress: rawAction.initialCollateralTokenAddress,
298
- initialCollateralDeltaAmount: bigNumberify(rawAction.initialCollateralDeltaAmount),
299
- minOutputAmount: bigNumberify(rawAction.minOutputAmount),
300
- executionAmountOut: rawAction.executionAmountOut ? bigNumberify(rawAction.executionAmountOut) : undefined,
301
- shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
302
- targetCollateralToken,
303
- initialCollateralToken,
304
- transaction: rawAction.transaction,
305
- reason: rawAction.reason,
306
- reasonBytes: rawAction.reasonBytes,
307
- };
308
- return tradeAction;
309
- }
310
- else {
311
- const marketAddress = (0, viem_1.getAddress)(rawAction.marketAddress);
312
- const marketInfo = (0, objects_1.getByKey)(marketsInfoData, marketAddress);
313
- const indexToken = marketInfo?.indexToken;
314
- const initialCollateralTokenAddress = (0, viem_1.getAddress)(rawAction.initialCollateralTokenAddress);
315
- const swapPath = rawAction.swapPath.map((address) => (0, viem_1.getAddress)(address));
316
- const swapPathOutputAddresses = (0, swapStats_1.getSwapPathOutputAddresses)({
317
- marketsInfoData,
318
- swapPath,
319
- initialCollateralAddress: initialCollateralTokenAddress,
320
- wrappedNativeTokenAddress: wrappedToken.address,
321
- shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
322
- isIncrease: (0, orders_2.isIncreaseOrderType)(rawAction.orderType),
323
- });
324
- const initialCollateralToken = (0, objects_1.getByKey)(tokensData, initialCollateralTokenAddress);
325
- const targetCollateralToken = (0, objects_1.getByKey)(tokensData, swapPathOutputAddresses.outTokenAddress);
326
- if (!marketInfo || !indexToken || !initialCollateralToken || !targetCollateralToken) {
327
- return undefined;
328
- }
329
- const tradeAction = {
330
- id: rawAction.id,
331
- eventName: rawAction.eventName,
332
- account: rawAction.account,
333
- marketAddress,
334
- marketInfo,
335
- indexToken,
336
- swapPath,
337
- initialCollateralTokenAddress,
338
- initialCollateralToken,
339
- targetCollateralToken,
340
- initialCollateralDeltaAmount: bigNumberify(rawAction.initialCollateralDeltaAmount),
341
- sizeDeltaUsd: bigNumberify(rawAction.sizeDeltaUsd),
342
- triggerPrice: rawAction.triggerPrice
343
- ? (0, tokens_2.parseContractPrice)(bigNumberify(rawAction.triggerPrice), indexToken.decimals)
344
- : undefined,
345
- acceptablePrice: (0, tokens_2.parseContractPrice)(bigNumberify(rawAction.acceptablePrice), indexToken.decimals),
346
- executionPrice: rawAction.executionPrice
347
- ? (0, tokens_2.parseContractPrice)(bigNumberify(rawAction.executionPrice), indexToken.decimals)
348
- : undefined,
349
- minOutputAmount: bigNumberify(rawAction.minOutputAmount),
350
- collateralTokenPriceMax: rawAction.collateralTokenPriceMax
351
- ? (0, tokens_2.parseContractPrice)(bigNumberify(rawAction.collateralTokenPriceMax), initialCollateralToken.decimals)
352
- : undefined,
353
- collateralTokenPriceMin: rawAction.collateralTokenPriceMin
354
- ? (0, tokens_2.parseContractPrice)(bigNumberify(rawAction.collateralTokenPriceMin), initialCollateralToken.decimals)
355
- : undefined,
356
- indexTokenPriceMin: rawAction.indexTokenPriceMin
357
- ? (0, tokens_2.parseContractPrice)(BigInt(rawAction.indexTokenPriceMin), indexToken.decimals)
358
- : undefined,
359
- indexTokenPriceMax: rawAction.indexTokenPriceMax
360
- ? (0, tokens_2.parseContractPrice)(BigInt(rawAction.indexTokenPriceMax), indexToken.decimals)
361
- : undefined,
362
- orderType,
363
- orderKey: rawAction.orderKey,
364
- isLong: rawAction.isLong,
365
- pnlUsd: rawAction.pnlUsd ? BigInt(rawAction.pnlUsd) : undefined,
366
- basePnlUsd: rawAction.basePnlUsd ? BigInt(rawAction.basePnlUsd) : undefined,
367
- priceImpactDiffUsd: rawAction.priceImpactDiffUsd ? BigInt(rawAction.priceImpactDiffUsd) : undefined,
368
- priceImpactUsd: rawAction.priceImpactUsd ? BigInt(rawAction.priceImpactUsd) : undefined,
369
- positionFeeAmount: rawAction.positionFeeAmount ? BigInt(rawAction.positionFeeAmount) : undefined,
370
- borrowingFeeAmount: rawAction.borrowingFeeAmount ? BigInt(rawAction.borrowingFeeAmount) : undefined,
371
- fundingFeeAmount: rawAction.fundingFeeAmount ? BigInt(rawAction.fundingFeeAmount) : undefined,
372
- reason: rawAction.reason,
373
- reasonBytes: rawAction.reasonBytes,
374
- transaction: rawAction.transaction,
375
- shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
376
- };
377
- return tradeAction;
378
- }
379
- };
380
- }
381
- function bigNumberify(n) {
382
- try {
383
- if (n === undefined)
384
- throw new Error("n is undefined");
385
- if (n === null)
386
- throw new Error("n is null");
387
- return BigInt(n);
388
- }
389
- catch (e) {
390
- // eslint-disable-next-line no-console
391
- console.error("bigNumberify error", e);
392
- return undefined;
393
- }
394
- }
395
- exports.bigNumberify = bigNumberify;
@@ -96,7 +96,6 @@ const subgraph_1 = require("../subgraph");
96
96
  foo: "bar",
97
97
  },
98
98
  ],
99
- // @ts-expect-error
100
99
  baz: "qux",
101
100
  };
102
101
  const getResult = () => (0, subgraph_1.buildFiltersBody)(input);
@@ -109,7 +108,6 @@ const subgraph_1 = require("../subgraph");
109
108
  foo: "bar",
110
109
  },
111
110
  ],
112
- // @ts-expect-error
113
111
  baz: "qux",
114
112
  };
115
113
  const getResult = () => (0, subgraph_1.buildFiltersBody)(input);
@@ -118,7 +116,6 @@ const subgraph_1 = require("../subgraph");
118
116
  (0, vitest_1.it)("should throw not error if empty or is mixed with other filters", () => {
119
117
  const input = {
120
118
  or: [],
121
- // @ts-expect-error
122
119
  baz: "qux",
123
120
  };
124
121
  const getResult = () => (0, subgraph_1.buildFiltersBody)(input);
@@ -127,7 +124,6 @@ const subgraph_1 = require("../subgraph");
127
124
  (0, vitest_1.it)("should throw not error if empty and is mixed with other filters", () => {
128
125
  const input = {
129
126
  and: [],
130
- // @ts-expect-error
131
127
  baz: "qux",
132
128
  };
133
129
  const getResult = () => (0, subgraph_1.buildFiltersBody)(input);
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.applyImpactFactor = exports.calculateImpactForCrossoverRebalance = exports.calculateImpactForSameSideRebalance = exports.getPriceImpactUsd = exports.getNextPoolAmountsParams = exports.getPriceImpactForSwap = exports.getPriceImpactForPosition = exports.getCappedPositionImpactUsd = exports.applySwapImpactWithCap = exports.getPriceImpactByAcceptablePrice = void 0;
4
- const trades_1 = require("../../modules/trades/trades");
5
4
  const bigmath_1 = require("../bigmath");
6
5
  const markets_1 = require("../markets");
7
6
  const numbers_1 = require("../numbers");
8
7
  const tokens_1 = require("../tokens");
8
+ const tradeHistory_1 = require("../tradeHistory");
9
9
  function getPriceImpactByAcceptablePrice(p) {
10
10
  const { sizeDeltaUsd, acceptablePrice, indexPrice: markPrice, isLong, isIncrease } = p;
11
11
  const shouldFlipPriceDiff = isIncrease ? !isLong : isLong;
@@ -293,7 +293,7 @@ function applyImpactFactor(diff, factor, exponent) {
293
293
  const _diff = Number(diff) / 10 ** 30;
294
294
  const _exponent = Number(exponent) / 10 ** 30;
295
295
  // Pow and convert back to BigInt with 30 decimals
296
- let result = (0, trades_1.bigNumberify)(BigInt(Math.round(_diff ** _exponent * 10 ** 30)));
296
+ let result = (0, tradeHistory_1.bigNumberify)(BigInt(Math.round(_diff ** _exponent * 10 ** 30)));
297
297
  result = (result * factor) / (0, numbers_1.expandDecimals)(1, 30);
298
298
  return result;
299
299
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildFiltersBody = void 0;
4
- function buildFiltersBody(filters) {
4
+ function buildFiltersBody(filters, options) {
5
5
  const res = {};
6
6
  let hadOr = false;
7
7
  let hadAnd = false;
@@ -10,7 +10,12 @@ function buildFiltersBody(filters) {
10
10
  continue;
11
11
  }
12
12
  if (typeof value === "string") {
13
- res[key] = `"${value}"`;
13
+ if (options?.enums?.[value]) {
14
+ res[key] = value;
15
+ }
16
+ else {
17
+ res[key] = `"${value}"`;
18
+ }
14
19
  }
15
20
  else if (typeof value === "number") {
16
21
  res[key] = `${value}`;
@@ -23,13 +28,18 @@ function buildFiltersBody(filters) {
23
28
  value
24
29
  .map((el) => {
25
30
  if (typeof el === "string") {
26
- return `"${el}"`;
31
+ if (options?.enums?.[el]) {
32
+ return el;
33
+ }
34
+ else {
35
+ return `"${el}"`;
36
+ }
27
37
  }
28
38
  else if (typeof el === "number") {
29
39
  return `${el}`;
30
40
  }
31
41
  else {
32
- const elemStr = buildFiltersBody(el);
42
+ const elemStr = buildFiltersBody(el, options);
33
43
  if (elemStr === "{}") {
34
44
  return "";
35
45
  }
@@ -49,9 +59,9 @@ function buildFiltersBody(filters) {
49
59
  res[key] = null;
50
60
  }
51
61
  else {
52
- const valueStr = buildFiltersBody(value);
62
+ const valueStr = buildFiltersBody(value, options);
53
63
  if (valueStr !== "{}") {
54
- res[key + "_"] = buildFiltersBody(value);
64
+ res[key + "_"] = buildFiltersBody(value, options);
55
65
  }
56
66
  }
57
67
  if (hadOr) {
@@ -17,7 +17,6 @@ exports.arbitrumSdkConfig = {
17
17
  oracleUrl: "https://arbitrum-api.gmxinfra.io",
18
18
  rpcUrl: "https://arb1.arbitrum.io/rpc",
19
19
  walletClient: client,
20
- subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum:live/api/graphql",
21
- subgraphUrl: "https://subgraph.satsuma-prod.com/3b2ced13c8d9/gmx/synthetics-arbitrum-stats/api",
20
+ subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum@e27d93/api/graphql",
22
21
  };
23
22
  exports.arbitrumSdk = new index_1.GmxSdk(exports.arbitrumSdkConfig);
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bigNumberify = exports.createRawTradeActionTransformer = void 0;
4
+ const viem_1 = require("viem");
5
+ const objects_1 = require("./objects");
6
+ const orders_1 = require("./orders");
7
+ const swapStats_1 = require("./swap/swapStats");
8
+ const tokens_1 = require("./tokens");
9
+ function createRawTradeActionTransformer(marketsInfoData, wrappedToken, tokensData) {
10
+ return (rawAction) => {
11
+ const orderType = Number(rawAction.orderType);
12
+ if ((0, orders_1.isSwapOrderType)(orderType)) {
13
+ const initialCollateralTokenAddress = (0, viem_1.getAddress)(rawAction.initialCollateralTokenAddress);
14
+ const swapPath = rawAction.swapPath.map((address) => (0, viem_1.getAddress)(address));
15
+ const swapPathOutputAddresses = (0, swapStats_1.getSwapPathOutputAddresses)({
16
+ marketsInfoData,
17
+ swapPath,
18
+ initialCollateralAddress: initialCollateralTokenAddress,
19
+ wrappedNativeTokenAddress: wrappedToken.address,
20
+ shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
21
+ isIncrease: false,
22
+ });
23
+ const initialCollateralToken = (0, objects_1.getByKey)(tokensData, initialCollateralTokenAddress);
24
+ const targetCollateralToken = (0, objects_1.getByKey)(tokensData, swapPathOutputAddresses.outTokenAddress);
25
+ if (!initialCollateralToken || !targetCollateralToken) {
26
+ return undefined;
27
+ }
28
+ const tradeAction = {
29
+ id: rawAction.id,
30
+ eventName: rawAction.eventName,
31
+ account: rawAction.account,
32
+ swapPath,
33
+ orderType,
34
+ orderKey: rawAction.orderKey,
35
+ initialCollateralTokenAddress: rawAction.initialCollateralTokenAddress,
36
+ initialCollateralDeltaAmount: bigNumberify(rawAction.initialCollateralDeltaAmount),
37
+ minOutputAmount: bigNumberify(rawAction.minOutputAmount),
38
+ executionAmountOut: rawAction.executionAmountOut ? bigNumberify(rawAction.executionAmountOut) : undefined,
39
+ shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
40
+ targetCollateralToken,
41
+ initialCollateralToken,
42
+ timestamp: rawAction.timestamp,
43
+ transaction: rawAction.transaction,
44
+ reason: rawAction.reason,
45
+ reasonBytes: rawAction.reasonBytes,
46
+ };
47
+ return tradeAction;
48
+ }
49
+ else {
50
+ const marketAddress = (0, viem_1.getAddress)(rawAction.marketAddress);
51
+ const marketInfo = (0, objects_1.getByKey)(marketsInfoData, marketAddress);
52
+ const indexToken = marketInfo?.indexToken;
53
+ const initialCollateralTokenAddress = (0, viem_1.getAddress)(rawAction.initialCollateralTokenAddress);
54
+ const swapPath = rawAction.swapPath.map((address) => (0, viem_1.getAddress)(address));
55
+ const swapPathOutputAddresses = (0, swapStats_1.getSwapPathOutputAddresses)({
56
+ marketsInfoData,
57
+ swapPath,
58
+ initialCollateralAddress: initialCollateralTokenAddress,
59
+ wrappedNativeTokenAddress: wrappedToken.address,
60
+ shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
61
+ isIncrease: (0, orders_1.isIncreaseOrderType)(rawAction.orderType),
62
+ });
63
+ const initialCollateralToken = (0, objects_1.getByKey)(tokensData, initialCollateralTokenAddress);
64
+ const targetCollateralToken = (0, objects_1.getByKey)(tokensData, swapPathOutputAddresses.outTokenAddress);
65
+ if (!marketInfo || !indexToken || !initialCollateralToken || !targetCollateralToken) {
66
+ return undefined;
67
+ }
68
+ const tradeAction = {
69
+ id: rawAction.id,
70
+ eventName: rawAction.eventName,
71
+ account: rawAction.account,
72
+ marketAddress,
73
+ marketInfo,
74
+ indexToken,
75
+ swapPath,
76
+ initialCollateralTokenAddress,
77
+ initialCollateralToken,
78
+ targetCollateralToken,
79
+ initialCollateralDeltaAmount: bigNumberify(rawAction.initialCollateralDeltaAmount),
80
+ sizeDeltaUsd: bigNumberify(rawAction.sizeDeltaUsd),
81
+ triggerPrice: rawAction.triggerPrice
82
+ ? (0, tokens_1.parseContractPrice)(bigNumberify(rawAction.triggerPrice), indexToken.decimals)
83
+ : undefined,
84
+ acceptablePrice: (0, tokens_1.parseContractPrice)(bigNumberify(rawAction.acceptablePrice), indexToken.decimals),
85
+ executionPrice: rawAction.executionPrice
86
+ ? (0, tokens_1.parseContractPrice)(bigNumberify(rawAction.executionPrice), indexToken.decimals)
87
+ : undefined,
88
+ minOutputAmount: bigNumberify(rawAction.minOutputAmount),
89
+ collateralTokenPriceMax: rawAction.collateralTokenPriceMax
90
+ ? (0, tokens_1.parseContractPrice)(bigNumberify(rawAction.collateralTokenPriceMax), initialCollateralToken.decimals)
91
+ : undefined,
92
+ collateralTokenPriceMin: rawAction.collateralTokenPriceMin
93
+ ? (0, tokens_1.parseContractPrice)(bigNumberify(rawAction.collateralTokenPriceMin), initialCollateralToken.decimals)
94
+ : undefined,
95
+ indexTokenPriceMin: rawAction.indexTokenPriceMin
96
+ ? (0, tokens_1.parseContractPrice)(BigInt(rawAction.indexTokenPriceMin), indexToken.decimals)
97
+ : undefined,
98
+ indexTokenPriceMax: rawAction.indexTokenPriceMax
99
+ ? (0, tokens_1.parseContractPrice)(BigInt(rawAction.indexTokenPriceMax), indexToken.decimals)
100
+ : undefined,
101
+ orderType,
102
+ orderKey: rawAction.orderKey,
103
+ isLong: rawAction.isLong,
104
+ pnlUsd: rawAction.pnlUsd ? BigInt(rawAction.pnlUsd) : undefined,
105
+ basePnlUsd: rawAction.basePnlUsd ? BigInt(rawAction.basePnlUsd) : undefined,
106
+ priceImpactDiffUsd: rawAction.priceImpactDiffUsd ? BigInt(rawAction.priceImpactDiffUsd) : undefined,
107
+ priceImpactUsd: rawAction.priceImpactUsd ? BigInt(rawAction.priceImpactUsd) : undefined,
108
+ positionFeeAmount: rawAction.positionFeeAmount ? BigInt(rawAction.positionFeeAmount) : undefined,
109
+ borrowingFeeAmount: rawAction.borrowingFeeAmount ? BigInt(rawAction.borrowingFeeAmount) : undefined,
110
+ fundingFeeAmount: rawAction.fundingFeeAmount ? BigInt(rawAction.fundingFeeAmount) : undefined,
111
+ liquidationFeeAmount: rawAction.liquidationFeeAmount ? BigInt(rawAction.liquidationFeeAmount) : undefined,
112
+ reason: rawAction.reason,
113
+ reasonBytes: rawAction.reasonBytes,
114
+ transaction: rawAction.transaction,
115
+ timestamp: rawAction.timestamp,
116
+ shouldUnwrapNativeToken: rawAction.shouldUnwrapNativeToken,
117
+ };
118
+ return tradeAction;
119
+ }
120
+ };
121
+ }
122
+ exports.createRawTradeActionTransformer = createRawTradeActionTransformer;
123
+ function bigNumberify(n) {
124
+ try {
125
+ if (n === undefined)
126
+ throw new Error("n is undefined");
127
+ if (n === null)
128
+ throw new Error("n is null");
129
+ return BigInt(n);
130
+ }
131
+ catch (e) {
132
+ // eslint-disable-next-line no-console
133
+ console.error("bigNumberify error", e);
134
+ return undefined;
135
+ }
136
+ }
137
+ exports.bigNumberify = bigNumberify;