@gainsnetwork/sdk 0.1.29-rc3 → 0.2.0-rc3
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 +252 -261
- package/lib/constants.js +269 -277
- package/lib/contracts/addresses.d.ts +3 -3
- package/lib/contracts/addresses.js +31 -31
- package/lib/contracts/addresses.json +53 -89
- package/lib/contracts/index.d.ts +8 -8
- package/lib/contracts/index.js +37 -40
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +5215 -1740
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.js +2 -2
- package/lib/contracts/types/generated/GToken.d.ts +1946 -1946
- package/lib/contracts/types/generated/GToken.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/GNSMultiCollatDiamond__factory.d.ts +291 -144
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +11360 -3094
- 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/GToken__factory.d.ts +121 -121
- package/lib/contracts/types/generated/factories/GToken__factory.js +2944 -2944
- package/lib/contracts/types/generated/factories/index.d.ts +3 -10
- package/lib/contracts/types/generated/factories/index.js +12 -26
- package/lib/contracts/types/generated/index.d.ts +7 -21
- package/lib/contracts/types/generated/index.js +33 -47
- package/lib/contracts/types/index.d.ts +19 -27
- package/lib/contracts/types/index.js +10 -10
- package/lib/contracts/utils/borrowingFees.d.ts +8 -8
- package/lib/contracts/utils/borrowingFees.js +32 -28
- package/lib/contracts/utils/index.d.ts +3 -4
- package/lib/contracts/utils/index.js +19 -20
- package/lib/contracts/utils/openTrades.d.ts +9 -9
- package/lib/contracts/utils/openTrades.js +380 -188
- package/lib/contracts/utils/pairs.d.ts +7 -7
- package/lib/contracts/utils/pairs.js +339 -337
- package/lib/index.d.ts +7 -7
- package/lib/index.js +24 -24
- 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 +3 -3
- package/lib/markets/forex.js +45 -45
- 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/borrowing/converter.d.ts +15 -13
- package/lib/trade/fees/borrowing/converter.js +35 -41
- package/lib/trade/fees/borrowing/index.d.ts +54 -55
- package/lib/trade/fees/borrowing/index.js +165 -171
- package/lib/trade/fees/borrowing/types.d.ts +30 -36
- package/lib/trade/fees/borrowing/types.js +2 -2
- package/lib/trade/fees/index.d.ts +4 -4
- package/lib/trade/fees/index.js +30 -30
- package/lib/trade/fees/tiers/index.d.ts +12 -12
- package/lib/trade/fees/tiers/index.js +51 -51
- package/lib/trade/fees/tiers/types.d.ts +8 -8
- package/lib/trade/fees/tiers/types.js +2 -2
- package/lib/trade/index.d.ts +7 -8
- package/lib/trade/index.js +23 -24
- package/lib/trade/liquidation.d.ts +3 -3
- package/lib/trade/liquidation.js +15 -16
- package/lib/trade/oiWindows.d.ts +3 -3
- package/lib/trade/oiWindows.js +20 -20
- package/lib/trade/pnl.d.ts +7 -7
- package/lib/trade/pnl.js +33 -33
- package/lib/trade/spread.d.ts +2 -2
- package/lib/trade/spread.js +22 -22
- package/lib/trade/types.d.ts +413 -429
- package/lib/trade/types.js +255 -249
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +17 -17
- package/lib/utils/packing.d.ts +2 -2
- package/lib/utils/packing.js +39 -39
- package/lib/vault/index.d.ts +8 -8
- package/lib/vault/index.js +10 -10
- package/package.json +105 -105
- 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/GNSBorrowingFeesV6_4.d.ts +0 -1058
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.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/GNSPairInfosV6_1.d.ts +0 -911
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +0 -2
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +0 -660
- package/lib/contracts/types/generated/GNSPairsStorageV6.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/GNSTradingCallbacksV6_4.d.ts +0 -821
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.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/GTokenV6_3_2.d.ts +0 -1838
- package/lib/contracts/types/generated/GTokenV6_3_2.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/GNSBorrowingFeesV6_4__factory.d.ts +0 -113
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +0 -1742
- 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/GNSPairInfosV6_1__factory.d.ts +0 -98
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +0 -1485
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +0 -117
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +0 -1265
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +0 -82
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +0 -1326
- 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/types/generated/factories/GTokenV6_3_2__factory.d.ts +0 -110
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +0 -2682
- package/lib/contracts/utils/openLimitOrders.d.ts +0 -8
- package/lib/contracts/utils/openLimitOrders.js +0 -88
- package/lib/trade/fees.d.ts +0 -15
- package/lib/trade/fees.js +0 -45
- package/lib/trade/openLimitOrder.d.ts +0 -2
- package/lib/trade/openLimitOrder.js +0 -23
|
@@ -1,188 +1,380 @@
|
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const { gnsMultiCollatDiamond: multiCollatDiamondContract } = contracts;
|
|
29
|
-
try {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
let
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
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
|
+
const fetchOpenPairTrades = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const rawTrades = yield (0, exports.fetchOpenPairTradesRaw)(contracts, overrides);
|
|
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]));
|
|
17
|
+
});
|
|
18
|
+
exports.fetchOpenPairTrades = fetchOpenPairTrades;
|
|
19
|
+
// @todo rename
|
|
20
|
+
const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
if (!contracts) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
const { batchSize = 50,
|
|
25
|
+
// useMulticall = false,
|
|
26
|
+
// blockTag = "latest",
|
|
27
|
+
} = overrides;
|
|
28
|
+
const { gnsMultiCollatDiamond: multiCollatDiamondContract } = contracts;
|
|
29
|
+
try {
|
|
30
|
+
let allOpenPairTrades = [];
|
|
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;
|
|
55
|
+
}
|
|
56
|
+
return allOpenPairTrades;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.error(`Unexpected error while fetching open pair trades!`);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
exports.fetchOpenPairTradesRaw = fetchOpenPairTradesRaw;
|
|
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;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (actualOpenTradesInitialAccFees[tradeIndex] === undefined) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
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) => {
|
|
244
|
+
return pairTraderAddresses
|
|
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
|
+
);
|
|
258
|
+
}
|
|
259
|
+
return openTradesCalls;
|
|
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
|
+
),
|
|
306
|
+
]);
|
|
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;
|
|
324
|
+
}
|
|
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) => ({
|
|
352
|
+
trade: {
|
|
353
|
+
user: trade.user,
|
|
354
|
+
index: parseInt(trade.index.toString()),
|
|
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()),
|
|
363
|
+
openPrice: parseFloat(trade.openPrice.toString()) / 1e10,
|
|
364
|
+
tp: parseFloat(trade.tp.toString()) / 1e10,
|
|
365
|
+
sl: parseFloat(trade.sl.toString()) / 1e10,
|
|
366
|
+
},
|
|
367
|
+
tradeInfo: {
|
|
368
|
+
createdBlock: parseInt(tradeInfo.createdBlock.toString()),
|
|
369
|
+
tpLastUpdatedBlock: parseInt(tradeInfo.tpLastUpdatedBlock.toString()),
|
|
370
|
+
slLastUpdatedBlock: parseInt(tradeInfo.slLastUpdatedBlock.toString()),
|
|
371
|
+
maxSlippageP: parseFloat(tradeInfo.maxSlippageP.toString()) / 1e3,
|
|
372
|
+
lastOiUpdateTs: parseFloat(tradeInfo.lastOiUpdateTs),
|
|
373
|
+
collateralPriceUsd: parseFloat(tradeInfo.collateralPriceUsd.toString()) / 1e8,
|
|
374
|
+
},
|
|
375
|
+
initialAccFees: {
|
|
376
|
+
accPairFee: parseFloat(tradeInitialAccFees.accPairFee.toString()) / 1e10,
|
|
377
|
+
accGroupFee: parseFloat(tradeInitialAccFees.accGroupFee.toString()) / 1e10,
|
|
378
|
+
block: parseFloat(tradeInitialAccFees.block.toString()),
|
|
379
|
+
},
|
|
380
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Pair, Fee, OpenInterest, PairDepth, PairIndex } from "../../trade/types";
|
|
2
|
-
import { Contracts } from "../../contracts/types";
|
|
3
|
-
export declare const fetchPairs: (contracts: Contracts, pairIxs: PairIndex[]) => Promise<Pair[]>;
|
|
4
|
-
export declare const fetchPairDepths: (contracts: Contracts, pairIxs: number[]) => Promise<PairDepth[]>;
|
|
5
|
-
export declare const fetchFees: (contracts: Contracts, feeIxs: PairIndex[]) => Promise<Fee[]>;
|
|
6
|
-
export declare const fetchOpenInterest: (contracts: Contracts, pairIxs: number[]) => Promise<OpenInterest[]>;
|
|
7
|
-
export declare const getPairDescription: (pairIndex: PairIndex) => string;
|
|
1
|
+
import { Pair, Fee, OpenInterest, PairDepth, PairIndex } from "../../trade/types";
|
|
2
|
+
import { Contracts } from "../../contracts/types";
|
|
3
|
+
export declare const fetchPairs: (contracts: Contracts, pairIxs: PairIndex[]) => Promise<Pair[]>;
|
|
4
|
+
export declare const fetchPairDepths: (contracts: Contracts, pairIxs: number[]) => Promise<PairDepth[]>;
|
|
5
|
+
export declare const fetchFees: (contracts: Contracts, feeIxs: PairIndex[]) => Promise<Fee[]>;
|
|
6
|
+
export declare const fetchOpenInterest: (collateralIndex: number, contracts: Contracts, pairIxs: number[]) => Promise<OpenInterest[]>;
|
|
7
|
+
export declare const getPairDescription: (pairIndex: PairIndex) => string;
|