@drift-labs/sdk 2.45.0-beta.2 → 2.45.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.45.0-beta.2
1
+ 2.45.0-beta.4
@@ -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;
@@ -173,8 +173,8 @@ function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, to
173
173
  if (remainingBaseLiquidity.lt(baseSwapped)) {
174
174
  baseSwapped = remainingBaseLiquidity;
175
175
  [afterSwapQuoteReserves, afterSwapBaseReserves] =
176
- (0, __1.calculateAmmReservesAfterSwap)(bidAmm, 'base', baseSwapped, __1.SwapDirection.REMOVE);
177
- quoteSwapped = (0, __1.calculateQuoteAssetAmountSwapped)(bidAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), bidAmm.pegMultiplier, __1.SwapDirection.REMOVE);
176
+ (0, __1.calculateAmmReservesAfterSwap)(askAmm, 'base', baseSwapped, __1.SwapDirection.REMOVE);
177
+ quoteSwapped = (0, __1.calculateQuoteAssetAmountSwapped)(askAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), askAmm.pegMultiplier, __1.SwapDirection.REMOVE);
178
178
  }
179
179
  topOfBookAskSize = topOfBookAskSize.add(baseSwapped);
180
180
  askSize = openAsks
@@ -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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.45.0-beta.2",
3
+ "version": "2.45.0-beta.4",
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;
@@ -302,15 +302,15 @@ export function getVammL2Generator({
302
302
  baseSwapped = remainingBaseLiquidity;
303
303
  [afterSwapQuoteReserves, afterSwapBaseReserves] =
304
304
  calculateAmmReservesAfterSwap(
305
- bidAmm,
305
+ askAmm,
306
306
  'base',
307
307
  baseSwapped,
308
308
  SwapDirection.REMOVE
309
309
  );
310
310
 
311
311
  quoteSwapped = calculateQuoteAssetAmountSwapped(
312
- bidAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(),
313
- bidAmm.pegMultiplier,
312
+ askAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(),
313
+ askAmm.pegMultiplier,
314
314
  SwapDirection.REMOVE
315
315
  );
316
316
  }
@@ -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(
@@ -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
  });