@drift-labs/sdk 2.42.0-beta.7 → 2.42.0-beta.9

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.
Files changed (47) hide show
  1. package/VERSION +1 -1
  2. package/bun.lockb +0 -0
  3. package/lib/addresses/pda.d.ts +1 -0
  4. package/lib/adminClient.d.ts +1 -0
  5. package/lib/constants/numericConstants.d.ts +62 -59
  6. package/lib/constants/numericConstants.js +2 -1
  7. package/lib/constants/spotMarkets.d.ts +1 -0
  8. package/lib/dlob/DLOB.d.ts +10 -16
  9. package/lib/dlob/DLOB.js +9 -39
  10. package/lib/dlob/DLOBNode.d.ts +1 -0
  11. package/lib/dlob/NodeList.d.ts +1 -0
  12. package/lib/dlob/orderBookLevels.d.ts +2 -1
  13. package/lib/driftClient.d.ts +1 -0
  14. package/lib/factory/bigNum.d.ts +8 -7
  15. package/lib/jupiter/jupiterClient.d.ts +1 -0
  16. package/lib/marinade/index.d.ts +1 -0
  17. package/lib/math/amm.d.ts +2 -1
  18. package/lib/math/auction.d.ts +1 -0
  19. package/lib/math/conversion.d.ts +2 -1
  20. package/lib/math/funding.d.ts +1 -0
  21. package/lib/math/funding.js +2 -1
  22. package/lib/math/insurance.d.ts +1 -0
  23. package/lib/math/margin.d.ts +1 -0
  24. package/lib/math/market.d.ts +1 -0
  25. package/lib/math/oracles.d.ts +1 -0
  26. package/lib/math/orders.d.ts +1 -0
  27. package/lib/math/position.d.ts +1 -0
  28. package/lib/math/repeg.d.ts +1 -0
  29. package/lib/math/spotBalance.d.ts +3 -2
  30. package/lib/math/spotMarket.d.ts +1 -0
  31. package/lib/math/spotPosition.d.ts +1 -0
  32. package/lib/math/superStake.d.ts +1 -0
  33. package/lib/math/trade.d.ts +1 -0
  34. package/lib/math/utils.d.ts +1 -0
  35. package/lib/oracles/pythClient.d.ts +2 -1
  36. package/lib/oracles/strictOraclePrice.d.ts +1 -0
  37. package/lib/oracles/types.d.ts +1 -0
  38. package/lib/orderParams.d.ts +1 -0
  39. package/lib/phoenix/phoenixSubscriber.d.ts +1 -0
  40. package/lib/serum/serumSubscriber.d.ts +1 -0
  41. package/lib/tokenFaucet.d.ts +1 -0
  42. package/lib/types.d.ts +1 -0
  43. package/lib/user.d.ts +2 -1
  44. package/package.json +2 -1
  45. package/src/constants/numericConstants.ts +1 -0
  46. package/src/dlob/DLOB.ts +23 -67
  47. package/src/math/funding.ts +6 -1
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.42.0-beta.7
1
+ 2.42.0-beta.9
package/bun.lockb CHANGED
Binary file
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { PublicKey } from '@solana/web3.js';
2
3
  import { BN } from '@coral-xyz/anchor';
3
4
  export declare function getDriftStateAccountPublicKeyAndNonce(programId: PublicKey): Promise<[PublicKey, number]>;
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { PublicKey, TransactionSignature } from '@solana/web3.js';
2
3
  import { FeeStructure, OracleGuardRails, OracleSource, ExchangeStatus, MarketStatus, ContractTier, AssetTier, SpotFulfillmentConfigStatus } from './types';
3
4
  import { BN } from '@coral-xyz/anchor';
