@drift-labs/sdk 2.38.1-beta.8 → 2.39.1-beta.0

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.
@@ -21,7 +21,7 @@ import {
21
21
  OrderRecord,
22
22
  ZERO,
23
23
  ContractTier,
24
- SPOT_MARKET_BALANCE_PRECISION,
24
+ SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION
25
25
  } from '../../src';
26
26
 
27
27
  export const mockPerpPosition: PerpPosition = {
@@ -288,8 +288,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
288
288
  optimalUtilization: 0,
289
289
  optimalBorrowRate: 0,
290
290
  maxBorrowRate: 0,
291
- cumulativeDepositInterest: SPOT_MARKET_BALANCE_PRECISION,
292
- cumulativeBorrowInterest: SPOT_MARKET_BALANCE_PRECISION,
291
+ cumulativeDepositInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
292
+ cumulativeBorrowInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
293
293
  totalSocialLoss: new BN(0),
294
294
  totalQuoteSocialLoss: new BN(0),
295
295
  depositBalance: new BN(0),
@@ -301,6 +301,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
301
301
  maintenanceAssetWeight: 0,
302
302
  initialLiabilityWeight: 0,
303
303
  maintenanceLiabilityWeight: 0,
304
+ scaleInitialAssetWeightStart: new BN(0),
304
305
  imfFactor: 0,
305
306
  withdrawGuardThreshold: new BN(0),
306
307
  depositTokenTwap: new BN(0),
@@ -368,8 +369,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
368
369
  optimalUtilization: 0,
369
370
  optimalBorrowRate: 0,
370
371
  maxBorrowRate: 0,
371
- cumulativeDepositInterest: new BN(0),
372
- cumulativeBorrowInterest: new BN(0),
372
+ cumulativeDepositInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
373
+ cumulativeBorrowInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
373
374
  totalSocialLoss: new BN(0),
374
375
  totalQuoteSocialLoss: new BN(0),
375
376
  depositBalance: new BN(0),
@@ -381,6 +382,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
381
382
  maintenanceAssetWeight: 0,
382
383
  initialLiabilityWeight: 0,
383
384
  maintenanceLiabilityWeight: 0,
385
+ scaleInitialAssetWeightStart: new BN(0),
384
386
  imfFactor: 0,
385
387
  withdrawGuardThreshold: new BN(0),
386
388
  depositTokenTwap: new BN(0),
@@ -448,8 +450,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
448
450
  optimalUtilization: 0,
449
451
  optimalBorrowRate: 0,
450
452
  maxBorrowRate: 0,
451
- cumulativeDepositInterest: new BN(0),
452
- cumulativeBorrowInterest: new BN(0),
453
+ cumulativeDepositInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
454
+ cumulativeBorrowInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
453
455
  totalSocialLoss: new BN(0),
454
456
  totalQuoteSocialLoss: new BN(0),
455
457
  depositBalance: new BN(0),
@@ -461,6 +463,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
461
463
  maintenanceAssetWeight: 0,
462
464
  initialLiabilityWeight: 0,
463
465
  maintenanceLiabilityWeight: 0,
466
+ scaleInitialAssetWeightStart: new BN(0),
464
467
  imfFactor: 0,
465
468
  withdrawGuardThreshold: new BN(0),
466
469
  depositTokenTwap: new BN(0),
@@ -0,0 +1,77 @@
1
+ import { expect } from 'chai';
2
+ import { PriorityFeeCalculator } from '../../src/tx/priorityFeeCalculator';
3
+
4
+ describe('PriorityFeeCalculator', () => {
5
+ let priorityFeeCalculator: PriorityFeeCalculator;
6
+
7
+ const startTime = 1000000;
8
+ const latch_duration = 10_000;
9
+
10
+ beforeEach(() => {
11
+ priorityFeeCalculator = new PriorityFeeCalculator(
12
+ startTime,
13
+ latch_duration
14
+ );
15
+ });
16
+
17
+ it('should trigger priority fee when timeout count increases', () => {
18
+ const timeoutCount = 1;
19
+ expect(priorityFeeCalculator.updatePriorityFee(startTime, timeoutCount)).to
20
+ .be.true;
21
+ expect(
22
+ priorityFeeCalculator.updatePriorityFee(
23
+ startTime + latch_duration,
24
+ timeoutCount + 1
25
+ )
26
+ ).to.be.true;
27
+ expect(
28
+ priorityFeeCalculator.updatePriorityFee(
29
+ startTime + latch_duration,
30
+ timeoutCount + 2
31
+ )
32
+ ).to.be.true;
33
+ });
34
+
35
+ it('should trigger priority fee when timeout count increases, and stay latched until latch duration', () => {
36
+ const timeoutCount = 1;
37
+ expect(priorityFeeCalculator.updatePriorityFee(startTime, timeoutCount)).to
38
+ .be.true;
39
+ expect(
40
+ priorityFeeCalculator.updatePriorityFee(
41
+ startTime + latch_duration / 2,
42
+ timeoutCount
43
+ )
44
+ ).to.be.true;
45
+ expect(
46
+ priorityFeeCalculator.updatePriorityFee(
47
+ startTime + latch_duration - 1,
48
+ timeoutCount
49
+ )
50
+ ).to.be.true;
51
+ expect(
52
+ priorityFeeCalculator.updatePriorityFee(
53
+ startTime + latch_duration * 2,
54
+ timeoutCount
55
+ )
56
+ ).to.be.false;
57
+ });
58
+
59
+ it('should not trigger priority fee when timeout count does not increase', () => {
60
+ const timeoutCount = 0;
61
+ expect(priorityFeeCalculator.updatePriorityFee(startTime, timeoutCount)).to
62
+ .be.false;
63
+ });
64
+
65
+ it('should correctly calculate compute unit price', () => {
66
+ const computeUnitLimit = 1_000_000;
67
+ const additionalFeeMicroLamports = 1_000_000_000; // 1000 lamports
68
+ const actualComputeUnitPrice =
69
+ priorityFeeCalculator.calculateComputeUnitPrice(
70
+ computeUnitLimit,
71
+ additionalFeeMicroLamports
72
+ );
73
+ expect(actualComputeUnitPrice * computeUnitLimit).to.equal(
74
+ additionalFeeMicroLamports
75
+ );
76
+ });
77
+ });
@@ -12,10 +12,15 @@ import {
12
12
  QUOTE_PRECISION,
13
13
  calculatePositionPNL,
14
14
  SPOT_MARKET_BALANCE_PRECISION,
15
+ getWorstCaseTokenAmounts,
16
+ StrictOraclePrice,
17
+ LAMPORTS_PRECISION,
18
+ SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
19
+ SpotBalanceType
15
20
  } from '../../src';
16
21
  import { MockUserMap, mockPerpMarkets, mockSpotMarkets } from '../dlob/helpers';
17
22
  import { assert } from '../../src/assert/assert';
18
- import { mockUserAccount } from './helpers';
23
+ import {mockUserAccount} from './helpers';
19
24
  import * as _ from 'lodash';
20
25
 
21
26
  async function makeMockUser(
@@ -222,8 +227,8 @@ describe('User Tests', () => {
222
227
 
223
228
  myMockPerpMarkets[0].imfFactor = 550;
224
229
  myMockUserAccount.spotPositions[0].scaledBalance = new BN(
225
- 100000 * SPOT_MARKET_BALANCE_PRECISION.toNumber()
226
- ); //100k
230
+ 10000 * SPOT_MARKET_BALANCE_PRECISION.toNumber()
231
+ ); //10k
227
232
 
228
233
  const mockUser: User = await makeMockUser(
229
234
  myMockPerpMarkets,
@@ -241,7 +246,7 @@ describe('User Tests', () => {
241
246
 
242
247
  assert(
243
248
  uA.spotPositions[0].scaledBalance.eq(
244
- new BN(100000 * SPOT_MARKET_BALANCE_PRECISION.toNumber())
249
+ new BN(10000 * SPOT_MARKET_BALANCE_PRECISION.toNumber())
245
250
  )
246
251
  );
247
252
  for (let i = 1; i < 8; i++) {
@@ -283,4 +288,72 @@ describe('User Tests', () => {
283
288
  );
284
289
  assert(mockUser.getMaxLeverageForPerp(0).eq(new BN('0')));
285
290
  });
291
+
292
+ it('worst case token amount', async () => {
293
+ const myMockUserAccount = _.cloneDeep(mockUserAccount);
294
+
295
+ const solMarket = Object.assign({}, _.cloneDeep(mockSpotMarkets[1]), {
296
+ initialAssetWeight: 8000,
297
+ initialLiabilityWeight: 12000,
298
+ cumulativeDepositInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
299
+ cumulativeBorrowInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
300
+ });
301
+
302
+ const strictOraclePrice = new StrictOraclePrice(PRICE_PRECISION.muln(100));
303
+
304
+ let spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
305
+ marketIndex: 1,
306
+ openBids: new BN(100).mul( LAMPORTS_PRECISION),
307
+ });
308
+
309
+ let worstCase = getWorstCaseTokenAmounts(spotPosition, solMarket, strictOraclePrice, 'Initial');
310
+
311
+ assert(worstCase.tokenAmount.eq(new BN(100).mul( LAMPORTS_PRECISION))); // 100
312
+ assert(worstCase.tokenValue.eq(new BN(10000).mul(PRICE_PRECISION))); // $10k
313
+ assert(worstCase.weightedTokenValue.eq(new BN(8000).mul(PRICE_PRECISION))); // $8k
314
+ assert(worstCase.ordersValue.eq(new BN(-10000).mul(PRICE_PRECISION))); // -$10k
315
+ assert(worstCase.freeCollateralContribution.eq(new BN(-2000).mul(QUOTE_PRECISION))); // -$2k
316
+
317
+ spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
318
+ marketIndex: 1,
319
+ scaledBalance: new BN(100).mul(SPOT_MARKET_BALANCE_PRECISION),
320
+ openBids: new BN(100).mul( LAMPORTS_PRECISION),
321
+ });
322
+
323
+ worstCase = getWorstCaseTokenAmounts(spotPosition, solMarket, strictOraclePrice, 'Initial');
324
+
325
+ assert(worstCase.tokenAmount.eq(new BN(200).mul(LAMPORTS_PRECISION))); // 200
326
+ assert(worstCase.tokenValue.eq(new BN(20000).mul(PRICE_PRECISION))); // $20k
327
+ assert(worstCase.weightedTokenValue.eq(new BN(16000).mul(PRICE_PRECISION))); // $16k
328
+ assert(worstCase.ordersValue.eq(new BN(-10000).mul(PRICE_PRECISION))); // -$10k
329
+ assert(worstCase.freeCollateralContribution.eq(new BN(6000).mul(QUOTE_PRECISION))); // $6k
330
+
331
+ spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
332
+ marketIndex: 1,
333
+ openAsks: new BN(-100).mul( LAMPORTS_PRECISION),
334
+ });
335
+
336
+ worstCase = getWorstCaseTokenAmounts(spotPosition, solMarket, strictOraclePrice, 'Initial');
337
+
338
+ assert(worstCase.tokenAmount.eq(new BN(-100).mul( LAMPORTS_PRECISION)));
339
+ assert(worstCase.tokenValue.eq(new BN(-10000).mul(PRICE_PRECISION))); // -$10k
340
+ assert(worstCase.weightedTokenValue.eq(new BN(-12000).mul(PRICE_PRECISION))); // -$12k
341
+ assert(worstCase.ordersValue.eq(new BN(10000).mul(PRICE_PRECISION))); // $10k
342
+ assert(worstCase.freeCollateralContribution.eq(new BN(-2000).mul(QUOTE_PRECISION))); // -$2k
343
+
344
+ spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
345
+ marketIndex: 1,
346
+ balanceType: SpotBalanceType.BORROW,
347
+ scaledBalance: new BN(100).mul(SPOT_MARKET_BALANCE_PRECISION),
348
+ openAsks: new BN(-100).mul( LAMPORTS_PRECISION),
349
+ });
350
+
351
+ worstCase = getWorstCaseTokenAmounts(spotPosition, solMarket, strictOraclePrice, 'Initial');
352
+
353
+ assert(worstCase.tokenAmount.eq(new BN(-200).mul( LAMPORTS_PRECISION)));
354
+ assert(worstCase.tokenValue.eq(new BN(-20000).mul(PRICE_PRECISION))); // -$20k
355
+ assert(worstCase.weightedTokenValue.eq(new BN(-24000).mul(PRICE_PRECISION))); // -$24k
356
+ assert(worstCase.ordersValue.eq(new BN(10000).mul(PRICE_PRECISION))); // $10k
357
+ assert(worstCase.freeCollateralContribution.eq(new BN(-14000).mul(QUOTE_PRECISION))); // -$2k
358
+ });
286
359
  });