@drift-labs/sdk 2.42.0-beta.0 → 2.42.0-beta.10
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 +1 -1
- package/bun.lockb +0 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/adminClient.d.ts +1 -0
- package/lib/constants/numericConstants.d.ts +62 -59
- package/lib/constants/numericConstants.js +2 -1
- package/lib/constants/spotMarkets.d.ts +1 -0
- package/lib/dlob/DLOB.d.ts +10 -16
- package/lib/dlob/DLOB.js +9 -39
- package/lib/dlob/DLOBNode.d.ts +1 -0
- package/lib/dlob/NodeList.d.ts +1 -0
- package/lib/dlob/orderBookLevels.d.ts +2 -1
- package/lib/driftClient.d.ts +12 -4
- package/lib/driftClient.js +51 -38
- package/lib/factory/bigNum.d.ts +8 -7
- package/lib/jupiter/jupiterClient.d.ts +1 -0
- package/lib/marinade/index.d.ts +1 -0
- package/lib/math/amm.d.ts +2 -1
- package/lib/math/auction.d.ts +1 -0
- package/lib/math/conversion.d.ts +2 -1
- package/lib/math/funding.d.ts +1 -0
- package/lib/math/funding.js +2 -1
- package/lib/math/insurance.d.ts +1 -0
- package/lib/math/margin.d.ts +1 -0
- package/lib/math/market.d.ts +2 -1
- package/lib/math/market.js +3 -2
- package/lib/math/oracles.d.ts +1 -0
- package/lib/math/orders.d.ts +1 -0
- package/lib/math/position.d.ts +1 -0
- package/lib/math/repeg.d.ts +1 -0
- package/lib/math/spotBalance.d.ts +3 -2
- package/lib/math/spotMarket.d.ts +2 -1
- package/lib/math/spotMarket.js +9 -3
- package/lib/math/spotPosition.d.ts +4 -3
- package/lib/math/spotPosition.js +18 -7
- package/lib/math/superStake.d.ts +1 -0
- package/lib/math/trade.d.ts +1 -0
- package/lib/math/utils.d.ts +1 -0
- package/lib/oracles/pythClient.d.ts +2 -1
- package/lib/oracles/strictOraclePrice.d.ts +1 -0
- package/lib/oracles/types.d.ts +1 -0
- package/lib/orderParams.d.ts +1 -0
- package/lib/phoenix/phoenixSubscriber.d.ts +1 -0
- package/lib/serum/serumSubscriber.d.ts +1 -0
- package/lib/tokenFaucet.d.ts +1 -0
- package/lib/types.d.ts +7 -0
- package/lib/types.js +2 -0
- package/lib/user.d.ts +15 -3
- package/lib/user.js +85 -28
- package/package.json +2 -1
- package/src/constants/numericConstants.ts +1 -0
- package/src/dlob/DLOB.ts +23 -67
- package/src/driftClient.ts +79 -53
- package/src/math/funding.ts +6 -1
- package/src/math/market.ts +12 -7
- package/src/math/spotMarket.ts +13 -3
- package/src/math/spotPosition.ts +29 -7
- package/src/types.ts +2 -0
- package/src/user.ts +136 -34
- package/tests/amm/test.ts +7 -5
- package/tests/auctions/test.ts +22 -11
- package/tests/dlob/helpers.ts +15 -13
- package/tests/tx/priorityFeeCalculator.ts +1 -1
- package/tests/user/test.ts +171 -24
package/tests/user/test.ts
CHANGED
|
@@ -16,11 +16,12 @@ import {
|
|
|
16
16
|
StrictOraclePrice,
|
|
17
17
|
LAMPORTS_PRECISION,
|
|
18
18
|
SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
|
|
19
|
-
SpotBalanceType
|
|
19
|
+
SpotBalanceType,
|
|
20
|
+
MARGIN_PRECISION,
|
|
20
21
|
} from '../../src';
|
|
21
22
|
import { MockUserMap, mockPerpMarkets, mockSpotMarkets } from '../dlob/helpers';
|
|
22
23
|
import { assert } from '../../src/assert/assert';
|
|
23
|
-
import {mockUserAccount} from './helpers';
|
|
24
|
+
import { mockUserAccount } from './helpers';
|
|
24
25
|
import * as _ from 'lodash';
|
|
25
26
|
|
|
26
27
|
async function makeMockUser(
|
|
@@ -260,15 +261,16 @@ describe('User Tests', () => {
|
|
|
260
261
|
'spot cumulativeDepositInterest:',
|
|
261
262
|
mockSpotMarkets[0].cumulativeDepositInterest.toString()
|
|
262
263
|
);
|
|
263
|
-
|
|
264
|
-
assert(mockUser.
|
|
264
|
+
const expectedAmount = new BN('10000000000');
|
|
265
|
+
assert(mockUser.getTokenAmount(0).eq(expectedAmount));
|
|
266
|
+
assert(mockUser.getNetSpotMarketValue().eq(expectedAmount));
|
|
265
267
|
assert(
|
|
266
268
|
mockUser
|
|
267
269
|
.getSpotMarketAssetAndLiabilityValue()
|
|
268
270
|
.totalLiabilityValue.eq(ZERO)
|
|
269
271
|
);
|
|
270
272
|
|
|
271
|
-
assert(mockUser.getFreeCollateral().
|
|
273
|
+
assert(mockUser.getFreeCollateral().gt(ZERO));
|
|
272
274
|
const upnl = mockUser.getUnrealizedPNL(true, 0, undefined, false);
|
|
273
275
|
console.log('upnl:', upnl.toString());
|
|
274
276
|
assert(upnl.eq(new BN('0'))); // $10
|
|
@@ -277,7 +279,7 @@ describe('User Tests', () => {
|
|
|
277
279
|
console.log(liqResult);
|
|
278
280
|
assert(liqResult.canBeLiquidated == false);
|
|
279
281
|
assert(liqResult.marginRequirement.eq(new BN('0'))); //10x maint leverage
|
|
280
|
-
assert(liqResult.totalCollateral.eq(
|
|
282
|
+
assert(liqResult.totalCollateral.eq(expectedAmount));
|
|
281
283
|
|
|
282
284
|
console.log(mockUser.getHealth());
|
|
283
285
|
assert(mockUser.getHealth() == 100);
|
|
@@ -286,7 +288,9 @@ describe('User Tests', () => {
|
|
|
286
288
|
'getMaxLeverageForPerp:',
|
|
287
289
|
mockUser.getMaxLeverageForPerp(0).toString()
|
|
288
290
|
);
|
|
289
|
-
assert(mockUser.getMaxLeverageForPerp(0).eq(new BN('
|
|
291
|
+
assert(mockUser.getMaxLeverageForPerp(0).eq(new BN('50000'))); // 5x
|
|
292
|
+
assert(mockUser.getMaxLeverageForPerp(0, 'Maintenance').eq(new BN('100000'))); // 10x
|
|
293
|
+
|
|
290
294
|
});
|
|
291
295
|
|
|
292
296
|
it('worst case token amount', async () => {
|
|
@@ -303,57 +307,200 @@ describe('User Tests', () => {
|
|
|
303
307
|
|
|
304
308
|
let spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
|
|
305
309
|
marketIndex: 1,
|
|
306
|
-
openBids: new BN(100).mul(
|
|
310
|
+
openBids: new BN(100).mul(LAMPORTS_PRECISION),
|
|
307
311
|
});
|
|
308
312
|
|
|
309
|
-
let worstCase = getWorstCaseTokenAmounts(
|
|
313
|
+
let worstCase = getWorstCaseTokenAmounts(
|
|
314
|
+
spotPosition,
|
|
315
|
+
solMarket,
|
|
316
|
+
strictOraclePrice,
|
|
317
|
+
'Initial'
|
|
318
|
+
);
|
|
310
319
|
|
|
311
|
-
assert(worstCase.tokenAmount.eq(new BN(100).mul(
|
|
320
|
+
assert(worstCase.tokenAmount.eq(new BN(100).mul(LAMPORTS_PRECISION))); // 100
|
|
312
321
|
assert(worstCase.tokenValue.eq(new BN(10000).mul(PRICE_PRECISION))); // $10k
|
|
313
322
|
assert(worstCase.weightedTokenValue.eq(new BN(8000).mul(PRICE_PRECISION))); // $8k
|
|
314
323
|
assert(worstCase.ordersValue.eq(new BN(-10000).mul(PRICE_PRECISION))); // -$10k
|
|
315
|
-
assert(
|
|
324
|
+
assert(
|
|
325
|
+
worstCase.freeCollateralContribution.eq(
|
|
326
|
+
new BN(-2000).mul(QUOTE_PRECISION)
|
|
327
|
+
)
|
|
328
|
+
); // -$2k
|
|
316
329
|
|
|
317
330
|
spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
|
|
318
331
|
marketIndex: 1,
|
|
319
332
|
scaledBalance: new BN(100).mul(SPOT_MARKET_BALANCE_PRECISION),
|
|
320
|
-
openBids: new BN(100).mul(
|
|
333
|
+
openBids: new BN(100).mul(LAMPORTS_PRECISION),
|
|
321
334
|
});
|
|
322
335
|
|
|
323
|
-
worstCase = getWorstCaseTokenAmounts(
|
|
336
|
+
worstCase = getWorstCaseTokenAmounts(
|
|
337
|
+
spotPosition,
|
|
338
|
+
solMarket,
|
|
339
|
+
strictOraclePrice,
|
|
340
|
+
'Initial'
|
|
341
|
+
);
|
|
324
342
|
|
|
325
343
|
assert(worstCase.tokenAmount.eq(new BN(200).mul(LAMPORTS_PRECISION))); // 200
|
|
326
344
|
assert(worstCase.tokenValue.eq(new BN(20000).mul(PRICE_PRECISION))); // $20k
|
|
327
345
|
assert(worstCase.weightedTokenValue.eq(new BN(16000).mul(PRICE_PRECISION))); // $16k
|
|
328
346
|
assert(worstCase.ordersValue.eq(new BN(-10000).mul(PRICE_PRECISION))); // -$10k
|
|
329
|
-
assert(
|
|
347
|
+
assert(
|
|
348
|
+
worstCase.freeCollateralContribution.eq(new BN(6000).mul(QUOTE_PRECISION))
|
|
349
|
+
); // $6k
|
|
330
350
|
|
|
331
351
|
spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
|
|
332
352
|
marketIndex: 1,
|
|
333
|
-
openAsks: new BN(-100).mul(
|
|
353
|
+
openAsks: new BN(-100).mul(LAMPORTS_PRECISION),
|
|
334
354
|
});
|
|
335
355
|
|
|
336
|
-
worstCase = getWorstCaseTokenAmounts(
|
|
356
|
+
worstCase = getWorstCaseTokenAmounts(
|
|
357
|
+
spotPosition,
|
|
358
|
+
solMarket,
|
|
359
|
+
strictOraclePrice,
|
|
360
|
+
'Initial'
|
|
361
|
+
);
|
|
337
362
|
|
|
338
|
-
assert(worstCase.tokenAmount.eq(new BN(-100).mul(
|
|
363
|
+
assert(worstCase.tokenAmount.eq(new BN(-100).mul(LAMPORTS_PRECISION)));
|
|
339
364
|
assert(worstCase.tokenValue.eq(new BN(-10000).mul(PRICE_PRECISION))); // -$10k
|
|
340
|
-
assert(
|
|
365
|
+
assert(
|
|
366
|
+
worstCase.weightedTokenValue.eq(new BN(-12000).mul(PRICE_PRECISION))
|
|
367
|
+
); // -$12k
|
|
341
368
|
assert(worstCase.ordersValue.eq(new BN(10000).mul(PRICE_PRECISION))); // $10k
|
|
342
|
-
assert(
|
|
369
|
+
assert(
|
|
370
|
+
worstCase.freeCollateralContribution.eq(
|
|
371
|
+
new BN(-2000).mul(QUOTE_PRECISION)
|
|
372
|
+
)
|
|
373
|
+
); // -$2k
|
|
343
374
|
|
|
344
375
|
spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
|
|
345
376
|
marketIndex: 1,
|
|
346
377
|
balanceType: SpotBalanceType.BORROW,
|
|
347
378
|
scaledBalance: new BN(100).mul(SPOT_MARKET_BALANCE_PRECISION),
|
|
348
|
-
openAsks: new BN(-100).mul(
|
|
379
|
+
openAsks: new BN(-100).mul(LAMPORTS_PRECISION),
|
|
349
380
|
});
|
|
350
381
|
|
|
351
|
-
worstCase = getWorstCaseTokenAmounts(
|
|
382
|
+
worstCase = getWorstCaseTokenAmounts(
|
|
383
|
+
spotPosition,
|
|
384
|
+
solMarket,
|
|
385
|
+
strictOraclePrice,
|
|
386
|
+
'Initial'
|
|
387
|
+
);
|
|
352
388
|
|
|
353
|
-
assert(worstCase.tokenAmount.eq(new BN(-200).mul(
|
|
389
|
+
assert(worstCase.tokenAmount.eq(new BN(-200).mul(LAMPORTS_PRECISION)));
|
|
354
390
|
assert(worstCase.tokenValue.eq(new BN(-20000).mul(PRICE_PRECISION))); // -$20k
|
|
355
|
-
assert(
|
|
391
|
+
assert(
|
|
392
|
+
worstCase.weightedTokenValue.eq(new BN(-24000).mul(PRICE_PRECISION))
|
|
393
|
+
); // -$24k
|
|
356
394
|
assert(worstCase.ordersValue.eq(new BN(10000).mul(PRICE_PRECISION))); // $10k
|
|
357
|
-
assert(
|
|
395
|
+
assert(
|
|
396
|
+
worstCase.freeCollateralContribution.eq(
|
|
397
|
+
new BN(-14000).mul(QUOTE_PRECISION)
|
|
398
|
+
)
|
|
399
|
+
); // -$2k
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
it('custom margin ratio (sol spot)', async() => {
|
|
404
|
+
const myMockUserAccount = _.cloneDeep(mockUserAccount);
|
|
405
|
+
|
|
406
|
+
const solMarket = Object.assign({}, _.cloneDeep(mockSpotMarkets[1]), {
|
|
407
|
+
initialAssetWeight: 8000,
|
|
408
|
+
initialLiabilityWeight: 12000,
|
|
409
|
+
cumulativeDepositInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
|
|
410
|
+
cumulativeBorrowInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// $25
|
|
414
|
+
const strictOraclePrice = new StrictOraclePrice(PRICE_PRECISION.muln(25));
|
|
415
|
+
|
|
416
|
+
const spotPosition = Object.assign({}, myMockUserAccount.spotPositions[1], {
|
|
417
|
+
marketIndex: 1,
|
|
418
|
+
openBids: new BN(100).mul(LAMPORTS_PRECISION),
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
const worstCase = getWorstCaseTokenAmounts(
|
|
422
|
+
spotPosition,
|
|
423
|
+
solMarket,
|
|
424
|
+
strictOraclePrice,
|
|
425
|
+
'Initial',
|
|
426
|
+
myMockUserAccount.maxMarginRatio
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
console.log(worstCase);
|
|
430
|
+
assert(worstCase.weight.eq(new BN(8000)));
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
myMockUserAccount.maxMarginRatio = MARGIN_PRECISION.toNumber(); // max 1x pls
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
const worstCaseAfter = getWorstCaseTokenAmounts(
|
|
437
|
+
spotPosition,
|
|
438
|
+
solMarket,
|
|
439
|
+
strictOraclePrice,
|
|
440
|
+
'Initial',
|
|
441
|
+
myMockUserAccount.maxMarginRatio
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
console.log(worstCaseAfter);
|
|
445
|
+
assert(worstCaseAfter.weight.eq(new BN(0))); // not allowed to increase exposure
|
|
446
|
+
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it('custom margin ratio (sol perp)', async() => {
|
|
450
|
+
const myMockPerpMarkets = _.cloneDeep(mockPerpMarkets);
|
|
451
|
+
const myMockSpotMarkets = _.cloneDeep(mockSpotMarkets);
|
|
452
|
+
const myMockUserAccount = _.cloneDeep(mockUserAccount);
|
|
453
|
+
// myMockPerpMarkets[0].imfFactor = 550;
|
|
454
|
+
myMockPerpMarkets[0].marginRatioInitial = 2000; // 5x
|
|
455
|
+
myMockPerpMarkets[0].marginRatioMaintenance = 1000; // 10x
|
|
456
|
+
|
|
457
|
+
myMockSpotMarkets[0].initialAssetWeight = 1000;
|
|
458
|
+
myMockSpotMarkets[0].initialLiabilityWeight = 1000;
|
|
459
|
+
|
|
460
|
+
myMockUserAccount.spotPositions[0].scaledBalance = new BN(
|
|
461
|
+
10000 * SPOT_MARKET_BALANCE_PRECISION.toNumber()
|
|
462
|
+
); //10k
|
|
463
|
+
|
|
464
|
+
const mockUser: User = await makeMockUser(
|
|
465
|
+
myMockPerpMarkets,
|
|
466
|
+
myMockSpotMarkets,
|
|
467
|
+
myMockUserAccount,
|
|
468
|
+
[1, 1, 1, 1, 1, 1, 1, 1],
|
|
469
|
+
[1, 1, 1, 1, 1, 1, 1, 1]
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
assert(mockUser.getTokenAmount(0).eq(new BN('10000000000')));
|
|
474
|
+
assert(mockUser.getNetSpotMarketValue().eq(new BN('10000000000')));
|
|
475
|
+
assert(
|
|
476
|
+
mockUser
|
|
477
|
+
.getSpotMarketAssetAndLiabilityValue()
|
|
478
|
+
.totalLiabilityValue.eq(ZERO)
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
assert(mockUser.getFreeCollateral().gt(ZERO));
|
|
482
|
+
|
|
483
|
+
let iLev = mockUser.getMaxLeverageForPerp(0, 'Initial').toNumber();
|
|
484
|
+
let mLev = mockUser.getMaxLeverageForPerp(0, 'Maintenance').toNumber();
|
|
485
|
+
console.log(iLev, mLev);
|
|
486
|
+
assert(iLev == 5000);
|
|
487
|
+
assert(mLev == 10000);
|
|
488
|
+
|
|
489
|
+
myMockUserAccount.maxMarginRatio = MARGIN_PRECISION.div(new BN(2)).toNumber(); // 2x max pls
|
|
490
|
+
|
|
491
|
+
const mockUser2: User = await makeMockUser(
|
|
492
|
+
myMockPerpMarkets,
|
|
493
|
+
myMockSpotMarkets,
|
|
494
|
+
myMockUserAccount,
|
|
495
|
+
[1, 1, 1, 1, 1, 1, 1, 1],
|
|
496
|
+
[1, 1, 1, 1, 1, 1, 1, 1]
|
|
497
|
+
);
|
|
498
|
+
iLev = mockUser2.getMaxLeverageForPerp(0, 'Initial').toNumber();
|
|
499
|
+
mLev = mockUser2.getMaxLeverageForPerp(0, 'Maintenance').toNumber();
|
|
500
|
+
console.log(iLev, mLev);
|
|
501
|
+
|
|
502
|
+
assert(iLev == 2000);
|
|
503
|
+
assert(mLev == 10000);
|
|
504
|
+
|
|
358
505
|
});
|
|
359
506
|
});
|