@gainsnetwork/sdk 0.1.28-rc1 → 0.2.0-rc1
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/lib/constants.d.ts +1 -8
- package/lib/constants.js +3 -9
- package/lib/contracts/addresses.json +8 -44
- package/lib/contracts/index.js +1 -4
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +4608 -1133
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +190 -43
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +9988 -1722
- package/lib/contracts/types/generated/factories/index.d.ts +0 -7
- package/lib/contracts/types/generated/factories/index.js +1 -15
- package/lib/contracts/types/generated/index.d.ts +0 -14
- package/lib/contracts/types/generated/index.js +1 -15
- package/lib/contracts/types/index.d.ts +2 -10
- package/lib/contracts/utils/borrowingFees.d.ts +4 -4
- package/lib/contracts/utils/borrowingFees.js +9 -5
- package/lib/contracts/utils/index.d.ts +0 -1
- package/lib/contracts/utils/index.js +0 -1
- package/lib/contracts/utils/openTrades.d.ts +1 -1
- package/lib/contracts/utils/openTrades.js +330 -138
- package/lib/contracts/utils/pairs.d.ts +1 -1
- package/lib/contracts/utils/pairs.js +27 -23
- package/lib/trade/fees/borrowing/converter.d.ts +12 -10
- package/lib/trade/fees/borrowing/converter.js +18 -24
- package/lib/trade/fees/borrowing/index.d.ts +3 -4
- package/lib/trade/fees/borrowing/index.js +12 -18
- package/lib/trade/fees/borrowing/types.d.ts +14 -20
- package/lib/trade/fees/index.js +2 -2
- package/lib/trade/index.d.ts +0 -1
- package/lib/trade/index.js +0 -1
- package/lib/trade/liquidation.d.ts +3 -3
- package/lib/trade/liquidation.js +4 -5
- package/lib/trade/pnl.js +12 -12
- package/lib/trade/types.d.ts +33 -69
- package/lib/trade/types.js +9 -16
- package/package.json +1 -1
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +0 -1911
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +0 -1067
- package/lib/contracts/types/generated/GNSBorrowingFees.js +0 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +0 -533
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +0 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +0 -613
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +0 -2
- package/lib/contracts/types/generated/GNSTrading.d.ts +0 -758
- package/lib/contracts/types/generated/GNSTrading.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +0 -875
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +0 -2
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +0 -1387
- package/lib/contracts/types/generated/GNSTradingStorage.js +0 -2
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +0 -83
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +0 -2691
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +0 -124
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +0 -1784
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +0 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +0 -1116
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +0 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +0 -1003
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +0 -113
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +0 -1428
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +0 -96
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +0 -2241
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +0 -95
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +0 -1071
- package/lib/contracts/utils/openLimitOrders.d.ts +0 -8
- package/lib/contracts/utils/openLimitOrders.js +0 -88
- package/lib/trade/openLimitOrder.d.ts +0 -2
- package/lib/trade/openLimitOrder.js +0 -23
|
@@ -10,32 +10,48 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.fetchOpenPairTradesRaw = exports.fetchOpenPairTrades = void 0;
|
|
13
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
14
|
-
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
15
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
16
|
-
const ethcall_1 = require("ethcall");
|
|
17
13
|
const fetchOpenPairTrades = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
14
|
const rawTrades = yield (0, exports.fetchOpenPairTradesRaw)(contracts, overrides);
|
|
19
|
-
const
|
|
20
|
-
return rawTrades.map(rawTrade => _prepareTradeContainer(rawTrade.trade, rawTrade.tradeInfo, rawTrade.initialAccFees, rawTrade.
|
|
15
|
+
const collateralPrecisions = (yield contracts.gnsMultiCollatDiamond.getCollaterals()).map(({ precision }) => precision);
|
|
16
|
+
return rawTrades.map(rawTrade => _prepareTradeContainer(rawTrade.trade, rawTrade.tradeInfo, rawTrade.initialAccFees, collateralPrecisions[parseInt(rawTrade.trade.collateralIndex.toString()) - 1]));
|
|
21
17
|
});
|
|
22
18
|
exports.fetchOpenPairTrades = fetchOpenPairTrades;
|
|
19
|
+
// @todo rename
|
|
23
20
|
const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
21
|
if (!contracts) {
|
|
25
22
|
return [];
|
|
26
23
|
}
|
|
27
|
-
const {
|
|
24
|
+
const { batchSize = 50,
|
|
25
|
+
// useMulticall = false,
|
|
26
|
+
// blockTag = "latest",
|
|
27
|
+
} = overrides;
|
|
28
28
|
const { gnsMultiCollatDiamond: multiCollatDiamondContract } = contracts;
|
|
29
29
|
try {
|
|
30
|
-
const totalPairIndexes = (yield multiCollatDiamondContract.pairsCount({ blockTag })).toNumber() -
|
|
31
|
-
1;
|
|
32
30
|
let allOpenPairTrades = [];
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
let running = true;
|
|
32
|
+
let offset = 0;
|
|
33
|
+
while (running) {
|
|
34
|
+
console.log("fetching with", offset, batchSize);
|
|
35
|
+
const trades = yield multiCollatDiamondContract.getAllTrades(offset, offset + batchSize);
|
|
36
|
+
const tradeInfos = yield multiCollatDiamondContract.getAllTradeInfos(offset, offset + batchSize);
|
|
37
|
+
// Array is always of length `batchSize`
|
|
38
|
+
// so we need to filter out the empty trades, indexes are reliable
|
|
39
|
+
const openTrades = trades
|
|
40
|
+
.filter(t => t.collateralIndex > 0)
|
|
41
|
+
.map((trade, ix) => ({
|
|
42
|
+
trade,
|
|
43
|
+
tradeInfo: tradeInfos[ix],
|
|
44
|
+
initialAccFees: {
|
|
45
|
+
accPairFee: 0,
|
|
46
|
+
accGroupFee: 0,
|
|
47
|
+
block: 0,
|
|
48
|
+
__placeholder: 0,
|
|
49
|
+
}, // @todo fetch initialAccFees
|
|
50
|
+
}));
|
|
51
|
+
allOpenPairTrades = allOpenPairTrades.concat(openTrades);
|
|
52
|
+
offset += batchSize + 1;
|
|
53
|
+
running =
|
|
54
|
+
parseInt(trades[trades.length - 1].collateralIndex.toString()) > 0;
|
|
39
55
|
}
|
|
40
56
|
return allOpenPairTrades;
|
|
41
57
|
}
|
|
@@ -45,144 +61,320 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
|
|
|
45
61
|
}
|
|
46
62
|
});
|
|
47
63
|
exports.fetchOpenPairTradesRaw = fetchOpenPairTradesRaw;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
// @todo deprecate
|
|
65
|
+
/*
|
|
66
|
+
const fetchOpenPairTradesBatch = async (
|
|
67
|
+
contracts: Contracts,
|
|
68
|
+
startPairIndex: number,
|
|
69
|
+
endPairIndex: number
|
|
70
|
+
): Promise<TradeContainerRaw[]> => {
|
|
71
|
+
const {
|
|
72
|
+
gfarmTradingStorageV5: storageContract,
|
|
73
|
+
gnsBorrowingFees: borrowingFeesContract,
|
|
74
|
+
gnsTradingCallbacks: callbacksContract,
|
|
75
|
+
} = contracts;
|
|
76
|
+
|
|
77
|
+
const maxTradesPerPair = (
|
|
78
|
+
await storageContract.maxTradesPerPair()
|
|
79
|
+
).toNumber();
|
|
80
|
+
|
|
81
|
+
const pairIndexesToFetch = Array.from(
|
|
82
|
+
{ length: endPairIndex - startPairIndex + 1 },
|
|
83
|
+
(_, i) => i + startPairIndex
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const rawTrades = await Promise.all(
|
|
87
|
+
pairIndexesToFetch.map(async pairIndex => {
|
|
88
|
+
const pairTraderAddresses = await storageContract.pairTradersArray(
|
|
89
|
+
pairIndex
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
if (pairTraderAddresses.length === 0) {
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const openTradesForPairTraders = await Promise.all(
|
|
97
|
+
pairTraderAddresses.map(async pairTraderAddress => {
|
|
98
|
+
const openTradesCalls = new Array(maxTradesPerPair);
|
|
99
|
+
for (
|
|
100
|
+
let pairTradeIndex = 0;
|
|
101
|
+
pairTradeIndex < maxTradesPerPair;
|
|
102
|
+
pairTradeIndex++
|
|
103
|
+
) {
|
|
104
|
+
openTradesCalls[pairTradeIndex] = storageContract.openTrades(
|
|
105
|
+
pairTraderAddress,
|
|
106
|
+
pairIndex,
|
|
107
|
+
pairTradeIndex
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const openTradesForTraderAddress = await Promise.all(openTradesCalls);
|
|
112
|
+
|
|
113
|
+
// Filter out any of the trades that aren't *really* open (NOTE: these will have an empty trader address, so just test against that)
|
|
114
|
+
const actualOpenTradesForTrader = openTradesForTraderAddress.filter(
|
|
115
|
+
openTrade => openTrade.trader === pairTraderAddress
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const [
|
|
119
|
+
actualOpenTradesTradeInfos,
|
|
120
|
+
actualOpenTradesInitialAccFees,
|
|
121
|
+
actualOpenTradesTradeData,
|
|
122
|
+
] = await Promise.all([
|
|
123
|
+
Promise.all(
|
|
124
|
+
actualOpenTradesForTrader.map(aot =>
|
|
125
|
+
storageContract.openTradesInfo(
|
|
126
|
+
aot.trader,
|
|
127
|
+
aot.pairIndex,
|
|
128
|
+
aot.index
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
),
|
|
132
|
+
Promise.all(
|
|
133
|
+
actualOpenTradesForTrader.map(aot =>
|
|
134
|
+
borrowingFeesContract.initialAccFees(
|
|
135
|
+
aot.trader,
|
|
136
|
+
aot.pairIndex,
|
|
137
|
+
aot.index
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
),
|
|
141
|
+
Promise.all(
|
|
142
|
+
actualOpenTradesForTrader.map(aot =>
|
|
143
|
+
callbacksContract.tradeData(
|
|
144
|
+
aot.trader,
|
|
145
|
+
aot.pairIndex,
|
|
146
|
+
aot.index,
|
|
147
|
+
0
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
),
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
const finalOpenTradesForTrader = new Array(
|
|
154
|
+
actualOpenTradesForTrader.length
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
for (
|
|
158
|
+
let tradeIndex = 0;
|
|
159
|
+
tradeIndex < actualOpenTradesForTrader.length;
|
|
160
|
+
tradeIndex++
|
|
161
|
+
) {
|
|
162
|
+
const tradeInfo = actualOpenTradesTradeInfos[tradeIndex];
|
|
163
|
+
|
|
164
|
+
if (tradeInfo === undefined) {
|
|
165
|
+
continue;
|
|
61
166
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const [actualOpenTradesTradeInfos, actualOpenTradesInitialAccFees, actualOpenTradesTradeData,] = yield Promise.all([
|
|
66
|
-
Promise.all(actualOpenTradesForTrader.map(aot => storageContract.openTradesInfo(aot.trader, aot.pairIndex, aot.index))),
|
|
67
|
-
Promise.all(actualOpenTradesForTrader.map(aot => borrowingFeesContract.initialAccFees(aot.trader, aot.pairIndex, aot.index))),
|
|
68
|
-
Promise.all(actualOpenTradesForTrader.map(aot => callbacksContract.tradeData(aot.trader, aot.pairIndex, aot.index, 0))),
|
|
69
|
-
]);
|
|
70
|
-
const finalOpenTradesForTrader = new Array(actualOpenTradesForTrader.length);
|
|
71
|
-
for (let tradeIndex = 0; tradeIndex < actualOpenTradesForTrader.length; tradeIndex++) {
|
|
72
|
-
const tradeInfo = actualOpenTradesTradeInfos[tradeIndex];
|
|
73
|
-
if (tradeInfo === undefined) {
|
|
74
|
-
continue;
|
|
75
|
-
}
|
|
76
|
-
if (actualOpenTradesInitialAccFees[tradeIndex] === undefined) {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
const trade = actualOpenTradesForTrader[tradeIndex];
|
|
80
|
-
const tradeData = actualOpenTradesTradeData[tradeIndex];
|
|
81
|
-
finalOpenTradesForTrader[tradeIndex] = {
|
|
82
|
-
trade,
|
|
83
|
-
tradeInfo,
|
|
84
|
-
initialAccFees: {
|
|
85
|
-
borrowing: actualOpenTradesInitialAccFees[tradeIndex],
|
|
86
|
-
},
|
|
87
|
-
tradeData,
|
|
88
|
-
};
|
|
167
|
+
|
|
168
|
+
if (actualOpenTradesInitialAccFees[tradeIndex] === undefined) {
|
|
169
|
+
continue;
|
|
89
170
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
171
|
+
|
|
172
|
+
const trade = actualOpenTradesForTrader[tradeIndex];
|
|
173
|
+
const tradeData = actualOpenTradesTradeData[tradeIndex];
|
|
174
|
+
|
|
175
|
+
finalOpenTradesForTrader[tradeIndex] = {
|
|
176
|
+
trade,
|
|
177
|
+
tradeInfo,
|
|
178
|
+
initialAccFees: {
|
|
179
|
+
borrowing: actualOpenTradesInitialAccFees[tradeIndex],
|
|
180
|
+
},
|
|
181
|
+
tradeData,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return finalOpenTradesForTrader;
|
|
186
|
+
})
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
return openTradesForPairTraders;
|
|
190
|
+
})
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
const perPairTrades = rawTrades.reduce((a, b) => a.concat(b), []);
|
|
194
|
+
return perPairTrades.reduce((a, b) => a.concat(b), []);
|
|
195
|
+
};
|
|
196
|
+
*/
|
|
197
|
+
// @todo deprecate
|
|
198
|
+
/*
|
|
199
|
+
const fetchOpenPairTradesBatchMulticall = async (
|
|
200
|
+
contracts: Contracts,
|
|
201
|
+
startPairIndex: number,
|
|
202
|
+
endPairIndex: number,
|
|
203
|
+
blockTag: BlockTag
|
|
204
|
+
): Promise<TradeContainerRaw[]> => {
|
|
205
|
+
const {
|
|
206
|
+
gfarmTradingStorageV5: storageContract,
|
|
207
|
+
gnsBorrowingFees: borrowingFeesContract,
|
|
208
|
+
gnsTradingCallbacks: callbacksContract,
|
|
209
|
+
} = contracts;
|
|
210
|
+
|
|
211
|
+
// Convert to Multicall for efficient RPC usage
|
|
212
|
+
const multicallProvider = new Provider();
|
|
213
|
+
await multicallProvider.init(storageContract.provider);
|
|
214
|
+
const storageContractMulticall = new Contract(storageContract.address, [
|
|
215
|
+
...storageContract.interface.fragments,
|
|
216
|
+
]);
|
|
217
|
+
const borrowingFeesContractMulticall = new Contract(
|
|
218
|
+
borrowingFeesContract.address,
|
|
219
|
+
[...borrowingFeesContract.interface.fragments]
|
|
220
|
+
);
|
|
221
|
+
const callbacksContractMulticall = new Contract(callbacksContract.address, [
|
|
222
|
+
...callbacksContract.interface.fragments,
|
|
223
|
+
]);
|
|
224
|
+
|
|
225
|
+
const maxTradesPerPair = (
|
|
226
|
+
await storageContract.maxTradesPerPair()
|
|
227
|
+
).toNumber();
|
|
228
|
+
|
|
229
|
+
const pairIndexesToFetch = Array.from(
|
|
230
|
+
{ length: endPairIndex - startPairIndex + 1 },
|
|
231
|
+
(_, i) => i + startPairIndex
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const mcPairTraderAddresses: string[][] = await multicallProvider.all(
|
|
235
|
+
pairIndexesToFetch.map(pairIndex =>
|
|
236
|
+
storageContractMulticall.pairTradersArray(pairIndex)
|
|
237
|
+
),
|
|
238
|
+
blockTag
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
const mcFlatOpenTrades: any[] = await multicallProvider.all(
|
|
242
|
+
mcPairTraderAddresses
|
|
243
|
+
.map((pairTraderAddresses, _ix) => {
|
|
114
244
|
return pairTraderAddresses
|
|
115
|
-
|
|
116
|
-
const openTradesCalls = new Array(maxTradesPerPair);
|
|
117
|
-
for (
|
|
118
|
-
|
|
119
|
-
|
|
245
|
+
.map((pairTraderAddress: string) => {
|
|
246
|
+
const openTradesCalls: Call[] = new Array(maxTradesPerPair);
|
|
247
|
+
for (
|
|
248
|
+
let pairTradeIndex = 0;
|
|
249
|
+
pairTradeIndex < maxTradesPerPair;
|
|
250
|
+
pairTradeIndex++
|
|
251
|
+
) {
|
|
252
|
+
openTradesCalls[pairTradeIndex] =
|
|
253
|
+
storageContractMulticall.openTrades(
|
|
254
|
+
pairTraderAddress,
|
|
255
|
+
_ix + startPairIndex,
|
|
256
|
+
pairTradeIndex
|
|
257
|
+
);
|
|
120
258
|
}
|
|
121
259
|
return openTradesCalls;
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
260
|
+
})
|
|
261
|
+
.reduce((acc, val) => acc.concat(val), []);
|
|
262
|
+
})
|
|
263
|
+
.reduce((acc, val) => acc.concat(val), [] as Call[]),
|
|
264
|
+
blockTag
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
const openTrades = mcFlatOpenTrades.filter(
|
|
268
|
+
openTrade => openTrade[0] !== "0x0000000000000000000000000000000000000000"
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
const [openTradesTradeInfos, openTradesInitialAccFees, openTradesTradeData] =
|
|
272
|
+
await Promise.all([
|
|
273
|
+
multicallProvider.all(
|
|
274
|
+
openTrades.map(openTrade =>
|
|
275
|
+
storageContractMulticall.openTradesInfo(
|
|
276
|
+
openTrade.trader,
|
|
277
|
+
openTrade.pairIndex,
|
|
278
|
+
openTrade.index
|
|
279
|
+
)
|
|
280
|
+
),
|
|
281
|
+
blockTag
|
|
282
|
+
),
|
|
283
|
+
multicallProvider.all<
|
|
284
|
+
Awaited<ReturnType<typeof borrowingFeesContract.initialAccFees>>
|
|
285
|
+
>(
|
|
286
|
+
openTrades.map(openTrade =>
|
|
287
|
+
borrowingFeesContractMulticall.initialAccFees(
|
|
288
|
+
openTrade.trader,
|
|
289
|
+
openTrade.pairIndex,
|
|
290
|
+
openTrade.index
|
|
291
|
+
)
|
|
292
|
+
),
|
|
293
|
+
blockTag
|
|
294
|
+
),
|
|
295
|
+
multicallProvider.all(
|
|
296
|
+
openTrades.map(openTrade =>
|
|
297
|
+
callbacksContractMulticall.tradeData(
|
|
298
|
+
openTrade.trader,
|
|
299
|
+
openTrade.pairIndex,
|
|
300
|
+
openTrade.index,
|
|
301
|
+
0
|
|
302
|
+
)
|
|
303
|
+
),
|
|
304
|
+
blockTag
|
|
305
|
+
),
|
|
131
306
|
]);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
borrowing: openTradesInitialAccFees[tradeIndex],
|
|
150
|
-
},
|
|
151
|
-
tradeData,
|
|
152
|
-
};
|
|
307
|
+
|
|
308
|
+
const finalTrades = new Array(openTrades.length);
|
|
309
|
+
|
|
310
|
+
for (
|
|
311
|
+
let tradeIndex = 0;
|
|
312
|
+
tradeIndex < openTradesTradeInfos.length;
|
|
313
|
+
tradeIndex++
|
|
314
|
+
) {
|
|
315
|
+
const tradeInfo = openTradesTradeInfos[tradeIndex];
|
|
316
|
+
|
|
317
|
+
if (tradeInfo === undefined) {
|
|
318
|
+
console.error(
|
|
319
|
+
"No trade info found for open trade while fetching open trades!",
|
|
320
|
+
{ trade: openTradesTradeInfos[tradeIndex] }
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
continue;
|
|
153
324
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
325
|
+
|
|
326
|
+
if (openTradesInitialAccFees[tradeIndex] === undefined) {
|
|
327
|
+
console.error(
|
|
328
|
+
"No initial fees found for open trade while fetching open trades!",
|
|
329
|
+
{ trade: openTrades[tradeIndex] }
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const trade = openTrades[tradeIndex];
|
|
336
|
+
const tradeData = openTradesTradeData[tradeIndex];
|
|
337
|
+
|
|
338
|
+
finalTrades[tradeIndex] = {
|
|
339
|
+
trade,
|
|
340
|
+
tradeInfo,
|
|
341
|
+
initialAccFees: {
|
|
342
|
+
borrowing: openTradesInitialAccFees[tradeIndex],
|
|
343
|
+
},
|
|
344
|
+
tradeData,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return finalTrades.filter(trade => trade !== undefined);
|
|
349
|
+
};
|
|
350
|
+
*/
|
|
351
|
+
const _prepareTradeContainer = (trade, tradeInfo, tradeInitialAccFees, collateralPrecision) => ({
|
|
157
352
|
trade: {
|
|
158
|
-
|
|
159
|
-
pairIndex: parseInt(trade.pairIndex.toString()),
|
|
353
|
+
user: trade.user,
|
|
160
354
|
index: parseInt(trade.index.toString()),
|
|
161
|
-
|
|
355
|
+
pairIndex: parseInt(trade.pairIndex.toString()),
|
|
356
|
+
leverage: parseFloat(trade.leverage.toString()) / 1e3,
|
|
357
|
+
long: trade.long.toString() === "true",
|
|
358
|
+
isOpen: trade.isOpen.toString() === "true",
|
|
359
|
+
collateralIndex: parseInt(trade.collateralIndex.toString()),
|
|
360
|
+
tradeType: trade.tradeType,
|
|
361
|
+
collateralAmount: parseFloat(trade.collateralAmount.toString()) /
|
|
362
|
+
parseFloat(collateralPrecision.toString()),
|
|
162
363
|
openPrice: parseFloat(trade.openPrice.toString()) / 1e10,
|
|
163
|
-
buy: trade.buy.toString() === "true",
|
|
164
|
-
leverage: parseInt(trade.leverage.toString()),
|
|
165
364
|
tp: parseFloat(trade.tp.toString()) / 1e10,
|
|
166
365
|
sl: parseFloat(trade.sl.toString()) / 1e10,
|
|
167
366
|
},
|
|
168
367
|
tradeInfo: {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
},
|
|
176
|
-
tradeData: {
|
|
177
|
-
maxSlippageP: parseFloat(tradeData.maxSlippageP.toString()) / 1e10,
|
|
178
|
-
lastOiUpdateTs: parseFloat(tradeData.lastOiUpdateTs),
|
|
179
|
-
collateralPriceUsd: parseFloat(tradeData.collateralPriceUsd.toString()) / 1e8,
|
|
368
|
+
createdBlock: parseInt(tradeInfo.createdBlock.toString()),
|
|
369
|
+
tpLastUpdatedBlock: parseInt(tradeInfo.tpLastUpdatedBlock.toString()),
|
|
370
|
+
slLastUpdatedBlock: parseInt(tradeInfo.slLastUpdatedBlock.toString()),
|
|
371
|
+
maxSlippageP: parseFloat(tradeInfo.maxSlippageP.toString()) / 1e10,
|
|
372
|
+
lastOiUpdateTs: parseFloat(tradeInfo.lastOiUpdateTs),
|
|
373
|
+
collateralPriceUsd: parseFloat(tradeInfo.collateralPriceUsd.toString()) / 1e8,
|
|
180
374
|
},
|
|
181
375
|
initialAccFees: {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
block: parseFloat(tradeInitialAccFees.borrowing.block.toString()),
|
|
186
|
-
},
|
|
376
|
+
accPairFee: parseFloat(tradeInitialAccFees.accPairFee.toString()) / 1e10,
|
|
377
|
+
accGroupFee: parseFloat(tradeInitialAccFees.accGroupFee.toString()) / 1e10,
|
|
378
|
+
block: parseFloat(tradeInitialAccFees.block.toString()),
|
|
187
379
|
},
|
|
188
380
|
});
|
|
@@ -3,5 +3,5 @@ import { Contracts } from "../../contracts/types";
|
|
|
3
3
|
export declare const fetchPairs: (contracts: Contracts, pairIxs: PairIndex[]) => Promise<Pair[]>;
|
|
4
4
|
export declare const fetchPairDepths: (contracts: Contracts, pairIxs: number[]) => Promise<PairDepth[]>;
|
|
5
5
|
export declare const fetchFees: (contracts: Contracts, feeIxs: PairIndex[]) => Promise<Fee[]>;
|
|
6
|
-
export declare const fetchOpenInterest: (contracts: Contracts, pairIxs: number[]) => Promise<OpenInterest[]>;
|
|
6
|
+
export declare const fetchOpenInterest: (collateralIndex: number, contracts: Contracts, pairIxs: number[]) => Promise<OpenInterest[]>;
|
|
7
7
|
export declare const getPairDescription: (pairIndex: PairIndex) => string;
|
|
@@ -69,10 +69,10 @@ const fetchFees = (contracts, feeIxs) => __awaiter(void 0, void 0, void 0, funct
|
|
|
69
69
|
const fees = yield Promise.all(feeIxs.map(pairIndex => multiCollatContract.fees(pairIndex)));
|
|
70
70
|
return fees.map(fee => {
|
|
71
71
|
return {
|
|
72
|
-
closeFeeP: parseFloat(fee.closeFeeP.toString()) / 1e12,
|
|
73
|
-
minLevPosUsd: parseFloat(fee.minLevPosUsd.toString()) / 1e18,
|
|
74
|
-
nftLimitOrderFeeP: parseFloat(fee.nftLimitOrderFeeP.toString()) / 1e12,
|
|
75
72
|
openFeeP: parseFloat(fee.openFeeP.toString()) / 1e12,
|
|
73
|
+
closeFeeP: parseFloat(fee.closeFeeP.toString()) / 1e12,
|
|
74
|
+
triggerOrderFeeP: parseFloat(fee.triggerOrderFeeP.toString()) / 1e12,
|
|
75
|
+
minPositionSizeUsd: parseFloat(fee.minPositionSizeUsd.toString()) / 1e18,
|
|
76
76
|
};
|
|
77
77
|
});
|
|
78
78
|
}
|
|
@@ -82,19 +82,30 @@ const fetchFees = (contracts, feeIxs) => __awaiter(void 0, void 0, void 0, funct
|
|
|
82
82
|
}
|
|
83
83
|
});
|
|
84
84
|
exports.fetchFees = fetchFees;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
])));
|
|
85
|
+
// @todo test
|
|
86
|
+
const fetchOpenInterest = (collateralIndex, contracts, pairIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
87
|
+
if (pairIxs.length === 0) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
const { precision: collateralPrecision, precisionDelta: collateralPrecisionDelta, } = yield contracts.gnsMultiCollatDiamond.getCollateral(collateralIndex);
|
|
92
91
|
const precision = parseFloat(collateralPrecision.toString());
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
92
|
+
const precisionDelta = parseFloat(collateralPrecisionDelta.toString());
|
|
93
|
+
const openInterests = (yield contracts.gnsMultiCollatDiamond.getAllBorrowingPairs(collateralIndex))[1];
|
|
94
|
+
return pairIxs.map(pairIndex => {
|
|
95
|
+
const openInterest = openInterests[pairIndex];
|
|
96
|
+
if (!openInterest) {
|
|
97
|
+
return { long: 0, short: 0, max: 0 };
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
long: (parseFloat(openInterest[0].toString()) * precisionDelta) /
|
|
101
|
+
1e10 /
|
|
102
|
+
precision,
|
|
103
|
+
short: (parseFloat(openInterest[1].toString()) * precisionDelta) /
|
|
104
|
+
1e10 /
|
|
105
|
+
precision,
|
|
106
|
+
max: parseFloat(openInterest[2].toString()) / 1e10,
|
|
107
|
+
};
|
|
108
|
+
});
|
|
98
109
|
});
|
|
99
110
|
exports.fetchOpenInterest = fetchOpenInterest;
|
|
100
111
|
const getPairDescription = (pairIndex) => {
|
|
@@ -323,13 +334,6 @@ const PAIR_INDEX_TO_DESCRIPTION = {
|
|
|
323
334
|
[types_1.PairIndex.RONINUSD]: "Ronin to US Dollar",
|
|
324
335
|
[types_1.PairIndex.ENAUSD]: "Ethena to US Dollar",
|
|
325
336
|
[types_1.PairIndex.WUSD]: "Wormhole to US Dollar",
|
|
326
|
-
[types_1.PairIndex.ZEUSUSD]: "Zeus to US Dollar",
|
|
327
337
|
[types_1.PairIndex.TNSRUSD]: "Tensor to US Dollar",
|
|
328
|
-
[types_1.PairIndex.
|
|
329
|
-
[types_1.PairIndex.OMNIUSD]: "Omni Network to US Dollar",
|
|
330
|
-
[types_1.PairIndex.PRCLUSD]: "Parcl to US Dollar",
|
|
331
|
-
[types_1.PairIndex.MERLUSD]: "Merlin Chain to US Dollar",
|
|
332
|
-
[types_1.PairIndex.SAFEUSD]: "Safe to US Dollar",
|
|
333
|
-
[types_1.PairIndex.SAGAUSD]: "Saga to US Dollar",
|
|
334
|
-
[types_1.PairIndex.LLUSD]: "Light Link to US Dollar",
|
|
338
|
+
[types_1.PairIndex.ZEUSUSD]: "Zeus to US Dollar",
|
|
335
339
|
};
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IBorrowingFees } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
2
2
|
import { BorrowingFee } from ".";
|
|
3
|
-
export declare const convertPairGroupBorrowingFee: (pairGroup:
|
|
4
|
-
export declare const convertPairBorrowingFee: (pair:
|
|
5
|
-
export declare const convertPairBorrowingFees: ([pairs, pairOi]: [
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
export declare const convertPairGroupBorrowingFee: (pairGroup: IBorrowingFees.BorrowingPairGroupStructOutput) => BorrowingFee.PairGroup;
|
|
4
|
+
export declare const convertPairBorrowingFee: (pair: IBorrowingFees.BorrowingDataStructOutput, pairOi: IBorrowingFees.OpenInterestStructOutput, pairGroup: IBorrowingFees.BorrowingPairGroupStructOutput[]) => BorrowingFee.Pair;
|
|
5
|
+
export declare const convertPairBorrowingFees: ([pairs, pairOi, pairGroups]: [
|
|
6
|
+
IBorrowingFees.BorrowingDataStructOutput[],
|
|
7
|
+
IBorrowingFees.OpenInterestStructOutput[],
|
|
8
|
+
IBorrowingFees.BorrowingPairGroupStructOutput[][]
|
|
8
9
|
]) => BorrowingFee.Pair[];
|
|
9
|
-
export declare const convertGroupBorrowingFee: (group:
|
|
10
|
-
export declare const
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
export declare const convertGroupBorrowingFee: (group: IBorrowingFees.BorrowingDataStructOutput, groupOi: IBorrowingFees.OpenInterestStructOutput) => BorrowingFee.Group;
|
|
11
|
+
export declare const convertGroupBorrowingData: (obj: IBorrowingFees.BorrowingDataStructOutput, oi: IBorrowingFees.OpenInterestStructOutput) => BorrowingFee.BorrowingData & BorrowingFee.BorrowingOi;
|
|
12
|
+
export declare const convertGroupBorrowingFees: ([groups, groupOis]: [
|
|
13
|
+
IBorrowingFees.BorrowingDataStructOutput[],
|
|
14
|
+
IBorrowingFees.OpenInterestStructOutput[]
|
|
13
15
|
]) => BorrowingFee.Group[];
|