@drift-labs/sdk 2.5.0 → 2.6.0-beta.1

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.
@@ -7,7 +7,8 @@ class OrderNode {
7
7
  constructor(order, userAccount) {
8
8
  this.haveFilled = false;
9
9
  this.haveTrigger = false;
10
- this.order = order;
10
+ // Copy the order over to the node
11
+ this.order = { ...order };
11
12
  this.userAccount = userAccount;
12
13
  this.sortValue = this.getSortValue(order);
13
14
  }
@@ -187,6 +187,9 @@
187
187
  },
188
188
  {
189
189
  "name": "TriggerLimit"
190
+ },
191
+ {
192
+ "name": "Oracle"
190
193
  }
191
194
  ]
192
195
  }
@@ -163,9 +163,10 @@ export declare class DriftClient {
163
163
  * @param orderParams
164
164
  * @param userAccountPublicKey
165
165
  * @param userAccount
166
+ * @param makerInfo
166
167
  * @returns
167
168
  */
168
- sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount): Promise<{
169
+ sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo): Promise<{
169
170
  txSig: TransactionSignature;
170
171
  signedFillTx: Transaction;
171
172
  }>;
@@ -1019,16 +1019,17 @@ class DriftClient {
1019
1019
  * @param orderParams
1020
1020
  * @param userAccountPublicKey
1021
1021
  * @param userAccount
1022
+ * @param makerInfo
1022
1023
  * @returns
1023
1024
  */
1024
- async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount) {
1025
+ async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo) {
1025
1026
  const marketIndex = orderParams.marketIndex;
1026
1027
  const orderId = userAccount.nextOrderId;
1027
1028
  const marketOrderTx = (0, utils_1.wrapInTx)(await this.getPlacePerpOrderIx(orderParams));
1028
1029
  const fillTx = (0, utils_1.wrapInTx)(await this.getFillPerpOrderIx(userAccountPublicKey, userAccount, {
1029
1030
  orderId,
1030
1031
  marketIndex,
1031
- }));
1032
+ }, makerInfo));
1032
1033
  // Apply the latest blockhash to the txs so that we can sign before sending them
1033
1034
  const currentBlockHash = (await this.connection.getLatestBlockhash('finalized')).blockhash;
1034
1035
  marketOrderTx.recentBlockhash = currentBlockHash;
@@ -63,7 +63,7 @@ class EventSubscriber {
63
63
  this.awaitTxResolver.get(txSig)();
64
64
  this.awaitTxResolver.delete(txSig);
65
65
  }
