@gainsnetwork/sdk 0.0.18 → 0.0.19
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/constants.d.ts +135 -0
- package/lib/constants.js +123 -0
- package/lib/contracts/addresses.d.ts +2 -2
- package/lib/contracts/addresses.js +15 -15
- package/lib/contracts/addresses.json +23 -23
- package/lib/contracts/index.d.ts +6 -6
- package/lib/contracts/index.js +32 -32
- 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 -533
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -2
- 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 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -1003
- 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 -5
- package/lib/contracts/types/generated/factories/index.js +16 -16
- package/lib/contracts/types/generated/index.d.ts +11 -11
- package/lib/contracts/types/generated/index.js +37 -37
- package/lib/contracts/types/index.d.ts +16 -16
- package/lib/contracts/types/index.js +2 -2
- package/lib/contracts/utils/index.d.ts +3 -3
- package/lib/contracts/utils/index.js +19 -19
- package/lib/contracts/utils/openLimitOrders.d.ts +8 -8
- package/lib/contracts/utils/openLimitOrders.js +77 -77
- package/lib/contracts/utils/openTrades.d.ts +9 -9
- package/lib/contracts/utils/openTrades.js +193 -169
- package/lib/contracts/utils/pairs.d.ts +6 -6
- package/lib/contracts/utils/pairs.js +102 -102
- package/lib/index.d.ts +4 -3
- package/lib/index.js +20 -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/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 -157
- package/lib/trade/types.js +8 -8
- package/package.json +104 -104
- package/lib/contracts/types/GFarmTradingStorageV5.d.ts +0 -1911
- package/lib/contracts/types/GFarmTradingStorageV5.js +0 -2
- package/lib/contracts/types/GNSPairInfosV6_1.d.ts +0 -911
- package/lib/contracts/types/GNSPairInfosV6_1.js +0 -2
- package/lib/contracts/types/GNSPairsStorageV6.d.ts +0 -660
- package/lib/contracts/types/GNSPairsStorageV6.js +0 -2
- package/lib/contracts/types/common.d.ts +0 -22
- package/lib/contracts/types/common.js +0 -2
- package/lib/contracts/types/factories/GFarmTradingStorageV5__factory.d.ts +0 -83
- package/lib/contracts/types/factories/GFarmTradingStorageV5__factory.js +0 -2691
- package/lib/contracts/types/factories/GNSPairInfosV6_1__factory.d.ts +0 -98
- package/lib/contracts/types/factories/GNSPairInfosV6_1__factory.js +0 -1485
- package/lib/contracts/types/factories/GNSPairsStorageV6__factory.d.ts +0 -117
- package/lib/contracts/types/factories/GNSPairsStorageV6__factory.js +0 -1265
- package/lib/contracts/types/factories/index.d.ts +0 -3
- package/lib/contracts/types/factories/index.js +0 -12
- package/lib/contracts/utils/openTradesOriginal.d.ts +0 -3
- package/lib/contracts/utils/openTradesOriginal.js +0 -134
- package/lib/src/contracts/addresses.d.ts +0 -2
- package/lib/src/contracts/addresses.js +0 -15
- package/lib/src/contracts/addresses.json +0 -20
- package/lib/src/contracts/index.d.ts +0 -6
- package/lib/src/contracts/index.js +0 -31
- package/lib/src/contracts/types/generated/GFarmTradingStorageV5.d.ts +0 -1911
- package/lib/src/contracts/types/generated/GFarmTradingStorageV5.js +0 -2
- package/lib/src/contracts/types/generated/GNSPairInfosV6_1.d.ts +0 -911
- package/lib/src/contracts/types/generated/GNSPairInfosV6_1.js +0 -2
- package/lib/src/contracts/types/generated/GNSPairsStorageV6.d.ts +0 -660
- package/lib/src/contracts/types/generated/GNSPairsStorageV6.js +0 -2
- package/lib/src/contracts/types/generated/GTokenOpenPnlFeed.d.ts +0 -557
- package/lib/src/contracts/types/generated/GTokenOpenPnlFeed.js +0 -2
- package/lib/src/contracts/types/generated/common.d.ts +0 -22
- package/lib/src/contracts/types/generated/common.js +0 -2
- package/lib/src/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +0 -83
- package/lib/src/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +0 -2691
- package/lib/src/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +0 -98
- package/lib/src/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +0 -1485
- package/lib/src/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +0 -117
- package/lib/src/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +0 -1265
- package/lib/src/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.d.ts +0 -59
- package/lib/src/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.js +0 -765
- package/lib/src/contracts/types/generated/factories/index.d.ts +0 -4
- package/lib/src/contracts/types/generated/factories/index.js +0 -14
- package/lib/src/contracts/types/generated/index.d.ts +0 -9
- package/lib/src/contracts/types/generated/index.js +0 -35
- package/lib/src/contracts/types/index.d.ts +0 -13
- package/lib/src/contracts/types/index.js +0 -2
- package/lib/src/contracts/utils/index.d.ts +0 -2
- package/lib/src/contracts/utils/index.js +0 -18
- package/lib/src/contracts/utils/openTrades.d.ts +0 -3
- package/lib/src/contracts/utils/openTrades.js +0 -107
- package/lib/src/contracts/utils/pairs.d.ts +0 -6
- package/lib/src/contracts/utils/pairs.js +0 -102
- package/lib/src/index.d.ts +0 -3
- package/lib/src/index.js +0 -19
- package/lib/src/markets/commodities.d.ts +0 -1
- package/lib/src/markets/commodities.js +0 -31
- package/lib/src/markets/crypto.d.ts +0 -1
- package/lib/src/markets/crypto.js +0 -6
- package/lib/src/markets/forex.d.ts +0 -2
- package/lib/src/markets/forex.js +0 -38
- package/lib/src/markets/index.d.ts +0 -5
- package/lib/src/markets/index.js +0 -21
- package/lib/src/markets/indices.d.ts +0 -1
- package/lib/src/markets/indices.js +0 -6
- package/lib/src/markets/stocks.d.ts +0 -3
- package/lib/src/markets/stocks.js +0 -54
- package/lib/src/trade/fees.d.ts +0 -15
- package/lib/src/trade/fees.js +0 -45
- package/lib/src/trade/index.d.ts +0 -2
- package/lib/src/trade/index.js +0 -18
- package/lib/src/trade/pnl.d.ts +0 -7
- package/lib/src/trade/pnl.js +0 -43
- package/lib/src/trade/types.d.ts +0 -143
- package/lib/src/trade/types.js +0 -8
- package/lib/test/index.d.ts +0 -1
- package/lib/test/index.js +0 -22
- package/lib/test.d.ts +0 -1
- package/lib/test.js +0 -37
|
@@ -1,169 +1,193 @@
|
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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.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
|
+
console.info(`Fetched ${allOpenPairTrades.length} total open pair trade(s).`);
|
|
39
|
+
return allOpenPairTrades;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.error(`Unexpected error while fetching open pair trades!`);
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
exports.fetchOpenPairTradesRaw = fetchOpenPairTradesRaw;
|
|
47
|
+
const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
48
|
+
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract, } = contracts;
|
|
49
|
+
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
50
|
+
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
51
|
+
const rawTrades = yield Promise.all(pairIndexesToFetch.map((pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
52
|
+
console.debug(`Fetching pair traders for pairIndex ${pairIndex}...`);
|
|
53
|
+
const pairTradersCallStartTime = performance.now();
|
|
54
|
+
const pairTraderAddresses = yield storageContract.pairTradersArray(pairIndex);
|
|
55
|
+
if (pairTraderAddresses.length === 0) {
|
|
56
|
+
console.debug(`No pair traders found for pairIndex ${pairIndex}; no processing left to do!`);
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
console.debug(`Fetched ${pairTraderAddresses.length} pair traders for pairIndex ${pairIndex} in ${performance.now() - pairTradersCallStartTime}ms; now fetching all open trades...`);
|
|
60
|
+
const openTradesForPairTraders = yield Promise.all(pairTraderAddresses.map((pairTraderAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
const openTradesCalls = new Array(maxTradesPerPair);
|
|
62
|
+
const traderOpenTradesCallsStartTime = performance.now();
|
|
63
|
+
for (let pairTradeIndex = 0; pairTradeIndex < maxTradesPerPair; pairTradeIndex++) {
|
|
64
|
+
openTradesCalls[pairTradeIndex] = storageContract.openTrades(pairTraderAddress, pairIndex, pairTradeIndex);
|
|
65
|
+
}
|
|
66
|
+
/*console.debug(
|
|
67
|
+
`Waiting on ${openTradesCalls.length} StorageContract::openTrades calls for trader ${pairTraderAddress}...`
|
|
68
|
+
);*/
|
|
69
|
+
const openTradesForTraderAddress = yield Promise.all(openTradesCalls);
|
|
70
|
+
console.debug(`Received all trades for trader ${pairTraderAddress} and pair ${pairIndex} in ${performance.now() - traderOpenTradesCallsStartTime}ms.`);
|
|
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
|
+
tradeInfo,
|
|
102
|
+
initialAccFees: tradeInitialAccFees,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/*console.debug(
|
|
106
|
+
`Trade info and initial fees fetched for ${finalOpenTradesForTrader.length} trades for trader ${pairTraderAddress} and pair ${pairIndex}; done!`
|
|
107
|
+
);*/
|
|
108
|
+
return finalOpenTradesForTrader;
|
|
109
|
+
})));
|
|
110
|
+
return openTradesForPairTraders;
|
|
111
|
+
})));
|
|
112
|
+
const perPairTrades = rawTrades.reduce((a, b) => a.concat(b), []);
|
|
113
|
+
return perPairTrades.reduce((a, b) => a.concat(b), []);
|
|
114
|
+
});
|
|
115
|
+
const fetchOpenPairTradesBatchMulticall = (contracts, startPairIndex, endPairIndex, blockTag) => __awaiter(void 0, void 0, void 0, function* () {
|
|
116
|
+
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract, } = contracts;
|
|
117
|
+
// Convert to Multicall for efficient RPC usage
|
|
118
|
+
const multicallProvider = new ethcall_1.Provider();
|
|
119
|
+
yield multicallProvider.init(storageContract.provider);
|
|
120
|
+
const storageContractMulticall = new ethcall_1.Contract(storageContract.address, [
|
|
121
|
+
...storageContract.interface.fragments,
|
|
122
|
+
]);
|
|
123
|
+
const pairInfosContractMulticall = new ethcall_1.Contract(pairInfosContract.address, [
|
|
124
|
+
...pairInfosContract.interface.fragments,
|
|
125
|
+
]);
|
|
126
|
+
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
127
|
+
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
128
|
+
const mcPairTraderAddresses = yield multicallProvider.all(pairIndexesToFetch.map(pairIndex => storageContractMulticall.pairTradersArray(pairIndex)), blockTag);
|
|
129
|
+
const mcFlatOpenTrades = yield multicallProvider.all(mcPairTraderAddresses
|
|
130
|
+
.map((pairTraderAddresses, _ix) => {
|
|
131
|
+
return pairTraderAddresses
|
|
132
|
+
.map((pairTraderAddress) => {
|
|
133
|
+
const openTradesCalls = new Array(maxTradesPerPair);
|
|
134
|
+
for (let pairTradeIndex = 0; pairTradeIndex < maxTradesPerPair; pairTradeIndex++) {
|
|
135
|
+
openTradesCalls[pairTradeIndex] =
|
|
136
|
+
storageContractMulticall.openTrades(pairTraderAddress, _ix + startPairIndex, pairTradeIndex);
|
|
137
|
+
}
|
|
138
|
+
return openTradesCalls;
|
|
139
|
+
})
|
|
140
|
+
.reduce((acc, val) => acc.concat(val), []);
|
|
141
|
+
})
|
|
142
|
+
.reduce((acc, val) => acc.concat(val), []), blockTag);
|
|
143
|
+
const openTrades = mcFlatOpenTrades.filter(openTrade => openTrade[0] !== "0x0000000000000000000000000000000000000000");
|
|
144
|
+
const [openTradesTradeInfos, openTradesInitialAccFees] = yield Promise.all([
|
|
145
|
+
multicallProvider.all(openTrades.map(openTrade => storageContractMulticall.openTradesInfo(openTrade.trader, openTrade.pairIndex, openTrade.index)), blockTag),
|
|
146
|
+
multicallProvider.all(openTrades.map(openTrade => pairInfosContractMulticall.tradeInitialAccFees(openTrade.trader, openTrade.pairIndex, openTrade.index)), blockTag),
|
|
147
|
+
]);
|
|
148
|
+
const finalTrades = new Array(openTrades.length);
|
|
149
|
+
for (let tradeIndex = 0; tradeIndex < openTradesTradeInfos.length; tradeIndex++) {
|
|
150
|
+
const tradeInfo = openTradesTradeInfos[tradeIndex];
|
|
151
|
+
if (tradeInfo === undefined) {
|
|
152
|
+
console.error("No trade info found for open trade while fetching open trades!", { trade: openTradesTradeInfos[tradeIndex] });
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const tradeInitialAccFees = openTradesInitialAccFees[tradeIndex];
|
|
156
|
+
if (tradeInitialAccFees === undefined) {
|
|
157
|
+
console.error("No initial fees found for open trade while fetching open trades!", { trade: openTrades[tradeIndex] });
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const trade = openTrades[tradeIndex];
|
|
161
|
+
finalTrades[tradeIndex] = {
|
|
162
|
+
trade,
|
|
163
|
+
tradeInfo,
|
|
164
|
+
initialAccFees: tradeInitialAccFees,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return finalTrades.filter(trade => trade !== undefined);
|
|
168
|
+
});
|
|
169
|
+
const _prepareTradeContainer = (trade, tradeInfo, tradeInitialAccFees) => ({
|
|
170
|
+
trade: {
|
|
171
|
+
trader: trade.trader,
|
|
172
|
+
pairIndex: parseInt(trade.pairIndex.toString()),
|
|
173
|
+
index: parseInt(trade.index.toString()),
|
|
174
|
+
initialPosToken: parseFloat(trade.initialPosToken.toString()) / 1e18,
|
|
175
|
+
openPrice: parseFloat(trade.openPrice.toString()) / 1e10,
|
|
176
|
+
buy: trade.buy.toString() === "true",
|
|
177
|
+
leverage: parseInt(trade.leverage.toString()),
|
|
178
|
+
tp: parseFloat(trade.tp.toString()) / 1e10,
|
|
179
|
+
sl: parseFloat(trade.sl.toString()) / 1e10,
|
|
180
|
+
},
|
|
181
|
+
tradeInfo: {
|
|
182
|
+
beingMarketClosed: tradeInfo.beingMarketClosed.toString() === "true",
|
|
183
|
+
tokenPriceDai: parseFloat(tradeInfo.tokenPriceDai.toString()) / 1e10,
|
|
184
|
+
openInterestDai: parseFloat(tradeInfo.openInterestDai.toString()) / 1e18,
|
|
185
|
+
tpLastUpdated: tradeInfo.tpLastUpdated,
|
|
186
|
+
slLastUpdated: tradeInfo.slLastUpdated,
|
|
187
|
+
},
|
|
188
|
+
initialAccFees: {
|
|
189
|
+
rollover: parseFloat(tradeInitialAccFees.rollover.toString()) / 1e18,
|
|
190
|
+
funding: parseFloat(tradeInitialAccFees.funding.toString()) / 1e18,
|
|
191
|
+
openedAfterUpdate: tradeInitialAccFees.openedAfterUpdate.toString() === "true",
|
|
192
|
+
},
|
|
193
|
+
});
|
|
@@ -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[]>;
|
|
@@ -1,102 +1,102 @@
|
|
|
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.fetchFees = exports.fetchPairsRolloverFees = exports.fetchPairsParams = exports.fetchPairs = void 0;
|
|
13
|
-
const fetchPairs = (contracts, pairIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
-
if (!contracts) {
|
|
15
|
-
return [];
|
|
16
|
-
}
|
|
17
|
-
const { gnsPairsStorageV6: pairsStorageContract } = contracts;
|
|
18
|
-
try {
|
|
19
|
-
const pairs = yield Promise.all(pairIxs.map((pairIndex) => pairsStorageContract.pairs(pairIndex)));
|
|
20
|
-
return pairs.map((pair, index) => {
|
|
21
|
-
return {
|
|
22
|
-
name: pair.from + "/" + pair.to,
|
|
23
|
-
from: pair.from,
|
|
24
|
-
to: pair.to,
|
|
25
|
-
feeIndex: parseInt(pair.feeIndex.toString()),
|
|
26
|
-
groupIndex: parseInt(pair.groupIndex.toString()),
|
|
27
|
-
pairIndex: pairIxs[index],
|
|
28
|
-
spreadP: parseFloat(pair.spreadP.toString()) / 1e12,
|
|
29
|
-
};
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
catch (error) {
|
|
33
|
-
console.error(`Unexpected error while fetching pairs!`);
|
|
34
|
-
throw error;
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
exports.fetchPairs = fetchPairs;
|
|
38
|
-
const fetchPairsParams = (contracts, pairIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
|
-
if (!contracts) {
|
|
40
|
-
return [];
|
|
41
|
-
}
|
|
42
|
-
const { gnsPairInfosV6_1: pairInfosContract } = contracts;
|
|
43
|
-
try {
|
|
44
|
-
const pairParams = yield Promise.all(pairIxs.map((pairIndex) => pairInfosContract.pairParams(pairIndex)));
|
|
45
|
-
return pairParams.map((pair) => {
|
|
46
|
-
return {
|
|
47
|
-
onePercentDepthAbove: parseFloat(pair.onePercentDepthAbove.toString()),
|
|
48
|
-
onePercentDepthBelow: parseFloat(pair.onePercentDepthBelow.toString()),
|
|
49
|
-
rolloverFeePerBlockP: parseFloat(pair.rolloverFeePerBlockP.toString()) / 1e12,
|
|
50
|
-
fundingFeePerBlockP: parseFloat(pair.fundingFeePerBlockP.toString()) / 1e12,
|
|
51
|
-
};
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
console.error(`Unexpected error while fetching pairs!`);
|
|
56
|
-
throw error;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
exports.fetchPairsParams = fetchPairsParams;
|
|
60
|
-
const fetchPairsRolloverFees = (contracts, pairIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
-
if (!contracts) {
|
|
62
|
-
return [];
|
|
63
|
-
}
|
|
64
|
-
const { gnsPairInfosV6_1: pairInfosContract } = contracts;
|
|
65
|
-
try {
|
|
66
|
-
const pairsRolloverFees = yield Promise.all(pairIxs.map((pairIndex) => pairInfosContract.pairRolloverFees(pairIndex)));
|
|
67
|
-
return pairsRolloverFees.map((pairData) => {
|
|
68
|
-
return {
|
|
69
|
-
accPerCollateral: parseFloat(pairData.accPerCollateral.toString()) / 1e18,
|
|
70
|
-
lastUpdateBlock: parseInt(pairData.lastUpdateBlock.toString()),
|
|
71
|
-
};
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
catch (error) {
|
|
75
|
-
console.error(`Unexpected error while fetching pairs!`);
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
exports.fetchPairsRolloverFees = fetchPairsRolloverFees;
|
|
80
|
-
const fetchFees = (contracts, feeIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
-
if (!contracts) {
|
|
82
|
-
return [];
|
|
83
|
-
}
|
|
84
|
-
const { gnsPairsStorageV6: pairsStorageContract } = contracts;
|
|
85
|
-
try {
|
|
86
|
-
const fees = yield Promise.all(feeIxs.map((pairIndex) => pairsStorageContract.fees(pairIndex)));
|
|
87
|
-
return fees.map((fee) => {
|
|
88
|
-
return {
|
|
89
|
-
closeFeeP: parseFloat(fee.closeFeeP.toString()) / 1e12,
|
|
90
|
-
minLevPosDai: parseFloat(fee.minLevPosDai.toString()) / 1e12,
|
|
91
|
-
nftLimitOrderFeeP: parseFloat(fee.nftLimitOrderFeeP.toString()) / 1e12,
|
|
92
|
-
openFeeP: parseFloat(fee.openFeeP.toString()) / 1e12,
|
|
93
|
-
referralFeeP: parseFloat(fee.referralFeeP.toString()) / 1e12,
|
|
94
|
-
};
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
console.error(`Unexpected error while fetching pairs!`);
|
|
99
|
-
throw error;
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
exports.fetchFees = fetchFees;
|
|
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.fetchFees = exports.fetchPairsRolloverFees = exports.fetchPairsParams = exports.fetchPairs = void 0;
|
|
13
|
+
const fetchPairs = (contracts, pairIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
if (!contracts) {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
const { gnsPairsStorageV6: pairsStorageContract } = contracts;
|
|
18
|
+
try {
|
|
19
|
+
const pairs = yield Promise.all(pairIxs.map((pairIndex) => pairsStorageContract.pairs(pairIndex)));
|
|
20
|
+
return pairs.map((pair, index) => {
|
|
21
|
+
return {
|
|
22
|
+
name: pair.from + "/" + pair.to,
|
|
23
|
+
from: pair.from,
|
|
24
|
+
to: pair.to,
|
|
25
|
+
feeIndex: parseInt(pair.feeIndex.toString()),
|
|
26
|
+
groupIndex: parseInt(pair.groupIndex.toString()),
|
|
27
|
+
pairIndex: pairIxs[index],
|
|
28
|
+
spreadP: parseFloat(pair.spreadP.toString()) / 1e12,
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.error(`Unexpected error while fetching pairs!`);
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
exports.fetchPairs = fetchPairs;
|
|
38
|
+
const fetchPairsParams = (contracts, pairIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
|
+
if (!contracts) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
const { gnsPairInfosV6_1: pairInfosContract } = contracts;
|
|
43
|
+
try {
|
|
44
|
+
const pairParams = yield Promise.all(pairIxs.map((pairIndex) => pairInfosContract.pairParams(pairIndex)));
|
|
45
|
+
return pairParams.map((pair) => {
|
|
46
|
+
return {
|
|
47
|
+
onePercentDepthAbove: parseFloat(pair.onePercentDepthAbove.toString()),
|
|
48
|
+
onePercentDepthBelow: parseFloat(pair.onePercentDepthBelow.toString()),
|
|
49
|
+
rolloverFeePerBlockP: parseFloat(pair.rolloverFeePerBlockP.toString()) / 1e12,
|
|
50
|
+
fundingFeePerBlockP: parseFloat(pair.fundingFeePerBlockP.toString()) / 1e12,
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
console.error(`Unexpected error while fetching pairs!`);
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
exports.fetchPairsParams = fetchPairsParams;
|
|
60
|
+
const fetchPairsRolloverFees = (contracts, pairIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
if (!contracts) {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
const { gnsPairInfosV6_1: pairInfosContract } = contracts;
|
|
65
|
+
try {
|
|
66
|
+
const pairsRolloverFees = yield Promise.all(pairIxs.map((pairIndex) => pairInfosContract.pairRolloverFees(pairIndex)));
|
|
67
|
+
return pairsRolloverFees.map((pairData) => {
|
|
68
|
+
return {
|
|
69
|
+
accPerCollateral: parseFloat(pairData.accPerCollateral.toString()) / 1e18,
|
|
70
|
+
lastUpdateBlock: parseInt(pairData.lastUpdateBlock.toString()),
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
console.error(`Unexpected error while fetching pairs!`);
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
exports.fetchPairsRolloverFees = fetchPairsRolloverFees;
|
|
80
|
+
const fetchFees = (contracts, feeIxs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
+
if (!contracts) {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
const { gnsPairsStorageV6: pairsStorageContract } = contracts;
|
|
85
|
+
try {
|
|
86
|
+
const fees = yield Promise.all(feeIxs.map((pairIndex) => pairsStorageContract.fees(pairIndex)));
|
|
87
|
+
return fees.map((fee) => {
|
|
88
|
+
return {
|
|
89
|
+
closeFeeP: parseFloat(fee.closeFeeP.toString()) / 1e12,
|
|
90
|
+
minLevPosDai: parseFloat(fee.minLevPosDai.toString()) / 1e12,
|
|
91
|
+
nftLimitOrderFeeP: parseFloat(fee.nftLimitOrderFeeP.toString()) / 1e12,
|
|
92
|
+
openFeeP: parseFloat(fee.openFeeP.toString()) / 1e12,
|
|
93
|
+
referralFeeP: parseFloat(fee.referralFeeP.toString()) / 1e12,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error(`Unexpected error while fetching pairs!`);
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
exports.fetchFees = fetchFees;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export * from "./trade";
|
|
2
|
-
export * from "./contracts";
|
|
3
|
-
export * from "./markets";
|
|
1
|
+
export * from "./trade";
|
|
2
|
+
export * from "./contracts";
|
|
3
|
+
export * from "./markets";
|
|
4
|
+
export * from "./constants";
|