@gainsnetwork/sdk 0.0.0-v10.rc3 → 0.0.0-v10.rc4
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/backend/index.d.ts +1 -0
- package/lib/backend/index.js +10 -0
- package/lib/backend/tradingVariables/converter.d.ts +2 -1
- package/lib/backend/tradingVariables/converter.js +2 -1
- package/lib/backend/tradingVariables/index.d.ts +2 -0
- package/lib/backend/tradingVariables/index.js +17 -0
- package/lib/constants.d.ts +10 -0
- package/lib/constants.js +11 -1
- package/lib/contracts/utils/pairs.js +10 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +4 -0
- package/lib/trade/fees/borrowing/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +33 -0
- package/lib/trade/fees/borrowing/index.d.ts +3 -2
- package/lib/trade/fees/borrowing/index.js +13 -4
- package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
- package/lib/trade/fees/borrowingV2/builder.js +24 -0
- package/lib/trade/fees/borrowingV2/converter.d.ts +12 -3
- package/lib/trade/fees/borrowingV2/converter.js +29 -18
- package/lib/trade/fees/borrowingV2/fetcher.js +5 -13
- package/lib/trade/fees/borrowingV2/index.d.ts +5 -17
- package/lib/trade/fees/borrowingV2/index.js +9 -37
- package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
- package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
- package/lib/trade/fees/fundingFees/builder.js +35 -0
- package/lib/trade/fees/fundingFees/index.d.ts +12 -2
- package/lib/trade/fees/fundingFees/index.js +23 -3
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/index.d.ts +1 -1
- package/lib/trade/fees/index.js +2 -3
- package/lib/trade/fees/trading/builder.d.ts +18 -0
- package/lib/trade/fees/trading/builder.js +20 -0
- package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFees.js +66 -0
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
- package/lib/trade/fees/trading/index.d.ts +19 -11
- package/lib/trade/fees/trading/index.js +29 -21
- package/lib/trade/liquidation/builder.d.ts +25 -0
- package/lib/trade/liquidation/builder.js +59 -0
- package/lib/trade/liquidation/index.d.ts +8 -13
- package/lib/trade/liquidation/index.js +43 -88
- package/lib/trade/liquidation/types.d.ts +36 -21
- package/lib/trade/pnl/builder.d.ts +16 -0
- package/lib/trade/pnl/builder.js +44 -0
- package/lib/trade/pnl/index.d.ts +21 -30
- package/lib/trade/pnl/index.js +119 -50
- package/lib/trade/pnl/types.d.ts +29 -1
- package/lib/trade/types.d.ts +11 -1
- package/lib/trade/types.js +10 -0
- package/package.json +1 -1
- package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +0 -75
- package/lib/contracts/fetch/fees/borrowingFeesV2.js +0 -193
- package/lib/contracts/fetch/fees/fundingFees.d.ts +0 -66
- package/lib/contracts/fetch/fees/fundingFees.js +0 -150
- package/lib/contracts/fetch/priceImpact/skew.d.ts +0 -63
- package/lib/contracts/fetch/priceImpact/skew.js +0 -168
- package/lib/trade/liquidation.d.ts +0 -12
- package/lib/trade/liquidation.js +0 -55
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -33
|
@@ -1,150 +0,0 @@
|
|
|
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;
|
|
@@ -1,63 +0,0 @@
|
|
|
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>;
|
|
@@ -1,168 +0,0 @@
|
|
|
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.calculateTradeSkewPriceImpact = exports.fetchCollateralDecimals = exports.fetchSkewPriceImpactContext = exports.fetchPairSkewDepths = exports.fetchPairSkewDepth = exports.fetchPairOisAfterV10Token = exports.fetchPairOiAfterV10Token = void 0;
|
|
13
|
-
const priceImpact_1 = require("../../../trade/priceImpact");
|
|
14
|
-
/**
|
|
15
|
-
* @dev Fetches pair open interest in tokens for a specific pair
|
|
16
|
-
* @param contract GNSMultiCollatDiamond contract instance
|
|
17
|
-
* @param collateralIndex Collateral index
|
|
18
|
-
* @param pairIndex Pair index
|
|
19
|
-
* @returns Promise resolving to pair OI in tokens
|
|
20
|
-
*/
|
|
21
|
-
const fetchPairOiAfterV10Token = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
-
try {
|
|
23
|
-
const contractData = yield contract.getPairOiAfterV10Token(collateralIndex, pairIndex);
|
|
24
|
-
return (0, priceImpact_1.convertPairOiToken)(contractData);
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
console.error("Error fetching pair OI token:", error);
|
|
28
|
-
throw error;
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
exports.fetchPairOiAfterV10Token = fetchPairOiAfterV10Token;
|
|
32
|
-
/**
|
|
33
|
-
* @dev Fetches pair open interest in tokens for multiple pairs
|
|
34
|
-
* @param contract GNSMultiCollatDiamond contract instance
|
|
35
|
-
* @param collateralIndices Array of collateral indices
|
|
36
|
-
* @param pairIndices Array of pair indices
|
|
37
|
-
* @returns Promise resolving to array of pair OI in tokens
|
|
38
|
-
*/
|
|
39
|
-
const fetchPairOisAfterV10Token = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
-
if (collateralIndices.length !== pairIndices.length) {
|
|
41
|
-
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
42
|
-
}
|
|
43
|
-
try {
|
|
44
|
-
const contractDataArray = yield contract.getPairOisAfterV10Token(collateralIndices, pairIndices);
|
|
45
|
-
return contractDataArray.map(priceImpact_1.convertPairOiToken);
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
console.error("Error fetching pair OIs token:", error);
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
|
|
53
|
-
/**
|
|
54
|
-
* @dev Fetches skew depth for a specific pair
|
|
55
|
-
* @param contract GNSMultiCollatDiamond contract instance
|
|
56
|
-
* @param collateralIndex Collateral index
|
|
57
|
-
* @param pairIndex Pair index
|
|
58
|
-
* @param collateralDecimals Number of decimals for the collateral
|
|
59
|
-
* @returns Promise resolving to normalized skew depth
|
|
60
|
-
*/
|
|
61
|
-
const fetchPairSkewDepth = (contract, collateralIndex, pairIndex, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
-
try {
|
|
63
|
-
const contractDepth = yield contract.getPairSkewDepth(collateralIndex, pairIndex);
|
|
64
|
-
return (0, priceImpact_1.normalizeSkewDepth)(contractDepth.toBigInt(), collateralDecimals);
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
console.error("Error fetching skew depth:", error);
|
|
68
|
-
throw error;
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
exports.fetchPairSkewDepth = fetchPairSkewDepth;
|
|
72
|
-
/**
|
|
73
|
-
* @dev Fetches skew depths for multiple pairs
|
|
74
|
-
* @param contract GNSMultiCollatDiamond contract instance
|
|
75
|
-
* @param collateralIndices Array of collateral indices
|
|
76
|
-
* @param pairIndices Array of pair indices
|
|
77
|
-
* @param collateralDecimals Array of collateral decimals for each pair
|
|
78
|
-
* @returns Promise resolving to array of normalized skew depths
|
|
79
|
-
*/
|
|
80
|
-
const fetchPairSkewDepths = (contract, collateralIndices, pairIndices, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
-
if (collateralIndices.length !== pairIndices.length ||
|
|
82
|
-
pairIndices.length !== collateralDecimals.length) {
|
|
83
|
-
throw new Error("All input arrays must have the same length");
|
|
84
|
-
}
|
|
85
|
-
try {
|
|
86
|
-
const contractDepths = yield contract.getPairSkewDepths(collateralIndices, pairIndices);
|
|
87
|
-
return contractDepths.map((depth, i) => (0, priceImpact_1.normalizeSkewDepth)(depth.toBigInt(), collateralDecimals[i]));
|
|
88
|
-
}
|
|
89
|
-
catch (error) {
|
|
90
|
-
console.error("Error fetching skew depths:", error);
|
|
91
|
-
throw error;
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
exports.fetchPairSkewDepths = fetchPairSkewDepths;
|
|
95
|
-
/**
|
|
96
|
-
* @dev Fetches complete skew price impact context for multiple pairs
|
|
97
|
-
* @param contract GNSMultiCollatDiamond contract instance
|
|
98
|
-
* @param collateralIndices Array of collateral indices
|
|
99
|
-
* @param pairIndices Array of pair indices
|
|
100
|
-
* @param collateralDecimals Array of collateral decimals for each pair
|
|
101
|
-
* @returns Promise resolving to complete skew price impact context
|
|
102
|
-
*/
|
|
103
|
-
const fetchSkewPriceImpactContext = (contract, collateralIndices, pairIndices, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
|
-
try {
|
|
105
|
-
// Fetch OI data and skew depths in parallel
|
|
106
|
-
const [pairOiTokens, skewDepths] = yield Promise.all([
|
|
107
|
-
(0, exports.fetchPairOisAfterV10Token)(contract, collateralIndices, pairIndices),
|
|
108
|
-
(0, exports.fetchPairSkewDepths)(contract, collateralIndices, pairIndices, collateralDecimals),
|
|
109
|
-
]);
|
|
110
|
-
return (0, priceImpact_1.createSkewPriceImpactContext)(collateralIndices, pairIndices, skewDepths, pairOiTokens);
|
|
111
|
-
}
|
|
112
|
-
catch (error) {
|
|
113
|
-
console.error("Error fetching skew price impact context:", error);
|
|
114
|
-
throw error;
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
exports.fetchSkewPriceImpactContext = fetchSkewPriceImpactContext;
|
|
118
|
-
/**
|
|
119
|
-
* @dev Fetches collateral decimals for given collateral indices
|
|
120
|
-
* @param contract GNSMultiCollatDiamond contract instance
|
|
121
|
-
* @param collateralIndices Array of collateral indices
|
|
122
|
-
* @returns Promise resolving to array of decimals
|
|
123
|
-
*/
|
|
124
|
-
const fetchCollateralDecimals = (contract, collateralIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
125
|
-
try {
|
|
126
|
-
// Get unique collateral indices to minimize calls
|
|
127
|
-
const uniqueIndices = [...new Set(collateralIndices)];
|
|
128
|
-
// Fetch collateral info for unique indices
|
|
129
|
-
const promises = uniqueIndices.map((index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
130
|
-
const collateral = yield contract.getCollateral(index);
|
|
131
|
-
return { index, decimals: Number(collateral.precision) };
|
|
132
|
-
}));
|
|
133
|
-
const collateralData = yield Promise.all(promises);
|
|
134
|
-
// Create a map for quick lookup
|
|
135
|
-
const decimalsMap = new Map(collateralData.map(data => [data.index, data.decimals]));
|
|
136
|
-
// Return decimals in the same order as input
|
|
137
|
-
return collateralIndices.map(index => decimalsMap.get(index) || 18 // Default to 18 if not found
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
console.error("Error fetching collateral decimals:", error);
|
|
142
|
-
throw error;
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
exports.fetchCollateralDecimals = fetchCollateralDecimals;
|
|
146
|
-
/**
|
|
147
|
-
* @dev Calculates skew price impact for a trade using contract call
|
|
148
|
-
* @param contract GNSMultiCollatDiamond contract instance
|
|
149
|
-
* @param collateralIndex Collateral index
|
|
150
|
-
* @param pairIndex Pair index
|
|
151
|
-
* @param long Whether trade is long
|
|
152
|
-
* @param positionSizeToken Position size in tokens
|
|
153
|
-
* @param open Whether trade is opening
|
|
154
|
-
* @returns Promise resolving to price impact percentage (1e10)
|
|
155
|
-
*/
|
|
156
|
-
const calculateTradeSkewPriceImpact = (contract, collateralIndex, pairIndex, long, positionSizeToken, open) => __awaiter(void 0, void 0, void 0, function* () {
|
|
157
|
-
try {
|
|
158
|
-
const priceImpactP = yield contract.getTradeSkewPriceImpactP(collateralIndex, pairIndex, long, BigInt(Math.round(positionSizeToken * 1e18)), // Convert to 1e18 precision
|
|
159
|
-
open);
|
|
160
|
-
// Convert from int256 1e10 to percentage
|
|
161
|
-
return Number(priceImpactP) / 1e10;
|
|
162
|
-
}
|
|
163
|
-
catch (error) {
|
|
164
|
-
console.error("Error calculating trade skew price impact:", error);
|
|
165
|
-
throw error;
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
exports.calculateTradeSkewPriceImpact = calculateTradeSkewPriceImpact;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
-
import { Fee, LiquidationParams, Trade, UserPriceImpact } from "./types";
|
|
3
|
-
import { ContractsVersion } from "../contracts/types";
|
|
4
|
-
export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
|
|
5
|
-
liquidationParams: LiquidationParams | undefined;
|
|
6
|
-
pairSpreadP: number | undefined;
|
|
7
|
-
collateralPriceUsd: number | undefined;
|
|
8
|
-
contractsVersion: ContractsVersion | undefined;
|
|
9
|
-
userPriceImpact?: UserPriceImpact | undefined;
|
|
10
|
-
};
|
|
11
|
-
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
|
|
12
|
-
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|
package/lib/trade/liquidation.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
|
|
4
|
-
const fees_1 = require("./fees");
|
|
5
|
-
const spread_1 = require("./spread");
|
|
6
|
-
const types_1 = require("../contracts/types");
|
|
7
|
-
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
8
|
-
var _a, _b;
|
|
9
|
-
const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
|
|
10
|
-
const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
11
|
-
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
12
|
-
let liqPriceDistance = (trade.openPrice *
|
|
13
|
-
(trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
|
|
14
|
-
trade.collateralAmount /
|
|
15
|
-
trade.leverage;
|
|
16
|
-
if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
|
|
17
|
-
context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
18
|
-
((((_a = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
19
|
-
context.liquidationParams.maxLiqSpreadP > 0) ||
|
|
20
|
-
(((_b = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !== undefined &&
|
|
21
|
-
context.userPriceImpact.fixedSpreadP > 0))) {
|
|
22
|
-
const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
|
|
23
|
-
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
24
|
-
}
|
|
25
|
-
return trade.long
|
|
26
|
-
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
27
|
-
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
28
|
-
};
|
|
29
|
-
exports.getLiquidationPrice = getLiquidationPrice;
|
|
30
|
-
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
31
|
-
if (liquidationParams === undefined ||
|
|
32
|
-
leverage === undefined ||
|
|
33
|
-
liquidationParams.maxLiqSpreadP === 0 ||
|
|
34
|
-
liquidationParams.startLiqThresholdP === 0 ||
|
|
35
|
-
liquidationParams.endLiqThresholdP === 0 ||
|
|
36
|
-
liquidationParams.startLeverage === 0 ||
|
|
37
|
-
liquidationParams.endLeverage === 0) {
|
|
38
|
-
return 0.9;
|
|
39
|
-
}
|
|
40
|
-
if (leverage < liquidationParams.startLeverage) {
|
|
41
|
-
return liquidationParams.startLiqThresholdP;
|
|
42
|
-
}
|
|
43
|
-
if (leverage > liquidationParams.endLeverage) {
|
|
44
|
-
return liquidationParams.endLiqThresholdP;
|
|
45
|
-
}
|
|
46
|
-
if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
|
|
47
|
-
return liquidationParams.endLiqThresholdP;
|
|
48
|
-
}
|
|
49
|
-
return (liquidationParams.startLiqThresholdP -
|
|
50
|
-
((leverage - liquidationParams.startLeverage) *
|
|
51
|
-
(liquidationParams.startLiqThresholdP -
|
|
52
|
-
liquidationParams.endLiqThresholdP)) /
|
|
53
|
-
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
54
|
-
};
|
|
55
|
-
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
package/lib/trade/pnl.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { GetBorrowingFeeContext } from "./fees";
|
|
2
|
-
import { Fee, LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
3
|
-
import { ContractsVersion } from "../contracts/types";
|
|
4
|
-
export type GetPnlContext = GetBorrowingFeeContext & {
|
|
5
|
-
fee: Fee | undefined;
|
|
6
|
-
collateralPriceUsd: number | undefined;
|
|
7
|
-
contractsVersion: ContractsVersion | undefined;
|
|
8
|
-
feeMultiplier: number | undefined;
|
|
9
|
-
};
|
|
10
|
-
export declare const getPnl: (price: number | undefined, trade: Trade, tradeInfo: TradeInfo, initialAccFees: TradeInitialAccFees, liquidationParams: LiquidationParams, useFees: boolean, context: GetPnlContext) => number[] | undefined;
|
package/lib/trade/pnl.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getPnl = void 0;
|
|
4
|
-
const fees_1 = require("./fees");
|
|
5
|
-
const liquidation_1 = require("./liquidation");
|
|
6
|
-
const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
7
|
-
if (!price) {
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
const posCollat = trade.collateralAmount;
|
|
11
|
-
const { openPrice, leverage } = trade;
|
|
12
|
-
const { fee } = context;
|
|
13
|
-
let pnlCollat = trade.long
|
|
14
|
-
? ((price - openPrice) / openPrice) * leverage * posCollat
|
|
15
|
-
: ((openPrice - price) / openPrice) * leverage * posCollat;
|
|
16
|
-
if (useFees) {
|
|
17
|
-
pnlCollat -= (0, fees_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
18
|
-
}
|
|
19
|
-
let pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
20
|
-
// Can be liquidated
|
|
21
|
-
if (pnlPercentage <=
|
|
22
|
-
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
|
|
23
|
-
pnlPercentage = -100;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd, context.feeMultiplier);
|
|
27
|
-
pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
28
|
-
}
|
|
29
|
-
pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
|
|
30
|
-
pnlCollat = (posCollat * pnlPercentage) / 100;
|
|
31
|
-
return [pnlCollat, pnlPercentage];
|
|
32
|
-
};
|
|
33
|
-
exports.getPnl = getPnl;
|