@gainsnetwork/sdk 0.0.0-v10.rc5 → 0.0.0-v10.rc7
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.
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.encodeUiRealizedPnlData = exports.encodeTradeFeesData = exports.convertUiRealizedPnlDataArray = exports.convertUiRealizedPnlData = exports.convertTradeFeesDataArray = exports.convertTradeFeesData = void 0;
|
|
7
|
+
const borrowingV2_1 = require("./borrowingV2");
|
|
8
|
+
const converter_1 = require("./fundingFees/converter");
|
|
7
9
|
/**
|
|
8
10
|
* @dev Converts contract TradeFeesData to SDK format
|
|
9
11
|
* @param data Trade fees data from contract
|
|
@@ -22,8 +24,10 @@ const convertTradeFeesData = (data, collateralConfig) => {
|
|
|
22
24
|
Math.pow(10, decimals),
|
|
23
25
|
virtualAvailableCollateralInDiamond: parseFloat(data.virtualAvailableCollateralInDiamond.toString()) /
|
|
24
26
|
Math.pow(10, decimals),
|
|
25
|
-
initialAccFundingFeeP: parseFloat(data.initialAccFundingFeeP.toString()) /
|
|
26
|
-
|
|
27
|
+
initialAccFundingFeeP: parseFloat(data.initialAccFundingFeeP.toString()) /
|
|
28
|
+
converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P,
|
|
29
|
+
initialAccBorrowingFeeP: parseFloat(data.initialAccBorrowingFeeP.toString()) /
|
|
30
|
+
borrowingV2_1.BORROWING_V2_PRECISION.ACC_FEE,
|
|
27
31
|
};
|
|
28
32
|
};
|
|
29
33
|
exports.convertTradeFeesData = convertTradeFeesData;
|
|
@@ -85,8 +89,8 @@ const encodeTradeFeesData = (data, collateralConfig) => {
|
|
|
85
89
|
alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral * Math.pow(10, decimals)),
|
|
86
90
|
virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond * Math.pow(10, decimals)),
|
|
87
91
|
__placeholder: 0,
|
|
88
|
-
initialAccFundingFeeP: Math.round(data.initialAccFundingFeeP *
|
|
89
|
-
initialAccBorrowingFeeP: Math.round(data.initialAccBorrowingFeeP *
|
|
92
|
+
initialAccFundingFeeP: Math.round(data.initialAccFundingFeeP * converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P),
|
|
93
|
+
initialAccBorrowingFeeP: Math.round(data.initialAccBorrowingFeeP * borrowingV2_1.BORROWING_V2_PRECISION.ACC_FEE),
|
|
90
94
|
};
|
|
91
95
|
};
|
|
92
96
|
exports.encodeTradeFeesData = encodeTradeFeesData;
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* @dev Based on skew-based funding rate model with velocity and APR multipliers
|
|
4
4
|
*/
|
|
5
5
|
import { FundingFeeParams, PairFundingFeeData, GetFundingFeeContext, TradeFundingFeeResult, PairPendingAccFundingFeesResult, PairOiAfterV10 } from "./types";
|
|
6
|
+
import { GetPairFundingFeeContext } from "./pairContext";
|
|
6
7
|
/**
|
|
7
8
|
* @dev Calculates current funding velocity per year based on skew
|
|
8
9
|
* @param netExposureToken Net exposure (long - short) in tokens
|
|
@@ -57,12 +58,12 @@ export declare const getLongShortAprMultiplier: (avgFundingRatePerSecondP: numbe
|
|
|
57
58
|
*/
|
|
58
59
|
export declare const getPairPendingAccFundingFees: (params: FundingFeeParams, data: PairFundingFeeData, currentPairPrice: number, pairOiToken: PairOiAfterV10, netExposureToken: number, netExposureUsd: number, currentTimestamp: number) => PairPendingAccFundingFeesResult;
|
|
59
60
|
/**
|
|
60
|
-
* @dev Calculates funding fees for a specific trade
|
|
61
|
-
* @param trade Trade parameters (collateral amount, leverage, open price, long/short
|
|
61
|
+
* @dev Calculates funding fees for a specific trade
|
|
62
|
+
* @param trade Trade parameters (collateral amount, leverage, open price, long/short)
|
|
62
63
|
* @param tradeInfo Trade info (contracts version)
|
|
63
64
|
* @param tradeFeesData Trade fees data containing initial acc funding fee
|
|
64
65
|
* @param currentPairPrice Current pair price
|
|
65
|
-
* @param context
|
|
66
|
+
* @param context Pair-specific funding fee context
|
|
66
67
|
* @returns Funding fee in collateral tokens
|
|
67
68
|
*/
|
|
68
69
|
export declare const getTradeFundingFeesCollateral: (trade: {
|
|
@@ -70,36 +71,11 @@ export declare const getTradeFundingFeesCollateral: (trade: {
|
|
|
70
71
|
leverage: number;
|
|
71
72
|
openPrice: number;
|
|
72
73
|
long: boolean;
|
|
73
|
-
collateralIndex?: number;
|
|
74
|
-
pairIndex?: number;
|
|
75
74
|
}, tradeInfo: {
|
|
76
75
|
contractsVersion: number;
|
|
77
76
|
}, tradeFeesData: {
|
|
78
77
|
initialAccFundingFeeP: number;
|
|
79
|
-
}, currentPairPrice: number, context
|
|
80
|
-
pairOiAfterV10?: {
|
|
81
|
-
[collateralIndex: number]: {
|
|
82
|
-
[pairIndex: number]: PairOiAfterV10;
|
|
83
|
-
};
|
|
84
|
-
} | undefined;
|
|
85
|
-
netExposureToken?: {
|
|
86
|
-
[collateralIndex: number]: {
|
|
87
|
-
[pairIndex: number]: number;
|
|
88
|
-
};
|
|
89
|
-
} | undefined;
|
|
90
|
-
netExposureUsd?: {
|
|
91
|
-
[collateralIndex: number]: {
|
|
92
|
-
[pairIndex: number]: number;
|
|
93
|
-
};
|
|
94
|
-
} | undefined;
|
|
95
|
-
}) | {
|
|
96
|
-
currentTimestamp: number;
|
|
97
|
-
params: FundingFeeParams;
|
|
98
|
-
data: PairFundingFeeData;
|
|
99
|
-
pairOi?: PairOiAfterV10 | undefined;
|
|
100
|
-
netExposureToken?: number | undefined;
|
|
101
|
-
netExposureUsd?: number | undefined;
|
|
102
|
-
} | undefined) => number;
|
|
78
|
+
}, currentPairPrice: number, context: GetPairFundingFeeContext) => number;
|
|
103
79
|
/**
|
|
104
80
|
* @dev Main function to calculate funding fees for a trade within context
|
|
105
81
|
* @param context Funding fee context with params and data
|
|
@@ -31,6 +31,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
31
31
|
};
|
|
32
32
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
33
|
exports.FundingFees = exports.getTradeFundingFeesCollateralSimple = exports.getTradeFundingFees = exports.getTradeFundingFeesCollateral = exports.getPairPendingAccFundingFees = exports.getLongShortAprMultiplier = exports.getAvgFundingRatePerSecondP = exports.getSecondsToReachZeroRate = exports.getCurrentFundingVelocityPerYear = void 0;
|
|
34
|
+
const types_1 = require("../../../contracts/types");
|
|
34
35
|
// Constants from contract
|
|
35
36
|
const FUNDING_APR_MULTIPLIER_CAP = 100; // Smaller side can earn up to 100x more APR
|
|
36
37
|
const ONE_YEAR = 365 * 24 * 60 * 60; // 1 year in seconds
|
|
@@ -225,59 +226,29 @@ const getPairPendingAccFundingFees = (params, data, currentPairPrice, pairOiToke
|
|
|
225
226
|
};
|
|
226
227
|
exports.getPairPendingAccFundingFees = getPairPendingAccFundingFees;
|
|
227
228
|
/**
|
|
228
|
-
* @dev Calculates funding fees for a specific trade
|
|
229
|
-
* @param trade Trade parameters (collateral amount, leverage, open price, long/short
|
|
229
|
+
* @dev Calculates funding fees for a specific trade
|
|
230
|
+
* @param trade Trade parameters (collateral amount, leverage, open price, long/short)
|
|
230
231
|
* @param tradeInfo Trade info (contracts version)
|
|
231
232
|
* @param tradeFeesData Trade fees data containing initial acc funding fee
|
|
232
233
|
* @param currentPairPrice Current pair price
|
|
233
|
-
* @param context
|
|
234
|
+
* @param context Pair-specific funding fee context
|
|
234
235
|
* @returns Funding fee in collateral tokens
|
|
235
236
|
*/
|
|
236
237
|
const getTradeFundingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
|
|
237
|
-
|
|
238
|
-
// Funding fees are only charged on post-v10 trades
|
|
239
|
-
if (tradeInfo.contractsVersion < 10) {
|
|
238
|
+
if (tradeInfo.contractsVersion < types_1.ContractsVersion.V10) {
|
|
240
239
|
return 0;
|
|
241
240
|
}
|
|
242
241
|
const positionSizeCollateral = trade.collateralAmount * trade.leverage;
|
|
243
|
-
if (!context) {
|
|
244
|
-
return 0;
|
|
245
|
-
}
|
|
246
|
-
// Check if we have a pair-specific context
|
|
247
|
-
if ("params" in context && "data" in context) {
|
|
248
|
-
// Pair-specific context
|
|
249
|
-
const { params, data, pairOi, netExposureToken, netExposureUsd } = context;
|
|
250
|
-
if (!params.fundingFeesEnabled) {
|
|
251
|
-
return 0;
|
|
252
|
-
}
|
|
253
|
-
// Calculate pending accumulated fees
|
|
254
|
-
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(params, data, currentPairPrice, pairOi || { oiLongToken: 0, oiShortToken: 0 }, netExposureToken || 0, netExposureUsd || 0, context.currentTimestamp);
|
|
255
|
-
const currentAccFundingFeeP = trade.long
|
|
256
|
-
? accFundingFeeLongP
|
|
257
|
-
: accFundingFeeShortP;
|
|
258
|
-
const fundingFeeDelta = currentAccFundingFeeP - tradeFeesData.initialAccFundingFeeP;
|
|
259
|
-
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice;
|
|
260
|
-
}
|
|
261
|
-
// Full context - original logic
|
|
262
|
-
if ("fundingParams" in context &&
|
|
263
|
-
trade.collateralIndex !== undefined &&
|
|
264
|
-
trade.pairIndex !== undefined) {
|
|
265
|
-
const params = (_a = context.fundingParams[trade.collateralIndex]) === null || _a === void 0 ? void 0 : _a[trade.pairIndex];
|
|
266
|
-
const data = (_b = context.fundingData[trade.collateralIndex]) === null || _b === void 0 ? void 0 : _b[trade.pairIndex];
|
|
267
|
-
const pairOi = (_d = (_c = context.pairOiAfterV10) === null || _c === void 0 ? void 0 : _c[trade.collateralIndex]) === null || _d === void 0 ? void 0 : _d[trade.pairIndex];
|
|
268
|
-
const netExposureToken = ((_f = (_e = context.netExposureToken) === null || _e === void 0 ? void 0 : _e[trade.collateralIndex]) === null || _f === void 0 ? void 0 : _f[trade.pairIndex]) || 0;
|
|
269
|
-
const netExposureUsd = ((_h = (_g = context.netExposureUsd) === null || _g === void 0 ? void 0 : _g[trade.collateralIndex]) === null || _h === void 0 ? void 0 : _h[trade.pairIndex]) || 0;
|
|
270
|
-
if (params && data && pairOi) {
|
|
271
|
-
// Calculate pending accumulated fees
|
|
272
|
-
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(params, data, currentPairPrice, pairOi, netExposureToken, netExposureUsd, context.currentTimestamp);
|
|
273
|
-
const currentAccFundingFeeP = trade.long
|
|
274
|
-
? accFundingFeeLongP
|
|
275
|
-
: accFundingFeeShortP;
|
|
276
|
-
const fundingFeeDelta = currentAccFundingFeeP - tradeFeesData.initialAccFundingFeeP;
|
|
277
|
-
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice;
|
|
278
|
-
}
|
|
242
|
+
if (!context.params.fundingFeesEnabled) {
|
|
243
|
+
return 0;
|
|
279
244
|
}
|
|
280
|
-
|
|
245
|
+
// Calculate pending accumulated fees
|
|
246
|
+
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(context.params, context.data, currentPairPrice, context.pairOi || { oiLongToken: 0, oiShortToken: 0 }, context.netExposureToken || 0, context.netExposureUsd || 0, context.currentTimestamp);
|
|
247
|
+
const currentAccFundingFeeP = trade.long
|
|
248
|
+
? accFundingFeeLongP
|
|
249
|
+
: accFundingFeeShortP;
|
|
250
|
+
const fundingFeeDelta = currentAccFundingFeeP - tradeFeesData.initialAccFundingFeeP;
|
|
251
|
+
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice / 100;
|
|
281
252
|
};
|
|
282
253
|
exports.getTradeFundingFeesCollateral = getTradeFundingFeesCollateral;
|
|
283
254
|
/**
|
|
@@ -331,12 +302,12 @@ exports.getTradeFundingFees = getTradeFundingFees;
|
|
|
331
302
|
*/
|
|
332
303
|
const getTradeFundingFeesCollateralSimple = (trade, tradeInfo, initialAccFundingFeeP, currentAccFundingFeeP) => {
|
|
333
304
|
// Funding fees are only charged on post-v10 trades
|
|
334
|
-
if (tradeInfo.contractsVersion <
|
|
305
|
+
if (tradeInfo.contractsVersion < types_1.ContractsVersion.V10) {
|
|
335
306
|
return 0;
|
|
336
307
|
}
|
|
337
308
|
const positionSizeCollateral = trade.collateralAmount * trade.leverage;
|
|
338
309
|
const fundingFeeDelta = currentAccFundingFeeP - initialAccFundingFeeP;
|
|
339
|
-
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice;
|
|
310
|
+
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice / 100;
|
|
340
311
|
};
|
|
341
312
|
exports.getTradeFundingFeesCollateralSimple = getTradeFundingFeesCollateralSimple;
|
|
342
313
|
// Export namespace for types
|
|
@@ -32,6 +32,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
32
32
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
33
|
exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpactWithChecks = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = void 0;
|
|
34
34
|
const utils_1 = require("../../utils");
|
|
35
|
+
const types_1 = require("../../../contracts/types");
|
|
35
36
|
// Constants
|
|
36
37
|
const PRICE_IMPACT_DIVIDER = 2; // Half price impact to match cumulative volume impact scale
|
|
37
38
|
/**
|
|
@@ -139,7 +140,7 @@ exports.getTradeSkewPriceImpact = getTradeSkewPriceImpact;
|
|
|
139
140
|
*/
|
|
140
141
|
const getTradeSkewPriceImpactWithChecks = (params, context) => {
|
|
141
142
|
// v10+ trades only
|
|
142
|
-
if (params.contractsVersion <
|
|
143
|
+
if (params.contractsVersion < types_1.ContractsVersion.V10) {
|
|
143
144
|
return 0;
|
|
144
145
|
}
|
|
145
146
|
// Counter trades don't pay skew impact
|