@drift-labs/sdk 0.1.36-master.6 → 0.1.36-master.7

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.
@@ -11,6 +11,7 @@ import {
11
11
  } from '../constants/numericConstants';
12
12
  import { Market, PositionDirection, UserPosition } from '../types';
13
13
  import { calculateAmmReservesAfterSwap, getSwapDirection } from './amm';
14
+ import { SETTLEMENT_RATIO_PRECISION, SETTLEMENT_RATIOS } from '../settlement';
14
15
 
15
16
  /**
16
17
  * calculateBaseAssetValue
@@ -90,6 +91,27 @@ export function calculatePositionPNL(
90
91
  return pnl;
91
92
  }
92
93
 
94
+ export function calculateSettledPositionPNL(
95
+ market: Market,
96
+ marketPosition: UserPosition
97
+ ): BN {
98
+ let pnl = calculatePositionPNL(market, marketPosition);
99
+
100
+ if (pnl.gt(ZERO)) {
101
+ try {
102
+ pnl = pnl
103
+ .mul(new BN(SETTLEMENT_RATIOS[marketPosition.marketIndex.toNumber()]))
104
+ .div(SETTLEMENT_RATIO_PRECISION);
105
+ } catch (e) {
106
+ console.log(pnl.toString());
107
+ console.log(marketPosition.marketIndex.toNumber());
108
+ throw e;
109
+ }
110
+ }
111
+
112
+ return pnl;
113
+ }
114
+
93
115
  /**
94
116
  *
95
117
  * @param market
@@ -0,0 +1,9 @@
1
+ import { BN } from '@project-serum/anchor';
2
+
3
+ export const SETTLEMENT_RATIO_PRECISION = new BN(1000000);
4
+
5
+ export const SETTLEMENT_RATIOS = [
6
+ 291786, 243613, 712271, 293771, 555181, 604938, 241133, 218489, 251741,
7
+ 883058, 51614, 531737, 956092, 106424, 727107, 477277, 217325, 127477, 248561,
8
+ 565938, 59349,
9
+ ];
package/src/types.ts CHANGED
@@ -268,6 +268,13 @@ export type OrderStateAccount = {
268
268
  minOrderQuoteAssetAmount: BN;
269
269
  };
270
270
 
271
+ export type SettlementStateAccount = {
272
+ totalSettlementValue: BN;
273
+ collateralAvailableToClaim: BN;
274
+ collateralClaimed: BN;
275
+ enabled: boolean;
276
+ };
277
+
271
278
  export type MarketsAccount = {
272
279
  markets: Market[];
273
280
  };
@@ -336,6 +343,11 @@ export type UserAccount = {
336
343
  totalTokenDiscount: BN;
337
344
  totalReferralReward: BN;
338
345
  totalRefereeDiscount: BN;
346
+ settledPositionValue: BN;
347
+ collateralClaimed: BN;
348
+ lastCollateralAvailableToClaim: BN;
349
+ forgoPositionSettlement: number;
350
+ hasSettledPosition: number;
339
351
  };
340
352
 
341
353
  export type UserOrdersAccount = {