@gainsnetwork/sdk 1.0.1-rc2 → 1.0.2-rc2
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/markets/oi/converter.js +9 -0
- package/lib/markets/oi/types.d.ts +16 -0
- 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/pnl/index.js +1 -2
- package/package.json +1 -1
|
@@ -96,6 +96,15 @@ const computeOiValues = (pairOi, tokenPriceCollateral) => {
|
|
|
96
96
|
long: totalDynamicLong,
|
|
97
97
|
short: totalDynamicShort,
|
|
98
98
|
},
|
|
99
|
+
// v10-only values for funding fee markets
|
|
100
|
+
v10StaticCollateral: {
|
|
101
|
+
long: pairOi.collateral.long,
|
|
102
|
+
short: pairOi.collateral.short,
|
|
103
|
+
},
|
|
104
|
+
v10DynamicCollateral: {
|
|
105
|
+
long: tokenLongCollateral,
|
|
106
|
+
short: tokenShortCollateral,
|
|
107
|
+
},
|
|
99
108
|
skewToken,
|
|
100
109
|
};
|
|
101
110
|
};
|
|
@@ -65,6 +65,22 @@ export interface ComputedOi {
|
|
|
65
65
|
long: number;
|
|
66
66
|
short: number;
|
|
67
67
|
};
|
|
68
|
+
/**
|
|
69
|
+
* @dev v10-only OI using static collateral values
|
|
70
|
+
* @dev Used for funding fee markets display
|
|
71
|
+
*/
|
|
72
|
+
v10StaticCollateral: {
|
|
73
|
+
long: number;
|
|
74
|
+
short: number;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* @dev v10-only OI using dynamic values: token * price
|
|
78
|
+
* @dev Used for funding fee markets real-time display
|
|
79
|
+
*/
|
|
80
|
+
v10DynamicCollateral: {
|
|
81
|
+
long: number;
|
|
82
|
+
short: number;
|
|
83
|
+
};
|
|
68
84
|
/**
|
|
69
85
|
* @dev Net skew in tokens (v10+ only)
|
|
70
86
|
* @dev Positive = more longs, negative = more shorts
|
|
@@ -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
|
*/
|
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,
|