@drift-labs/sdk 2.45.0-beta.1 → 2.45.0-beta.3

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.45.0-beta.1
1
+ 2.45.0-beta.3
@@ -27,6 +27,7 @@ export declare class BulkAccountLoader {
27
27
  loadChunk(accountsToLoadChunks: AccountToLoad[][]): Promise<void>;
28
28
  handleAccountCallbacks(accountToLoad: AccountToLoad, buffer: Buffer, slot: number): void;
29
29
  getBufferAndSlot(publicKey: PublicKey): BufferAndSlot | undefined;
30
+ getSlot(): number;
30
31
  startPolling(): void;
31
32
  stopPolling(): void;
32
33
  log(msg: string): void;
@@ -186,6 +186,9 @@ class BulkAccountLoader {
186
186
  getBufferAndSlot(publicKey) {
187
187
  return this.bufferAndSlotMap.get(publicKey.toString());
188
188
  }
189
+ getSlot() {
190
+ return this.mostRecentSlot;
191
+ }
189
192
  startPolling() {
190
193
  if (this.intervalId) {
191
194
  return;
@@ -63,8 +63,8 @@ export declare function calculateSpotMarketBorrowCapacity(spotMarketAccount: Spo
63
63
  remainingCapacity: BN;
64
64
  };
65
65
  export declare function calculateInterestRate(bank: SpotMarketAccount, delta?: BN): BN;
66
- export declare function calculateDepositRate(bank: SpotMarketAccount): BN;
67
- export declare function calculateBorrowRate(bank: SpotMarketAccount): BN;
66
+ export declare function calculateDepositRate(bank: SpotMarketAccount, delta?: BN): BN;
67
+ export declare function calculateBorrowRate(bank: SpotMarketAccount, delta?: BN): BN;
68
68
  export declare function calculateInterestAccumulated(bank: SpotMarketAccount, now: BN): {
69
69
  borrowInterest: BN;
70
70
  depositInterest: BN;
@@ -263,9 +263,11 @@ function calculateInterestRate(bank, delta = numericConstants_1.ZERO) {
263
263
  return interestRate;
264
264
  }
265
265
  exports.calculateInterestRate = calculateInterestRate;
266
- function calculateDepositRate(bank) {
267
- const utilization = calculateUtilization(bank);
268
- const borrowRate = calculateBorrowRate(bank);
266
+ function calculateDepositRate(bank, delta = numericConstants_1.ZERO) {
267
+ // positive delta => adding to deposit
268
+ // negative delta => adding to borrow
269
+ const utilization = calculateUtilization(bank, delta);
270
+ const borrowRate = calculateBorrowRate(bank, delta);
269
271
  const depositRate = borrowRate
270
272
  .mul(numericConstants_2.PERCENTAGE_PRECISION.sub(new anchor_1.BN(bank.insuranceFund.totalFactor)))
271
273
  .mul(utilization)
@@ -274,8 +276,8 @@ function calculateDepositRate(bank) {
274
276
  return depositRate;
275
277
  }
276
278
  exports.calculateDepositRate = calculateDepositRate;
277
- function calculateBorrowRate(bank) {
278
- return calculateInterestRate(bank);
279
+ function calculateBorrowRate(bank, delta = numericConstants_1.ZERO) {
280
+ return calculateInterestRate(bank, delta);
279
281
  }
280
282
  exports.calculateBorrowRate = calculateBorrowRate;
281
283
  function calculateInterestAccumulated(bank, now) {
@@ -36,7 +36,7 @@ export declare class PriorityFeeCalculator {
36
36
  /**
37
37
  * This method generates a list of transaction instructions for the ComputeBudget program, and includes a priority fee if it's required
38
38
  * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
39
- * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered()`.
39
+ * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered`.
40
40
  * @param additionalFeeMicroLamports - The additional fee to be paid, in micro lamports, the actual price will be calculated.
41
41
  * @returns An array of transaction instructions.
42
42
  */
@@ -67,7 +67,7 @@ class PriorityFeeCalculator {
67
67
  /**
68
68
  * This method generates a list of transaction instructions for the ComputeBudget program, and includes a priority fee if it's required
69
69
  * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
70
- * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered()`.
70
+ * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered`.
71
71
  * @param additionalFeeMicroLamports - The additional fee to be paid, in micro lamports, the actual price will be calculated.
72
72
  * @returns An array of transaction instructions.
73
73
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.45.0-beta.1",
3
+ "version": "2.45.0-beta.3",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -249,6 +249,10 @@ export class BulkAccountLoader {
249
249
  return this.bufferAndSlotMap.get(publicKey.toString());
250
250
  }
251
251
 
252
+ public getSlot(): number {
253
+ return this.mostRecentSlot;
254
+ }
255
+
252
256
  public startPolling(): void {
253
257
  if (this.intervalId) {
254
258
  return;
@@ -398,9 +398,15 @@ export function calculateInterestRate(
398
398
  return interestRate;
399
399
  }
400
400
 
401
- export function calculateDepositRate(bank: SpotMarketAccount): BN {
402
- const utilization = calculateUtilization(bank);
403
- const borrowRate = calculateBorrowRate(bank);
401
+ export function calculateDepositRate(
402
+ bank: SpotMarketAccount,
403
+ delta = ZERO
404
+ ): BN {
405
+ // positive delta => adding to deposit
406
+ // negative delta => adding to borrow
407
+
408
+ const utilization = calculateUtilization(bank, delta);
409
+ const borrowRate = calculateBorrowRate(bank, delta);
404
410
  const depositRate = borrowRate
405
411
  .mul(PERCENTAGE_PRECISION.sub(new BN(bank.insuranceFund.totalFactor)))
406
412
  .mul(utilization)
@@ -409,8 +415,8 @@ export function calculateDepositRate(bank: SpotMarketAccount): BN {
409
415
  return depositRate;
410
416
  }
411
417
 
412
- export function calculateBorrowRate(bank: SpotMarketAccount): BN {
413
- return calculateInterestRate(bank);
418
+ export function calculateBorrowRate(bank: SpotMarketAccount, delta = ZERO): BN {
419
+ return calculateInterestRate(bank, delta);
414
420
  }
415
421
 
416
422
  export function calculateInterestAccumulated(
@@ -89,7 +89,7 @@ export class PriorityFeeCalculator {
89
89
  /**
90
90
  * This method generates a list of transaction instructions for the ComputeBudget program, and includes a priority fee if it's required
91
91
  * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
92
- * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered()`.
92
+ * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered`.
93
93
  * @param additionalFeeMicroLamports - The additional fee to be paid, in micro lamports, the actual price will be calculated.
94
94
  * @returns An array of transaction instructions.
95
95
  */
@@ -4,6 +4,8 @@ import {
4
4
  calculateSpotMarketBorrowCapacity,
5
5
  SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
6
6
  calculateSizePremiumLiabilityWeight,
7
+ calculateBorrowRate,
8
+ calculateDepositRate,
7
9
  } from '../../src';
8
10
  import { mockSpotMarkets } from '../dlob/helpers';
9
11
  import * as _ from 'lodash';
@@ -172,4 +174,53 @@ describe('Spot Tests', () => {
172
174
  // console.log('belowOptAmount3:', belowOptAmount4.toNumber());
173
175
  assert(belowOptAmount4.eq(new BN('0')));
174
176
  });
177
+
178
+ it('borrow rates', () => {
179
+ const mockSpot = _.cloneDeep(mockSpotMarkets[0]);
180
+ mockSpot.maxBorrowRate = 1000000;
181
+ mockSpot.optimalBorrowRate = 70000;
182
+ mockSpot.optimalUtilization = 700000;
183
+
184
+ mockSpot.decimals = 9;
185
+ mockSpot.cumulativeDepositInterest = new BN(
186
+ 1.0154217042 * SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION.toNumber()
187
+ );
188
+ mockSpot.cumulativeBorrowInterest = new BN(
189
+ 1.0417153549 * SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION.toNumber()
190
+ );
191
+
192
+ mockSpot.depositBalance = new BN(88522.734106451 * 1e9);
193
+ mockSpot.borrowBalance = new BN(17089.91675884 * 1e9);
194
+
195
+ const noDeltad = calculateDepositRate(mockSpot);
196
+ // console.log(noDeltad.toNumber());
197
+ assert(noDeltad.eqn(3922));
198
+ const noDelta = calculateBorrowRate(mockSpot);
199
+ // console.log(noDelta.toNumber());
200
+ assert(noDelta.eqn(19805));
201
+
202
+ // manually update deposits
203
+ mockSpot.depositBalance = new BN((88522.734106451 + 9848.12512736) * 1e9);
204
+ const noDeltad2 = calculateDepositRate(mockSpot);
205
+ console.log(noDeltad2.toNumber());
206
+ assert(noDeltad2.eqn(3176));
207
+ const noDelta2 = calculateBorrowRate(mockSpot);
208
+ console.log(noDelta2.toNumber());
209
+ assert(noDelta2.eqn(17822));
210
+
211
+ mockSpot.depositBalance = new BN(88522.734106451 * 1e9);
212
+ const addDep1d = calculateDepositRate(mockSpot, new BN(10000 * 1e9));
213
+ // console.log(addDep1d.toNumber());
214
+ assert(addDep1d.eqn(3176)); // went down
215
+ const addDep1 = calculateBorrowRate(mockSpot, new BN(10000 * 1e9));
216
+ // console.log(addDep1.toNumber());
217
+ assert(addDep1.eqn(17822)); // went down
218
+
219
+ const addBord1 = calculateDepositRate(mockSpot, new BN(-1000 * 1e9));
220
+ // console.log(addBord1.toNumber());
221
+ assert(addBord1.eqn(4375)); // went up
222
+ const addBor1 = calculateBorrowRate(mockSpot, new BN(-1000 * 1e9));
223
+ // console.log(addBor1.toNumber());
224
+ assert(addBor1.eqn(20918)); // went up
225
+ });
175
226
  });