@gainsnetwork/sdk 0.0.14-rc1 → 0.0.15
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.
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { TradeContainer } from "@/trade/types";
|
|
2
2
|
import { Contracts } from "@/contracts/types";
|
|
3
|
-
export declare const fetchOpenPairTrades: (contracts: Contracts, pairBatchSize?: number
|
|
3
|
+
export declare const fetchOpenPairTrades: (contracts: Contracts, pairBatchSize?: number) => Promise<TradeContainer[]>;
|
|
@@ -10,11 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.fetchOpenPairTrades = void 0;
|
|
13
|
-
|
|
14
|
-
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
15
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
16
|
-
const ethers_multicall_1 = require("ethers-multicall");
|
|
17
|
-
const fetchOpenPairTrades = (contracts, pairBatchSize = 50, useMulticall = false) => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
|
+
const fetchOpenPairTrades = (contracts, pairBatchSize = 10) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
14
|
if (!contracts) {
|
|
19
15
|
return [];
|
|
20
16
|
}
|
|
@@ -24,9 +20,7 @@ const fetchOpenPairTrades = (contracts, pairBatchSize = 50, useMulticall = false
|
|
|
24
20
|
let allOpenPairTrades = [];
|
|
25
21
|
for (let batchStartPairIndex = 0; batchStartPairIndex < totalPairIndexes; batchStartPairIndex += pairBatchSize) {
|
|
26
22
|
const batchEndPairIndex = Math.min(batchStartPairIndex + pairBatchSize - 1, totalPairIndexes);
|
|
27
|
-
const openPairTradesBatch =
|
|
28
|
-
? yield fetchOpenPairTradesBatchMulticall(contracts, batchStartPairIndex, batchEndPairIndex)
|
|
29
|
-
: yield fetchOpenPairTradesBatch(contracts, batchStartPairIndex, batchEndPairIndex);
|
|
23
|
+
const openPairTradesBatch = yield fetchOpenPairTradesBatch(contracts, batchStartPairIndex, batchEndPairIndex);
|
|
30
24
|
allOpenPairTrades = allOpenPairTrades.concat(openPairTradesBatch);
|
|
31
25
|
}
|
|
32
26
|
console.info(`Fetched ${allOpenPairTrades.length} total open pair trade(s).`);
|
|
@@ -39,7 +33,7 @@ const fetchOpenPairTrades = (contracts, pairBatchSize = 50, useMulticall = false
|
|
|
39
33
|
});
|
|
40
34
|
exports.fetchOpenPairTrades = fetchOpenPairTrades;
|
|
41
35
|
const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
-
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract
|
|
36
|
+
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract } = contracts;
|
|
43
37
|
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
44
38
|
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
45
39
|
const rawTrades = yield Promise.all(pairIndexesToFetch.map((pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -75,18 +69,12 @@ const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __
|
|
|
75
69
|
for (let tradeIndex = 0; tradeIndex < actualOpenTradesForTrader.length; tradeIndex++) {
|
|
76
70
|
const tradeInfo = actualOpenTradesTradeInfos[tradeIndex];
|
|
77
71
|
if (tradeInfo === undefined) {
|
|
78
|
-
|
|
79
|
-
// "No trade info found for open trade while fetching open trades!",
|
|
80
|
-
// { trade: actualOpenTradesForTrader[tradeIndex] }
|
|
81
|
-
// );
|
|
72
|
+
console.error("No trade info found for open trade while fetching open trades!", { trade: actualOpenTradesForTrader[tradeIndex] });
|
|
82
73
|
continue;
|
|
83
74
|
}
|
|
84
75
|
const tradeInitialAccFees = actualOpenTradesInitialAccFees[tradeIndex];
|
|
85
76
|
if (tradeInitialAccFees === undefined) {
|
|
86
|
-
|
|
87
|
-
// "No initial fees found for open trade while fetching open trades!",
|
|
88
|
-
// { trade: actualOpenTradesForTrader[tradeIndex] }
|
|
89
|
-
// );
|
|
77
|
+
console.error("No initial fees found for open trade while fetching open trades!", { trade: actualOpenTradesForTrader[tradeIndex] });
|
|
90
78
|
continue;
|
|
91
79
|
}
|
|
92
80
|
const trade = actualOpenTradesForTrader[tradeIndex];
|
|
@@ -126,77 +114,3 @@ const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __
|
|
|
126
114
|
const perPairTrades = rawTrades.reduce((a, b) => a.concat(b), []);
|
|
127
115
|
return perPairTrades.reduce((a, b) => a.concat(b), []);
|
|
128
116
|
});
|
|
129
|
-
const fetchOpenPairTradesBatchMulticall = (contracts, startPairIndex, endPairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
130
|
-
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract, } = contracts;
|
|
131
|
-
// Convert to Multicall for efficient RPC usage
|
|
132
|
-
const chainId = (yield storageContract.provider.getNetwork()).chainId;
|
|
133
|
-
const multicallProvider = new ethers_multicall_1.Provider(storageContract.provider, chainId);
|
|
134
|
-
const storageContractMulticall = new ethers_multicall_1.Contract(storageContract.address, [
|
|
135
|
-
...storageContract.interface.fragments,
|
|
136
|
-
]);
|
|
137
|
-
const pairInfosContractMulticall = new ethers_multicall_1.Contract(pairInfosContract.address, [
|
|
138
|
-
...pairInfosContract.interface.fragments,
|
|
139
|
-
]);
|
|
140
|
-
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
141
|
-
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
142
|
-
const mcPairTraderAddresses = yield multicallProvider.all(pairIndexesToFetch.map(pairIndex => storageContractMulticall.pairTradersArray(pairIndex)));
|
|
143
|
-
const mcFlatOpenTrades = yield multicallProvider.all(mcPairTraderAddresses
|
|
144
|
-
.map((pairTraderAddresses, _ix) => {
|
|
145
|
-
return pairTraderAddresses
|
|
146
|
-
.map((pairTraderAddress) => {
|
|
147
|
-
const openTradesCalls = new Array(maxTradesPerPair);
|
|
148
|
-
for (let pairTradeIndex = 0; pairTradeIndex < maxTradesPerPair; pairTradeIndex++) {
|
|
149
|
-
openTradesCalls[pairTradeIndex] =
|
|
150
|
-
storageContractMulticall.openTrades(pairTraderAddress, _ix + startPairIndex, pairTradeIndex);
|
|
151
|
-
}
|
|
152
|
-
return openTradesCalls;
|
|
153
|
-
})
|
|
154
|
-
.reduce((acc, val) => acc.concat(val), []);
|
|
155
|
-
})
|
|
156
|
-
.reduce((acc, val) => acc.concat(val), []));
|
|
157
|
-
const openTrades = mcFlatOpenTrades.filter(openTrade => openTrade[0] !== "0x0000000000000000000000000000000000000000");
|
|
158
|
-
const [openTradesTradeInfos, openTradesInitialAccFees] = yield Promise.all([
|
|
159
|
-
multicallProvider.all(openTrades.map(openTrade => storageContractMulticall.openTradesInfo(openTrade.trader, openTrade.pairIndex, openTrade.index))),
|
|
160
|
-
multicallProvider.all(openTrades.map(openTrade => pairInfosContractMulticall.tradeInitialAccFees(openTrade.trader, openTrade.pairIndex, openTrade.index))),
|
|
161
|
-
]);
|
|
162
|
-
const finalTrades = new Array(openTrades.length);
|
|
163
|
-
for (let tradeIndex = 0; tradeIndex < openTradesTradeInfos.length; tradeIndex++) {
|
|
164
|
-
const tradeInfo = openTradesTradeInfos[tradeIndex];
|
|
165
|
-
if (tradeInfo === undefined) {
|
|
166
|
-
console.error("No trade info found for open trade while fetching open trades!", { trade: openTradesTradeInfos[tradeIndex] });
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
const tradeInitialAccFees = openTradesInitialAccFees[tradeIndex];
|
|
170
|
-
if (tradeInitialAccFees === undefined) {
|
|
171
|
-
console.error("No initial fees found for open trade while fetching open trades!", { trade: openTrades[tradeIndex] });
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
const trade = openTrades[tradeIndex];
|
|
175
|
-
finalTrades[tradeIndex] = {
|
|
176
|
-
trade: {
|
|
177
|
-
trader: trade.trader,
|
|
178
|
-
pairIndex: parseInt(trade.pairIndex.toString()),
|
|
179
|
-
index: parseInt(trade.index.toString()),
|
|
180
|
-
initialPosToken: parseFloat(trade.initialPosToken.toString()) / 1e18,
|
|
181
|
-
openPrice: parseFloat(trade.openPrice.toString()) / 1e10,
|
|
182
|
-
buy: trade.buy.toString() === "true",
|
|
183
|
-
leverage: parseInt(trade.leverage.toString()),
|
|
184
|
-
tp: parseFloat(trade.tp.toString()) / 1e10,
|
|
185
|
-
sl: parseFloat(trade.sl.toString()) / 1e10,
|
|
186
|
-
},
|
|
187
|
-
tradeInfo: {
|
|
188
|
-
beingMarketClosed: tradeInfo.beingMarketClosed.toString() === "true",
|
|
189
|
-
tokenPriceDai: parseFloat(tradeInfo.tokenPriceDai.toString()) / 1e10,
|
|
190
|
-
openInterestDai: parseFloat(tradeInfo.openInterestDai.toString()) / 1e18,
|
|
191
|
-
tpLastUpdated: tradeInfo.tpLastUpdated,
|
|
192
|
-
slLastUpdated: tradeInfo.slLastUpdated,
|
|
193
|
-
},
|
|
194
|
-
initialAccFees: {
|
|
195
|
-
rollover: parseFloat(tradeInitialAccFees.rollover.toString()) / 1e18,
|
|
196
|
-
funding: parseFloat(tradeInitialAccFees.funding.toString()) / 1e18,
|
|
197
|
-
openedAfterUpdate: tradeInitialAccFees.openedAfterUpdate.toString() === "true",
|
|
198
|
-
},
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
return finalTrades.filter(trade => trade !== undefined);
|
|
202
|
-
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gainsnetwork/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"description": "Gains Network SDK",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -98,7 +98,6 @@
|
|
|
98
98
|
"dependencies": {
|
|
99
99
|
"@ethersproject/providers": "^5.7.2",
|
|
100
100
|
"ethers": "^5.7.2",
|
|
101
|
-
"ethers-multicall": "^0.2.3",
|
|
102
101
|
"luxon": "^3.2.0"
|
|
103
102
|
}
|
|
104
103
|
}
|