@gainsnetwork/sdk 0.2.67-rc6 → 0.2.68
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/constants.js +1 -2
- package/lib/contracts/addresses.json +20 -0
- package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +76 -0
- package/lib/contracts/fetch/fees/borrowingFeesV2.js +193 -0
- package/lib/contracts/fetch/fees/fundingFees.d.ts +66 -0
- package/lib/contracts/fetch/fees/fundingFees.js +150 -0
- package/lib/contracts/fetch/priceImpact/skew.d.ts +63 -0
- package/lib/contracts/fetch/priceImpact/skew.js +168 -0
- package/lib/contracts/index.js +3 -1
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/trade/fees/borrowingV2/converter.d.ts +66 -0
- package/lib/trade/fees/borrowingV2/converter.js +121 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +59 -0
- package/lib/trade/fees/borrowingV2/index.js +139 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +79 -0
- package/lib/trade/fees/borrowingV2/types.js +5 -0
- package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
- package/lib/trade/fees/fundingFees/converter.js +196 -0
- package/lib/trade/fees/fundingFees/index.d.ts +135 -0
- package/lib/trade/fees/fundingFees/index.js +322 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/trading/converter.d.ts +30 -0
- package/lib/trade/fees/trading/converter.js +43 -0
- package/lib/trade/fees/trading/index.d.ts +34 -0
- package/lib/trade/fees/trading/index.js +104 -0
- package/lib/trade/fees/trading/types.d.ts +39 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/priceImpact/index.d.ts +8 -0
- package/lib/trade/priceImpact/index.js +32 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +77 -0
- package/lib/trade/priceImpact/skew/converter.js +171 -0
- package/lib/trade/priceImpact/skew/index.d.ts +57 -0
- package/lib/trade/priceImpact/skew/index.js +175 -0
- package/lib/trade/priceImpact/skew/types.d.ts +55 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/package.json +1 -1
package/lib/constants.js
CHANGED
|
@@ -446,8 +446,7 @@ exports.delistedPairIxs = new Set([
|
|
|
446
446
|
99, 101, 106, 111, 113, 114, 116, 118, 120, 122, 123, 125, 127, 130, 147, 152,
|
|
447
447
|
160, 163, 170, 179, 182, 183, 187, 188, 189, 190, 208, 209, 225, 229, 230,
|
|
448
448
|
231, 238, 239, 241, 250, 253, 254, 258, 270, 275, 276, 278, 279, 282, 285,
|
|
449
|
-
290, 294, 296, 305, 330, 349, 352, 353, 354, 355, 357, 365, 366,
|
|
450
|
-
379, 384, 385,
|
|
449
|
+
290, 294, 296, 305, 330, 349, 352, 353, 354, 355, 357, 365, 366, 384, 385,
|
|
451
450
|
]);
|
|
452
451
|
exports.delistedGroupsIxs = new Set([]);
|
|
453
452
|
exports.DEFAULT_PROTECTION_CLOSE_FACTOR = 1;
|
|
@@ -22,6 +22,10 @@
|
|
|
22
22
|
"GNS": {
|
|
23
23
|
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
24
24
|
"gToken": "0x0000000000000000000000000000000000000000"
|
|
25
|
+
},
|
|
26
|
+
"BTCUSD": {
|
|
27
|
+
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
28
|
+
"gToken": "0x0000000000000000000000000000000000000000"
|
|
25
29
|
}
|
|
26
30
|
},
|
|
27
31
|
"42161": {
|
|
@@ -47,6 +51,10 @@
|
|
|
47
51
|
"GNS": {
|
|
48
52
|
"gTokenOpenPnlFeed": "0x4ca1638754Be8060E544Aca4A4F43702Be30E0D1",
|
|
49
53
|
"gToken": "0x4BeeF1113F968326905224D2Ca272f3032A9a9F4"
|
|
54
|
+
},
|
|
55
|
+
"BTCUSD": {
|
|
56
|
+
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
57
|
+
"gToken": "0x0000000000000000000000000000000000000000"
|
|
50
58
|
}
|
|
51
59
|
},
|
|
52
60
|
"421614": {
|
|
@@ -72,6 +80,10 @@
|
|
|
72
80
|
"GNS": {
|
|
73
81
|
"gTokenOpenPnlFeed": "0x68F8D4ec2EF23a15e61e1642E730b6f69fB9A5De",
|
|
74
82
|
"gToken": "0x5ed4bEA869300DB39bE2a92a8B42e53453742a43"
|
|
83
|
+
},
|
|
84
|
+
"BTCUSD": {
|
|
85
|
+
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
86
|
+
"gToken": "0x0000000000000000000000000000000000000000"
|
|
75
87
|
}
|
|
76
88
|
},
|
|
77
89
|
"8453": {
|
|
@@ -97,6 +109,10 @@
|
|
|
97
109
|
"GNS": {
|
|
98
110
|
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
99
111
|
"gToken": "0x0000000000000000000000000000000000000000"
|
|
112
|
+
},
|
|
113
|
+
"BTCUSD": {
|
|
114
|
+
"gTokenOpenPnlFeed": "0xA22F7b92d81Dd74bE5ca5758DF3FccF0A4CB19cc",
|
|
115
|
+
"gToken": "0xB7Cb7cB7C3cD96e251c9bF8800B9631134BbAdc6"
|
|
100
116
|
}
|
|
101
117
|
},
|
|
102
118
|
"33139": {
|
|
@@ -122,6 +138,10 @@
|
|
|
122
138
|
"GNS": {
|
|
123
139
|
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
124
140
|
"gToken": "0x0000000000000000000000000000000000000000"
|
|
141
|
+
},
|
|
142
|
+
"BTCUSD": {
|
|
143
|
+
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
144
|
+
"gToken": "0x0000000000000000000000000000000000000000"
|
|
125
145
|
}
|
|
126
146
|
}
|
|
127
147
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/// <reference types="mocha" />
|
|
2
|
+
import type { GNSMultiCollatDiamond } from "../../types/generated";
|
|
3
|
+
import { BorrowingFeeV2 } from "../../../trade/fees/borrowingV2";
|
|
4
|
+
/**
|
|
5
|
+
* @dev Fetches borrowing fee parameters v2 for specific pairs
|
|
6
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
7
|
+
* @param collateralIndices Array of collateral indices
|
|
8
|
+
* @param pairIndices Array of pair indices
|
|
9
|
+
* @returns Promise resolving to array of borrowing fee parameters
|
|
10
|
+
*/
|
|
11
|
+
export declare const fetchBorrowingFeeParamsV2: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.BorrowingFeeParams[]>;
|
|
12
|
+
/**
|
|
13
|
+
* @dev Fetches pair borrowing fee data v2 for specific pairs
|
|
14
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
15
|
+
* @param collateralIndices Array of collateral indices
|
|
16
|
+
* @param pairIndices Array of pair indices
|
|
17
|
+
* @returns Promise resolving to array of pair borrowing fee data
|
|
18
|
+
*/
|
|
19
|
+
export declare const fetchPairBorrowingFeeDataV2: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.PairBorrowingFeeData[]>;
|
|
20
|
+
/**
|
|
21
|
+
* @dev Fetches borrowing fees in collateral tokens for a specific trade
|
|
22
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
23
|
+
* @param trader Address of the trader
|
|
24
|
+
* @param index Trade index
|
|
25
|
+
* @param currentPairPrice Current price of the trading pair (1e6 precision)
|
|
26
|
+
* @returns Promise resolving to borrowing fees in collateral tokens
|
|
27
|
+
*/
|
|
28
|
+
export declare const fetchTradeBorrowingFeesCollateralV2: (contract: GNSMultiCollatDiamond, trader: string, index: number, currentPairPrice: number) => Promise<number>;
|
|
29
|
+
/**
|
|
30
|
+
* @dev Fetches pending accumulated borrowing fees for a specific pair
|
|
31
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
32
|
+
* @param collateralIndex Index of the collateral
|
|
33
|
+
* @param pairIndex Index of the trading pair
|
|
34
|
+
* @param currentPairPrice Current price of the trading pair (1e6 precision)
|
|
35
|
+
* @returns Promise resolving to pending accumulated borrowing fee
|
|
36
|
+
*/
|
|
37
|
+
export declare const fetchPairPendingAccBorrowingFeesV2: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, currentPairPrice: number) => Promise<number>;
|
|
38
|
+
/**
|
|
39
|
+
* @dev Convenience function to fetch all borrowing v2 data for specific pairs
|
|
40
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
41
|
+
* @param collateralIndex Index of the collateral
|
|
42
|
+
* @param pairIndices Array of pair indices
|
|
43
|
+
* @returns Promise resolving to complete borrowing v2 data set
|
|
44
|
+
*/
|
|
45
|
+
export declare const fetchAllBorrowingV2Data: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndices: number[]) => Promise<{
|
|
46
|
+
params: BorrowingFeeV2.BorrowingFeeParams[];
|
|
47
|
+
data: BorrowingFeeV2.PairBorrowingFeeData[];
|
|
48
|
+
context: BorrowingFeeV2.GetBorrowingFeeV2Context;
|
|
49
|
+
}>;
|
|
50
|
+
/**
|
|
51
|
+
* @dev Creates a complete borrowing v2 context from contract data
|
|
52
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
53
|
+
* @param collateralIndex Index of the collateral
|
|
54
|
+
* @param pairIndices Array of pair indices
|
|
55
|
+
* @param currentTimestamp Optional current timestamp for calculations
|
|
56
|
+
* @returns Promise resolving to complete borrowing v2 context
|
|
57
|
+
*/
|
|
58
|
+
export declare const createBorrowingV2ContextFromContract: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndices: number[], currentTimestamp?: number) => Promise<BorrowingFeeV2.GetBorrowingFeeV2Context>;
|
|
59
|
+
/**
|
|
60
|
+
* @dev Helper function to create context from already fetched arrays
|
|
61
|
+
* @param collateralIndices Array of collateral indices
|
|
62
|
+
* @param pairIndices Array of pair indices
|
|
63
|
+
* @param params Array of borrowing fee parameters
|
|
64
|
+
* @param data Array of pair borrowing fee data
|
|
65
|
+
* @param currentTimestamp Optional current timestamp
|
|
66
|
+
* @returns Complete borrowing v2 context
|
|
67
|
+
*/
|
|
68
|
+
export declare const createBorrowingV2ContextFromArrays: (collateralIndices: number[], pairIndices: number[], params: BorrowingFeeV2.BorrowingFeeParams[], data: BorrowingFeeV2.PairBorrowingFeeData[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
|
|
69
|
+
/**
|
|
70
|
+
* @dev Fetches borrowing v2 data for multiple collateral/pair combinations
|
|
71
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
72
|
+
* @param collateralIndices Array of collateral indices
|
|
73
|
+
* @param pairIndices Array of pair indices (must match collateralIndices length)
|
|
74
|
+
* @returns Promise resolving to complete borrowing v2 context
|
|
75
|
+
*/
|
|
76
|
+
export declare const fetchBorrowingV2DataForPairs: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.GetBorrowingFeeV2Context>;
|
|
@@ -0,0 +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.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = void 0;
|
|
13
|
+
const converter_1 = require("../../../trade/fees/borrowingV2/converter");
|
|
14
|
+
/**
|
|
15
|
+
* @dev Fetches borrowing fee parameters v2 for specific pairs
|
|
16
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
17
|
+
* @param collateralIndices Array of collateral indices
|
|
18
|
+
* @param pairIndices Array of pair indices
|
|
19
|
+
* @returns Promise resolving to array of borrowing fee parameters
|
|
20
|
+
*/
|
|
21
|
+
const fetchBorrowingFeeParamsV2 = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
if (collateralIndices.length !== pairIndices.length) {
|
|
23
|
+
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const contractParams = yield contract.getPairBorrowingFeeParams(collateralIndices, pairIndices);
|
|
27
|
+
return (0, converter_1.convertBorrowingFeeParamsArray)(contractParams);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.error("Error fetching borrowing fee params v2:", error);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
|
|
35
|
+
/**
|
|
36
|
+
* @dev Fetches pair borrowing fee data v2 for specific pairs
|
|
37
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
38
|
+
* @param collateralIndices Array of collateral indices
|
|
39
|
+
* @param pairIndices Array of pair indices
|
|
40
|
+
* @returns Promise resolving to array of pair borrowing fee data
|
|
41
|
+
*/
|
|
42
|
+
const fetchPairBorrowingFeeDataV2 = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
|
+
if (collateralIndices.length !== pairIndices.length) {
|
|
44
|
+
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const contractData = yield contract.getPairBorrowingFeeData(collateralIndices, pairIndices);
|
|
48
|
+
return (0, converter_1.convertPairBorrowingFeeDataArray)(contractData);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error("Error fetching pair borrowing fee data v2:", error);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
|
|
56
|
+
/**
|
|
57
|
+
* @dev Fetches borrowing fees in collateral tokens for a specific trade
|
|
58
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
59
|
+
* @param trader Address of the trader
|
|
60
|
+
* @param index Trade index
|
|
61
|
+
* @param currentPairPrice Current price of the trading pair (1e6 precision)
|
|
62
|
+
* @returns Promise resolving to borrowing fees in collateral tokens
|
|
63
|
+
*/
|
|
64
|
+
const fetchTradeBorrowingFeesCollateralV2 = (contract, trader, index, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
65
|
+
try {
|
|
66
|
+
const feesCollateral = yield contract.getTradeBorrowingFeesCollateral(trader, index, currentPairPrice);
|
|
67
|
+
// Convert BigNumber to normalized float
|
|
68
|
+
// Note: Collateral precision varies by chain, but contract returns proper precision
|
|
69
|
+
return parseFloat(feesCollateral.toString());
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
console.error("Error fetching trade borrowing fees collateral v2:", error);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV2;
|
|
77
|
+
/**
|
|
78
|
+
* @dev Fetches pending accumulated borrowing fees for a specific pair
|
|
79
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
80
|
+
* @param collateralIndex Index of the collateral
|
|
81
|
+
* @param pairIndex Index of the trading pair
|
|
82
|
+
* @param currentPairPrice Current price of the trading pair (1e6 precision)
|
|
83
|
+
* @returns Promise resolving to pending accumulated borrowing fee
|
|
84
|
+
*/
|
|
85
|
+
const fetchPairPendingAccBorrowingFeesV2 = (contract, collateralIndex, pairIndex, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
86
|
+
try {
|
|
87
|
+
const accBorrowingFeeP = yield contract.getPairPendingAccBorrowingFees(collateralIndex, pairIndex, currentPairPrice);
|
|
88
|
+
// Convert BigNumber to normalized float
|
|
89
|
+
return parseFloat(accBorrowingFeeP.toString()) / 1e20;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.error("Error fetching pair pending acc borrowing fees v2:", error);
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
|
|
97
|
+
/**
|
|
98
|
+
* @dev Convenience function to fetch all borrowing v2 data for specific pairs
|
|
99
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
100
|
+
* @param collateralIndex Index of the collateral
|
|
101
|
+
* @param pairIndices Array of pair indices
|
|
102
|
+
* @returns Promise resolving to complete borrowing v2 data set
|
|
103
|
+
*/
|
|
104
|
+
const fetchAllBorrowingV2Data = (contract, collateralIndex, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
105
|
+
const collateralIndices = new Array(pairIndices.length).fill(collateralIndex);
|
|
106
|
+
try {
|
|
107
|
+
// Fetch both parameters and data in parallel
|
|
108
|
+
const [params, data] = yield Promise.all([
|
|
109
|
+
(0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
|
|
110
|
+
(0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
|
|
111
|
+
]);
|
|
112
|
+
// Create context from fetched data
|
|
113
|
+
const context = (0, exports.createBorrowingV2ContextFromArrays)(collateralIndices, pairIndices, params, data);
|
|
114
|
+
return { params, data, context };
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
console.error("Error fetching all borrowing v2 data:", error);
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
|
|
122
|
+
/**
|
|
123
|
+
* @dev Creates a complete borrowing v2 context from contract data
|
|
124
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
125
|
+
* @param collateralIndex Index of the collateral
|
|
126
|
+
* @param pairIndices Array of pair indices
|
|
127
|
+
* @param currentTimestamp Optional current timestamp for calculations
|
|
128
|
+
* @returns Promise resolving to complete borrowing v2 context
|
|
129
|
+
*/
|
|
130
|
+
const createBorrowingV2ContextFromContract = (contract, collateralIndex, pairIndices, currentTimestamp) => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const { context } = yield (0, exports.fetchAllBorrowingV2Data)(contract, collateralIndex, pairIndices);
|
|
132
|
+
return Object.assign(Object.assign({}, context), { currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000) });
|
|
133
|
+
});
|
|
134
|
+
exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContract;
|
|
135
|
+
/**
|
|
136
|
+
* @dev Helper function to create context from already fetched arrays
|
|
137
|
+
* @param collateralIndices Array of collateral indices
|
|
138
|
+
* @param pairIndices Array of pair indices
|
|
139
|
+
* @param params Array of borrowing fee parameters
|
|
140
|
+
* @param data Array of pair borrowing fee data
|
|
141
|
+
* @param currentTimestamp Optional current timestamp
|
|
142
|
+
* @returns Complete borrowing v2 context
|
|
143
|
+
*/
|
|
144
|
+
const createBorrowingV2ContextFromArrays = (collateralIndices, pairIndices, params, data, currentTimestamp) => {
|
|
145
|
+
const context = {
|
|
146
|
+
currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
|
|
147
|
+
borrowingParams: {},
|
|
148
|
+
borrowingData: {},
|
|
149
|
+
};
|
|
150
|
+
// Build nested objects indexed by collateralIndex and pairIndex
|
|
151
|
+
for (let i = 0; i < collateralIndices.length; i++) {
|
|
152
|
+
const collateralIndex = collateralIndices[i];
|
|
153
|
+
const pairIndex = pairIndices[i];
|
|
154
|
+
// Initialize collateral index objects if they don't exist
|
|
155
|
+
if (!context.borrowingParams[collateralIndex]) {
|
|
156
|
+
context.borrowingParams[collateralIndex] = {};
|
|
157
|
+
}
|
|
158
|
+
if (!context.borrowingData[collateralIndex]) {
|
|
159
|
+
context.borrowingData[collateralIndex] = {};
|
|
160
|
+
}
|
|
161
|
+
// Store data
|
|
162
|
+
context.borrowingParams[collateralIndex][pairIndex] = params[i];
|
|
163
|
+
context.borrowingData[collateralIndex][pairIndex] = data[i];
|
|
164
|
+
}
|
|
165
|
+
return context;
|
|
166
|
+
};
|
|
167
|
+
exports.createBorrowingV2ContextFromArrays = createBorrowingV2ContextFromArrays;
|
|
168
|
+
/**
|
|
169
|
+
* @dev Fetches borrowing v2 data for multiple collateral/pair combinations
|
|
170
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
171
|
+
* @param collateralIndices Array of collateral indices
|
|
172
|
+
* @param pairIndices Array of pair indices (must match collateralIndices length)
|
|
173
|
+
* @returns Promise resolving to complete borrowing v2 context
|
|
174
|
+
*/
|
|
175
|
+
const fetchBorrowingV2DataForPairs = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
176
|
+
if (collateralIndices.length !== pairIndices.length) {
|
|
177
|
+
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
// Fetch both parameters and data in parallel
|
|
181
|
+
const [params, data] = yield Promise.all([
|
|
182
|
+
(0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
|
|
183
|
+
(0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
|
|
184
|
+
]);
|
|
185
|
+
// Create and return context
|
|
186
|
+
return (0, exports.createBorrowingV2ContextFromArrays)(collateralIndices, pairIndices, params, data);
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
console.error("Error fetching borrowing v2 data for pairs:", error);
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
exports.fetchBorrowingV2DataForPairs = fetchBorrowingV2DataForPairs;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { GNSMultiCollatDiamond } from "../../types/generated";
|
|
2
|
+
import type { TradeInitialAccFees } from "../../../trade/fees/fundingFees";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Fetches pending accumulated funding fees for a specific pair
|
|
5
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
6
|
+
* @param collateralIndex Collateral index
|
|
7
|
+
* @param pairIndex Pair index
|
|
8
|
+
* @param currentPairPrice Current pair price (1e10)
|
|
9
|
+
* @returns Promise resolving to accumulated funding fees and current rate
|
|
10
|
+
*/
|
|
11
|
+
export declare const fetchPairPendingAccFundingFees: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, currentPairPrice: bigint) => Promise<{
|
|
12
|
+
accFundingFeeLongP: number;
|
|
13
|
+
accFundingFeeShortP: number;
|
|
14
|
+
currentFundingRatePerSecondP: number;
|
|
15
|
+
}>;
|
|
16
|
+
/**
|
|
17
|
+
* @dev Fetches funding fees for a specific trade in collateral tokens
|
|
18
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
19
|
+
* @param trader Trader address
|
|
20
|
+
* @param index Trade index
|
|
21
|
+
* @param currentPairPrice Current pair price (1e10)
|
|
22
|
+
* @returns Promise resolving to funding fee in collateral tokens
|
|
23
|
+
*/
|
|
24
|
+
export declare const fetchTradeFundingFeesCollateral: (contract: GNSMultiCollatDiamond, trader: string, index: number, currentPairPrice: bigint) => Promise<number>;
|
|
25
|
+
/**
|
|
26
|
+
* @dev Fetches trade fees data for a specific trade
|
|
27
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
28
|
+
* @param trader Trader address
|
|
29
|
+
* @param index Trade index
|
|
30
|
+
* @returns Promise resolving to trade fees data
|
|
31
|
+
*/
|
|
32
|
+
export declare const fetchTradeFeesData: (contract: GNSMultiCollatDiamond, trader: string, index: number) => Promise<TradeInitialAccFees>;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Fetches trade fees data for multiple trades
|
|
35
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
36
|
+
* @param traders Array of trader addresses
|
|
37
|
+
* @param indices Array of trade indices
|
|
38
|
+
* @returns Promise resolving to array of trade fees data
|
|
39
|
+
*/
|
|
40
|
+
export declare const fetchTradeFeesDataBatch: (contract: GNSMultiCollatDiamond, traders: string[], indices: number[]) => Promise<TradeInitialAccFees[]>;
|
|
41
|
+
/**
|
|
42
|
+
* @dev Fetches pending accumulated funding fees for multiple pairs
|
|
43
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
44
|
+
* @param collateralIndices Array of collateral indices
|
|
45
|
+
* @param pairIndices Array of pair indices
|
|
46
|
+
* @param currentPairPrices Array of current pair prices (1e10)
|
|
47
|
+
* @returns Promise resolving to array of accumulated funding fees
|
|
48
|
+
*/
|
|
49
|
+
export declare const fetchPairPendingAccFundingFeesBatch: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[], currentPairPrices: bigint[]) => Promise<Array<{
|
|
50
|
+
accFundingFeeLongP: number;
|
|
51
|
+
accFundingFeeShortP: number;
|
|
52
|
+
currentFundingRatePerSecondP: number;
|
|
53
|
+
}>>;
|
|
54
|
+
/**
|
|
55
|
+
* @dev Helper to convert price from number to contract format
|
|
56
|
+
* @param price Price as number
|
|
57
|
+
* @returns Price in contract format (1e10)
|
|
58
|
+
*/
|
|
59
|
+
export declare const priceToContractFormat: (price: number) => bigint;
|
|
60
|
+
/**
|
|
61
|
+
* @dev Helper to convert collateral amount to contract format
|
|
62
|
+
* @param amount Amount as number
|
|
63
|
+
* @param decimals Collateral decimals (6 for USDC, 18 for others)
|
|
64
|
+
* @returns Amount in contract format
|
|
65
|
+
*/
|
|
66
|
+
export declare const collateralToContractFormat: (amount: number, decimals: number) => bigint;
|
|
@@ -0,0 +1,150 @@
|
|
|
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.collateralToContractFormat = exports.priceToContractFormat = exports.fetchPairPendingAccFundingFeesBatch = exports.fetchTradeFeesDataBatch = exports.fetchTradeFeesData = exports.fetchTradeFundingFeesCollateral = exports.fetchPairPendingAccFundingFees = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* @dev Fetches pending accumulated funding fees for a specific pair
|
|
15
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
16
|
+
* @param collateralIndex Collateral index
|
|
17
|
+
* @param pairIndex Pair index
|
|
18
|
+
* @param currentPairPrice Current pair price (1e10)
|
|
19
|
+
* @returns Promise resolving to accumulated funding fees and current rate
|
|
20
|
+
*/
|
|
21
|
+
const fetchPairPendingAccFundingFees = (contract, collateralIndex, pairIndex, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
try {
|
|
23
|
+
const result = yield contract.getPairPendingAccFundingFees(collateralIndex, pairIndex, currentPairPrice);
|
|
24
|
+
return {
|
|
25
|
+
accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
|
|
26
|
+
accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
|
|
27
|
+
currentFundingRatePerSecondP: Number(result.currentFundingRatePerSecondP) / 1e18, // FUNDING_RATE_PER_SECOND_P precision
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
console.error("Error fetching pair pending acc funding fees:", error);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
|
|
36
|
+
/**
|
|
37
|
+
* @dev Fetches funding fees for a specific trade in collateral tokens
|
|
38
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
39
|
+
* @param trader Trader address
|
|
40
|
+
* @param index Trade index
|
|
41
|
+
* @param currentPairPrice Current pair price (1e10)
|
|
42
|
+
* @returns Promise resolving to funding fee in collateral tokens
|
|
43
|
+
*/
|
|
44
|
+
const fetchTradeFundingFeesCollateral = (contract, trader, index, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
+
try {
|
|
46
|
+
const fundingFeeCollateral = yield contract.getTradeFundingFeesCollateral(trader, index, currentPairPrice);
|
|
47
|
+
// Convert from BigNumber to number (collateral precision already applied)
|
|
48
|
+
return Number(fundingFeeCollateral);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error("Error fetching trade funding fees:", error);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
|
|
56
|
+
/**
|
|
57
|
+
* @dev Fetches trade fees data for a specific trade
|
|
58
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
59
|
+
* @param trader Trader address
|
|
60
|
+
* @param index Trade index
|
|
61
|
+
* @returns Promise resolving to trade fees data
|
|
62
|
+
*/
|
|
63
|
+
const fetchTradeFeesData = (contract, trader, index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
64
|
+
try {
|
|
65
|
+
const feesData = yield contract.getTradeFeesData(trader, index);
|
|
66
|
+
return {
|
|
67
|
+
accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
68
|
+
accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
69
|
+
openBlock: 0, // Not available in this struct
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error("Error fetching trade fees data:", error);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
exports.fetchTradeFeesData = fetchTradeFeesData;
|
|
78
|
+
/**
|
|
79
|
+
* @dev Fetches trade fees data for multiple trades
|
|
80
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
81
|
+
* @param traders Array of trader addresses
|
|
82
|
+
* @param indices Array of trade indices
|
|
83
|
+
* @returns Promise resolving to array of trade fees data
|
|
84
|
+
*/
|
|
85
|
+
const fetchTradeFeesDataBatch = (contract, traders, indices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
86
|
+
if (traders.length !== indices.length) {
|
|
87
|
+
throw new Error("Traders and indices arrays must have the same length");
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const feesDatas = yield contract.getTradeFeesDataArray(traders, indices);
|
|
91
|
+
return feesDatas.map(feesData => ({
|
|
92
|
+
accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
93
|
+
accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
94
|
+
openBlock: 0,
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error("Error fetching trade fees data batch:", error);
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
|
|
103
|
+
/**
|
|
104
|
+
* @dev Fetches pending accumulated funding fees for multiple pairs
|
|
105
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
106
|
+
* @param collateralIndices Array of collateral indices
|
|
107
|
+
* @param pairIndices Array of pair indices
|
|
108
|
+
* @param currentPairPrices Array of current pair prices (1e10)
|
|
109
|
+
* @returns Promise resolving to array of accumulated funding fees
|
|
110
|
+
*/
|
|
111
|
+
const fetchPairPendingAccFundingFeesBatch = (contract, collateralIndices, pairIndices, currentPairPrices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
112
|
+
if (collateralIndices.length !== pairIndices.length ||
|
|
113
|
+
pairIndices.length !== currentPairPrices.length) {
|
|
114
|
+
throw new Error("All input arrays must have the same length");
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
// Fetch all in parallel
|
|
118
|
+
const promises = collateralIndices.map((collateralIndex, i) => contract.getPairPendingAccFundingFees(collateralIndex, pairIndices[i], currentPairPrices[i]));
|
|
119
|
+
const results = yield Promise.all(promises);
|
|
120
|
+
return results.map(result => ({
|
|
121
|
+
accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
|
|
122
|
+
accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
|
|
123
|
+
currentFundingRatePerSecondP: Number(result.currentFundingRatePerSecondP) / 1e18,
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
console.error("Error fetching pair pending acc funding fees batch:", error);
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
exports.fetchPairPendingAccFundingFeesBatch = fetchPairPendingAccFundingFeesBatch;
|
|
132
|
+
/**
|
|
133
|
+
* @dev Helper to convert price from number to contract format
|
|
134
|
+
* @param price Price as number
|
|
135
|
+
* @returns Price in contract format (1e10)
|
|
136
|
+
*/
|
|
137
|
+
const priceToContractFormat = (price) => {
|
|
138
|
+
return BigInt(Math.round(price * 1e10));
|
|
139
|
+
};
|
|
140
|
+
exports.priceToContractFormat = priceToContractFormat;
|
|
141
|
+
/**
|
|
142
|
+
* @dev Helper to convert collateral amount to contract format
|
|
143
|
+
* @param amount Amount as number
|
|
144
|
+
* @param decimals Collateral decimals (6 for USDC, 18 for others)
|
|
145
|
+
* @returns Amount in contract format
|
|
146
|
+
*/
|
|
147
|
+
const collateralToContractFormat = (amount, decimals) => {
|
|
148
|
+
return BigInt(Math.round(amount * Math.pow(10, decimals)));
|
|
149
|
+
};
|
|
150
|
+
exports.collateralToContractFormat = collateralToContractFormat;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { GNSMultiCollatDiamond } from "../../types/generated";
|
|
2
|
+
import type { PairOiToken, SkewPriceImpactContext } from "../../../trade/priceImpact";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Fetches pair open interest in tokens for a specific pair
|
|
5
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
6
|
+
* @param collateralIndex Collateral index
|
|
7
|
+
* @param pairIndex Pair index
|
|
8
|
+
* @returns Promise resolving to pair OI in tokens
|
|
9
|
+
*/
|
|
10
|
+
export declare const fetchPairOiAfterV10Token: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number) => Promise<PairOiToken>;
|
|
11
|
+
/**
|
|
12
|
+
* @dev Fetches pair open interest in tokens for multiple pairs
|
|
13
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
14
|
+
* @param collateralIndices Array of collateral indices
|
|
15
|
+
* @param pairIndices Array of pair indices
|
|
16
|
+
* @returns Promise resolving to array of pair OI in tokens
|
|
17
|
+
*/
|
|
18
|
+
export declare const fetchPairOisAfterV10Token: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<PairOiToken[]>;
|
|
19
|
+
/**
|
|
20
|
+
* @dev Fetches skew depth for a specific pair
|
|
21
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
22
|
+
* @param collateralIndex Collateral index
|
|
23
|
+
* @param pairIndex Pair index
|
|
24
|
+
* @param collateralDecimals Number of decimals for the collateral
|
|
25
|
+
* @returns Promise resolving to normalized skew depth
|
|
26
|
+
*/
|
|
27
|
+
export declare const fetchPairSkewDepth: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, collateralDecimals: number) => Promise<number>;
|
|
28
|
+
/**
|
|
29
|
+
* @dev Fetches skew depths for multiple pairs
|
|
30
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
31
|
+
* @param collateralIndices Array of collateral indices
|
|
32
|
+
* @param pairIndices Array of pair indices
|
|
33
|
+
* @param collateralDecimals Array of collateral decimals for each pair
|
|
34
|
+
* @returns Promise resolving to array of normalized skew depths
|
|
35
|
+
*/
|
|
36
|
+
export declare const fetchPairSkewDepths: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[], collateralDecimals: number[]) => Promise<number[]>;
|
|
37
|
+
/**
|
|
38
|
+
* @dev Fetches complete skew price impact context for multiple pairs
|
|
39
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
40
|
+
* @param collateralIndices Array of collateral indices
|
|
41
|
+
* @param pairIndices Array of pair indices
|
|
42
|
+
* @param collateralDecimals Array of collateral decimals for each pair
|
|
43
|
+
* @returns Promise resolving to complete skew price impact context
|
|
44
|
+
*/
|
|
45
|
+
export declare const fetchSkewPriceImpactContext: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[], collateralDecimals: number[]) => Promise<SkewPriceImpactContext>;
|
|
46
|
+
/**
|
|
47
|
+
* @dev Fetches collateral decimals for given collateral indices
|
|
48
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
49
|
+
* @param collateralIndices Array of collateral indices
|
|
50
|
+
* @returns Promise resolving to array of decimals
|
|
51
|
+
*/
|
|
52
|
+
export declare const fetchCollateralDecimals: (contract: GNSMultiCollatDiamond, collateralIndices: number[]) => Promise<number[]>;
|
|
53
|
+
/**
|
|
54
|
+
* @dev Calculates skew price impact for a trade using contract call
|
|
55
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
56
|
+
* @param collateralIndex Collateral index
|
|
57
|
+
* @param pairIndex Pair index
|
|
58
|
+
* @param long Whether trade is long
|
|
59
|
+
* @param positionSizeToken Position size in tokens
|
|
60
|
+
* @param open Whether trade is opening
|
|
61
|
+
* @returns Promise resolving to price impact percentage (1e10)
|
|
62
|
+
*/
|
|
63
|
+
export declare const calculateTradeSkewPriceImpact: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, long: boolean, positionSizeToken: number, open: boolean) => Promise<number>;
|