@@ -1,60 +1,63 @@
1
- export declare const ZERO: any;
2
- export declare const ONE: any;
3
- export declare const TWO: any;
4
- export declare const THREE: any;
5
- export declare const FOUR: any;
6
- export declare const FIVE: any;
7
- export declare const SIX: any;
8
- export declare const SEVEN: any;
9
- export declare const EIGHT: any;
10
- export declare const NINE: any;
11
- export declare const TEN: any;
12
- export declare const TEN_THOUSAND: any;
13
- export declare const BN_MAX: any;
14
- export declare const TEN_MILLION: any;
15
- export declare const MAX_LEVERAGE: any;
16
- export declare const MAX_LEVERAGE_ORDER_SIZE: any;
17
- export declare const PERCENTAGE_PRECISION_EXP: any;
18
- export declare const PERCENTAGE_PRECISION: any;
19
- export declare const CONCENTRATION_PRECISION: any;
20
- export declare const QUOTE_PRECISION_EXP: any;
21
- export declare const FUNDING_RATE_BUFFER_PRECISION_EXP: any;
22
- export declare const PRICE_PRECISION_EXP: any;
23
- export declare const FUNDING_RATE_PRECISION_EXP: any;
24
- export declare const PEG_PRECISION_EXP: any;
25
- export declare const AMM_RESERVE_PRECISION_EXP: any;
26
- export declare const SPOT_MARKET_RATE_PRECISION_EXP: any;
27
- export declare const SPOT_MARKET_RATE_PRECISION: any;
28
- export declare const SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP: any;
29
- export declare const SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION: any;
30
- export declare const SPOT_MARKET_UTILIZATION_PRECISION_EXP: any;
31
- export declare const SPOT_MARKET_UTILIZATION_PRECISION: any;
32
- export declare const SPOT_MARKET_WEIGHT_PRECISION: any;
33
- export declare const SPOT_MARKET_BALANCE_PRECISION_EXP: any;
34
- export declare const SPOT_MARKET_BALANCE_PRECISION: any;
35
- export declare const SPOT_MARKET_IMF_PRECISION_EXP: any;
36
- export declare const SPOT_MARKET_IMF_PRECISION: any;
37
- export declare const LIQUIDATION_FEE_PRECISION: any;
38
- export declare const QUOTE_PRECISION: any;
39
- export declare const PRICE_PRECISION: any;
40
- export declare const FUNDING_RATE_PRECISION: any;
41
- export declare const FUNDING_RATE_BUFFER_PRECISION: any;
42
- export declare const PEG_PRECISION: any;
43
- export declare const AMM_RESERVE_PRECISION: any;
44
- export declare const BASE_PRECISION: any;
45
- export declare const BASE_PRECISION_EXP: any;
46
- export declare const AMM_TO_QUOTE_PRECISION_RATIO: any;
47
- export declare const PRICE_DIV_PEG: any;
48
- export declare const PRICE_TO_QUOTE_PRECISION: any;
49
- export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: any;
50
- export declare const MARGIN_PRECISION: any;
51
- export declare const BID_ASK_SPREAD_PRECISION: any;
52
- export declare const LIQUIDATION_PCT_PRECISION: any;
53
- export declare const FIVE_MINUTE: any;
54
- export declare const ONE_HOUR: any;
55
- export declare const ONE_YEAR: any;
1
+ /// <reference types="bn.js" />
2
+ import { BN } from '../';
3
+ export declare const ZERO: BN;
4
+ export declare const ONE: BN;
5
+ export declare const TWO: BN;
6
+ export declare const THREE: BN;
7
+ export declare const FOUR: BN;
8
+ export declare const FIVE: BN;
9
+ export declare const SIX: BN;
10
+ export declare const SEVEN: BN;
11
+ export declare const EIGHT: BN;
12
+ export declare const NINE: BN;
13
+ export declare const TEN: BN;
14
+ export declare const TEN_THOUSAND: BN;
15
+ export declare const BN_MAX: BN;
16
+ export declare const TEN_MILLION: BN;
17
+ export declare const MAX_LEVERAGE: BN;
18
+ export declare const MAX_LEVERAGE_ORDER_SIZE: BN;
19
+ export declare const PERCENTAGE_PRECISION_EXP: BN;
20
+ export declare const PERCENTAGE_PRECISION: BN;
21
+ export declare const CONCENTRATION_PRECISION: BN;
22
+ export declare const QUOTE_PRECISION_EXP: BN;
23
+ export declare const FUNDING_RATE_BUFFER_PRECISION_EXP: BN;
24
+ export declare const PRICE_PRECISION_EXP: BN;
25
+ export declare const FUNDING_RATE_PRECISION_EXP: BN;
26
+ export declare const PEG_PRECISION_EXP: BN;
27
+ export declare const AMM_RESERVE_PRECISION_EXP: BN;
28
+ export declare const SPOT_MARKET_RATE_PRECISION_EXP: BN;
29
+ export declare const SPOT_MARKET_RATE_PRECISION: BN;
30
+ export declare const SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP: BN;
31
+ export declare const SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION: BN;
32
+ export declare const SPOT_MARKET_UTILIZATION_PRECISION_EXP: BN;
33
+ export declare const SPOT_MARKET_UTILIZATION_PRECISION: BN;
34
+ export declare const SPOT_MARKET_WEIGHT_PRECISION: BN;
35
+ export declare const SPOT_MARKET_BALANCE_PRECISION_EXP: BN;
36
+ export declare const SPOT_MARKET_BALANCE_PRECISION: BN;
37
+ export declare const SPOT_MARKET_IMF_PRECISION_EXP: BN;
38
+ export declare const SPOT_MARKET_IMF_PRECISION: BN;
39
+ export declare const LIQUIDATION_FEE_PRECISION: BN;
40
+ export declare const QUOTE_PRECISION: BN;
41
+ export declare const PRICE_PRECISION: BN;
42
+ export declare const FUNDING_RATE_PRECISION: BN;
43
+ export declare const FUNDING_RATE_BUFFER_PRECISION: BN;
44
+ export declare const PEG_PRECISION: BN;
45
+ export declare const AMM_RESERVE_PRECISION: BN;
46
+ export declare const BASE_PRECISION: BN;
47
+ export declare const BASE_PRECISION_EXP: BN;
48
+ export declare const AMM_TO_QUOTE_PRECISION_RATIO: BN;
49
+ export declare const PRICE_DIV_PEG: BN;
50
+ export declare const PRICE_TO_QUOTE_PRECISION: BN;
51
+ export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: BN;
52
+ export declare const MARGIN_PRECISION: BN;
53
+ export declare const BID_ASK_SPREAD_PRECISION: BN;
54
+ export declare const LIQUIDATION_PCT_PRECISION: BN;
55
+ export declare const FUNDING_RATE_OFFSET_DENOMINATOR: BN;
56
+ export declare const FIVE_MINUTE: BN;
57
+ export declare const ONE_HOUR: BN;
58
+ export declare const ONE_YEAR: BN;
56
59
  export declare const QUOTE_SPOT_MARKET_INDEX = 0;
