@gmx-io/sdk 1.0.0-alpha.1 → 1.0.0-alpha.3
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 +5 -3
- package/build/cjs/src/configs/markets.js +7 -0
- package/build/cjs/src/configs/tokens.js +11 -0
- package/build/cjs/src/modules/trades/trades.js +27 -156
- package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +8 -0
- package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +50 -0
- package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +15 -0
- 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/configs/markets.js +7 -0
- package/build/esm/src/configs/tokens.js +11 -0
- package/build/esm/src/modules/trades/trades.js +25 -153
- package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +8 -0
- package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +50 -0
- package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +15 -0
- 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 +7 -7
|
@@ -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;
|