@gainsnetwork/sdk 0.0.2-rc.2 → 0.0.2
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 +1 -1
- package/lib/contracts/utils/index.d.ts +1 -0
- package/lib/contracts/utils/index.js +1 -0
- package/lib/contracts/utils/openTrades.d.ts +2 -7
- package/lib/contracts/utils/openTrades.js +25 -19
- package/lib/contracts/utils/pairs.d.ts +6 -0
- package/lib/contracts/utils/pairs.js +102 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
import { TradeContainer } from "@/trade/types";
|
|
2
|
-
import {
|
|
3
|
-
export
|
|
4
|
-
storage: GFarmTradingStorageV5;
|
|
5
|
-
pairInfos: GNSPairInfosV6_1;
|
|
6
|
-
pairsStorage: GNSPairsStorageV6;
|
|
7
|
-
};
|
|
8
|
-
export declare const fetchOpenPairTrades: (contracts: FetchTradesTradingContracts, pairBatchSize?: number) => Promise<TradeContainer[]>;
|
|
2
|
+
import { Contracts } from "@/contracts/types";
|
|
3
|
+
export declare const fetchOpenPairTrades: (contracts: Contracts, pairBatchSize?: number) => Promise<TradeContainer[]>;
|
|
@@ -14,7 +14,7 @@ const fetchOpenPairTrades = (contracts, pairBatchSize = 10) => __awaiter(void 0,
|
|
|
14
14
|
if (!contracts) {
|
|
15
15
|
return [];
|
|
16
16
|
}
|
|
17
|
-
const {
|
|
17
|
+
const { gnsPairsStorageV6: pairsStorageContract } = contracts;
|
|
18
18
|
try {
|
|
19
19
|
const totalPairIndexes = (yield pairsStorageContract.pairsCount()).toNumber() - 1;
|
|
20
20
|
let allOpenPairTrades = [];
|
|
@@ -33,7 +33,7 @@ const fetchOpenPairTrades = (contracts, pairBatchSize = 10) => __awaiter(void 0,
|
|
|
33
33
|
});
|
|
34
34
|
exports.fetchOpenPairTrades = fetchOpenPairTrades;
|
|
35
35
|
const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
-
const {
|
|
36
|
+
const { gfarmTradingStorageV5: storageContract, gnsPairInfosV6_1: pairInfosContract } = contracts;
|
|
37
37
|
const maxTradesPerPair = (yield storageContract.maxTradesPerPair()).toNumber();
|
|
38
38
|
const pairIndexesToFetch = Array.from({ length: endPairIndex - startPairIndex + 1 }, (_, i) => i + startPairIndex);
|
|
39
39
|
const rawTrades = yield Promise.all(pairIndexesToFetch.map((pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -51,12 +51,16 @@ const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __
|
|
|
51
51
|
for (let pairTradeIndex = 0; pairTradeIndex < maxTradesPerPair; pairTradeIndex++) {
|
|
52
52
|
openTradesCalls[pairTradeIndex] = storageContract.openTrades(pairTraderAddress, pairIndex, pairTradeIndex);
|
|
53
53
|
}
|
|
54
|
-
console.debug(
|
|
54
|
+
/*console.debug(
|
|
55
|
+
`Waiting on ${openTradesCalls.length} StorageContract::openTrades calls for trader ${pairTraderAddress}...`
|
|
56
|
+
);*/
|
|
55
57
|
const openTradesForTraderAddress = yield Promise.all(openTradesCalls);
|
|
56
58
|
console.debug(`Received all trades for trader ${pairTraderAddress} and pair ${pairIndex} in ${performance.now() - traderOpenTradesCallsStartTime}ms.`);
|
|
57
59
|
// Filter out any of the trades that aren't *really* open (NOTE: these will have an empty trader address, so just test against that)
|
|
58
60
|
const actualOpenTradesForTrader = openTradesForTraderAddress.filter(openTrade => openTrade.trader === pairTraderAddress);
|
|
59
|
-
console.debug(
|
|
61
|
+
/*console.debug(
|
|
62
|
+
`Filtered down to ${actualOpenTradesForTrader.length} actual open trades for trader ${pairTraderAddress} and pair ${pairIndex}; fetching corresponding trade info and initial fees...`
|
|
63
|
+
);*/
|
|
60
64
|
const [actualOpenTradesTradeInfos, actualOpenTradesInitialAccFees] = yield Promise.all([
|
|
61
65
|
Promise.all(actualOpenTradesForTrader.map(aot => storageContract.openTradesInfo(aot.trader, aot.pairIndex, aot.index))),
|
|
62
66
|
Promise.all(actualOpenTradesForTrader.map(aot => pairInfosContract.tradeInitialAccFees(aot.trader, aot.pairIndex, aot.index))),
|
|
@@ -77,30 +81,32 @@ const fetchOpenPairTradesBatch = (contracts, startPairIndex, endPairIndex) => __
|
|
|
77
81
|
finalOpenTradesForTrader[tradeIndex] = {
|
|
78
82
|
trade: {
|
|
79
83
|
trader: trade.trader,
|
|
80
|
-
pairIndex: trade.pairIndex,
|
|
81
|
-
index: trade.index,
|
|
82
|
-
initialPosToken: trade.initialPosToken,
|
|
83
|
-
openPrice: trade.openPrice,
|
|
84
|
-
buy: trade.buy,
|
|
85
|
-
leverage: trade.leverage,
|
|
86
|
-
tp: trade.tp,
|
|
87
|
-
sl: trade.sl,
|
|
84
|
+
pairIndex: parseInt(trade.pairIndex.toString()),
|
|
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,
|
|
88
92
|
},
|
|
89
93
|
tradeInfo: {
|
|
90
|
-
beingMarketClosed: tradeInfo.beingMarketClosed,
|
|
91
|
-
tokenPriceDai: tradeInfo.tokenPriceDai,
|
|
92
|
-
openInterestDai: tradeInfo.openInterestDai,
|
|
94
|
+
beingMarketClosed: tradeInfo.beingMarketClosed.toString() === "true",
|
|
95
|
+
tokenPriceDai: parseFloat(tradeInfo.tokenPriceDai.toString()) / 1e10,
|
|
96
|
+
openInterestDai: parseFloat(tradeInfo.openInterestDai.toString()) / 1e18,
|
|
93
97
|
tpLastUpdated: tradeInfo.tpLastUpdated,
|
|
94
98
|
slLastUpdated: tradeInfo.slLastUpdated,
|
|
95
99
|
},
|
|
96
100
|
initialAccFees: {
|
|
97
|
-
rollover: tradeInitialAccFees.rollover,
|
|
98
|
-
funding: tradeInitialAccFees.funding,
|
|
99
|
-
openedAfterUpdate: tradeInitialAccFees.openedAfterUpdate,
|
|
101
|
+
rollover: parseFloat(tradeInitialAccFees.rollover.toString()) / 1e18,
|
|
102
|
+
funding: parseFloat(tradeInitialAccFees.funding.toString()) / 1e18,
|
|
103
|
+
openedAfterUpdate: tradeInitialAccFees.openedAfterUpdate.toString() === "true",
|
|
100
104
|
},
|
|
101
105
|
};
|
|
102
106
|
}
|
|
103
|
-
console.debug(
|
|
107
|
+
/*console.debug(
|
|
108
|
+
`Trade info and initial fees fetched for ${finalOpenTradesForTrader.length} trades for trader ${pairTraderAddress} and pair ${pairIndex}; done!`
|
|
109
|
+
);*/
|
|
104
110
|
return finalOpenTradesForTrader;
|
|
105
111
|
})));
|
|
106
112
|
return openTradesForPairTraders;
|
|
@@ -0,0 +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[]>;
|
|
@@ -0,0 +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;
|