57
- export declare const LAMPORTS_PRECISION: any;
58
- export declare const LAMPORTS_EXP: any;
59
- export declare const OPEN_ORDER_MARGIN_REQUIREMENT: any;
60
- export declare const DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT: any;
60
+ export declare const LAMPORTS_PRECISION: BN;
61
+ export declare const LAMPORTS_EXP: BN;
62
+ export declare const OPEN_ORDER_MARGIN_REQUIREMENT: BN;
63
+ export declare const DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT: BN;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_RATE_BUFFER_PRECISION = exports.FUNDING_RATE_PRECISION = exports.PRICE_PRECISION = exports.QUOTE_PRECISION = exports.LIQUIDATION_FEE_PRECISION = exports.SPOT_MARKET_IMF_PRECISION = exports.SPOT_MARKET_IMF_PRECISION_EXP = exports.SPOT_MARKET_BALANCE_PRECISION = exports.SPOT_MARKET_BALANCE_PRECISION_EXP = exports.SPOT_MARKET_WEIGHT_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION_EXP = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP = exports.SPOT_MARKET_RATE_PRECISION = exports.SPOT_MARKET_RATE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.PRICE_PRECISION_EXP = exports.FUNDING_RATE_BUFFER_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.CONCENTRATION_PRECISION = exports.PERCENTAGE_PRECISION = exports.PERCENTAGE_PRECISION_EXP = exports.MAX_LEVERAGE_ORDER_SIZE = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.NINE = exports.EIGHT = exports.SEVEN = exports.SIX = exports.FIVE = exports.FOUR = exports.THREE = exports.TWO = exports.ONE = exports.ZERO = void 0;
4
- exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = exports.ONE_HOUR = exports.FIVE_MINUTE = exports.LIQUIDATION_PCT_PRECISION = exports.BID_ASK_SPREAD_PRECISION = void 0;
4
+ exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = exports.ONE_HOUR = exports.FIVE_MINUTE = exports.FUNDING_RATE_OFFSET_DENOMINATOR = exports.LIQUIDATION_PCT_PRECISION = exports.BID_ASK_SPREAD_PRECISION = void 0;
5
5
  const web3_js_1 = require("@solana/web3.js");
6
6
  const __1 = require("../");
7
7
  exports.ZERO = new __1.BN(0);
@@ -56,6 +56,7 @@ exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.m
56
56
  exports.MARGIN_PRECISION = exports.TEN_THOUSAND;
57
57
  exports.BID_ASK_SPREAD_PRECISION = new __1.BN(1000000); // 10^6
58
58
  exports.LIQUIDATION_PCT_PRECISION = exports.TEN_THOUSAND;
59
+ exports.FUNDING_RATE_OFFSET_DENOMINATOR = new __1.BN(5000);
59
60
  exports.FIVE_MINUTE = new __1.BN(60 * 5);
60
61
  exports.ONE_HOUR = new __1.BN(60 * 60);
61
62
  exports.ONE_YEAR = new __1.BN(31536000);
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { PublicKey } from '@solana/web3.js';
2
3
  import { BN, DriftEnv, OracleSource } from '../';
3
4
  export type SpotMarketConfig = {
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { NodeList } from './NodeList';
2
3
  import { BN, DLOBNode, DLOBNodeType, DriftClient, MarketType, MarketTypeStr, OraclePriceData, Order, OrderActionRecord, OrderRecord, PerpMarketAccount, PositionDirection, SlotSubscriber, SpotMarketAccount, StateAccount, TriggerOrderNode, UserMap } from '..';
3
4
  import { PublicKey } from '@solana/web3.js';
@@ -26,6 +27,13 @@ export type MarketNodeLists = {
26
27
  };
27
28
  };
28
29
  type OrderBookCallback = () => void;
