@drift-labs/sdk 2.97.0-beta.34 → 2.97.0-beta.35
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/VERSION +1 -1
- package/lib/browser/math/position.d.ts +16 -0
- package/lib/browser/math/position.js +30 -3
- package/lib/browser/user.js +2 -2
- package/lib/node/math/position.d.ts +16 -0
- package/lib/node/math/position.js +30 -3
- package/lib/node/user.js +2 -2
- package/package.json +1 -1
- package/src/math/position.ts +44 -2
- package/src/user.ts +7 -4
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.97.0-beta.
|
|
1
|
+
2.97.0-beta.35
|
|
@@ -23,11 +23,27 @@ export declare function calculateBaseAssetValue(market: PerpMarketAccount, userP
|
|
|
23
23
|
export declare function calculatePositionPNL(market: PerpMarketAccount, perpPosition: PerpPosition, withFunding: boolean, oraclePriceData: OraclePriceData): BN;
|
|
24
24
|
export declare function calculateClaimablePnl(market: PerpMarketAccount, spotMarket: SpotMarketAccount, perpPosition: PerpPosition, oraclePriceData: OraclePriceData): BN;
|
|
25
25
|
/**
|
|
26
|
+
* Returns total fees and funding pnl for a position
|
|
27
|
+
*
|
|
28
|
+
* @param market
|
|
29
|
+
* @param PerpPosition
|
|
30
|
+
* @param includeUnsettled include unsettled funding in return value (default: true)
|
|
31
|
+
* @returns — // QUOTE_PRECISION
|
|
32
|
+
*/
|
|
33
|
+
export declare function calculateFeesAndFundingPnl(market: PerpMarketAccount, perpPosition: PerpPosition, includeUnsettled?: boolean): BN;
|
|
34
|
+
/**
|
|
35
|
+
* Returns unsettled funding pnl for the position
|
|
36
|
+
*
|
|
37
|
+
* To calculate all fees and funding pnl including settled, use calculateFeesAndFundingPnl
|
|
26
38
|
*
|
|
27
39
|
* @param market
|
|
28
40
|
* @param PerpPosition
|
|
29
41
|
* @returns // QUOTE_PRECISION
|
|
30
42
|
*/
|
|
43
|
+
export declare function calculateUnsettledFundingPnl(market: PerpMarketAccount, perpPosition: PerpPosition): BN;
|
|
44
|
+
/**
|
|
45
|
+
* @deprecated use calculateUnsettledFundingPnl or calculateFeesAndFundingPnl instead
|
|
46
|
+
*/
|
|
31
47
|
export declare function calculatePositionFundingPNL(market: PerpMarketAccount, perpPosition: PerpPosition): BN;
|
|
32
48
|
export declare function positionIsAvailable(position: PerpPosition): boolean;
|
|
33
49
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasOpenOrders = exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateCostBasis = exports.calculateEntryPrice = exports.calculateBreakEvenPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculateClaimablePnl = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
3
|
+
exports.hasOpenOrders = exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateCostBasis = exports.calculateEntryPrice = exports.calculateBreakEvenPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculateUnsettledFundingPnl = exports.calculateFeesAndFundingPnl = exports.calculateClaimablePnl = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const types_1 = require("../types");
|
|
@@ -75,7 +75,7 @@ function calculatePositionPNL(market, perpPosition, withFunding = false, oracleP
|
|
|
75
75
|
.mul(baseAssetValueSign)
|
|
76
76
|
.add(perpPosition.quoteAssetAmount);
|
|
77
77
|
if (withFunding) {
|
|
78
|
-
const fundingRatePnL =
|
|
78
|
+
const fundingRatePnL = calculateUnsettledFundingPnl(market, perpPosition);
|
|
79
79
|
pnl = pnl.add(fundingRatePnL);
|
|
80
80
|
}
|
|
81
81
|
return pnl;
|
|
@@ -93,12 +93,32 @@ function calculateClaimablePnl(market, spotMarket, perpPosition, oraclePriceData
|
|
|
93
93
|
}
|
|
94
94
|
exports.calculateClaimablePnl = calculateClaimablePnl;
|
|
95
95
|
/**
|
|
96
|
+
* Returns total fees and funding pnl for a position
|
|
97
|
+
*
|
|
98
|
+
* @param market
|
|
99
|
+
* @param PerpPosition
|
|
100
|
+
* @param includeUnsettled include unsettled funding in return value (default: true)
|
|
101
|
+
* @returns — // QUOTE_PRECISION
|
|
102
|
+
*/
|
|
103
|
+
function calculateFeesAndFundingPnl(market, perpPosition, includeUnsettled = true) {
|
|
104
|
+
const settledFundingAndFeesPnl = perpPosition.quoteBreakEvenAmount.sub(perpPosition.quoteEntryAmount);
|
|
105
|
+
if (!includeUnsettled) {
|
|
106
|
+
return settledFundingAndFeesPnl;
|
|
107
|
+
}
|
|
108
|
+
const unsettledFundingPnl = calculateUnsettledFundingPnl(market, perpPosition);
|
|
109
|
+
return settledFundingAndFeesPnl.add(unsettledFundingPnl);
|
|
110
|
+
}
|
|
111
|
+
exports.calculateFeesAndFundingPnl = calculateFeesAndFundingPnl;
|
|
112
|
+
/**
|
|
113
|
+
* Returns unsettled funding pnl for the position
|
|
114
|
+
*
|
|
115
|
+
* To calculate all fees and funding pnl including settled, use calculateFeesAndFundingPnl
|
|
96
116
|
*
|
|
97
117
|
* @param market
|
|
98
118
|
* @param PerpPosition
|
|
99
119
|
* @returns // QUOTE_PRECISION
|
|
100
120
|
*/
|
|
101
|
-
function
|
|
121
|
+
function calculateUnsettledFundingPnl(market, perpPosition) {
|
|
102
122
|
if (perpPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
103
123
|
return numericConstants_1.ZERO;
|
|
104
124
|
}
|
|
@@ -117,6 +137,13 @@ function calculatePositionFundingPNL(market, perpPosition) {
|
|
|
117
137
|
.mul(new __1.BN(-1));
|
|
118
138
|
return perPositionFundingRate;
|
|
119
139
|
}
|
|
140
|
+
exports.calculateUnsettledFundingPnl = calculateUnsettledFundingPnl;
|
|
141
|
+
/**
|
|
142
|
+
* @deprecated use calculateUnsettledFundingPnl or calculateFeesAndFundingPnl instead
|
|
143
|
+
*/
|
|
144
|
+
function calculatePositionFundingPNL(market, perpPosition) {
|
|
145
|
+
return calculateUnsettledFundingPnl(market, perpPosition);
|
|
146
|
+
}
|
|
120
147
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
|
121
148
|
function positionIsAvailable(position) {
|
|
122
149
|
return (position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
package/lib/browser/user.js
CHANGED
|
@@ -289,7 +289,7 @@ class User {
|
|
|
289
289
|
}
|
|
290
290
|
const nShares = position.lpShares;
|
|
291
291
|
// incorp unsettled funding on pre settled position
|
|
292
|
-
const quoteFundingPnl = (0,
|
|
292
|
+
const quoteFundingPnl = (0, position_1.calculateUnsettledFundingPnl)(market, position);
|
|
293
293
|
let baseUnit = numericConstants_1.AMM_RESERVE_PRECISION;
|
|
294
294
|
if (market.amm.perLpBase == position.perLpBase) {
|
|
295
295
|
if (position.perLpBase >= 0 &&
|
|
@@ -546,7 +546,7 @@ class User {
|
|
|
546
546
|
.perpPositions.filter((pos) => marketIndex !== undefined ? pos.marketIndex === marketIndex : true)
|
|
547
547
|
.reduce((pnl, perpPosition) => {
|
|
548
548
|
const market = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
549
|
-
return pnl.add((0,
|
|
549
|
+
return pnl.add((0, position_1.calculateUnsettledFundingPnl)(market, perpPosition));
|
|
550
550
|
}, numericConstants_1.ZERO);
|
|
551
551
|
}
|
|
552
552
|
getFuelBonus(now, includeSettled = true, includeUnsettled = true) {
|
|
@@ -23,11 +23,27 @@ export declare function calculateBaseAssetValue(market: PerpMarketAccount, userP
|
|
|
23
23
|
export declare function calculatePositionPNL(market: PerpMarketAccount, perpPosition: PerpPosition, withFunding: boolean, oraclePriceData: OraclePriceData): BN;
|
|
24
24
|
export declare function calculateClaimablePnl(market: PerpMarketAccount, spotMarket: SpotMarketAccount, perpPosition: PerpPosition, oraclePriceData: OraclePriceData): BN;
|
|
25
25
|
/**
|
|
26
|
+
* Returns total fees and funding pnl for a position
|
|
27
|
+
*
|
|
28
|
+
* @param market
|
|
29
|
+
* @param PerpPosition
|
|
30
|
+
* @param includeUnsettled include unsettled funding in return value (default: true)
|
|
31
|
+
* @returns — // QUOTE_PRECISION
|
|
32
|
+
*/
|
|
33
|
+
export declare function calculateFeesAndFundingPnl(market: PerpMarketAccount, perpPosition: PerpPosition, includeUnsettled?: boolean): BN;
|
|
34
|
+
/**
|
|
35
|
+
* Returns unsettled funding pnl for the position
|
|
36
|
+
*
|
|
37
|
+
* To calculate all fees and funding pnl including settled, use calculateFeesAndFundingPnl
|
|
26
38
|
*
|
|
27
39
|
* @param market
|
|
28
40
|
* @param PerpPosition
|
|
29
41
|
* @returns // QUOTE_PRECISION
|
|
30
42
|
*/
|
|
43
|
+
export declare function calculateUnsettledFundingPnl(market: PerpMarketAccount, perpPosition: PerpPosition): BN;
|
|
44
|
+
/**
|
|
45
|
+
* @deprecated use calculateUnsettledFundingPnl or calculateFeesAndFundingPnl instead
|
|
46
|
+
*/
|
|
31
47
|
export declare function calculatePositionFundingPNL(market: PerpMarketAccount, perpPosition: PerpPosition): BN;
|
|
32
48
|
export declare function positionIsAvailable(position: PerpPosition): boolean;
|
|
33
49
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasOpenOrders = exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateCostBasis = exports.calculateEntryPrice = exports.calculateBreakEvenPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculateClaimablePnl = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
3
|
+
exports.hasOpenOrders = exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateCostBasis = exports.calculateEntryPrice = exports.calculateBreakEvenPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculateUnsettledFundingPnl = exports.calculateFeesAndFundingPnl = exports.calculateClaimablePnl = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const types_1 = require("../types");
|
|
@@ -75,7 +75,7 @@ function calculatePositionPNL(market, perpPosition, withFunding = false, oracleP
|
|
|
75
75
|
.mul(baseAssetValueSign)
|
|
76
76
|
.add(perpPosition.quoteAssetAmount);
|
|
77
77
|
if (withFunding) {
|
|
78
|
-
const fundingRatePnL =
|
|
78
|
+
const fundingRatePnL = calculateUnsettledFundingPnl(market, perpPosition);
|
|
79
79
|
pnl = pnl.add(fundingRatePnL);
|
|
80
80
|
}
|
|
81
81
|
return pnl;
|
|
@@ -93,12 +93,32 @@ function calculateClaimablePnl(market, spotMarket, perpPosition, oraclePriceData
|
|
|
93
93
|
}
|
|
94
94
|
exports.calculateClaimablePnl = calculateClaimablePnl;
|
|
95
95
|
/**
|
|
96
|
+
* Returns total fees and funding pnl for a position
|
|
97
|
+
*
|
|
98
|
+
* @param market
|
|
99
|
+
* @param PerpPosition
|
|
100
|
+
* @param includeUnsettled include unsettled funding in return value (default: true)
|
|
101
|
+
* @returns — // QUOTE_PRECISION
|
|
102
|
+
*/
|
|
103
|
+
function calculateFeesAndFundingPnl(market, perpPosition, includeUnsettled = true) {
|
|
104
|
+
const settledFundingAndFeesPnl = perpPosition.quoteBreakEvenAmount.sub(perpPosition.quoteEntryAmount);
|
|
105
|
+
if (!includeUnsettled) {
|
|
106
|
+
return settledFundingAndFeesPnl;
|
|
107
|
+
}
|
|
108
|
+
const unsettledFundingPnl = calculateUnsettledFundingPnl(market, perpPosition);
|
|
109
|
+
return settledFundingAndFeesPnl.add(unsettledFundingPnl);
|
|
110
|
+
}
|
|
111
|
+
exports.calculateFeesAndFundingPnl = calculateFeesAndFundingPnl;
|
|
112
|
+
/**
|
|
113
|
+
* Returns unsettled funding pnl for the position
|
|
114
|
+
*
|
|
115
|
+
* To calculate all fees and funding pnl including settled, use calculateFeesAndFundingPnl
|
|
96
116
|
*
|
|
97
117
|
* @param market
|
|
98
118
|
* @param PerpPosition
|
|
99
119
|
* @returns // QUOTE_PRECISION
|
|
100
120
|
*/
|
|
101
|
-
function
|
|
121
|
+
function calculateUnsettledFundingPnl(market, perpPosition) {
|
|
102
122
|
if (perpPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
103
123
|
return numericConstants_1.ZERO;
|
|
104
124
|
}
|
|
@@ -117,6 +137,13 @@ function calculatePositionFundingPNL(market, perpPosition) {
|
|
|
117
137
|
.mul(new __1.BN(-1));
|
|
118
138
|
return perPositionFundingRate;
|
|
119
139
|
}
|
|
140
|
+
exports.calculateUnsettledFundingPnl = calculateUnsettledFundingPnl;
|
|
141
|
+
/**
|
|
142
|
+
* @deprecated use calculateUnsettledFundingPnl or calculateFeesAndFundingPnl instead
|
|
143
|
+
*/
|
|
144
|
+
function calculatePositionFundingPNL(market, perpPosition) {
|
|
145
|
+
return calculateUnsettledFundingPnl(market, perpPosition);
|
|
146
|
+
}
|
|
120
147
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
|
121
148
|
function positionIsAvailable(position) {
|
|
122
149
|
return (position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
package/lib/node/user.js
CHANGED
|
@@ -289,7 +289,7 @@ class User {
|
|
|
289
289
|
}
|
|
290
290
|
const nShares = position.lpShares;
|
|
291
291
|
// incorp unsettled funding on pre settled position
|
|
292
|
-
const quoteFundingPnl = (0,
|
|
292
|
+
const quoteFundingPnl = (0, position_1.calculateUnsettledFundingPnl)(market, position);
|
|
293
293
|
let baseUnit = numericConstants_1.AMM_RESERVE_PRECISION;
|
|
294
294
|
if (market.amm.perLpBase == position.perLpBase) {
|
|
295
295
|
if (position.perLpBase >= 0 &&
|
|
@@ -546,7 +546,7 @@ class User {
|
|
|
546
546
|
.perpPositions.filter((pos) => marketIndex !== undefined ? pos.marketIndex === marketIndex : true)
|
|
547
547
|
.reduce((pnl, perpPosition) => {
|
|
548
548
|
const market = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
|
|
549
|
-
return pnl.add((0,
|
|
549
|
+
return pnl.add((0, position_1.calculateUnsettledFundingPnl)(market, perpPosition));
|
|
550
550
|
}, numericConstants_1.ZERO);
|
|
551
551
|
}
|
|
552
552
|
getFuelBonus(now, includeSettled = true, includeUnsettled = true) {
|
package/package.json
CHANGED
package/src/math/position.ts
CHANGED
|
@@ -118,7 +118,7 @@ export function calculatePositionPNL(
|
|
|
118
118
|
.add(perpPosition.quoteAssetAmount);
|
|
119
119
|
|
|
120
120
|
if (withFunding) {
|
|
121
|
-
const fundingRatePnL =
|
|
121
|
+
const fundingRatePnL = calculateUnsettledFundingPnl(market, perpPosition);
|
|
122
122
|
|
|
123
123
|
pnl = pnl.add(fundingRatePnL);
|
|
124
124
|
}
|
|
@@ -159,12 +159,44 @@ export function calculateClaimablePnl(
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
/**
|
|
162
|
+
* Returns total fees and funding pnl for a position
|
|
163
|
+
*
|
|
164
|
+
* @param market
|
|
165
|
+
* @param PerpPosition
|
|
166
|
+
* @param includeUnsettled include unsettled funding in return value (default: true)
|
|
167
|
+
* @returns — // QUOTE_PRECISION
|
|
168
|
+
*/
|
|
169
|
+
export function calculateFeesAndFundingPnl(
|
|
170
|
+
market: PerpMarketAccount,
|
|
171
|
+
perpPosition: PerpPosition,
|
|
172
|
+
includeUnsettled = true
|
|
173
|
+
): BN {
|
|
174
|
+
const settledFundingAndFeesPnl = perpPosition.quoteBreakEvenAmount.sub(
|
|
175
|
+
perpPosition.quoteEntryAmount
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
if (!includeUnsettled) {
|
|
179
|
+
return settledFundingAndFeesPnl;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const unsettledFundingPnl = calculateUnsettledFundingPnl(
|
|
183
|
+
market,
|
|
184
|
+
perpPosition
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
return settledFundingAndFeesPnl.add(unsettledFundingPnl);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Returns unsettled funding pnl for the position
|
|
192
|
+
*
|
|
193
|
+
* To calculate all fees and funding pnl including settled, use calculateFeesAndFundingPnl
|
|
162
194
|
*
|
|
163
195
|
* @param market
|
|
164
196
|
* @param PerpPosition
|
|
165
197
|
* @returns // QUOTE_PRECISION
|
|
166
198
|
*/
|
|
167
|
-
export function
|
|
199
|
+
export function calculateUnsettledFundingPnl(
|
|
168
200
|
market: PerpMarketAccount,
|
|
169
201
|
perpPosition: PerpPosition
|
|
170
202
|
): BN {
|
|
@@ -189,6 +221,16 @@ export function calculatePositionFundingPNL(
|
|
|
189
221
|
return perPositionFundingRate;
|
|
190
222
|
}
|
|
191
223
|
|
|
224
|
+
/**
|
|
225
|
+
* @deprecated use calculateUnsettledFundingPnl or calculateFeesAndFundingPnl instead
|
|
226
|
+
*/
|
|
227
|
+
export function calculatePositionFundingPNL(
|
|
228
|
+
market: PerpMarketAccount,
|
|
229
|
+
perpPosition: PerpPosition
|
|
230
|
+
): BN {
|
|
231
|
+
return calculateUnsettledFundingPnl(market, perpPosition);
|
|
232
|
+
}
|
|
233
|
+
|
|
192
234
|
export function positionIsAvailable(position: PerpPosition): boolean {
|
|
193
235
|
return (
|
|
194
236
|
position.baseAssetAmount.eq(ZERO) &&
|
package/src/user.ts
CHANGED
|
@@ -15,7 +15,11 @@ import {
|
|
|
15
15
|
UserStatus,
|
|
16
16
|
UserStatsAccount,
|
|
17
17
|
} from './types';
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
calculateEntryPrice,
|
|
20
|
+
calculateUnsettledFundingPnl,
|
|
21
|
+
positionIsAvailable,
|
|
22
|
+
} from './math/position';
|
|
19
23
|
import {
|
|
20
24
|
AMM_RESERVE_PRECISION,
|
|
21
25
|
AMM_RESERVE_PRECISION_EXP,
|
|
@@ -50,7 +54,6 @@ import {
|
|
|
50
54
|
calculateBaseAssetValue,
|
|
51
55
|
calculateMarketMarginRatio,
|
|
52
56
|
calculatePerpLiabilityValue,
|
|
53
|
-
calculatePositionFundingPNL,
|
|
54
57
|
calculatePositionPNL,
|
|
55
58
|
calculateReservePrice,
|
|
56
59
|
calculateSpotMarketMarginRatio,
|
|
@@ -495,7 +498,7 @@ export class User {
|
|
|
495
498
|
const nShares = position.lpShares;
|
|
496
499
|
|
|
497
500
|
// incorp unsettled funding on pre settled position
|
|
498
|
-
const quoteFundingPnl =
|
|
501
|
+
const quoteFundingPnl = calculateUnsettledFundingPnl(market, position);
|
|
499
502
|
|
|
500
503
|
let baseUnit = AMM_RESERVE_PRECISION;
|
|
501
504
|
if (market.amm.perLpBase == position.perLpBase) {
|
|
@@ -901,7 +904,7 @@ export class User {
|
|
|
901
904
|
const market = this.driftClient.getPerpMarketAccount(
|
|
902
905
|
perpPosition.marketIndex
|
|
903
906
|
);
|
|
904
|
-
return pnl.add(
|
|
907
|
+
return pnl.add(calculateUnsettledFundingPnl(market, perpPosition));
|
|
905
908
|
}, ZERO);
|
|
906
909
|
}
|
|
907
910
|
|