@drift-labs/sdk 0.2.0-master.36 → 0.2.0-master.38

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.
@@ -71,6 +71,7 @@ class ClearingHouseUser {
71
71
  marketIndex,
72
72
  quoteAssetAmount: numericConstants_1.ZERO,
73
73
  quoteEntryAmount: numericConstants_1.ZERO,
74
+ quoteBreakEvenAmount: numericConstants_1.ZERO,
74
75
  openOrders: 0,
75
76
  openBids: numericConstants_1.ZERO,
76
77
  openAsks: numericConstants_1.ZERO,
@@ -124,7 +125,7 @@ class ClearingHouseUser {
124
125
  */
125
126
  getLPBidAsks(marketIndex) {
126
127
  const position = this.getUserPosition(marketIndex);
127
- if (position.lpShares.eq(numericConstants_1.ZERO)) {
128
+ if (position === undefined || position.lpShares.eq(numericConstants_1.ZERO)) {
128
129
  return [numericConstants_1.ZERO, numericConstants_1.ZERO];
129
130
  }
130
131
  const market = this.clearingHouse.getPerpMarketAccount(marketIndex);
@@ -443,30 +444,9 @@ class ClearingHouseUser {
443
444
  perpPosition.baseAssetAmount =
444
445
  settledPosition.baseAssetAmount.add(dustBaa);
445
446
  perpPosition.quoteAssetAmount = settledPosition.quoteAssetAmount;
446
- // open orders
447
- let openAsks;
448
- if (market.amm.maxBaseAssetReserve > market.amm.baseAssetReserve) {
449
- openAsks = market.amm.maxBaseAssetReserve
450
- .sub(market.amm.baseAssetReserve)
451
- .mul(perpPosition.lpShares)
452
- .div(market.amm.sqrtK)
453
- .mul(new _1.BN(-1));
454
- }
455
- else {
456
- openAsks = numericConstants_1.ZERO;
457
- }
458
- let openBids;
459
- if (market.amm.minBaseAssetReserve < market.amm.baseAssetReserve) {
460
- openBids = market.amm.baseAssetReserve
461
- .sub(market.amm.minBaseAssetReserve)
462
- .mul(perpPosition.lpShares)
463
- .div(market.amm.sqrtK);
464
- }
465
- else {
466
- openBids = numericConstants_1.ZERO;
467
- }
468
- perpPosition.openAsks = perpPosition.openAsks.add(openAsks);
469
- perpPosition.openBids = perpPosition.openBids.add(openBids);
447
+ const [totalOpenBids, totalOpenAsks] = this.getPerpBidAsks(market.marketIndex);
448
+ perpPosition.openAsks = totalOpenAsks;
449
+ perpPosition.openBids = totalOpenBids;
470
450
  }
471
451
  let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
472
452
  if ((0, types_1.isVariant)(market.status, 'settlement')) {
@@ -673,6 +653,7 @@ class ClearingHouseUser {
673
653
  remainderBaseAssetAmount: 0,
674
654
  quoteAssetAmount: new _1.BN(0),
675
655
  lastCumulativeFundingRate: numericConstants_1.ZERO,
656
+ quoteBreakEvenAmount: new _1.BN(0),
676
657
  quoteEntryAmount: new _1.BN(0),
677
658
  openOrders: 0,
678
659
  openBids: new _1.BN(0),
@@ -903,7 +884,7 @@ class ClearingHouseUser {
903
884
  const precisionIncrease = numericConstants_1.TEN.pow(new _1.BN(spotMarket.decimals - 6));
904
885
  const amountWithdrawable = freeCollateral
905
886
  .mul(numericConstants_1.MARGIN_PRECISION)
906
- .div(spotMarket.initialAssetWeight)
887
+ .div(new _1.BN(spotMarket.initialAssetWeight))
907
888
  .mul(numericConstants_1.PRICE_PRECISION)
908
889
  .div(oracleData.price)
909
890
  .mul(precisionIncrease);
@@ -923,7 +904,7 @@ class ClearingHouseUser {
923
904
  : freeCollateral;
924
905
  const maxLiabilityAllowed = freeCollatAfterWithdraw
925
906
  .mul(numericConstants_1.MARGIN_PRECISION)
926
- .div(spotMarket.initialLiabilityWeight)
907
+ .div(new _1.BN(spotMarket.initialLiabilityWeight))
927
908
  .mul(numericConstants_1.PRICE_PRECISION)
928
909
  .div(oracleData.price)
929
910
  .mul(precisionIncrease);
package/lib/config.js CHANGED
@@ -7,7 +7,7 @@ exports.configs = {
7
7
  devnet: {
8
8
  ENV: 'devnet',
9
9
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
10
- CLEARING_HOUSE_PROGRAM_ID: 'BJG3o4CURrokB7huNyiHptYepAtHXj4YfefJiPgzUULV',
10
+ CLEARING_HOUSE_PROGRAM_ID: 'jAEeKs9twxAJmXZHqS2p459xW7FMDjoyvuqthRo9qGS',
11
11
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
12
12
  SERUM_V3: 'DESVgJVGajEgKGXhb6XmqDHGz3VjdgP7rEVESBgxmroY',
13
13
  V2_ALPHA_TICKET_MINT_ADDRESS: 'DeEiGWfCMP9psnLGkxGrBBMEAW5Jv8bBGMN8DCtFRCyB',
@@ -5,6 +5,7 @@ export declare class BigNum {
5
5
  precision: BN;
6
6
  static delim: string;
7
7
  static spacer: string;
8
+ static setLocale(locale: string): void;
8
9
  constructor(val: BN | number | string, precisionVal?: BN | number | string);
9
10
  private bigNumFromParam;
10
11
  add(bn: BigNum): BigNum;
@@ -10,6 +10,10 @@ class BigNum {
10
10
  this.val = new anchor_1.BN(val);
11
11
  this.precision = new anchor_1.BN(precisionVal);
12
12
  }
13
+ static setLocale(locale) {
14
+ BigNum.delim = (1.1).toLocaleString(locale).slice(1, 2) || '.';
15
+ BigNum.spacer = (1000).toLocaleString(locale).slice(1, 2) || ',';
16
+ }
13
17
  bigNumFromParam(bn) {
14
18
  return anchor_1.BN.isBN(bn) ? BigNum.from(bn) : bn;
15
19
  }
@@ -381,7 +385,21 @@ class BigNum {
381
385
  * @returns
382
386
  */
383
387
  toNum() {
384
- return parseFloat(this.print());
388
+ let printedValue = this.print();
389
+ // Must convert any non-US delimiters and spacers to US format before using parseFloat
390
+ if (BigNum.delim !== '.' || BigNum.spacer !== ',') {
391
+ printedValue = printedValue
392
+ .split('')
393
+ .map((char) => {
394
+ if (char === BigNum.delim)
395
+ return '.';
396
+ if (char === BigNum.spacer)
397
+ return ',';
398
+ return char;
399
+ })
400
+ .join('');
401
+ }
402
+ return parseFloat(printedValue);
385
403
  }
386
404
  static fromJSON(json) {
387
405
  return BigNum.from(new anchor_1.BN(json.val), new anchor_1.BN(json.precision));
@@ -407,10 +425,14 @@ class BigNum {
407
425
  // Handle empty number edge cases
408
426
  if (!val)
409
427
  return BigNum.from(numericConstants_1.ZERO, precisionShift);
410
- if (!val.replace(BigNum.delim, ''))
428
+ if (!val.replace(BigNum.delim, '')) {
411
429
  return BigNum.from(numericConstants_1.ZERO, precisionShift);
412
- const [leftSide, rightSide] = val.split(BigNum.delim);
413
- const rawBn = new anchor_1.BN(`${leftSide !== null && leftSide !== void 0 ? leftSide : ''}${rightSide !== null && rightSide !== void 0 ? rightSide : ''}`);
430
+ }
431
+ const sides = val.split(BigNum.delim);
432
+ const rightSide = sides[1];
433
+ const leftSide = sides[0].replace(/\D/g, '');
434
+ const bnInput = `${leftSide !== null && leftSide !== void 0 ? leftSide : ''}${rightSide !== null && rightSide !== void 0 ? rightSide : ''}`;
435
+ const rawBn = new anchor_1.BN(bnInput);
414
436
  const rightSideLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
415
437
  const totalShift = precisionShift
416
438
  ? precisionShift.sub(new anchor_1.BN(rightSideLength))