30
+ /**
31
+ * Receives a DLOBNode and is expected to return true if the node should
32
+ * be taken into account when generating, or false otherwise.
33
+ *
34
+ * Currently used in getRestingLimitBids and getRestingLimitAsks.
35
+ */
36
+ export type DLOBFilterFcn = (node: DLOBNode) => boolean;
29
37
  export type NodeToFill = {
30
38
  node: DLOBNode;
31
39
  makerNodes: DLOBNode[];
@@ -70,22 +78,8 @@ export declare class DLOB {
70
78
  getTakingBids(marketIndex: number, marketType: MarketType, slot: number, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
71
79
  getTakingAsks(marketIndex: number, marketType: MarketType, slot: number, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
72
80
  private getBestNode;
73
- getRestingLimitAsks(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
74
- /**
75
- * Filters the limit asks that are resting and do not cross fallback bid
76
- * Taking orders can only fill against orders that meet this criteria
77
- *
78
- * @returns
79
- */
80
- getMakerLimitAsks(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, fallbackBid?: BN): Generator<DLOBNode>;
81
- getRestingLimitBids(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
82
- /**
83
- * Filters the limit bids that are post only, have been place for sufficiently long or are below the fallback ask
84
- * Market orders can only fill against orders that meet this criteria
85
- *
86
- * @returns
87
- */
88
- getMakerLimitBids(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, fallbackAsk?: BN): Generator<DLOBNode>;
81
+ getRestingLimitAsks(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
82
+ getRestingLimitBids(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
89
83
  getAsks(marketIndex: number, fallbackAsk: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
90
84
  getBids(marketIndex: number, fallbackBid: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
91
85
  findCrossingRestingLimitOrders(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): NodeToFill[];
package/lib/dlob/DLOB.js CHANGED
@@ -572,7 +572,7 @@ class DLOB {
572
572
  return bestNode.order.slot.lt(currentNode.order.slot);
573
573
  });
574
574
  }
575
- *getBestNode(generatorList, oraclePriceData, slot, compareFcn) {
575
+ *getBestNode(generatorList, oraclePriceData, slot, compareFcn, filterFcn) {
576
576
  const generators = generatorList.map((generator) => {
577
577
  return {
578
578
  next: generator.next(),
@@ -600,6 +600,10 @@ class DLOB {
600
600
  bestGenerator.next = bestGenerator.generator.next();
601
601
  continue;
602
602
  }
603
+ if (filterFcn && filterFcn(bestGenerator.next.value)) {
604
+ bestGenerator.next = bestGenerator.generator.next();
605
+ continue;
606
+ }
603
607
  yield bestGenerator.next.value;
604
608
  bestGenerator.next = bestGenerator.generator.next();
605
609
  }
@@ -608,7 +612,7 @@ class DLOB {
608
612
  }
609
613
  }
610
614
  }
611
- *getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData) {
615
+ *getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData, filterFcn) {
612
616
  if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
613
617
  throw new Error('Must provide OraclePriceData to get spot asks');
614
618
  }
@@ -626,26 +630,9 @@ class DLOB {
626
630
  return bestNode
627
631
  .getPrice(oraclePriceData, slot)
628
632
  .lt(currentNode.getPrice(oraclePriceData, slot));
629
- });
633
+ }, filterFcn);
630
634
  }
631
- /**
632
- * Filters the limit asks that are resting and do not cross fallback bid
633
- * Taking orders can only fill against orders that meet this criteria
634
- *
635
- * @returns
636
- */
637
- *getMakerLimitAsks(marketIndex, slot, marketType, oraclePriceData, fallbackBid) {
638
- const isPerpMarket = (0, __1.isVariant)(marketType, 'perp');
639
- for (const node of this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData)) {
640
- if (isPerpMarket &&
641
- fallbackBid &&
642
- node.getPrice(oraclePriceData, slot).lte(fallbackBid)) {
643
- continue;
644
- }
645
- yield node;
646
- }
647
- }
648
- *getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData) {
635
+ *getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData, filterFcn) {
649
636
  if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
650
637
  throw new Error('Must provide OraclePriceData to get spot bids');
651
638
  }
@@ -663,24 +650,7 @@ class DLOB {
663
650
  return bestNode
664
651
  .getPrice(oraclePriceData, slot)
665
652
  .gt(currentNode.getPrice(oraclePriceData, slot));
666
- });
667
- }
668
- /**
669
- * Filters the limit bids that are post only, have been place for sufficiently long or are below the fallback ask
670
- * Market orders can only fill against orders that meet this criteria
671
- *
672
- * @returns
673
- */
674
- *getMakerLimitBids(marketIndex, slot, marketType, oraclePriceData, fallbackAsk) {
675
- const isPerpMarket = (0, __1.isVariant)(marketType, 'perp');
676
- for (const node of this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData)) {
677
- if (isPerpMarket &&
678
- fallbackAsk &&
679
- node.getPrice(oraclePriceData, slot).gte(fallbackAsk)) {
680
- continue;
681
- }
682
- yield node;
683
- }
653
+ }, filterFcn);
684
654
  }
685
655
  *getAsks(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
686
656
  if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN, OraclePriceData, Order } from '..';
2
3
  import { PublicKey } from '@solana/web3.js';
3
4
  export interface DLOBNode {
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN, MarketTypeStr, Order } from '..';
2
3
  import { PublicKey } from '@solana/web3.js';
