@gainsnetwork/sdk 1.0.1-rc1 → 1.0.2-rc1
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/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +5 -8
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +18 -40
- package/lib/trade/effectiveLeverage/index.d.ts +0 -1
- package/lib/trade/effectiveLeverage/index.js +0 -1
- package/lib/trade/effectiveLeverage/types.d.ts +4 -7
- package/lib/trade/fees/fundingFees/index.js +0 -32
- package/lib/trade/pnl/index.js +1 -2
- package/package.json +1 -1
|
@@ -2,22 +2,19 @@
|
|
|
2
2
|
* @dev Trade effective leverage calculations
|
|
3
3
|
* @dev Mirrors contract's TradingCommonUtils.getTradeNewEffectiveLeverage
|
|
4
4
|
*/
|
|
5
|
-
import { TradeEffectiveLeverageInput,
|
|
6
|
-
export type { TradeEffectiveLeverageInput,
|
|
7
|
-
export { buildTradeEffectiveLeverageContext } from "./builder";
|
|
5
|
+
import { TradeEffectiveLeverageInput, TradeEffectiveLeverageResult } from "./types";
|
|
6
|
+
export type { TradeEffectiveLeverageInput, TradeEffectiveLeverageResult };
|
|
8
7
|
/**
|
|
9
8
|
* @dev Calculates the effective leverage of a trade accounting for unrealized PnL
|
|
10
9
|
* @dev Effective leverage increases when PnL is negative and decreases when positive
|
|
11
10
|
* @dev Mirrors contract's getTradeNewEffectiveLeverage function
|
|
12
11
|
* @param input Trade parameters including new position values
|
|
13
|
-
* @param context Combined context for calculations
|
|
14
12
|
* @returns Effective leverage and related values
|
|
15
13
|
*/
|
|
16
|
-
export declare const getTradeNewEffectiveLeverage: (input: TradeEffectiveLeverageInput
|
|
14
|
+
export declare const getTradeNewEffectiveLeverage: (input: TradeEffectiveLeverageInput) => TradeEffectiveLeverageResult;
|
|
17
15
|
/**
|
|
18
|
-
* @dev Simplified version for existing positions
|
|
16
|
+
* @dev Simplified version for existing positions
|
|
19
17
|
* @param input Trade parameters
|
|
20
|
-
* @param context Combined context
|
|
21
18
|
* @returns Effective leverage and related values
|
|
22
19
|
*/
|
|
23
|
-
export declare const getTradeEffectiveLeverage: (input:
|
|
20
|
+
export declare const getTradeEffectiveLeverage: (input: TradeEffectiveLeverageInput) => TradeEffectiveLeverageResult;
|
|
@@ -1,64 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTradeEffectiveLeverage = exports.getTradeNewEffectiveLeverage =
|
|
4
|
-
const __1 = require("..");
|
|
5
|
-
var builder_1 = require("./builder");
|
|
6
|
-
Object.defineProperty(exports, "buildTradeEffectiveLeverageContext", { enumerable: true, get: function () { return builder_1.buildTradeEffectiveLeverageContext; } });
|
|
3
|
+
exports.getTradeEffectiveLeverage = exports.getTradeNewEffectiveLeverage = void 0;
|
|
7
4
|
/**
|
|
8
5
|
* @dev Calculates the effective leverage of a trade accounting for unrealized PnL
|
|
9
6
|
* @dev Effective leverage increases when PnL is negative and decreases when positive
|
|
10
7
|
* @dev Mirrors contract's getTradeNewEffectiveLeverage function
|
|
11
8
|
* @param input Trade parameters including new position values
|
|
12
|
-
* @param context Combined context for calculations
|
|
13
9
|
* @returns Effective leverage and related values
|
|
14
10
|
*/
|
|
15
|
-
const getTradeNewEffectiveLeverage = (input
|
|
16
|
-
const {
|
|
17
|
-
const { closingPriceImpactContext } = context;
|
|
11
|
+
const getTradeNewEffectiveLeverage = (input) => {
|
|
12
|
+
const { newOpenPrice, newCollateralAmount, newLeverage, currentPairPrice, tradeValueCollateral, } = input;
|
|
18
13
|
// Calculate new position size
|
|
19
14
|
const newPositionSize = newCollateralAmount * newLeverage;
|
|
20
|
-
// Calculate
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
contractsVersion: closingPriceImpactContext.tradeInfo.contractsVersion,
|
|
31
|
-
}, closingPriceImpactContext);
|
|
32
|
-
// Calculate unrealized PnL
|
|
33
|
-
// For longs: (exitPrice - entryPrice) * positionSizeToken
|
|
34
|
-
// For shorts: (entryPrice - exitPrice) * positionSizeToken
|
|
35
|
-
const priceDiff = trade.long
|
|
36
|
-
? closingPriceImpact.priceAfterImpact - newOpenPrice
|
|
37
|
-
: newOpenPrice - closingPriceImpact.priceAfterImpact;
|
|
38
|
-
const unrealizedPnl = priceDiff * closingPriceImpact.positionSizeToken;
|
|
39
|
-
// Calculate effective collateral (collateral + PnL - fees)
|
|
40
|
-
// Note: fees are subtracted because they reduce the effective collateral
|
|
41
|
-
const effectiveCollateral = newCollateralAmount + unrealizedPnl - openingFeesCollateral;
|
|
42
|
-
// Calculate effective leverage
|
|
43
|
-
// If effective collateral is <= 0, leverage is effectively infinite
|
|
44
|
-
const effectiveLeverage = effectiveCollateral > 0
|
|
45
|
-
? newPositionSize / effectiveCollateral
|
|
15
|
+
// Calculate dynamic position size (matching on-chain logic)
|
|
16
|
+
// This adjusts position size based on current price vs open price
|
|
17
|
+
const newPosSizeCollateralDynamic = (newPositionSize * currentPairPrice) / newOpenPrice;
|
|
18
|
+
// Use the provided trade value as margin value
|
|
19
|
+
// This already includes collateral + PnL with price impact - fees
|
|
20
|
+
const newMarginValueCollateral = tradeValueCollateral;
|
|
21
|
+
// Calculate effective leverage (matching on-chain)
|
|
22
|
+
// If margin value is <= 0, leverage is effectively infinite
|
|
23
|
+
const effectiveLeverage = newMarginValueCollateral > 0
|
|
24
|
+
? newPosSizeCollateralDynamic / newMarginValueCollateral
|
|
46
25
|
: Number.MAX_SAFE_INTEGER;
|
|
47
26
|
return {
|
|
48
27
|
effectiveLeverage,
|
|
49
|
-
unrealizedPnl,
|
|
50
|
-
effectiveCollateral,
|
|
28
|
+
unrealizedPnl: tradeValueCollateral - newCollateralAmount,
|
|
29
|
+
effectiveCollateral: newMarginValueCollateral,
|
|
51
30
|
positionSize: newPositionSize,
|
|
52
31
|
};
|
|
53
32
|
};
|
|
54
33
|
exports.getTradeNewEffectiveLeverage = getTradeNewEffectiveLeverage;
|
|
55
34
|
/**
|
|
56
|
-
* @dev Simplified version for existing positions
|
|
35
|
+
* @dev Simplified version for existing positions
|
|
57
36
|
* @param input Trade parameters
|
|
58
|
-
* @param context Combined context
|
|
59
37
|
* @returns Effective leverage and related values
|
|
60
38
|
*/
|
|
61
|
-
const getTradeEffectiveLeverage = (input
|
|
62
|
-
return (0, exports.getTradeNewEffectiveLeverage)(
|
|
39
|
+
const getTradeEffectiveLeverage = (input) => {
|
|
40
|
+
return (0, exports.getTradeNewEffectiveLeverage)(input);
|
|
63
41
|
};
|
|
64
42
|
exports.getTradeEffectiveLeverage = getTradeEffectiveLeverage;
|
|
@@ -16,7 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.getTradeEffectiveLeverage = exports.getTradeNewEffectiveLeverage = void 0;
|
|
18
18
|
__exportStar(require("./types"), exports);
|
|
19
|
-
__exportStar(require("./builder"), exports);
|
|
20
19
|
var getTradeNewEffectiveLeverage_1 = require("./getTradeNewEffectiveLeverage");
|
|
21
20
|
Object.defineProperty(exports, "getTradeNewEffectiveLeverage", { enumerable: true, get: function () { return getTradeNewEffectiveLeverage_1.getTradeNewEffectiveLeverage; } });
|
|
22
21
|
Object.defineProperty(exports, "getTradeEffectiveLeverage", { enumerable: true, get: function () { return getTradeNewEffectiveLeverage_1.getTradeEffectiveLeverage; } });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @dev Types for trade effective leverage calculations
|
|
3
3
|
*/
|
|
4
|
-
import { Trade
|
|
4
|
+
import { Trade } from "..";
|
|
5
5
|
/**
|
|
6
6
|
* @dev Input parameters for effective leverage calculation
|
|
7
7
|
* @dev Mirrors contract's parameters for getTradeNewEffectiveLeverage
|
|
@@ -12,16 +12,13 @@ export type TradeEffectiveLeverageInput = {
|
|
|
12
12
|
newCollateralAmount: number;
|
|
13
13
|
newLeverage: number;
|
|
14
14
|
currentPairPrice: number;
|
|
15
|
-
|
|
15
|
+
tradeValueCollateral: number;
|
|
16
16
|
};
|
|
17
17
|
/**
|
|
18
18
|
* @dev Context for effective leverage calculation
|
|
19
|
-
*
|
|
19
|
+
* Simplified context since price impact is now included in tradeValueCollateral
|
|
20
20
|
*/
|
|
21
|
-
export type TradeEffectiveLeverageContext = {
|
|
22
|
-
closingPriceImpactContext: TradeClosingPriceImpactContext;
|
|
23
|
-
baseSpreadP: number;
|
|
24
|
-
};
|
|
21
|
+
export type TradeEffectiveLeverageContext = {};
|
|
25
22
|
/**
|
|
26
23
|
* @dev Result of effective leverage calculation
|
|
27
24
|
*/
|
|
@@ -49,25 +49,17 @@ const getCurrentFundingVelocityPerYear = (netExposureToken, netExposureUsd, skew
|
|
|
49
49
|
if (netExposureToken === 0 ||
|
|
50
50
|
skewCoefficientPerYear === 0 ||
|
|
51
51
|
absoluteVelocityPerYearCap === 0) {
|
|
52
|
-
console.log("netExposureToken", netExposureToken);
|
|
53
|
-
console.log("skewCoefficientPerYear", skewCoefficientPerYear);
|
|
54
|
-
console.log("absoluteVelocityPerYearCap", absoluteVelocityPerYearCap);
|
|
55
52
|
return 0;
|
|
56
53
|
}
|
|
57
54
|
// Check theta threshold
|
|
58
55
|
const absNetExposureUsd = Math.abs(netExposureUsd);
|
|
59
56
|
if (absNetExposureUsd < thetaThresholdUsd) {
|
|
60
|
-
console.log("absNetExposureUsd", absNetExposureUsd);
|
|
61
|
-
console.log("thetaThresholdUsd", thetaThresholdUsd);
|
|
62
57
|
return 0;
|
|
63
58
|
}
|
|
64
59
|
// Calculate absolute velocity
|
|
65
60
|
const absoluteVelocityPerYear = Math.abs(netExposureToken) * skewCoefficientPerYear;
|
|
66
61
|
// Apply cap
|
|
67
62
|
const cappedAbsoluteVelocity = Math.min(absoluteVelocityPerYear, absoluteVelocityPerYearCap);
|
|
68
|
-
console.log("absoluteVelocityPerYear", absoluteVelocityPerYear);
|
|
69
|
-
console.log("absoluteVelocityPerYearCap", absoluteVelocityPerYearCap);
|
|
70
|
-
console.log("cappedAbsoluteVelocity", cappedAbsoluteVelocity);
|
|
71
63
|
// Return with proper sign
|
|
72
64
|
return netExposureToken < 0
|
|
73
65
|
? -cappedAbsoluteVelocity
|
|
@@ -112,8 +104,6 @@ const getAvgFundingRatePerSecondP = (lastFundingRatePerSecondP, absoluteRatePerS
|
|
|
112
104
|
};
|
|
113
105
|
}
|
|
114
106
|
const ratePerSecondCap = absoluteRatePerSecondCap * (currentVelocityPerYear < 0 ? -1 : 1);
|
|
115
|
-
console.log("ratePerSecondCap", ratePerSecondCap);
|
|
116
|
-
console.log("lastFundingRatePerSecondP", lastFundingRatePerSecondP);
|
|
117
107
|
// If rate is already at cap, just return it
|
|
118
108
|
if (ratePerSecondCap === lastFundingRatePerSecondP) {
|
|
119
109
|
return {
|
|
@@ -123,7 +113,6 @@ const getAvgFundingRatePerSecondP = (lastFundingRatePerSecondP, absoluteRatePerS
|
|
|
123
113
|
}
|
|
124
114
|
const secondsToReachCap = ((ratePerSecondCap - lastFundingRatePerSecondP) * ONE_YEAR) /
|
|
125
115
|
currentVelocityPerYear;
|
|
126
|
-
console.log("secondsToReachCap", secondsToReachCap);
|
|
127
116
|
if (secondsSinceLastUpdate > secondsToReachCap) {
|
|
128
117
|
// Rate reached cap during this period
|
|
129
118
|
const currentFundingRatePerSecondP = ratePerSecondCap;
|
|
@@ -132,8 +121,6 @@ const getAvgFundingRatePerSecondP = (lastFundingRatePerSecondP, absoluteRatePerS
|
|
|
132
121
|
const avgFundingRatePerSecondP = (avgFundingRatePerSecondP_1 * secondsToReachCap +
|
|
133
122
|
ratePerSecondCap * (secondsSinceLastUpdate - secondsToReachCap)) /
|
|
134
123
|
secondsSinceLastUpdate;
|
|
135
|
-
console.log("Reached cap");
|
|
136
|
-
console.log("avgFundingRatePerSecondP", avgFundingRatePerSecondP);
|
|
137
124
|
return { avgFundingRatePerSecondP, currentFundingRatePerSecondP };
|
|
138
125
|
}
|
|
139
126
|
else {
|
|
@@ -141,9 +128,6 @@ const getAvgFundingRatePerSecondP = (lastFundingRatePerSecondP, absoluteRatePerS
|
|
|
141
128
|
const currentFundingRatePerSecondP = lastFundingRatePerSecondP +
|
|
142
129
|
(secondsSinceLastUpdate * currentVelocityPerYear) / ONE_YEAR;
|
|
143
130
|
const avgFundingRatePerSecondP = (lastFundingRatePerSecondP + currentFundingRatePerSecondP) / 2;
|
|
144
|
-
console.log("Not reached cap");
|
|
145
|
-
console.log("avgFundingRatePerSecondP", avgFundingRatePerSecondP);
|
|
146
|
-
console.log("currentFundingRatePerSecondP", currentFundingRatePerSecondP);
|
|
147
131
|
return { avgFundingRatePerSecondP, currentFundingRatePerSecondP };
|
|
148
132
|
}
|
|
149
133
|
};
|
|
@@ -201,14 +185,10 @@ const getPairPendingAccFundingFees = (params, data, currentPairPrice, pairOiToke
|
|
|
201
185
|
};
|
|
202
186
|
}
|
|
203
187
|
const secondsSinceLastUpdate = currentTimestamp - data.lastFundingUpdateTs;
|
|
204
|
-
console.log("secondsSinceLastUpdate", secondsSinceLastUpdate);
|
|
205
188
|
// Calculate current velocity
|
|
206
189
|
const currentVelocityPerYear = (0, exports.getCurrentFundingVelocityPerYear)(netExposureToken, netExposureUsd, params.skewCoefficientPerYear, params.absoluteVelocityPerYearCap, params.thetaThresholdUsd);
|
|
207
190
|
// Get average and current funding rates
|
|
208
191
|
const { avgFundingRatePerSecondP, currentFundingRatePerSecondP } = (0, exports.getAvgFundingRatePerSecondP)(data.lastFundingRatePerSecondP, params.absoluteRatePerSecondCap, currentVelocityPerYear, secondsSinceLastUpdate);
|
|
209
|
-
console.log("currentVelocityPerYear", currentVelocityPerYear);
|
|
210
|
-
console.log("avgFundingRatePerSecondP", avgFundingRatePerSecondP);
|
|
211
|
-
console.log("currentFundingRatePerSecondP", currentFundingRatePerSecondP);
|
|
212
192
|
// Check if we need to handle rate sign change
|
|
213
193
|
const rateChangedSign = params.aprMultiplierEnabled &&
|
|
214
194
|
((currentFundingRatePerSecondP > 0 && data.lastFundingRatePerSecondP < 0) ||
|
|
@@ -230,9 +210,6 @@ const getPairPendingAccFundingFees = (params, data, currentPairPrice, pairOiToke
|
|
|
230
210
|
const { longAprMultiplier: longMultiplier2, shortAprMultiplier: shortMultiplier2, } = (0, exports.getLongShortAprMultiplier)(avgFundingRatePerSecondP_2, pairOiToken.oiLongToken, pairOiToken.oiShortToken, true);
|
|
231
211
|
accFundingFeeLongP += fundingFeesDeltaP_2 * longMultiplier2;
|
|
232
212
|
accFundingFeeShortP -= fundingFeesDeltaP_2 * shortMultiplier2;
|
|
233
|
-
console.log("Rate changed sign");
|
|
234
|
-
console.log("accFundingFeeLongP", accFundingFeeLongP);
|
|
235
|
-
console.log("accFundingFeeShortP", accFundingFeeShortP);
|
|
236
213
|
}
|
|
237
214
|
else {
|
|
238
215
|
// Single period calculation
|
|
@@ -240,9 +217,6 @@ const getPairPendingAccFundingFees = (params, data, currentPairPrice, pairOiToke
|
|
|
240
217
|
const { longAprMultiplier, shortAprMultiplier } = (0, exports.getLongShortAprMultiplier)(avgFundingRatePerSecondP, pairOiToken.oiLongToken, pairOiToken.oiShortToken, params.aprMultiplierEnabled);
|
|
241
218
|
accFundingFeeLongP += fundingFeesDeltaP * longAprMultiplier;
|
|
242
219
|
accFundingFeeShortP -= fundingFeesDeltaP * shortAprMultiplier;
|
|
243
|
-
console.log("Single period calculation");
|
|
244
|
-
console.log("accFundingFeeLongP", accFundingFeeLongP);
|
|
245
|
-
console.log("accFundingFeeShortP", accFundingFeeShortP);
|
|
246
220
|
}
|
|
247
221
|
return {
|
|
248
222
|
accFundingFeeLongP,
|
|
@@ -275,12 +249,6 @@ const getTradeFundingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentP
|
|
|
275
249
|
? accFundingFeeLongP
|
|
276
250
|
: accFundingFeeShortP;
|
|
277
251
|
const fundingFeeDelta = currentAccFundingFeeP - tradeFeesData.initialAccFundingFeeP;
|
|
278
|
-
console.log("currentAccFundingFeeP", currentAccFundingFeeP);
|
|
279
|
-
console.log("tradeFeesData.initialAccFundingFeeP", tradeFeesData.initialAccFundingFeeP);
|
|
280
|
-
console.log("fundingFeeDelta", fundingFeeDelta);
|
|
281
|
-
console.log("positionSizeCollateral", positionSizeCollateral);
|
|
282
|
-
console.log("trade.openPrice", trade.openPrice);
|
|
283
|
-
console.log("Final:", (positionSizeCollateral * fundingFeeDelta) / trade.openPrice / 100);
|
|
284
252
|
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice / 100;
|
|
285
253
|
};
|
|
286
254
|
exports.getTradeFundingFeesCollateral = getTradeFundingFeesCollateral;
|
package/lib/trade/pnl/index.js
CHANGED
|
@@ -141,8 +141,7 @@ const getComprehensivePnl = (trade, marketPrice, executionPrice, tradeInfo, cont
|
|
|
141
141
|
// This is what the trader would get if closing the position
|
|
142
142
|
const realizedPnlCollateral = impactPnlCollateral - totalFees + totalRealizedPnlCollateral;
|
|
143
143
|
const realizedPnlPercent = (realizedPnlCollateral / trade.collateralAmount) * 100;
|
|
144
|
-
|
|
145
|
-
const tradeValue = (0, exports.getTradeValue)(trade.collateralAmount, impactPnlPercent, totalFees);
|
|
144
|
+
const tradeValue = trade.collateralAmount + realizedPnlCollateral;
|
|
146
145
|
return {
|
|
147
146
|
// Raw PnL values (using market price, no price impact)
|
|
148
147
|
pnlPercent: rawPnlPercent,
|