@gainsnetwork/sdk 0.0.16 → 0.0.18
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 +11 -11
- package/lib/contracts/addresses.d.ts +2 -2
- package/lib/contracts/addresses.js +15 -15
- package/lib/contracts/addresses.json +23 -20
- package/lib/contracts/index.d.ts +6 -6
- package/lib/contracts/index.js +32 -31
- package/lib/contracts/types/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/common.d.ts +22 -0
- package/lib/contracts/types/common.js +2 -0
- package/lib/contracts/types/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/factories/index.d.ts +3 -0
- package/lib/contracts/types/factories/index.js +12 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -1911
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -911
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -2
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -660
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -2
- package/lib/contracts/types/generated/GTokenOpenPnlFeed.d.ts +557 -557
- package/lib/contracts/types/generated/GTokenOpenPnlFeed.js +2 -2
- package/lib/contracts/types/generated/common.d.ts +22 -22
- package/lib/contracts/types/generated/common.js +2 -2
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -83
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -2691
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -98
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -1485
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -117
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -1265
- package/lib/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.d.ts +59 -59
- package/lib/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.js +765 -765
- package/lib/contracts/types/generated/factories/index.d.ts +5 -4
- package/lib/contracts/types/generated/factories/index.js +16 -14
- package/lib/contracts/types/generated/index.d.ts +11 -9
- package/lib/contracts/types/generated/index.js +37 -35
- package/lib/contracts/types/index.d.ts +16 -13
- package/lib/contracts/types/index.js +2 -2
- package/lib/contracts/utils/index.d.ts +3 -2
- package/lib/contracts/utils/index.js +19 -18
- package/lib/contracts/utils/openLimitOrders.d.ts +8 -0
- package/lib/contracts/utils/openLimitOrders.js +77 -0
- package/lib/contracts/utils/openTrades.d.ts +9 -3
- package/lib/contracts/utils/openTrades.js +169 -116
- package/lib/contracts/utils/openTradesOriginal.d.ts +3 -0
- package/lib/contracts/utils/openTradesOriginal.js +134 -0
- package/lib/contracts/utils/pairs.d.ts +6 -6
- package/lib/contracts/utils/pairs.js +102 -102
- package/lib/index.d.ts +3 -3
- package/lib/index.js +19 -19
- package/lib/markets/commodities.d.ts +1 -1
- package/lib/markets/commodities.js +31 -31
- package/lib/markets/crypto.d.ts +1 -1
- package/lib/markets/crypto.js +6 -6
- package/lib/markets/forex.d.ts +2 -2
- package/lib/markets/forex.js +38 -38
- package/lib/markets/index.d.ts +5 -5
- package/lib/markets/index.js +21 -21
- package/lib/markets/indices.d.ts +1 -1
- package/lib/markets/indices.js +6 -6
- package/lib/markets/stocks.d.ts +3 -3
- package/lib/markets/stocks.js +54 -54
- package/lib/src/contracts/addresses.d.ts +2 -0
- package/lib/src/contracts/addresses.js +15 -0
- package/lib/src/contracts/addresses.json +20 -0
- package/lib/src/contracts/index.d.ts +6 -0
- package/lib/src/contracts/index.js +31 -0
- package/lib/src/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/src/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/src/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/src/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/src/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/src/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/src/contracts/types/generated/GTokenOpenPnlFeed.d.ts +557 -0
- package/lib/src/contracts/types/generated/GTokenOpenPnlFeed.js +2 -0
- package/lib/src/contracts/types/generated/common.d.ts +22 -0
- package/lib/src/contracts/types/generated/common.js +2 -0
- package/lib/src/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/src/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/src/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/src/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/src/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/src/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/src/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.d.ts +59 -0
- package/lib/src/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.js +765 -0
- package/lib/src/contracts/types/generated/factories/index.d.ts +4 -0
- package/lib/src/contracts/types/generated/factories/index.js +14 -0
- package/lib/src/contracts/types/generated/index.d.ts +9 -0
- package/lib/src/contracts/types/generated/index.js +35 -0
- package/lib/src/contracts/types/index.d.ts +13 -0
- package/lib/src/contracts/types/index.js +2 -0
- package/lib/src/contracts/utils/index.d.ts +2 -0
- package/lib/src/contracts/utils/index.js +18 -0
- package/lib/src/contracts/utils/openTrades.d.ts +3 -0
- package/lib/src/contracts/utils/openTrades.js +107 -0
- package/lib/src/contracts/utils/pairs.d.ts +6 -0
- package/lib/src/contracts/utils/pairs.js +102 -0
- package/lib/src/index.d.ts +3 -0
- package/lib/src/index.js +19 -0
- package/lib/src/markets/commodities.d.ts +1 -0
- package/lib/src/markets/commodities.js +31 -0
- package/lib/src/markets/crypto.d.ts +1 -0
- package/lib/src/markets/crypto.js +6 -0
- package/lib/src/markets/forex.d.ts +2 -0
- package/lib/src/markets/forex.js +38 -0
- package/lib/src/markets/index.d.ts +5 -0
- package/lib/src/markets/index.js +21 -0
- package/lib/src/markets/indices.d.ts +1 -0
- package/lib/src/markets/indices.js +6 -0
- package/lib/src/markets/stocks.d.ts +3 -0
- package/lib/src/markets/stocks.js +54 -0
- package/lib/src/trade/fees.d.ts +15 -0
- package/lib/src/trade/fees.js +45 -0
- package/lib/src/trade/index.d.ts +2 -0
- package/lib/src/trade/index.js +18 -0
- package/lib/src/trade/pnl.d.ts +7 -0
- package/lib/src/trade/pnl.js +43 -0
- package/lib/src/trade/types.d.ts +143 -0
- package/lib/src/trade/types.js +8 -0
- package/lib/test/index.d.ts +1 -0
- package/lib/test/index.js +22 -0
- package/lib/test.d.ts +1 -0
- package/lib/test.js +37 -0
- package/lib/trade/fees.d.ts +15 -15
- package/lib/trade/fees.js +45 -45
- package/lib/trade/index.d.ts +2 -2
- package/lib/trade/index.js +18 -18
- package/lib/trade/pnl.d.ts +7 -7
- package/lib/trade/pnl.js +43 -43
- package/lib/trade/types.d.ts +157 -143
- package/lib/trade/types.js +8 -8
- package/package.json +104 -104
|
@@ -1,116 +1,169 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.fetchOpenPairTrades = void 0;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
Promise.all(actualOpenTradesForTrader.map(aot =>
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
+
const fetchOpenPairTrades = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
const rawTrades = yield (0, exports.fetchOpenPairTradesRaw)(contracts, overrides);
|
|
19
|
+
return rawTrades.map(rawTrade => _prepareTradeContainer(rawTrade.trade, rawTrade.tradeInfo, rawTrade.initialAccFees));
|
|
20
|
+
});
|
|
21
|
+
exports.fetchOpenPairTrades = fetchOpenPairTrades;
|
|
22
|
+
const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
+
if (!contracts) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
const { pairBatchSize = 10, useMulticall = false, blockTag = "latest", } = overrides;
|
|
27
|
+
const { gnsPairsStorageV6: pairsStorageContract } = contracts;
|
|
28
|
+
try {
|
|
29
|
+
const totalPairIndexes = (yield pairsStorageContract.pairsCount({ blockTag })).toNumber() - 1;
|
|
30
|
+
let allOpenPairTrades = [];
|
|
31
|
+
for (let batchStartPairIndex = 0; batchStartPairIndex < totalPairIndexes; batchStartPairIndex += pairBatchSize) {
|
|
32
|
+
const batchEndPairIndex = Math.min(batchStartPairIndex + pairBatchSize - 1, totalPairIndexes);
|
|
33
|
+
const openPairTradesBatch = useMulticall
|
|
34
|
+
? yield fetchOpenPairTradesBatchMulticall(contracts, batchStartPairIndex, batchEndPairIndex, blockTag)
|
|
35
|
+
: yield fetchOpenPairTradesBatch(contracts, batchStartPairIndex, batchEndPairIndex);
|
|
36
|
+
allOpenPairTrades = allOpenPairTrades.concat(openPairTradesBatch);
|
|
37
|
+
}
|
|
38
|
+
return allOpenPairTrades;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
console.error(`Unexpected error while fetching open pair trades!`);
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
exports.fetchOpenPairTradesRaw = fetchOpenPairTradesRaw;
|
|
46
|
+
const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
47
|
+
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract, } = contracts;
|
|
48
|
+
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
49
|
+
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
50
|
+
const rawTrades = yield Promise.all(pairIndexesToFetch.map((pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
+
const pairTraderAddresses = yield storageContract.pairTradersArray(pairIndex);
|
|
52
|
+
if (pairTraderAddresses.length === 0) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const openTradesForPairTraders = yield Promise.all(pairTraderAddresses.map((pairTraderAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
|
+
const openTradesCalls = new Array(maxTradesPerPair);
|
|
57
|
+
for (let pairTradeIndex = 0; pairTradeIndex < maxTradesPerPair; pairTradeIndex++) {
|
|
58
|
+
openTradesCalls[pairTradeIndex] = storageContract.openTrades(pairTraderAddress, pairIndex, pairTradeIndex);
|
|
59
|
+
}
|
|
60
|
+
const openTradesForTraderAddress = yield Promise.all(openTradesCalls);
|
|
61
|
+
// Filter out any of the trades that aren't *really* open (NOTE: these will have an empty trader address, so just test against that)
|
|
62
|
+
const actualOpenTradesForTrader = openTradesForTraderAddress.filter(openTrade => openTrade.trader === pairTraderAddress);
|
|
63
|
+
const [actualOpenTradesTradeInfos, actualOpenTradesInitialAccFees] = yield Promise.all([
|
|
64
|
+
Promise.all(actualOpenTradesForTrader.map(aot => storageContract.openTradesInfo(aot.trader, aot.pairIndex, aot.index))),
|
|
65
|
+
Promise.all(actualOpenTradesForTrader.map(aot => pairInfosContract.tradeInitialAccFees(aot.trader, aot.pairIndex, aot.index))),
|
|
66
|
+
]);
|
|
67
|
+
const finalOpenTradesForTrader = new Array(actualOpenTradesForTrader.length);
|
|
68
|
+
for (let tradeIndex = 0; tradeIndex < actualOpenTradesForTrader.length; tradeIndex++) {
|
|
69
|
+
const tradeInfo = actualOpenTradesTradeInfos[tradeIndex];
|
|
70
|
+
if (tradeInfo === undefined) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const tradeInitialAccFees = actualOpenTradesInitialAccFees[tradeIndex];
|
|
74
|
+
if (tradeInitialAccFees === undefined) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const trade = actualOpenTradesForTrader[tradeIndex];
|
|
78
|
+
finalOpenTradesForTrader[tradeIndex] = {
|
|
79
|
+
trade,
|
|
80
|
+
tradeInfo,
|
|
81
|
+
initialAccFees: tradeInitialAccFees,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return finalOpenTradesForTrader;
|
|
85
|
+
})));
|
|
86
|
+
return openTradesForPairTraders;
|
|
87
|
+
})));
|
|
88
|
+
const perPairTrades = rawTrades.reduce((a, b) => a.concat(b), []);
|
|
89
|
+
return perPairTrades.reduce((a, b) => a.concat(b), []);
|
|
90
|
+
});
|
|
91
|
+
const fetchOpenPairTradesBatchMulticall = (contracts, startPairIndex, endPairIndex, blockTag) => __awaiter(void 0, void 0, void 0, function* () {
|
|
92
|
+
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract, } = contracts;
|
|
93
|
+
// Convert to Multicall for efficient RPC usage
|
|
94
|
+
const multicallProvider = new ethcall_1.Provider();
|
|
95
|
+
yield multicallProvider.init(storageContract.provider);
|
|
96
|
+
const storageContractMulticall = new ethcall_1.Contract(storageContract.address, [
|
|
97
|
+
...storageContract.interface.fragments,
|
|
98
|
+
]);
|
|
99
|
+
const pairInfosContractMulticall = new ethcall_1.Contract(pairInfosContract.address, [
|
|
100
|
+
...pairInfosContract.interface.fragments,
|
|
101
|
+
]);
|
|
102
|
+
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
103
|
+
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
104
|
+
const mcPairTraderAddresses = yield multicallProvider.all(pairIndexesToFetch.map(pairIndex => storageContractMulticall.pairTradersArray(pairIndex)), blockTag);
|
|
105
|
+
const mcFlatOpenTrades = yield multicallProvider.all(mcPairTraderAddresses
|
|
106
|
+
.map((pairTraderAddresses, _ix) => {
|
|
107
|
+
return pairTraderAddresses
|
|
108
|
+
.map((pairTraderAddress) => {
|
|
109
|
+
const openTradesCalls = new Array(maxTradesPerPair);
|
|
110
|
+
for (let pairTradeIndex = 0; pairTradeIndex < maxTradesPerPair; pairTradeIndex++) {
|
|
111
|
+
openTradesCalls[pairTradeIndex] =
|
|
112
|
+
storageContractMulticall.openTrades(pairTraderAddress, _ix + startPairIndex, pairTradeIndex);
|
|
113
|
+
}
|
|
114
|
+
return openTradesCalls;
|
|
115
|
+
})
|
|
116
|
+
.reduce((acc, val) => acc.concat(val), []);
|
|
117
|
+
})
|
|
118
|
+
.reduce((acc, val) => acc.concat(val), []), blockTag);
|
|
119
|
+
const openTrades = mcFlatOpenTrades.filter(openTrade => openTrade[0] !== "0x0000000000000000000000000000000000000000");
|
|
120
|
+
const [openTradesTradeInfos, openTradesInitialAccFees] = yield Promise.all([
|
|
121
|
+
multicallProvider.all(openTrades.map(openTrade => storageContractMulticall.openTradesInfo(openTrade.trader, openTrade.pairIndex, openTrade.index)), blockTag),
|
|
122
|
+
multicallProvider.all(openTrades.map(openTrade => pairInfosContractMulticall.tradeInitialAccFees(openTrade.trader, openTrade.pairIndex, openTrade.index)), blockTag),
|
|
123
|
+
]);
|
|
124
|
+
const finalTrades = new Array(openTrades.length);
|
|
125
|
+
for (let tradeIndex = 0; tradeIndex < openTradesTradeInfos.length; tradeIndex++) {
|
|
126
|
+
const tradeInfo = openTradesTradeInfos[tradeIndex];
|
|
127
|
+
if (tradeInfo === undefined) {
|
|
128
|
+
console.error("No trade info found for open trade while fetching open trades!", { trade: openTradesTradeInfos[tradeIndex] });
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const tradeInitialAccFees = openTradesInitialAccFees[tradeIndex];
|
|
132
|
+
if (tradeInitialAccFees === undefined) {
|
|
133
|
+
console.error("No initial fees found for open trade while fetching open trades!", { trade: openTrades[tradeIndex] });
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const trade = openTrades[tradeIndex];
|
|
137
|
+
finalTrades[tradeIndex] = {
|
|
138
|
+
trade,
|
|
139
|
+
tradeInfo,
|
|
140
|
+
initialAccFees: tradeInitialAccFees,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return finalTrades.filter(trade => trade !== undefined);
|
|
144
|
+
});
|
|
145
|
+
const _prepareTradeContainer = (trade, tradeInfo, tradeInitialAccFees) => ({
|
|
146
|
+
trade: {
|
|
147
|
+
trader: trade.trader,
|
|
148
|
+
pairIndex: parseInt(trade.pairIndex.toString()),
|
|
149
|
+
index: parseInt(trade.index.toString()),
|
|
150
|
+
initialPosToken: parseFloat(trade.initialPosToken.toString()) / 1e18,
|
|
151
|
+
openPrice: parseFloat(trade.openPrice.toString()) / 1e10,
|
|
152
|
+
buy: trade.buy.toString() === "true",
|
|
153
|
+
leverage: parseInt(trade.leverage.toString()),
|
|
154
|
+
tp: parseFloat(trade.tp.toString()) / 1e10,
|
|
155
|
+
sl: parseFloat(trade.sl.toString()) / 1e10,
|
|
156
|
+
},
|
|
157
|
+
tradeInfo: {
|
|
158
|
+
beingMarketClosed: tradeInfo.beingMarketClosed.toString() === "true",
|
|
159
|
+
tokenPriceDai: parseFloat(tradeInfo.tokenPriceDai.toString()) / 1e10,
|
|
160
|
+
openInterestDai: parseFloat(tradeInfo.openInterestDai.toString()) / 1e18,
|
|
161
|
+
tpLastUpdated: tradeInfo.tpLastUpdated,
|
|
162
|
+
slLastUpdated: tradeInfo.slLastUpdated,
|
|
163
|
+
},
|
|
164
|
+
initialAccFees: {
|
|
165
|
+
rollover: parseFloat(tradeInitialAccFees.rollover.toString()) / 1e18,
|
|
166
|
+
funding: parseFloat(tradeInitialAccFees.funding.toString()) / 1e18,
|
|
167
|
+
openedAfterUpdate: tradeInitialAccFees.openedAfterUpdate.toString() === "true",
|
|
168
|
+
},
|
|
169
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.fetchOpenPairTradesOriginal = void 0;
|
|
13
|
+
const fetchOpenPairTradesOriginal = (contracts, pairBatchSize = 10) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
if (!contracts) {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
const { gnsPairsStorageV6: pairsStorageContract } = contracts;
|
|
18
|
+
try {
|
|
19
|
+
const totalPairIndexes = (yield pairsStorageContract.pairsCount()).toNumber() - 1;
|
|
20
|
+
let allOpenPairTrades = [];
|
|
21
|
+
for (let batchStartPairIndex = 0; batchStartPairIndex < totalPairIndexes; batchStartPairIndex += pairBatchSize) {
|
|
22
|
+
const batchEndPairIndex = Math.min(batchStartPairIndex + pairBatchSize - 1, totalPairIndexes);
|
|
23
|
+
const openPairTradesBatch = yield fetchOpenPairTradesBatchOriginal(contracts, batchStartPairIndex, batchEndPairIndex);
|
|
24
|
+
console.log("openPairTradesBatch", openPairTradesBatch.length);
|
|
25
|
+
allOpenPairTrades = allOpenPairTrades.concat(openPairTradesBatch);
|
|
26
|
+
}
|
|
27
|
+
return allOpenPairTrades;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.error(`Unexpected error while fetching open pair trades!`);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
exports.fetchOpenPairTradesOriginal = fetchOpenPairTradesOriginal;
|
|
35
|
+
const fetchOpenPairTradesBatchOriginal = (contracts, startPairIndex, endPairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
+
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract, } = contracts;
|
|
37
|
+
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
38
|
+
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
39
|
+
const rawTrades = yield Promise.all(pairIndexesToFetch.map((pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
+
// console.debug(`Fetching pair traders for pairIndex ${pairIndex}...`);
|
|
41
|
+
const pairTradersCallStartTime = performance.now();
|
|
42
|
+
const pairTraderAddresses = yield storageContract.pairTradersArray(pairIndex);
|
|
43
|
+
if (pairTraderAddresses.length === 0) {
|
|
44
|
+
// console.debug(
|
|
45
|
+
// `No pair traders found for pairIndex ${pairIndex}; no processing left to do!`
|
|
46
|
+
// );
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
// console.debug(
|
|
50
|
+
// `Fetched ${
|
|
51
|
+
// pairTraderAddresses.length
|
|
52
|
+
// } pair traders for pairIndex ${pairIndex} in ${
|
|
53
|
+
// performance.now() - pairTradersCallStartTime
|
|
54
|
+
// }ms; now fetching all open trades...`
|
|
55
|
+
// );
|
|
56
|
+
const openTradesForPairTraders = yield Promise.all(pairTraderAddresses.map((pairTraderAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
+
const openTradesCalls = new Array(maxTradesPerPair);
|
|
58
|
+
const traderOpenTradesCallsStartTime = performance.now();
|
|
59
|
+
for (let pairTradeIndex = 0; pairTradeIndex < maxTradesPerPair; pairTradeIndex++) {
|
|
60
|
+
openTradesCalls[pairTradeIndex] = storageContract.openTrades(pairTraderAddress, pairIndex, pairTradeIndex);
|
|
61
|
+
}
|
|
62
|
+
/*console.debug(
|
|
63
|
+
`Waiting on ${openTradesCalls.length} StorageContract::openTrades calls for trader ${pairTraderAddress}...`
|
|
64
|
+
);*/
|
|
65
|
+
const openTradesForTraderAddress = yield Promise.all(openTradesCalls);
|
|
66
|
+
// console.debug(
|
|
67
|
+
// `Received all trades for trader ${pairTraderAddress} and pair ${pairIndex} in ${
|
|
68
|
+
// performance.now() - traderOpenTradesCallsStartTime
|
|
69
|
+
// }ms.`
|
|
70
|
+
// );
|
|
71
|
+
// Filter out any of the trades that aren't *really* open (NOTE: these will have an empty trader address, so just test against that)
|
|
72
|
+
const actualOpenTradesForTrader = openTradesForTraderAddress.filter(openTrade => openTrade.trader === pairTraderAddress);
|
|
73
|
+
/*console.debug(
|
|
74
|
+
`Filtered down to ${actualOpenTradesForTrader.length} actual open trades for trader ${pairTraderAddress} and pair ${pairIndex}; fetching corresponding trade info and initial fees...`
|
|
75
|
+
);*/
|
|
76
|
+
const [actualOpenTradesTradeInfos, actualOpenTradesInitialAccFees] = yield Promise.all([
|
|
77
|
+
Promise.all(actualOpenTradesForTrader.map(aot => storageContract.openTradesInfo(aot.trader, aot.pairIndex, aot.index))),
|
|
78
|
+
Promise.all(actualOpenTradesForTrader.map(aot => pairInfosContract.tradeInitialAccFees(aot.trader, aot.pairIndex, aot.index))),
|
|
79
|
+
]);
|
|
80
|
+
const finalOpenTradesForTrader = new Array(actualOpenTradesForTrader.length);
|
|
81
|
+
for (let tradeIndex = 0; tradeIndex < actualOpenTradesForTrader.length; tradeIndex++) {
|
|
82
|
+
const tradeInfo = actualOpenTradesTradeInfos[tradeIndex];
|
|
83
|
+
if (tradeInfo === undefined) {
|
|
84
|
+
// console.error(
|
|
85
|
+
// "No trade info found for open trade while fetching open trades!",
|
|
86
|
+
// { trade: actualOpenTradesForTrader[tradeIndex] }
|
|
87
|
+
// );
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const tradeInitialAccFees = actualOpenTradesInitialAccFees[tradeIndex];
|
|
91
|
+
if (tradeInitialAccFees === undefined) {
|
|
92
|
+
// console.error(
|
|
93
|
+
// "No initial fees found for open trade while fetching open trades!",
|
|
94
|
+
// { trade: actualOpenTradesForTrader[tradeIndex] }
|
|
95
|
+
// );
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const trade = actualOpenTradesForTrader[tradeIndex];
|
|
99
|
+
finalOpenTradesForTrader[tradeIndex] = {
|
|
100
|
+
trade: {
|
|
101
|
+
trader: trade.trader,
|
|
102
|
+
pairIndex: parseInt(trade.pairIndex.toString()),
|
|
103
|
+
index: parseInt(trade.index.toString()),
|
|
104
|
+
initialPosToken: parseFloat(trade.initialPosToken.toString()) / 1e18,
|
|
105
|
+
openPrice: parseFloat(trade.openPrice.toString()) / 1e10,
|
|
106
|
+
buy: trade.buy.toString() === "true",
|
|
107
|
+
leverage: parseInt(trade.leverage.toString()),
|
|
108
|
+
tp: parseFloat(trade.tp.toString()) / 1e10,
|
|
109
|
+
sl: parseFloat(trade.sl.toString()) / 1e10,
|
|
110
|
+
},
|
|
111
|
+
tradeInfo: {
|
|
112
|
+
beingMarketClosed: tradeInfo.beingMarketClosed.toString() === "true",
|
|
113
|
+
tokenPriceDai: parseFloat(tradeInfo.tokenPriceDai.toString()) / 1e10,
|
|
114
|
+
openInterestDai: parseFloat(tradeInfo.openInterestDai.toString()) / 1e18,
|
|
115
|
+
tpLastUpdated: tradeInfo.tpLastUpdated,
|
|
116
|
+
slLastUpdated: tradeInfo.slLastUpdated,
|
|
117
|
+
},
|
|
118
|
+
initialAccFees: {
|
|
119
|
+
rollover: parseFloat(tradeInitialAccFees.rollover.toString()) / 1e18,
|
|
120
|
+
funding: parseFloat(tradeInitialAccFees.funding.toString()) / 1e18,
|
|
121
|
+
openedAfterUpdate: tradeInitialAccFees.openedAfterUpdate.toString() === "true",
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/*console.debug(
|
|
126
|
+
`Trade info and initial fees fetched for ${finalOpenTradesForTrader.length} trades for trader ${pairTraderAddress} and pair ${pairIndex}; done!`
|
|
127
|
+
);*/
|
|
128
|
+
return finalOpenTradesForTrader;
|
|
129
|
+
})));
|
|
130
|
+
return openTradesForPairTraders;
|
|
131
|
+
})));
|
|
132
|
+
const perPairTrades = rawTrades.reduce((a, b) => a.concat(b), []);
|
|
133
|
+
return perPairTrades.reduce((a, b) => a.concat(b), []);
|
|
134
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Pair, PairParams, PairRolloverFees, Fee } from "@/trade/types";
|
|
2
|
-
import { Contracts } from "@/contracts/types";
|
|
3
|
-
export declare const fetchPairs: (contracts: Contracts, pairIxs: number[]) => Promise<Pair[]>;
|
|
4
|
-
export declare const fetchPairsParams: (contracts: Contracts, pairIxs: number[]) => Promise<PairParams[]>;
|
|
5
|
-
export declare const fetchPairsRolloverFees: (contracts: Contracts, pairIxs: number[]) => Promise<PairRolloverFees[]>;
|
|
6
|
-
export declare const fetchFees: (contracts: Contracts, feeIxs: number[]) => Promise<Fee[]>;
|
|
1
|
+
import { Pair, PairParams, PairRolloverFees, Fee } from "@/trade/types";
|
|
2
|
+
import { Contracts } from "@/contracts/types";
|
|
3
|
+
export declare const fetchPairs: (contracts: Contracts, pairIxs: number[]) => Promise<Pair[]>;
|
|
4
|
+
export declare const fetchPairsParams: (contracts: Contracts, pairIxs: number[]) => Promise<PairParams[]>;
|
|
5
|
+
export declare const fetchPairsRolloverFees: (contracts: Contracts, pairIxs: number[]) => Promise<PairRolloverFees[]>;
|
|
6
|
+
export declare const fetchFees: (contracts: Contracts, feeIxs: number[]) => Promise<Fee[]>;
|