3
4
  import { DLOBNode, DLOBNodeMap } from './DLOBNode';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN, DLOBNode, OraclePriceData, PerpMarketAccount } from '..';
2
3
  import { PublicKey } from '@solana/web3.js';
3
4
  type liquiditySource = 'serum' | 'vamm' | 'dlob' | 'phoenix';
@@ -26,7 +27,7 @@ export type L3OrderBook = {
26
27
  asks: L3Level[];
27
28
  bids: L3Level[];
28
29
  };
29
- export declare const DEFAULT_TOP_OF_BOOK_QUOTE_AMOUNTS: any[];
30
+ export declare const DEFAULT_TOP_OF_BOOK_QUOTE_AMOUNTS: BN[];
30
31
  /**
31
32
  * Get an {@link Generator<L2Level>} generator from a {@link Generator<DLOBNode>}
32
33
  * @param dlobNodes e.g. {@link DLOB#getRestingLimitAsks} or {@link DLOB#getRestingLimitBids}
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="bn.js" />
2
3
  import { AnchorProvider, BN, Program, ProgramAccount } from '@coral-xyz/anchor';
3
4
  import { StateAccount, IWallet, PositionDirection, UserAccount, PerpMarketAccount, OrderParams, Order, SpotMarketAccount, SpotPosition, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo, MarketType, TxParams, SerumV3FulfillmentConfigAccount, ReferrerNameAccount, OrderTriggerCondition, PerpMarketExtendedInfo, UserStatsAccount, PhoenixV1FulfillmentConfigAccount, ModifyOrderPolicy, SwapReduceOnly } from './types';
4
5
  import * as anchor from '@coral-xyz/anchor';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '@coral-xyz/anchor';
2
3
  export declare class BigNum {
3
4
  val: BN;
@@ -43,11 +44,11 @@ export declare class BigNum {
43
44
  gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
44
45
  lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
45
46
  eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
46
- eqZero(): any;
47
- gtZero(): any;
48
- ltZero(): any;
49
- gteZero(): any;
50
- lteZero(): any;
47
+ eqZero(): boolean;
48
+ gtZero(): boolean;
49
+ ltZero(): boolean;
50
+ gteZero(): boolean;
51
+ lteZero(): boolean;
51
52
  abs(): BigNum;
52
53
  neg(): BigNum;
53
54
  toString: (base?: number | 'hex', length?: number) => string;
@@ -87,8 +88,8 @@ export declare class BigNum {
87
88
  toNotional(useTradePrecision?: boolean, precisionOverride?: number): string;
88
89
  toMillified(precision?: number, rounded?: boolean): string;
89
90
  toJSON(): {
90
- val: any;
91
- precision: any;
91
+ val: string;
92
+ precision: string;
92
93
  };
93
94
  isNeg(): boolean;
94
95
  isPos(): boolean;
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { AddressLookupTableAccount, Connection, PublicKey, TransactionInstruction, TransactionMessage, VersionedTransaction } from '@solana/web3.js';
2
3
  import { BN } from '@coral-xyz/anchor';
3
4
  export type SwapMode = 'ExactIn' | 'ExactOut';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { AnchorProvider, BN, Program } from '@coral-xyz/anchor';
2
3
  import { MarinadeFinance } from './types';
3
4
  import { PublicKey, TransactionInstruction } from '@solana/web3.js';
package/lib/math/amm.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '@coral-xyz/anchor';
2
3
  import { AMM, PositionDirection, SwapDirection, PerpMarketAccount } from '../types';
3
4
  import { OraclePriceData } from '../oracles/types';
@@ -86,7 +87,7 @@ export declare function getSwapDirection(inputAssetType: AssetType, positionDire
86
87
  * @param market
87
88
  * @returns cost : Precision PRICE_PRECISION
88
89
  */
89
- export declare function calculateTerminalPrice(market: PerpMarketAccount): any;
90
+ export declare function calculateTerminalPrice(market: PerpMarketAccount): BN;
90
91
  export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, oraclePriceData?: OraclePriceData, now?: BN): [BN, PositionDirection];
91
92
  export declare function calculateQuoteAssetAmountSwapped(quoteAssetReserves: BN, pegMultiplier: BN, swapDirection: SwapDirection): BN;
92
93
  export declare function calculateMaxBaseAssetAmountFillable(amm: AMM, orderDirection: PositionDirection): BN;
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { Order, PositionDirection } from '../types';
2
3
  import { BN } from '../.';
3
4
  export declare function isAuctionComplete(order: Order, slot: number): boolean;
@@ -1 +1,2 @@
1
- export declare const convertToNumber: (bigNumber: BN, precision?: BN) => any;
1
+ import { BN } from '../';
2
+ export declare const convertToNumber: (bigNumber: BN, precision?: BN) => number;
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '@coral-xyz/anchor';
2
3
  import { PerpMarketAccount } from '../types';
3
4
  import { OraclePriceData } from '../oracles/types';