66
- if (slot > this.lastSeenSlot) {
66
+ if (!this.lastSeenSlot || slot > this.lastSeenSlot) {
67
67
  this.lastSeenTxSig = txSig;
68
68
  }
69
69
  if (this.lastSeenBlockTime === undefined ||
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.5.0",
2
+ "version": "2.6.0-beta.1",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/lib/math/amm.d.ts CHANGED
@@ -59,8 +59,8 @@ export declare function calculateSpreadBN(baseSpread: number, lastOracleReserveP
59
59
  longSpread: number;
60
60
  shortSpread: number;
61
61
  };
62
- export declare function calculateSpread(amm: AMM, oraclePriceData: OraclePriceData): [number, number];
63
- export declare function calculateSpreadReserves(amm: AMM, oraclePriceData: OraclePriceData): {
62
+ export declare function calculateSpread(amm: AMM, oraclePriceData: OraclePriceData, now?: BN): [number, number];
63
+ export declare function calculateSpreadReserves(amm: AMM, oraclePriceData: OraclePriceData, now?: BN): {
64
64
  baseAssetReserve: any;
65
65
  quoteAssetReserve: any;
66
66
  }[];
@@ -88,6 +88,6 @@ export declare function getSwapDirection(inputAssetType: AssetType, positionDire
88
88
  * @returns cost : Precision PRICE_PRECISION
89
89
  */
90
90
  export declare function calculateTerminalPrice(market: PerpMarketAccount): BN;
91
- export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, oraclePriceData?: OraclePriceData): [BN, PositionDirection];
91
+ export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, oraclePriceData?: OraclePriceData, now?: BN): [BN, PositionDirection];
92
92
  export declare function calculateQuoteAssetAmountSwapped(quoteAssetReserves: BN, pegMultiplier: BN, swapDirection: SwapDirection): BN;
93
93
  export declare function calculateMaxBaseAssetAmountFillable(amm: AMM, orderDirection: PositionDirection): BN;
package/lib/math/amm.js CHANGED
@@ -365,7 +365,7 @@ function calculateSpreadBN(baseSpread, lastOracleReservePriceSpreadPct, lastOrac
365
365
  return [longSpread, shortSpread];
366
366
  }
367
367
  exports.calculateSpreadBN = calculateSpreadBN;
368
- function calculateSpread(amm, oraclePriceData) {
368
+ function calculateSpread(amm, oraclePriceData, now) {
369
369
  if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
370
370
  return [amm.baseSpread / 2, amm.baseSpread / 2];
371
371
  }
@@ -379,7 +379,7 @@ function calculateSpread(amm, oraclePriceData) {
379
379
  const confIntervalPct = confInterval
380
380
  .mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
381
381
  .div(reservePrice);
382
- const now = new anchor_1.BN(new Date().getTime() / 1000); //todo
382
+ now = now || new anchor_1.BN(new Date().getTime() / 1000); //todo
383
383
  const liveOracleStd = (0, oracles_1.calculateLiveOracleStd)(amm, oraclePriceData, now);
384
384
  const spreads = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.baseAssetAmountWithAmm, reservePrice, amm.totalFeeMinusDistributions, amm.netRevenueSinceLastFunding, amm.baseAssetReserve, amm.minBaseAssetReserve, amm.maxBaseAssetReserve, amm.markStd, liveOracleStd, amm.longIntensityVolume, amm.shortIntensityVolume, amm.volume24H);
385
385
  const longSpread = spreads[0];
@@ -387,7 +387,7 @@ function calculateSpread(amm, oraclePriceData) {
387
387
  return [longSpread, shortSpread];
388
388
  }
389
389
  exports.calculateSpread = calculateSpread;
390
- function calculateSpreadReserves(amm, oraclePriceData) {
390
+ function calculateSpreadReserves(amm, oraclePriceData, now) {
391
391
  function calculateSpreadReserve(spread, direction, amm) {
392
392
  if (spread === 0) {
393
393
  return {
@@ -409,7 +409,7 @@ function calculateSpreadReserves(amm, oraclePriceData) {
409
409
  quoteAssetReserve,
410
410
  };
411
411
  }
412
- const [longSpread, shortSpread] = calculateSpread(amm, oraclePriceData);
412
+ const [longSpread, shortSpread] = calculateSpread(amm, oraclePriceData, now);
413
413
  const askReserves = calculateSpreadReserve(longSpread, types_1.PositionDirection.LONG, amm);
414
414
  const bidReserves = calculateSpreadReserve(shortSpread, types_1.PositionDirection.SHORT, amm);
415
415
  return [bidReserves, askReserves];
@@ -471,7 +471,7 @@ function calculateTerminalPrice(market) {
471
471
  return terminalPrice;
472
472
  }
473
473
  exports.calculateTerminalPrice = calculateTerminalPrice;
474
- function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oraclePriceData) {
474
+ function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oraclePriceData, now) {
475
475
  const invariant = amm.sqrtK.mul(amm.sqrtK);
476
476
  const newBaseAssetReserveSquared = invariant
477
477
  .mul(numericConstants_1.PRICE_PRECISION)
@@ -479,7 +479,7 @@ function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oracleP
479
479
  .div(limit_price)
480
480
  .div(numericConstants_1.PEG_PRECISION);
481
481
  const newBaseAssetReserve = (0, __1.squareRootBN)(newBaseAssetReserveSquared);
482
- const [shortSpreadReserves, longSpreadReserves] = calculateSpreadReserves(amm, oraclePriceData);
482
+ const [shortSpreadReserves, longSpreadReserves] = calculateSpreadReserves(amm, oraclePriceData, now);
483
483
  const baseAssetReserveBefore = (0, types_1.isVariant)(direction, 'long')
484
484
  ? longSpreadReserves.baseAssetReserve
485
485
  : shortSpreadReserves.baseAssetReserve;
@@ -5,6 +5,7 @@ const types_1 = require("../types");
5
5
  const anchor_1 = require("@project-serum/anchor");
6
6
  const numericConstants_1 = require("../constants/numericConstants");
7
7
  const margin_1 = require("./margin");
8
+ const numericConstants_2 = require("../constants/numericConstants");
8
9
  function getBalance(tokenAmount, spotMarket, balanceType) {
9
10
  const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
10
11
  const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
@@ -139,8 +140,10 @@ function calculateDepositRate(bank) {
139
140
  const utilization = calculateUtilization(bank);
140
141
  const borrowRate = calculateBorrowRate(bank);
141
142
  const depositRate = borrowRate
143
+ .mul(numericConstants_2.PERCENTAGE_PRECISION.sub(new anchor_1.BN(bank.insuranceFund.totalFactor)))
142
144
  .mul(utilization)
143
- .div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION);
145
+ .div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
146
+ .div(numericConstants_2.PERCENTAGE_PRECISION);
144
147
  return depositRate;
145
148
  }
146
149
  exports.calculateDepositRate = calculateDepositRate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.5.0",
3
+ "version": "2.6.0-beta.1",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -29,7 +29,8 @@ export abstract class OrderNode implements DLOBNode {
29
29
  haveTrigger = false;
30
30
 
31
31
  constructor(order: Order, userAccount: PublicKey) {
32
- this.order = order;
32
+ // Copy the order over to the node
33
+ this.order = { ...order };
33
34
  this.userAccount = userAccount;
34
35
  this.sortValue = this.getSortValue(order);
35
36
  }
@@ -187,6 +187,9 @@
187
187
  },
188
188
  {
189
189
  "name": "TriggerLimit"
190
+ },
191
+ {
192
+ "name": "Oracle"
190
193
  }
191
194
  ]
192
195
  }
@@ -1741,22 +1741,29 @@ export class DriftClient {
1741
1741
  * @param orderParams
1742
1742
  * @param userAccountPublicKey
1743
1743
  * @param userAccount
1744
+ * @param makerInfo
1744
1745
  * @returns
1745
1746
  */
1746
1747
  public async sendMarketOrderAndGetSignedFillTx(
1747
1748
  orderParams: OptionalOrderParams,
1748
1749
  userAccountPublicKey: PublicKey,
1749
- userAccount: UserAccount
1750
+ userAccount: UserAccount,
1751
+ makerInfo?: MakerInfo
1750
1752
  ): Promise<{ txSig: TransactionSignature; signedFillTx: Transaction }> {
1751
1753
  const marketIndex = orderParams.marketIndex;
1752
1754
  const orderId = userAccount.nextOrderId;
1753
1755
 
1754
1756
  const marketOrderTx = wrapInTx(await this.getPlacePerpOrderIx(orderParams));
1755
1757
  const fillTx = wrapInTx(
1756
- await this.getFillPerpOrderIx(userAccountPublicKey, userAccount, {
1757
- orderId,
1758
- marketIndex,
1759
- })
1758
+ await this.getFillPerpOrderIx(
1759
+ userAccountPublicKey,
1760
+ userAccount,
1761
+ {
1762
+ orderId,
1763
+ marketIndex,
1764
+ },
1765
+ makerInfo
1766
+ )
1760
1767
  );
1761
1768
 
1762
1769
  // Apply the latest blockhash to the txs so that we can sign before sending them
@@ -109,7 +109,7 @@ export class EventSubscriber {
109
109
  this.awaitTxResolver.delete(txSig);
110
110
  }
111
111
 
112
- if (slot > this.lastSeenSlot) {
112
+ if (!this.lastSeenSlot || slot > this.lastSeenSlot) {
113
113
  this.lastSeenTxSig = txSig;
114
114
  }
115
115
 
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.5.0",
2
+ "version": "2.6.0-beta.1",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/src/math/amm.ts CHANGED
@@ -655,7 +655,8 @@ export function calculateSpreadBN(
655
655
 
656
656
  export function calculateSpread(
657
657
  amm: AMM,
658
- oraclePriceData: OraclePriceData
658
+ oraclePriceData: OraclePriceData,
659
+ now?: BN
659
660
  ): [number, number] {
660
661
  if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
661
662
  return [amm.baseSpread / 2, amm.baseSpread / 2];
@@ -678,7 +679,7 @@ export function calculateSpread(
678
679
  .mul(BID_ASK_SPREAD_PRECISION)
679
680
  .div(reservePrice);
680
681
 
681
- const now = new BN(new Date().getTime() / 1000); //todo
682
+ now = now || new BN(new Date().getTime() / 1000); //todo
682
683
  const liveOracleStd = calculateLiveOracleStd(amm, oraclePriceData, now);
683
684
 
684
685
  const spreads = calculateSpreadBN(
@@ -710,7 +711,8 @@ export function calculateSpread(
710
711
 
711
712
  export function calculateSpreadReserves(
712
713
  amm: AMM,
713
- oraclePriceData: OraclePriceData
714
+ oraclePriceData: OraclePriceData,
715
+ now?: BN
714
716
  ) {
715
717
  function calculateSpreadReserve(
716
718
  spread: number,
@@ -745,7 +747,7 @@ export function calculateSpreadReserves(
745
747
  };
746
748
  }
747
749
 
748
- const [longSpread, shortSpread] = calculateSpread(amm, oraclePriceData);
750
+ const [longSpread, shortSpread] = calculateSpread(amm, oraclePriceData, now);
749
751
  const askReserves = calculateSpreadReserve(
750
752
  longSpread,
751
753
  PositionDirection.LONG,
@@ -838,7 +840,8 @@ export function calculateMaxBaseAssetAmountToTrade(
838
840
  amm: AMM,
839
841
  limit_price: BN,
840
842
  direction: PositionDirection,
841
- oraclePriceData?: OraclePriceData
843
+ oraclePriceData?: OraclePriceData,
844
+ now?: BN
842
845
  ): [BN, PositionDirection] {
843
846
  const invariant = amm.sqrtK.mul(amm.sqrtK);
844
847
 
@@ -851,7 +854,8 @@ export function calculateMaxBaseAssetAmountToTrade(
851
854
  const newBaseAssetReserve = squareRootBN(newBaseAssetReserveSquared);
852
855
  const [shortSpreadReserves, longSpreadReserves] = calculateSpreadReserves(
853
856
  amm,
854
- oraclePriceData
857
+ oraclePriceData,
858
+ now
855
859
  );
856
860
 
857
861
  const baseAssetReserveBefore: BN = isVariant(direction, 'long')
@@ -20,6 +20,7 @@ import {
20
20
  calculateSizePremiumLiabilityWeight,
21
21
  } from './margin';
22
22
  import { OraclePriceData } from '../oracles/types';
23
+ import { PERCENTAGE_PRECISION } from '../constants/numericConstants';
23
24
 
24
25
  export function getBalance(
25
26
  tokenAmount: BN,
@@ -228,8 +229,10 @@ export function calculateDepositRate(bank: SpotMarketAccount): BN {
228
229
  const utilization = calculateUtilization(bank);
229
230
  const borrowRate = calculateBorrowRate(bank);
230
231
  const depositRate = borrowRate
232
+ .mul(PERCENTAGE_PRECISION.sub(new BN(bank.insuranceFund.totalFactor)))
231
233
  .mul(utilization)
232
- .div(SPOT_MARKET_UTILIZATION_PRECISION);
234
+ .div(SPOT_MARKET_UTILIZATION_PRECISION)
235
+ .div(PERCENTAGE_PRECISION);
233
236
  return depositRate;
234
237
  }
235
238