@gainsnetwork/sdk 0.0.16 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/contracts/addresses.json +6 -3
- package/lib/contracts/index.js +1 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- 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/index.d.ts +1 -0
- package/lib/contracts/types/generated/factories/index.js +3 -1
- package/lib/contracts/types/generated/index.d.ts +2 -0
- package/lib/contracts/types/generated/index.js +3 -1
- package/lib/contracts/types/index.d.ts +4 -1
- package/lib/contracts/utils/index.d.ts +1 -0
- package/lib/contracts/utils/index.js +1 -0
- 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 +108 -31
- package/lib/trade/types.d.ts +14 -0
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { GFarmTradingStorageV5__factory } from "./GFarmTradingStorageV5__factory";
|
|
2
|
+
export { GNSNftRewardsV6__factory } from "./GNSNftRewardsV6__factory";
|
|
2
3
|
export { GNSPairInfosV6_1__factory } from "./GNSPairInfosV6_1__factory";
|
|
3
4
|
export { GNSPairsStorageV6__factory } from "./GNSPairsStorageV6__factory";
|
|
4
5
|
export { GTokenOpenPnlFeed__factory } from "./GTokenOpenPnlFeed__factory";
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GTokenOpenPnlFeed__factory = exports.GNSPairsStorageV6__factory = exports.GNSPairInfosV6_1__factory = exports.GFarmTradingStorageV5__factory = void 0;
|
|
3
|
+
exports.GTokenOpenPnlFeed__factory = exports.GNSPairsStorageV6__factory = exports.GNSPairInfosV6_1__factory = exports.GNSNftRewardsV6__factory = exports.GFarmTradingStorageV5__factory = void 0;
|
|
4
4
|
/* Autogenerated file. Do not edit manually. */
|
|
5
5
|
/* tslint:disable */
|
|
6
6
|
/* eslint-disable */
|
|
7
7
|
var GFarmTradingStorageV5__factory_1 = require("./GFarmTradingStorageV5__factory");
|
|
8
8
|
Object.defineProperty(exports, "GFarmTradingStorageV5__factory", { enumerable: true, get: function () { return GFarmTradingStorageV5__factory_1.GFarmTradingStorageV5__factory; } });
|
|
9
|
+
var GNSNftRewardsV6__factory_1 = require("./GNSNftRewardsV6__factory");
|
|
10
|
+
Object.defineProperty(exports, "GNSNftRewardsV6__factory", { enumerable: true, get: function () { return GNSNftRewardsV6__factory_1.GNSNftRewardsV6__factory; } });
|
|
9
11
|
var GNSPairInfosV6_1__factory_1 = require("./GNSPairInfosV6_1__factory");
|
|
10
12
|
Object.defineProperty(exports, "GNSPairInfosV6_1__factory", { enumerable: true, get: function () { return GNSPairInfosV6_1__factory_1.GNSPairInfosV6_1__factory; } });
|
|
11
13
|
var GNSPairsStorageV6__factory_1 = require("./GNSPairsStorageV6__factory");
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export type { GFarmTradingStorageV5 } from "./GFarmTradingStorageV5";
|
|
2
|
+
export type { GNSNftRewardsV6 } from "./GNSNftRewardsV6";
|
|
2
3
|
export type { GNSPairInfosV6_1 } from "./GNSPairInfosV6_1";
|
|
3
4
|
export type { GNSPairsStorageV6 } from "./GNSPairsStorageV6";
|
|
4
5
|
export type { GTokenOpenPnlFeed } from "./GTokenOpenPnlFeed";
|
|
5
6
|
export * as factories from "./factories";
|
|
6
7
|
export { GFarmTradingStorageV5__factory } from "./factories/GFarmTradingStorageV5__factory";
|
|
8
|
+
export { GNSNftRewardsV6__factory } from "./factories/GNSNftRewardsV6__factory";
|
|
7
9
|
export { GNSPairInfosV6_1__factory } from "./factories/GNSPairInfosV6_1__factory";
|
|
8
10
|
export { GNSPairsStorageV6__factory } from "./factories/GNSPairsStorageV6__factory";
|
|
9
11
|
export { GTokenOpenPnlFeed__factory } from "./factories/GTokenOpenPnlFeed__factory";
|
|
@@ -23,10 +23,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.GTokenOpenPnlFeed__factory = exports.GNSPairsStorageV6__factory = exports.GNSPairInfosV6_1__factory = exports.GFarmTradingStorageV5__factory = exports.factories = void 0;
|
|
26
|
+
exports.GTokenOpenPnlFeed__factory = exports.GNSPairsStorageV6__factory = exports.GNSPairInfosV6_1__factory = exports.GNSNftRewardsV6__factory = exports.GFarmTradingStorageV5__factory = exports.factories = void 0;
|
|
27
27
|
exports.factories = __importStar(require("./factories"));
|
|
28
28
|
var GFarmTradingStorageV5__factory_1 = require("./factories/GFarmTradingStorageV5__factory");
|
|
29
29
|
Object.defineProperty(exports, "GFarmTradingStorageV5__factory", { enumerable: true, get: function () { return GFarmTradingStorageV5__factory_1.GFarmTradingStorageV5__factory; } });
|
|
30
|
+
var GNSNftRewardsV6__factory_1 = require("./factories/GNSNftRewardsV6__factory");
|
|
31
|
+
Object.defineProperty(exports, "GNSNftRewardsV6__factory", { enumerable: true, get: function () { return GNSNftRewardsV6__factory_1.GNSNftRewardsV6__factory; } });
|
|
30
32
|
var GNSPairInfosV6_1__factory_1 = require("./factories/GNSPairInfosV6_1__factory");
|
|
31
33
|
Object.defineProperty(exports, "GNSPairInfosV6_1__factory", { enumerable: true, get: function () { return GNSPairInfosV6_1__factory_1.GNSPairInfosV6_1__factory; } });
|
|
32
34
|
var GNSPairsStorageV6__factory_1 = require("./factories/GNSPairsStorageV6__factory");
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { GFarmTradingStorageV5, GNSPairInfosV6_1, GNSPairsStorageV6, GTokenOpenPnlFeed } from "./generated";
|
|
1
|
+
import { GFarmTradingStorageV5, GNSPairInfosV6_1, GNSPairsStorageV6, GTokenOpenPnlFeed, GNSNftRewardsV6 } from "./generated";
|
|
2
2
|
export type Contracts = {
|
|
3
3
|
gfarmTradingStorageV5: GFarmTradingStorageV5;
|
|
4
4
|
gnsPairInfosV6_1: GNSPairInfosV6_1;
|
|
5
5
|
gnsPairsStorageV6: GNSPairsStorageV6;
|
|
6
6
|
gTokenOpenPnlFeed: GTokenOpenPnlFeed;
|
|
7
|
+
gnsNftRewardsV6: GNSNftRewardsV6;
|
|
7
8
|
};
|
|
8
9
|
export type ContractAddresses = {
|
|
9
10
|
gfarmTradingStorageV5: string;
|
|
10
11
|
gnsPairInfosV6_1: string;
|
|
11
12
|
gnsPairsStorageV6: string;
|
|
12
13
|
gTokenOpenPnlFeed: string;
|
|
14
|
+
gnsNftRewardsV6: string;
|
|
13
15
|
};
|
|
16
|
+
export type BlockTag = number | "latest" | "pending";
|
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./openTrades"), exports);
|
|
18
18
|
__exportStar(require("./pairs"), exports);
|
|
19
|
+
__exportStar(require("./openLimitOrders"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { LimitOrder } from "@/trade/types";
|
|
2
|
+
import { BlockTag, Contracts } from "../types";
|
|
3
|
+
export type FetchOpenLimitOrdersOverrides = {
|
|
4
|
+
blockTag?: BlockTag;
|
|
5
|
+
useMulticall?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare const fetchOpenLimitOrders: (contracts: Contracts, overrides?: FetchOpenLimitOrdersOverrides) => Promise<LimitOrder[]>;
|
|
8
|
+
export declare const fetchOpenLimitOrdersRaw: (contracts: Contracts, overrides?: FetchOpenLimitOrdersOverrides) => Promise<any[]>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
5
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
6
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
7
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
9
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
10
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
11
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.fetchOpenLimitOrdersRaw = exports.fetchOpenLimitOrders = void 0;
|
|
16
|
+
const ethcall_1 = require("ethcall");
|
|
17
|
+
const fetchOpenLimitOrders = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
const openLimitOrdersRaw = yield (0, exports.fetchOpenLimitOrdersRaw)(contracts, overrides);
|
|
19
|
+
return openLimitOrdersRaw.map((order) => ({
|
|
20
|
+
block: parseInt(order.block.toString()),
|
|
21
|
+
buy: order.buy,
|
|
22
|
+
index: parseInt(order.index.toString()),
|
|
23
|
+
leverage: parseInt(order.leverage.toString()),
|
|
24
|
+
maxPrice: parseFloat(order.maxPrice.toString()) / 1e10,
|
|
25
|
+
minPrice: parseFloat(order.minPrice.toString()) / 1e10,
|
|
26
|
+
pairIndex: parseInt(order.pairIndex.toString()),
|
|
27
|
+
positionSize: parseFloat(order.positionSize.toString()) / 1e18,
|
|
28
|
+
sl: parseFloat(order.sl.toString()) / 1e10,
|
|
29
|
+
spreadReductionP: parseInt(order.spreadReductionP.toString()) / 100,
|
|
30
|
+
tp: parseFloat(order.tp.toString()) / 1e10,
|
|
31
|
+
trader: order.trader,
|
|
32
|
+
type: parseInt(order.type.toString()),
|
|
33
|
+
}));
|
|
34
|
+
});
|
|
35
|
+
exports.fetchOpenLimitOrders = fetchOpenLimitOrders;
|
|
36
|
+
const fetchOpenLimitOrdersRaw = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
+
if (!contracts) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
console.time("fetchOpenLimitOrdersRaw");
|
|
41
|
+
const { useMulticall = false, blockTag = "latest" } = overrides;
|
|
42
|
+
const { gfarmTradingStorageV5: storageContract, gnsNftRewardsV6: nftRewards, } = contracts;
|
|
43
|
+
const openLimitOrders = yield storageContract.getOpenLimitOrders({
|
|
44
|
+
blockTag,
|
|
45
|
+
});
|
|
46
|
+
let openLimitOrderTypes = [];
|
|
47
|
+
if (useMulticall) {
|
|
48
|
+
const multicallProvider = new ethcall_1.Provider();
|
|
49
|
+
yield multicallProvider.init(storageContract.provider);
|
|
50
|
+
const nftRewardsContractMulticall = new ethcall_1.Contract(nftRewards.address, [
|
|
51
|
+
...nftRewards.interface.fragments,
|
|
52
|
+
]);
|
|
53
|
+
openLimitOrderTypes = yield multicallProvider.all(openLimitOrders.map(order => nftRewardsContractMulticall.openLimitOrderTypes(order.trader, order.pairIndex, order.index)), blockTag);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
openLimitOrderTypes = yield Promise.all(openLimitOrders.map(order => nftRewards.openLimitOrderTypes(order.trader, order.pairIndex, order.index, { blockTag })));
|
|
57
|
+
}
|
|
58
|
+
return openLimitOrderTypes.map((openLimitOrderType, index) => {
|
|
59
|
+
const openLimitOrder = openLimitOrders[index];
|
|
60
|
+
return {
|
|
61
|
+
trader: openLimitOrder.trader,
|
|
62
|
+
pairIndex: openLimitOrder.pairIndex,
|
|
63
|
+
index: openLimitOrder.index,
|
|
64
|
+
positionSize: openLimitOrder.positionSize,
|
|
65
|
+
spreadReductionP: openLimitOrder.spreadReductionP,
|
|
66
|
+
buy: openLimitOrder.buy,
|
|
67
|
+
leverage: openLimitOrder.leverage,
|
|
68
|
+
tp: openLimitOrder.tp,
|
|
69
|
+
sl: openLimitOrder.sl,
|
|
70
|
+
minPrice: openLimitOrder.minPrice,
|
|
71
|
+
maxPrice: openLimitOrder.maxPrice,
|
|
72
|
+
block: openLimitOrder.block,
|
|
73
|
+
type: openLimitOrderType,
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
exports.fetchOpenLimitOrdersRaw = fetchOpenLimitOrdersRaw;
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
import { TradeContainer } from "@/trade/types";
|
|
2
|
-
import { Contracts } from "@/contracts/types";
|
|
3
|
-
export
|
|
1
|
+
import { TradeContainer, TradeContainerRaw } from "@/trade/types";
|
|
2
|
+
import { Contracts, BlockTag } from "@/contracts/types";
|
|
3
|
+
export type FetchOpenPairTradesOverrides = {
|
|
4
|
+
pairBatchSize?: number;
|
|
5
|
+
useMulticall?: boolean;
|
|
6
|
+
blockTag?: BlockTag;
|
|
7
|
+
};
|
|
8
|
+
export declare const fetchOpenPairTrades: (contracts: Contracts, overrides?: FetchOpenPairTradesOverrides) => Promise<TradeContainer[]>;
|
|
9
|
+
export declare const fetchOpenPairTradesRaw: (contracts: Contracts, overrides?: FetchOpenPairTradesOverrides) => Promise<TradeContainerRaw[]>;
|
|
@@ -9,18 +9,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.fetchOpenPairTrades = void 0;
|
|
13
|
-
|
|
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* () {
|
|
14
23
|
if (!contracts) {
|
|
15
24
|
return [];
|
|
16
25
|
}
|
|
26
|
+
const { pairBatchSize = 10, useMulticall = false, blockTag = "latest", } = overrides;
|
|
17
27
|
const { gnsPairsStorageV6: pairsStorageContract } = contracts;
|
|
18
28
|
try {
|
|
19
|
-
const totalPairIndexes = (yield pairsStorageContract.pairsCount()).toNumber() - 1;
|
|
29
|
+
const totalPairIndexes = (yield pairsStorageContract.pairsCount({ blockTag })).toNumber() - 1;
|
|
20
30
|
let allOpenPairTrades = [];
|
|
21
31
|
for (let batchStartPairIndex = 0; batchStartPairIndex < totalPairIndexes; batchStartPairIndex += pairBatchSize) {
|
|
22
32
|
const batchEndPairIndex = Math.min(batchStartPairIndex + pairBatchSize - 1, totalPairIndexes);
|
|
23
|
-
const openPairTradesBatch =
|
|
33
|
+
const openPairTradesBatch = useMulticall
|
|
34
|
+
? yield fetchOpenPairTradesBatchMulticall(contracts, batchStartPairIndex, batchEndPairIndex, blockTag)
|
|
35
|
+
: yield fetchOpenPairTradesBatch(contracts, batchStartPairIndex, batchEndPairIndex);
|
|
24
36
|
allOpenPairTrades = allOpenPairTrades.concat(openPairTradesBatch);
|
|
25
37
|
}
|
|
26
38
|
console.info(`Fetched ${allOpenPairTrades.length} total open pair trade(s).`);
|
|
@@ -31,9 +43,9 @@ const fetchOpenPairTrades = (contracts, pairBatchSize = 10) => __awaiter(void 0,
|
|
|
31
43
|
throw error;
|
|
32
44
|
}
|
|
33
45
|
});
|
|
34
|
-
exports.
|
|
46
|
+
exports.fetchOpenPairTradesRaw = fetchOpenPairTradesRaw;
|
|
35
47
|
const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
-
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract } = contracts;
|
|
48
|
+
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract, } = contracts;
|
|
37
49
|
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
38
50
|
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
39
51
|
const rawTrades = yield Promise.all(pairIndexesToFetch.map((pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -69,39 +81,25 @@ const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __
|
|
|
69
81
|
for (let tradeIndex = 0; tradeIndex < actualOpenTradesForTrader.length; tradeIndex++) {
|
|
70
82
|
const tradeInfo = actualOpenTradesTradeInfos[tradeIndex];
|
|
71
83
|
if (tradeInfo === undefined) {
|
|
72
|
-
console.error(
|
|
84
|
+
// console.error(
|
|
85
|
+
// "No trade info found for open trade while fetching open trades!",
|
|
86
|
+
// { trade: actualOpenTradesForTrader[tradeIndex] }
|
|
87
|
+
// );
|
|
73
88
|
continue;
|
|
74
89
|
}
|
|
75
90
|
const tradeInitialAccFees = actualOpenTradesInitialAccFees[tradeIndex];
|
|
76
91
|
if (tradeInitialAccFees === undefined) {
|
|
77
|
-
console.error(
|
|
92
|
+
// console.error(
|
|
93
|
+
// "No initial fees found for open trade while fetching open trades!",
|
|
94
|
+
// { trade: actualOpenTradesForTrader[tradeIndex] }
|
|
95
|
+
// );
|
|
78
96
|
continue;
|
|
79
97
|
}
|
|
80
98
|
const trade = actualOpenTradesForTrader[tradeIndex];
|
|
81
99
|
finalOpenTradesForTrader[tradeIndex] = {
|
|
82
|
-
trade
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
index: parseInt(trade.index.toString()),
|
|
86
|
-
initialPosToken: parseFloat(trade.initialPosToken.toString()) / 1e18,
|
|
87
|
-
openPrice: parseFloat(trade.openPrice.toString()) / 1e10,
|
|
88
|
-
buy: trade.buy.toString() === "true",
|
|
89
|
-
leverage: parseInt(trade.leverage.toString()),
|
|
90
|
-
tp: parseFloat(trade.tp.toString()) / 1e10,
|
|
91
|
-
sl: parseFloat(trade.sl.toString()) / 1e10,
|
|
92
|
-
},
|
|
93
|
-
tradeInfo: {
|
|
94
|
-
beingMarketClosed: tradeInfo.beingMarketClosed.toString() === "true",
|
|
95
|
-
tokenPriceDai: parseFloat(tradeInfo.tokenPriceDai.toString()) / 1e10,
|
|
96
|
-
openInterestDai: parseFloat(tradeInfo.openInterestDai.toString()) / 1e18,
|
|
97
|
-
tpLastUpdated: tradeInfo.tpLastUpdated,
|
|
98
|
-
slLastUpdated: tradeInfo.slLastUpdated,
|
|
99
|
-
},
|
|
100
|
-
initialAccFees: {
|
|
101
|
-
rollover: parseFloat(tradeInitialAccFees.rollover.toString()) / 1e18,
|
|
102
|
-
funding: parseFloat(tradeInitialAccFees.funding.toString()) / 1e18,
|
|
103
|
-
openedAfterUpdate: tradeInitialAccFees.openedAfterUpdate.toString() === "true",
|
|
104
|
-
},
|
|
100
|
+
trade,
|
|
101
|
+
tradeInfo,
|
|
102
|
+
initialAccFees: tradeInitialAccFees,
|
|
105
103
|
};
|
|
106
104
|
}
|
|
107
105
|
/*console.debug(
|
|
@@ -114,3 +112,82 @@ const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __
|
|
|
114
112
|
const perPairTrades = rawTrades.reduce((a, b) => a.concat(b), []);
|
|
115
113
|
return perPairTrades.reduce((a, b) => a.concat(b), []);
|
|
116
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
|
+
});
|
package/lib/trade/types.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { GFarmTradingStorageV5 } from "@/contracts/types/generated";
|
|
2
|
+
import { BigNumber } from "ethers";
|
|
1
3
|
export type PairIndexes = {
|
|
2
4
|
[key: string]: number;
|
|
3
5
|
};
|
|
@@ -49,6 +51,9 @@ export type LimitOrder = {
|
|
|
49
51
|
trader: string;
|
|
50
52
|
type: number;
|
|
51
53
|
};
|
|
54
|
+
export type LimitOrderRaw = GFarmTradingStorageV5.OpenLimitOrderStructOutput & {
|
|
55
|
+
type: number;
|
|
56
|
+
};
|
|
52
57
|
export type Fee = {
|
|
53
58
|
closeFeeP: number;
|
|
54
59
|
minLevPosDai: number;
|
|
@@ -141,3 +146,12 @@ export declare enum PositionType {
|
|
|
141
146
|
LONG = "LONG",
|
|
142
147
|
SHORT = "SHORT"
|
|
143
148
|
}
|
|
149
|
+
export type TradeContainerRaw = {
|
|
150
|
+
trade: GFarmTradingStorageV5.TradeStruct;
|
|
151
|
+
tradeInfo: GFarmTradingStorageV5.TradeInfoStruct;
|
|
152
|
+
initialAccFees: {
|
|
153
|
+
rollover: BigNumber;
|
|
154
|
+
funding: BigNumber;
|
|
155
|
+
openedAfterUpdate: boolean;
|
|
156
|
+
};
|
|
157
|
+
};
|