@@ -72,7 +72,8 @@ async function calculateAllEstimatedFundingRate(market, oraclePriceData, markPri
72
72
  // console.log('shrink orac:', liveOracleTwap.toString(), '->', oracleTwap.toString());
73
73
  // }
74
74
  const twapSpread = markTwap.sub(oracleTwap);
75
- const twapSpreadPct = twapSpread
75
+ const twapSpreadWithOffset = twapSpread.add(oracleTwap.abs().div(numericConstants_1.FUNDING_RATE_OFFSET_DENOMINATOR));
76
+ const twapSpreadPct = twapSpreadWithOffset
76
77
  .mul(numericConstants_1.PRICE_PRECISION)
77
78
  .mul(new anchor_1.BN(100))
78
79
  .div(oracleTwap);
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '../index';
2
3
  export declare function stakeAmountToShares(amount: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
3
4
  export declare function unstakeSharesToAmount(nShares: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '@coral-xyz/anchor';
2
3
  import { OraclePriceData } from '../oracles/types';
3
4
  import { PerpMarketAccount, PerpPosition } from '..';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '@coral-xyz/anchor';
2
3
  import { PerpMarketAccount, PositionDirection, MarginCategory, SpotMarketAccount } from '../types';
3
4
  import { OraclePriceData } from '../oracles/types';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { AMM, OracleGuardRails } from '../types';
2
3
  import { OraclePriceData } from '../oracles/types';
3
4
  import { BN, HistoricalOracleData, PerpMarketAccount } from '../index';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { User } from '../user';
2
3
  import { PerpMarketAccount, AMM, Order, PositionDirection } from '../types';
3
4
  import { BN } from '@coral-xyz/anchor';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN, SpotMarketAccount } from '../';
2
3
  import { OraclePriceData } from '../oracles/types';
3
4
  import { PerpMarketAccount, PositionDirection, PerpPosition } from '../types';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '@coral-xyz/anchor';
2
3
  import { AMM } from '../types';
3
4
  /**
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { SpotMarketAccount, SpotBalanceType, MarginCategory } from '../types';
2
3
  import { BN } from '@coral-xyz/anchor';
3
4
  import { OraclePriceData } from '../oracles/types';
@@ -50,7 +51,7 @@ export declare function getTokenValue(tokenAmount: BN, spotDecimals: number, ora
50
51
  export declare function calculateAssetWeight(balanceAmount: BN, oraclePrice: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
51
52
  export declare function calculateScaledInitialAssetWeight(spotMarket: SpotMarketAccount, oraclePrice: BN): BN;
52
53
  export declare function calculateLiabilityWeight(size: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
53
- export declare function calculateUtilization(bank: SpotMarketAccount, delta?: any): BN;
54
+ export declare function calculateUtilization(bank: SpotMarketAccount, delta?: BN): BN;
54
55
  /**
55
56
  * calculates max borrow amount where rate would stay below targetBorrowRate
56
57
  * @param spotMarketAccount
@@ -61,7 +62,7 @@ export declare function calculateSpotMarketBorrowCapacity(spotMarketAccount: Spo
61
62
  totalCapacity: BN;
62
63
  remainingCapacity: BN;
63
64
  };
64
- export declare function calculateInterestRate(bank: SpotMarketAccount, delta?: any): BN;
65
+ export declare function calculateInterestRate(bank: SpotMarketAccount, delta?: BN): BN;
65
66
  export declare function calculateDepositRate(bank: SpotMarketAccount): BN;
66
67
  export declare function calculateBorrowRate(bank: SpotMarketAccount): BN;
67
68
  export declare function calculateInterestAccumulated(bank: SpotMarketAccount, now: BN): {
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '@coral-xyz/anchor';
2
3
  import { MarginCategory, SpotBalanceType, SpotMarketAccount } from '../types';
3
4
  export declare function castNumberToSpotPrecision(value: number | BN, spotMarket: SpotMarketAccount): BN;
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { MarginCategory, SpotMarketAccount, SpotPosition } from '../types';
2
3
  import { BN } from '@coral-xyz/anchor';
3
4
  import { StrictOraclePrice } from '../oracles/strictOraclePrice';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { AddressLookupTableAccount, PublicKey, TransactionInstruction } from '@solana/web3.js';
2
3
  import { JupiterClient } from '../jupiter/jupiterClient';
3
4
  import { DriftClient } from '../driftClient';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { PerpMarketAccount, PositionDirection, SpotMarketAccount } from '../types';
2
3
  import { BN } from '@coral-xyz/anchor';
3
4
  import { AssetType } from './amm';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '../';
2
3
  export declare function clampBN(x: BN, min: BN, max: BN): BN;
3
4
  export declare const squareRootBN: (n: BN) => BN;
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  /// <reference types="node" />
2
3
  import { Connection, PublicKey } from '@solana/web3.js';
3
4
  import { OracleClient, OraclePriceData } from './types';
@@ -6,7 +7,7 @@ export declare class PythClient implements OracleClient {
6
7
  private connection;
7
8
  private multiple;
8
9
  private stableCoin;
9
- constructor(connection: Connection, multiple?: any, stableCoin?: boolean);
10
+ constructor(connection: Connection, multiple?: BN, stableCoin?: boolean);
10
11
  getOraclePriceData(pricePublicKey: PublicKey): Promise<OraclePriceData>;
11
12
  getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData;
12
13
  }
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { BN } from '@coral-xyz/anchor';
2
3
  export declare class StrictOraclePrice {
3
4
  current: BN;
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  /// <reference types="node" />
2
3
  import { BN } from '@coral-xyz/anchor';
3
4
  import { PublicKey } from '@solana/web3.js';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { OptionalOrderParams, OrderParams, OrderTriggerCondition } from './types';
2
3
  import { BN } from '@coral-xyz/anchor';
3
4
  export declare function getLimitOrderParams(params: Omit<OptionalOrderParams, 'orderType'> & {
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { Connection, PublicKey } from '@solana/web3.js';
2
3
  import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
3
4
  import { Client, Market } from '@ellipsis-labs/phoenix-sdk';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { Connection, PublicKey } from '@solana/web3.js';
2
3
  import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
3
4
  import { Market, Orderbook } from '@project-serum/serum';
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import * as anchor from '@coral-xyz/anchor';
2
3
  import { AnchorProvider, Program } from '@coral-xyz/anchor';
3
4
  import { Account } from '@solana/spl-token';
package/lib/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import { PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js';
2
3
  import { BN } from '.';
3
4
  export declare enum ExchangeStatus {
package/lib/user.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="bn.js" />
2
3
  import { PublicKey } from '@solana/web3.js';
3
4
  import { EventEmitter } from 'events';
4
5
  import StrictEventEmitter from 'strict-event-emitter-types';
@@ -99,7 +100,7 @@ export declare class User {
99
100
  * calculates Buying Power = free collateral / initial margin ratio
100
101
  * @returns : Precision QUOTE_PRECISION
101
102
  */
102
- getPerpBuyingPower(marketIndex: number, collateralBuffer?: any): BN;
103
+ getPerpBuyingPower(marketIndex: number, collateralBuffer?: BN): BN;
103
104
  getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex: number, freeCollateral: BN, baseAssetAmount: BN): BN;
104
105
  /**
105
106
  * calculates Free Collateral = Total collateral - margin requirement
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.42.0-beta.7",
3
+ "version": "2.42.0-beta.9",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -45,6 +45,7 @@
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/big.js": "^6.2.0",
48
+ "@types/bn.js": "^5.1.3",
48
49
  "@types/chai": "^4.3.1",
49
50
  "@types/jest": "^28.1.3",
50
51
  "@types/mocha": "^9.1.1",
@@ -83,6 +83,7 @@ export const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO =
83
83
  export const MARGIN_PRECISION = TEN_THOUSAND;
84
84
  export const BID_ASK_SPREAD_PRECISION = new BN(1000000); // 10^6
85
85
  export const LIQUIDATION_PCT_PRECISION = TEN_THOUSAND;
86
+ export const FUNDING_RATE_OFFSET_DENOMINATOR = new BN(5000);
86
87
 
87
88
  export const FIVE_MINUTE = new BN(60 * 5);
88
89
  export const ONE_HOUR = new BN(60 * 60);
package/src/dlob/DLOB.ts CHANGED
@@ -73,6 +73,14 @@ export type MarketNodeLists = {
73
73
 
74
74
  type OrderBookCallback = () => void;
75
75
 
76
+ /**
77
+ * Receives a DLOBNode and is expected to return true if the node should
78
+ * be taken into account when generating, or false otherwise.
79
+ *
80
+ * Currently used in getRestingLimitBids and getRestingLimitAsks.
81
+ */
82
+ export type DLOBFilterFcn = (node: DLOBNode) => boolean;
83
+
76
84
  export type NodeToFill = {
77
85
  node: DLOBNode;
78
86
  makerNodes: DLOBNode[];
@@ -1065,7 +1073,8 @@ export class DLOB {
1065
1073
  currentDLOBNode: DLOBNode,
1066
1074
  slot: number,
1067
1075
  oraclePriceData: OraclePriceData
1068
- ) => boolean
1076
+ ) => boolean,
1077
+ filterFcn?: DLOBFilterFcn
1069
1078
  ): Generator<DLOBNode> {
1070
1079
  const generators = generatorList.map((generator) => {
1071
1080
  return {
@@ -1102,6 +1111,11 @@ export class DLOB {
1102
1111
  continue;
1103
1112
  }
1104
1113
 
1114
+ if (filterFcn && filterFcn(bestGenerator.next.value)) {
1115
+ bestGenerator.next = bestGenerator.generator.next();
1116
+ continue;
1117
+ }
1118
+
1105
1119
  yield bestGenerator.next.value;
1106
1120
  bestGenerator.next = bestGenerator.generator.next();
1107
1121
  } else {
@@ -1114,7 +1128,8 @@ export class DLOB {
1114
1128
  marketIndex: number,
1115
1129
  slot: number,
1116
1130
  marketType: MarketType,
1117
- oraclePriceData: OraclePriceData
1131
+ oraclePriceData: OraclePriceData,
1132
+ filterFcn?: DLOBFilterFcn
1118
1133
  ): Generator<DLOBNode> {
1119
1134
  if (isVariant(marketType, 'spot') && !oraclePriceData) {
1120
1135
  throw new Error('Must provide OraclePriceData to get spot asks');
@@ -1142,46 +1157,17 @@ export class DLOB {
1142
1157
  return bestNode
1143
1158
  .getPrice(oraclePriceData, slot)
1144
1159
  .lt(currentNode.getPrice(oraclePriceData, slot));
1145
- }
1160
+ },
1161
+ filterFcn
1146
1162
  );
1147
1163
  }
1148
1164
 
1149
- /**
1150
- * Filters the limit asks that are resting and do not cross fallback bid
1151
- * Taking orders can only fill against orders that meet this criteria
1152
- *
1153
- * @returns
1154
- */
1155
- *getMakerLimitAsks(
1156
- marketIndex: number,
1157
- slot: number,
1158
- marketType: MarketType,
1159
- oraclePriceData: OraclePriceData,
1160
- fallbackBid?: BN
1161
- ): Generator<DLOBNode> {
1162
- const isPerpMarket = isVariant(marketType, 'perp');
1163
- for (const node of this.getRestingLimitAsks(
1164
- marketIndex,
1165
- slot,
1166
- marketType,
1167
- oraclePriceData
1168
- )) {
1169
- if (
1170
- isPerpMarket &&
1171
- fallbackBid &&
1172
- node.getPrice(oraclePriceData, slot).lte(fallbackBid)
1173
- ) {
1174
- continue;
1175
- }
1176
- yield node;
1177
- }
1178
- }
1179
-
1180
1165
  *getRestingLimitBids(
1181
1166
  marketIndex: number,
1182
1167
  slot: number,
1183
1168
  marketType: MarketType,
1184
- oraclePriceData: OraclePriceData
1169
+ oraclePriceData: OraclePriceData,
1170
+ filterFcn?: DLOBFilterFcn
1185
1171
  ): Generator<DLOBNode> {
1186
1172
  if (isVariant(marketType, 'spot') && !oraclePriceData) {
1187
1173
  throw new Error('Must provide OraclePriceData to get spot bids');
@@ -1209,41 +1195,11 @@ export class DLOB {
1209
1195
  return bestNode
1210
1196
  .getPrice(oraclePriceData, slot)
1211
1197
  .gt(currentNode.getPrice(oraclePriceData, slot));
1212
- }
1198
+ },
1199
+ filterFcn
1213
1200
  );
1214
1201
  }
1215
1202
 
1216
- /**
1217
- * Filters the limit bids that are post only, have been place for sufficiently long or are below the fallback ask
1218
- * Market orders can only fill against orders that meet this criteria
1219
- *
1220
- * @returns
1221
- */
1222
- *getMakerLimitBids(
1223
- marketIndex: number,
1224
- slot: number,
1225
- marketType: MarketType,
1226
- oraclePriceData: OraclePriceData,
1227
- fallbackAsk?: BN
1228
- ): Generator<DLOBNode> {
1229
- const isPerpMarket = isVariant(marketType, 'perp');
1230
- for (const node of this.getRestingLimitBids(
1231
- marketIndex,
1232
- slot,
1233
- marketType,
1234
- oraclePriceData
1235
- )) {
1236
- if (
1237
- isPerpMarket &&
1238
- fallbackAsk &&
1239
- node.getPrice(oraclePriceData, slot).gte(fallbackAsk)
1240
- ) {
1241
- continue;
1242
- }
1243
- yield node;
1244
- }
1245
- }
1246
-
1247
1203
  *getAsks(
1248
1204
  marketIndex: number,
1249
1205
  fallbackAsk: BN | undefined,
@@ -5,6 +5,7 @@ import {
5
5
  QUOTE_PRECISION,
6
6
  ZERO,
7
7
  ONE,
8
+ FUNDING_RATE_OFFSET_DENOMINATOR,
8
9
  } from '../constants/numericConstants';
9
10
  import { PerpMarketAccount, isVariant } from '../types';
10
11
  import { OraclePriceData } from '../oracles/types';
@@ -156,7 +157,11 @@ export async function calculateAllEstimatedFundingRate(
156
157
  // }
157
158
 
158
159
  const twapSpread = markTwap.sub(oracleTwap);
159
- const twapSpreadPct = twapSpread
160
+ const twapSpreadWithOffset = twapSpread.add(
161
+ oracleTwap.abs().div(FUNDING_RATE_OFFSET_DENOMINATOR)
162
+ );
163
+
164
+ const twapSpreadPct = twapSpreadWithOffset
160
165
  .mul(PRICE_PRECISION)
161
166
  .mul(new BN(100))
162
167
  .div(oracleTwap);