@drift-labs/sdk 2.52.0-beta.2 → 2.52.0-beta.4

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 CHANGED
@@ -1 +1 @@
1
- 2.52.0-beta.2
1
+ 2.52.0-beta.4
@@ -1,5 +1,5 @@
1
1
  import { Connection, PublicKey } from '@solana/web3.js';
2
- import { PRICE_PRECISION, PhoenixSubscriber } from '../src';
2
+ import { BASE_PRECISION, PRICE_PRECISION, PhoenixSubscriber } from '../src';
3
3
  import { PROGRAM_ID } from '@ellipsis-labs/phoenix-sdk';
4
4
 
5
5
  export async function listenToBook(): Promise<void> {
@@ -19,9 +19,16 @@ export async function listenToBook(): Promise<void> {
19
19
  await phoenixSubscriber.subscribe();
20
20
 
21
21
  for (let i = 0; i < 10; i++) {
22
- const bid = phoenixSubscriber.getBestBid().toNumber() / PRICE_PRECISION;
23
- const ask = phoenixSubscriber.getBestAsk().toNumber() / PRICE_PRECISION;
24
- console.log(`iter ${i}:`, bid.toFixed(3), '@', ask.toFixed(3));
22
+ const bids = phoenixSubscriber.getL2Levels("bids");
23
+ const asks = phoenixSubscriber.getL2Levels("asks");
24
+ console.log("bids");
25
+ for (const bid of bids) {
26
+ console.log(bid.price.toNumber() / PRICE_PRECISION.toNumber(), bid.size.toNumber() / BASE_PRECISION.toNumber());
27
+ }
28
+ console.log("asks");
29
+ for (const ask of asks) {
30
+ console.log(ask.price.toNumber() / PRICE_PRECISION.toNumber(), ask.size.toNumber() / BASE_PRECISION.toNumber());
31
+ }
25
32
  await new Promise((r) => setTimeout(r, 2000));
26
33
  }
27
34
 
@@ -99,14 +99,18 @@ class PhoenixSubscriber {
99
99
  return this.getL2Levels('asks');
100
100
  }
101
101
  *getL2Levels(side) {
102
- const basePrecision = Math.pow(10, this.market.data.header.baseParams.decimals);
103
- const pricePrecision = numericConstants_1.PRICE_PRECISION.toNumber();
104
- const ladder = (0, phoenix_sdk_1.getMarketUiLadder)(this.market, this.lastSlot, this.lastUnixTimestamp, 20);
102
+ const tickSize = this.market.data.header
103
+ .tickSizeInQuoteAtomsPerBaseUnit;
104
+ const baseLotsToRawBaseUnits = this.market.baseLotsToRawBaseUnits(1);
105
+ const basePrecision = new anchor_1.BN(Math.pow(10, this.market.data.header.baseParams.decimals) *
106
+ baseLotsToRawBaseUnits);
107
+ const pricePrecision = numericConstants_1.PRICE_PRECISION.div(tickSize);
108
+ const ladder = (0, phoenix_sdk_1.getMarketLadder)(this.market, this.lastSlot, this.lastUnixTimestamp, 20);
105
109
  for (let i = 0; i < ladder[side].length; i++) {
106
- const { price, quantity } = ladder[side][i];
107
- const size = new anchor_1.BN(quantity).mul(new anchor_1.BN(basePrecision));
110
+ const { priceInTicks, sizeInBaseLots } = ladder[side][i];
111
+ const size = sizeInBaseLots.mul(basePrecision);
108
112
  yield {
109
- price: new anchor_1.BN(price).mul(new anchor_1.BN(pricePrecision)),
113
+ price: priceInTicks.mul(new anchor_1.BN(pricePrecision)),
110
114
  size,
111
115
  sources: {
112
116
  phoenix: size,
package/lib/user.d.ts CHANGED
@@ -353,7 +353,7 @@ export declare class User {
353
353
  * @param quoteAmount
354
354
  * @returns feeForQuote : Precision QUOTE_PRECISION
355
355
  */
356
- calculateFeeForQuoteAmount(quoteAmount: BN): BN;
356
+ calculateFeeForQuoteAmount(quoteAmount: BN, marketIndex?: number): BN;
357
357
  /**
358
358
  * Calculates a user's max withdrawal amounts for a spot market. If reduceOnly is true,
359
359
  * it will return the max withdrawal amount without opening a liability for the user
package/lib/user.js CHANGED
@@ -1645,7 +1645,7 @@ class User {
1645
1645
  const feeTier = this.getUserFeeTier(marketType);
1646
1646
  let takerFee = feeTier.feeNumerator / feeTier.feeDenominator;
1647
1647
  let makerFee = feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
1648
- if (marketIndex && (0, types_1.isVariant)(marketType, 'perp')) {
1648
+ if (marketIndex !== undefined && (0, types_1.isVariant)(marketType, 'perp')) {
1649
1649
  const marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
1650
1650
  takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
1651
1651
  makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
@@ -1660,11 +1660,19 @@ class User {
1660
1660
  * @param quoteAmount
1661
1661
  * @returns feeForQuote : Precision QUOTE_PRECISION
1662
1662
  */
1663
- calculateFeeForQuoteAmount(quoteAmount) {
1664
- const feeTier = this.getUserFeeTier(_1.MarketType.PERP);
1665
- return quoteAmount
1666
- .mul(new _1.BN(feeTier.feeNumerator))
1667
- .div(new _1.BN(feeTier.feeDenominator));
1663
+ calculateFeeForQuoteAmount(quoteAmount, marketIndex) {
1664
+ if (marketIndex !== undefined) {
1665
+ const takerFeeMultiplier = this.getMarketFees(_1.MarketType.PERP, marketIndex).takerFee;
1666
+ const feeAmountNum = _1.BigNum.from(quoteAmount, numericConstants_1.QUOTE_PRECISION_EXP).toNum() *
1667
+ takerFeeMultiplier;
1668
+ return _1.BigNum.fromPrint(feeAmountNum.toString(), numericConstants_1.QUOTE_PRECISION_EXP).val;
1669
+ }
1670
+ else {
1671
+ const feeTier = this.getUserFeeTier(_1.MarketType.PERP);
1672
+ return quoteAmount
1673
+ .mul(new _1.BN(feeTier.feeNumerator))
1674
+ .div(new _1.BN(feeTier.feeDenominator));
1675
+ }
1668
1676
  }
1669
1677
  /**
1670
1678
  * Calculates a user's max withdrawal amounts for a spot market. If reduceOnly is true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.52.0-beta.2",
3
+ "version": "2.52.0-beta.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -6,6 +6,7 @@ import {
6
6
  toNum,
7
7
  getMarketUiLadder,
8
8
  Market,
9
+ getMarketLadder,
9
10
  } from '@ellipsis-labs/phoenix-sdk';
10
11
  import { PRICE_PRECISION } from '../constants/numericConstants';
11
12
  import { BN } from '@coral-xyz/anchor';
@@ -163,13 +164,18 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
163
164
  }
164
165
 
165
166
  *getL2Levels(side: 'bids' | 'asks'): Generator<L2Level> {
166
- const basePrecision = Math.pow(
167
- 10,
168
- this.market.data.header.baseParams.decimals
167
+ const tickSize = this.market.data.header
168
+ .tickSizeInQuoteAtomsPerBaseUnit as BN;
169
+ const baseLotsToRawBaseUnits = this.market.baseLotsToRawBaseUnits(1);
170
+
171
+ const basePrecision = new BN(
172
+ Math.pow(10, this.market.data.header.baseParams.decimals) *
173
+ baseLotsToRawBaseUnits
169
174
  );
170
- const pricePrecision = PRICE_PRECISION.toNumber();
171
175
 
172
- const ladder = getMarketUiLadder(
176
+ const pricePrecision = PRICE_PRECISION.div(tickSize as BN);
177
+
178
+ const ladder = getMarketLadder(
173
179
  this.market,
174
180
  this.lastSlot,
175
181
  this.lastUnixTimestamp,
@@ -177,10 +183,10 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
177
183
  );
178
184
 
179
185
  for (let i = 0; i < ladder[side].length; i++) {
180
- const { price, quantity } = ladder[side][i];
181
- const size = new BN(quantity).mul(new BN(basePrecision));
186
+ const { priceInTicks, sizeInBaseLots } = ladder[side][i];
187
+ const size = sizeInBaseLots.mul(basePrecision);
182
188
  yield {
183
- price: new BN(price).mul(new BN(pricePrecision)),
189
+ price: priceInTicks.mul(new BN(pricePrecision)),
184
190
  size,
185
191
  sources: {
186
192
  phoenix: size,
package/src/user.ts CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  OPEN_ORDER_MARGIN_REQUIREMENT,
28
28
  PRICE_PRECISION,
29
29
  QUOTE_PRECISION,
30
+ QUOTE_PRECISION_EXP,
30
31
  QUOTE_SPOT_MARKET_INDEX,
31
32
  SPOT_MARKET_WEIGHT_PRECISION,
32
33
  TEN,
@@ -40,6 +41,7 @@ import {
40
41
  UserAccountSubscriber,
41
42
  } from './accounts/types';
42
43
  import {
44
+ BigNum,
43
45
  BN,
44
46
  calculateBaseAssetValue,
45
47
  calculateMarketMarginRatio,
@@ -3013,7 +3015,7 @@ export class User {
3013
3015
  let makerFee =
3014
3016
  feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
3015
3017
 
3016
- if (marketIndex && isVariant(marketType, 'perp')) {
3018
+ if (marketIndex !== undefined && isVariant(marketType, 'perp')) {
3017
3019
  const marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
3018
3020
  takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
3019
3021
  makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
@@ -3030,11 +3032,22 @@ export class User {
3030
3032
  * @param quoteAmount
3031
3033
  * @returns feeForQuote : Precision QUOTE_PRECISION
3032
3034
  */
3033
- public calculateFeeForQuoteAmount(quoteAmount: BN): BN {
3034
- const feeTier = this.getUserFeeTier(MarketType.PERP);
3035
- return quoteAmount
3036
- .mul(new BN(feeTier.feeNumerator))
3037
- .div(new BN(feeTier.feeDenominator));
3035
+ public calculateFeeForQuoteAmount(quoteAmount: BN, marketIndex?: number): BN {
3036
+ if (marketIndex !== undefined) {
3037
+ const takerFeeMultiplier = this.getMarketFees(
3038
+ MarketType.PERP,
3039
+ marketIndex
3040
+ ).takerFee;
3041
+ const feeAmountNum =
3042
+ BigNum.from(quoteAmount, QUOTE_PRECISION_EXP).toNum() *
3043
+ takerFeeMultiplier;
3044
+ return BigNum.fromPrint(feeAmountNum.toString(), QUOTE_PRECISION_EXP).val;
3045
+ } else {
3046
+ const feeTier = this.getUserFeeTier(MarketType.PERP);
3047
+ return quoteAmount
3048
+ .mul(new BN(feeTier.feeNumerator))
3049
+ .div(new BN(feeTier.feeDenominator));
3050
+ }
3038
3051
  }
3039
3052
 
3040
3053
  /**