@drift-labs/sdk 0.2.0-master.30 → 0.2.0-master.31
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/lib/admin.d.ts +11 -7
- package/lib/admin.js +50 -12
- package/lib/clearingHouse.d.ts +26 -23
- package/lib/clearingHouse.js +290 -652
- package/lib/clearingHouseUser.d.ts +1 -1
- package/lib/clearingHouseUser.js +9 -12
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +7 -0
- package/lib/constants/numericConstants.js +8 -1
- package/lib/constants/spotMarkets.js +4 -4
- package/lib/dlob/DLOB.d.ts +6 -5
- package/lib/dlob/DLOB.js +130 -88
- package/lib/dlob/DLOBNode.d.ts +2 -0
- package/lib/dlob/DLOBNode.js +3 -0
- package/lib/dlob/NodeList.d.ts +1 -1
- package/lib/dlob/NodeList.js +5 -4
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +14 -0
- package/lib/idl/clearing_house.json +1091 -611
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +31 -28
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +6 -6
- package/lib/math/spotBalance.js +7 -8
- package/lib/math/trade.js +11 -11
- package/lib/types.d.ts +117 -40
- package/lib/types.js +33 -3
- package/package.json +4 -2
- package/src/admin.ts +129 -29
- package/src/clearingHouse.ts +380 -787
- package/src/clearingHouseUser.ts +11 -14
- package/src/config.ts +1 -1
- package/src/constants/numericConstants.ts +7 -0
- package/src/constants/spotMarkets.ts +5 -4
- package/src/dlob/DLOB.ts +188 -103
- package/src/dlob/DLOBNode.ts +5 -0
- package/src/dlob/NodeList.ts +9 -5
- package/src/events/types.ts +3 -0
- package/src/factory/bigNum.ts +23 -0
- package/src/idl/clearing_house.json +1091 -611
- package/src/math/amm.ts +42 -29
- package/src/math/market.ts +9 -4
- package/src/math/spotBalance.ts +11 -8
- package/src/math/trade.ts +11 -11
- package/src/types.ts +81 -40
- package/tests/bn/test.ts +12 -7
- package/tests/dlob/helpers.ts +56 -33
- package/tests/dlob/test.ts +1227 -404
package/src/math/amm.ts
CHANGED
|
@@ -47,7 +47,7 @@ export function calculateOptimalPegAndBudget(
|
|
|
47
47
|
amm: AMM,
|
|
48
48
|
oraclePriceData: OraclePriceData
|
|
49
49
|
): [BN, BN, BN, boolean] {
|
|
50
|
-
const
|
|
50
|
+
const reservePriceBefore = calculatePrice(
|
|
51
51
|
amm.baseAssetReserve,
|
|
52
52
|
amm.quoteAssetReserve,
|
|
53
53
|
amm.pegMultiplier
|
|
@@ -70,15 +70,15 @@ export function calculateOptimalPegAndBudget(
|
|
|
70
70
|
let newTargetPrice: BN;
|
|
71
71
|
let newOptimalPeg: BN;
|
|
72
72
|
let newBudget: BN;
|
|
73
|
-
const targetPriceGap =
|
|
73
|
+
const targetPriceGap = reservePriceBefore.sub(targetPrice);
|
|
74
74
|
|
|
75
75
|
if (targetPriceGap.abs().gt(maxPriceSpread)) {
|
|
76
76
|
const markAdj = targetPriceGap.abs().sub(maxPriceSpread);
|
|
77
77
|
|
|
78
78
|
if (targetPriceGap.lt(new BN(0))) {
|
|
79
|
-
newTargetPrice =
|
|
79
|
+
newTargetPrice = reservePriceBefore.add(markAdj);
|
|
80
80
|
} else {
|
|
81
|
-
newTargetPrice =
|
|
81
|
+
newTargetPrice = reservePriceBefore.sub(markAdj);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
newOptimalPeg = calculatePegFromTargetPrice(
|
|
@@ -336,6 +336,7 @@ export function calculateInventoryScale(
|
|
|
336
336
|
minBaseAssetReserve: BN,
|
|
337
337
|
maxBaseAssetReserve: BN
|
|
338
338
|
): number {
|
|
339
|
+
const maxScale = BID_ASK_SPREAD_PRECISION.mul(new BN(10));
|
|
339
340
|
// inventory skew
|
|
340
341
|
const [openBids, openAsks] = calculateMarketOpenBidAsk(
|
|
341
342
|
baseAssetReserve,
|
|
@@ -348,10 +349,10 @@ export function calculateInventoryScale(
|
|
|
348
349
|
BN.min(openBids.abs(), openAsks.abs())
|
|
349
350
|
);
|
|
350
351
|
const inventoryScale =
|
|
351
|
-
BN.min(
|
|
352
|
-
|
|
353
|
-
.div(minSideLiquidity)
|
|
354
|
-
|
|
352
|
+
BN.min(
|
|
353
|
+
maxScale,
|
|
354
|
+
netBaseAssetAmount.mul(maxScale).div(minSideLiquidity).abs()
|
|
355
|
+
).toNumber() / BID_ASK_SPREAD_PRECISION.toNumber();
|
|
355
356
|
|
|
356
357
|
return inventoryScale;
|
|
357
358
|
}
|
|
@@ -362,7 +363,7 @@ export function calculateEffectiveLeverage(
|
|
|
362
363
|
terminalQuoteAssetReserve: BN,
|
|
363
364
|
pegMultiplier: BN,
|
|
364
365
|
netBaseAssetAmount: BN,
|
|
365
|
-
|
|
366
|
+
reservePrice: BN,
|
|
366
367
|
totalFeeMinusDistributions: BN
|
|
367
368
|
): number {
|
|
368
369
|
// inventory skew
|
|
@@ -372,7 +373,7 @@ export function calculateEffectiveLeverage(
|
|
|
372
373
|
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
373
374
|
|
|
374
375
|
const localBaseAssetValue = netBaseAssetAmount
|
|
375
|
-
.mul(
|
|
376
|
+
.mul(reservePrice)
|
|
376
377
|
.div(AMM_TO_QUOTE_PRECISION_RATIO.mul(PRICE_PRECISION));
|
|
377
378
|
|
|
378
379
|
const effectiveLeverage =
|
|
@@ -393,14 +394,14 @@ export function calculateMaxSpread(marginRatioInitial: number): number {
|
|
|
393
394
|
|
|
394
395
|
export function calculateSpreadBN(
|
|
395
396
|
baseSpread: number,
|
|
396
|
-
|
|
397
|
+
lastOracleReservePriceSpreadPct: BN,
|
|
397
398
|
lastOracleConfPct: BN,
|
|
398
399
|
maxSpread: number,
|
|
399
400
|
quoteAssetReserve: BN,
|
|
400
401
|
terminalQuoteAssetReserve: BN,
|
|
401
402
|
pegMultiplier: BN,
|
|
402
403
|
netBaseAssetAmount: BN,
|
|
403
|
-
|
|
404
|
+
reservePrice: BN,
|
|
404
405
|
totalFeeMinusDistributions: BN,
|
|
405
406
|
baseAssetReserve: BN,
|
|
406
407
|
minBaseAssetReserve: BN,
|
|
@@ -409,24 +410,26 @@ export function calculateSpreadBN(
|
|
|
409
410
|
let longSpread = baseSpread / 2;
|
|
410
411
|
let shortSpread = baseSpread / 2;
|
|
411
412
|
|
|
412
|
-
if (
|
|
413
|
+
if (lastOracleReservePriceSpreadPct.gt(ZERO)) {
|
|
413
414
|
shortSpread = Math.max(
|
|
414
415
|
shortSpread,
|
|
415
|
-
|
|
416
|
+
lastOracleReservePriceSpreadPct.abs().toNumber() +
|
|
417
|
+
lastOracleConfPct.toNumber()
|
|
416
418
|
);
|
|
417
|
-
} else if (
|
|
419
|
+
} else if (lastOracleReservePriceSpreadPct.lt(ZERO)) {
|
|
418
420
|
longSpread = Math.max(
|
|
419
421
|
longSpread,
|
|
420
|
-
|
|
422
|
+
lastOracleReservePriceSpreadPct.abs().toNumber() +
|
|
423
|
+
lastOracleConfPct.toNumber()
|
|
421
424
|
);
|
|
422
425
|
}
|
|
423
426
|
|
|
424
427
|
const maxTargetSpread: number = Math.max(
|
|
425
428
|
maxSpread,
|
|
426
|
-
|
|
429
|
+
lastOracleReservePriceSpreadPct.abs().toNumber()
|
|
427
430
|
);
|
|
428
431
|
|
|
429
|
-
const
|
|
432
|
+
const MAX_BID_ASK_INVENTORY_SKEW_FACTOR = 10;
|
|
430
433
|
|
|
431
434
|
const inventoryScale = calculateInventoryScale(
|
|
432
435
|
netBaseAssetAmount,
|
|
@@ -434,7 +437,10 @@ export function calculateSpreadBN(
|
|
|
434
437
|
minBaseAssetReserve,
|
|
435
438
|
maxBaseAssetReserve
|
|
436
439
|
);
|
|
437
|
-
const inventorySpreadScale = Math.min(
|
|
440
|
+
const inventorySpreadScale = Math.min(
|
|
441
|
+
MAX_BID_ASK_INVENTORY_SKEW_FACTOR,
|
|
442
|
+
1 + inventoryScale
|
|
443
|
+
);
|
|
438
444
|
|
|
439
445
|
if (netBaseAssetAmount.gt(ZERO)) {
|
|
440
446
|
longSpread *= inventorySpreadScale;
|
|
@@ -448,20 +454,23 @@ export function calculateSpreadBN(
|
|
|
448
454
|
terminalQuoteAssetReserve,
|
|
449
455
|
pegMultiplier,
|
|
450
456
|
netBaseAssetAmount,
|
|
451
|
-
|
|
457
|
+
reservePrice,
|
|
452
458
|
totalFeeMinusDistributions
|
|
453
459
|
);
|
|
454
460
|
|
|
455
461
|
if (totalFeeMinusDistributions.gt(ZERO)) {
|
|
456
|
-
const spreadScale = Math.min(
|
|
462
|
+
const spreadScale = Math.min(
|
|
463
|
+
MAX_BID_ASK_INVENTORY_SKEW_FACTOR,
|
|
464
|
+
1 + effectiveLeverage
|
|
465
|
+
);
|
|
457
466
|
if (netBaseAssetAmount.gt(ZERO)) {
|
|
458
467
|
longSpread *= spreadScale;
|
|
459
468
|
} else {
|
|
460
469
|
shortSpread *= spreadScale;
|
|
461
470
|
}
|
|
462
471
|
} else {
|
|
463
|
-
longSpread *=
|
|
464
|
-
shortSpread *=
|
|
472
|
+
longSpread *= MAX_BID_ASK_INVENTORY_SKEW_FACTOR;
|
|
473
|
+
shortSpread *= MAX_BID_ASK_INVENTORY_SKEW_FACTOR;
|
|
465
474
|
}
|
|
466
475
|
|
|
467
476
|
const totalSpread = longSpread + shortSpread;
|
|
@@ -487,23 +496,23 @@ export function calculateSpread(
|
|
|
487
496
|
return amm.baseSpread / 2;
|
|
488
497
|
}
|
|
489
498
|
|
|
490
|
-
const
|
|
499
|
+
const reservePrice = calculatePrice(
|
|
491
500
|
amm.baseAssetReserve,
|
|
492
501
|
amm.quoteAssetReserve,
|
|
493
502
|
amm.pegMultiplier
|
|
494
503
|
);
|
|
495
504
|
|
|
496
|
-
const targetPrice = oraclePriceData?.price ||
|
|
505
|
+
const targetPrice = oraclePriceData?.price || reservePrice;
|
|
497
506
|
const confInterval = oraclePriceData.confidence || ZERO;
|
|
498
507
|
|
|
499
|
-
const targetMarkSpreadPct =
|
|
508
|
+
const targetMarkSpreadPct = reservePrice
|
|
500
509
|
.sub(targetPrice)
|
|
501
510
|
.mul(BID_ASK_SPREAD_PRECISION)
|
|
502
|
-
.div(
|
|
511
|
+
.div(reservePrice);
|
|
503
512
|
|
|
504
513
|
const confIntervalPct = confInterval
|
|
505
514
|
.mul(BID_ASK_SPREAD_PRECISION)
|
|
506
|
-
.div(
|
|
515
|
+
.div(reservePrice);
|
|
507
516
|
|
|
508
517
|
const [longSpread, shortSpread] = calculateSpreadBN(
|
|
509
518
|
amm.baseSpread,
|
|
@@ -514,7 +523,7 @@ export function calculateSpread(
|
|
|
514
523
|
amm.terminalQuoteAssetReserve,
|
|
515
524
|
amm.pegMultiplier,
|
|
516
525
|
amm.netBaseAssetAmount,
|
|
517
|
-
|
|
526
|
+
reservePrice,
|
|
518
527
|
amm.totalFeeMinusDistributions,
|
|
519
528
|
amm.baseAssetReserve,
|
|
520
529
|
amm.minBaseAssetReserve,
|
|
@@ -684,6 +693,10 @@ export function calculateQuoteAssetAmountSwapped(
|
|
|
684
693
|
pegMultiplier: BN,
|
|
685
694
|
swapDirection: SwapDirection
|
|
686
695
|
): BN {
|
|
696
|
+
if (isVariant(swapDirection, 'remove')) {
|
|
697
|
+
quoteAssetReserves = quoteAssetReserves.add(ONE);
|
|
698
|
+
}
|
|
699
|
+
|
|
687
700
|
let quoteAssetAmount = quoteAssetReserves
|
|
688
701
|
.mul(pegMultiplier)
|
|
689
702
|
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
package/src/math/market.ts
CHANGED
|
@@ -106,12 +106,12 @@ export function calculateNewMarketAfterTrade(
|
|
|
106
106
|
return newMarket;
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
export function
|
|
109
|
+
export function calculateOracleReserveSpread(
|
|
110
110
|
market: PerpMarketAccount,
|
|
111
111
|
oraclePriceData: OraclePriceData
|
|
112
112
|
): BN {
|
|
113
|
-
const
|
|
114
|
-
return calculateOracleSpread(
|
|
113
|
+
const reservePrice = calculateReservePrice(market, oraclePriceData);
|
|
114
|
+
return calculateOracleSpread(reservePrice, oraclePriceData);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
export function calculateOracleSpread(
|
|
@@ -137,7 +137,12 @@ export function calculateMarketMarginRatio(
|
|
|
137
137
|
).toNumber();
|
|
138
138
|
break;
|
|
139
139
|
case 'Maintenance':
|
|
140
|
-
marginRatio =
|
|
140
|
+
marginRatio = calculateSizePremiumLiabilityWeight(
|
|
141
|
+
size,
|
|
142
|
+
market.imfFactor,
|
|
143
|
+
new BN(market.marginRatioMaintenance),
|
|
144
|
+
MARGIN_PRECISION
|
|
145
|
+
).toNumber();
|
|
141
146
|
break;
|
|
142
147
|
}
|
|
143
148
|
|
package/src/math/spotBalance.ts
CHANGED
|
@@ -196,22 +196,25 @@ export function calculateInterestRate(bank: SpotMarketAccount): BN {
|
|
|
196
196
|
const utilization = calculateUtilization(bank);
|
|
197
197
|
|
|
198
198
|
let interestRate: BN;
|
|
199
|
-
if (utilization.gt(bank.optimalUtilization)) {
|
|
200
|
-
const surplusUtilization = utilization.sub(bank.optimalUtilization);
|
|
201
|
-
const borrowRateSlope = bank.maxBorrowRate
|
|
202
|
-
.sub(bank.optimalBorrowRate)
|
|
199
|
+
if (utilization.gt(new BN(bank.optimalUtilization))) {
|
|
200
|
+
const surplusUtilization = utilization.sub(new BN(bank.optimalUtilization));
|
|
201
|
+
const borrowRateSlope = new BN(bank.maxBorrowRate - bank.optimalBorrowRate)
|
|
203
202
|
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
204
|
-
.div(
|
|
203
|
+
.div(
|
|
204
|
+
SPOT_MARKET_UTILIZATION_PRECISION.sub(new BN(bank.optimalUtilization))
|
|
205
|
+
);
|
|
205
206
|
|
|
206
|
-
interestRate = bank.optimalBorrowRate.add(
|
|
207
|
+
interestRate = new BN(bank.optimalBorrowRate).add(
|
|
207
208
|
surplusUtilization
|
|
208
209
|
.mul(borrowRateSlope)
|
|
209
210
|
.div(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
210
211
|
);
|
|
211
212
|
} else {
|
|
212
|
-
const borrowRateSlope = bank.optimalBorrowRate
|
|
213
|
+
const borrowRateSlope = new BN(bank.optimalBorrowRate)
|
|
213
214
|
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
214
|
-
.div(
|
|
215
|
+
.div(
|
|
216
|
+
SPOT_MARKET_UTILIZATION_PRECISION.sub(new BN(bank.optimalUtilization))
|
|
217
|
+
);
|
|
215
218
|
|
|
216
219
|
interestRate = utilization
|
|
217
220
|
.mul(borrowRateSlope)
|
package/src/math/trade.ts
CHANGED
|
@@ -215,20 +215,20 @@ export function calculateTargetPriceTrade(
|
|
|
215
215
|
assert(targetPrice.gt(ZERO));
|
|
216
216
|
assert(pct.lte(MAXPCT) && pct.gt(ZERO));
|
|
217
217
|
|
|
218
|
-
const
|
|
218
|
+
const reservePriceBefore = calculateReservePrice(market, oraclePriceData);
|
|
219
219
|
const bidPriceBefore = calculateBidPrice(market, oraclePriceData);
|
|
220
220
|
const askPriceBefore = calculateAskPrice(market, oraclePriceData);
|
|
221
221
|
|
|
222
222
|
let direction;
|
|
223
|
-
if (targetPrice.gt(
|
|
224
|
-
const priceGap = targetPrice.sub(
|
|
223
|
+
if (targetPrice.gt(reservePriceBefore)) {
|
|
224
|
+
const priceGap = targetPrice.sub(reservePriceBefore);
|
|
225
225
|
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
226
|
-
targetPrice =
|
|
226
|
+
targetPrice = reservePriceBefore.add(priceGapScaled);
|
|
227
227
|
direction = PositionDirection.LONG;
|
|
228
228
|
} else {
|
|
229
|
-
const priceGap =
|
|
229
|
+
const priceGap = reservePriceBefore.sub(targetPrice);
|
|
230
230
|
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
231
|
-
targetPrice =
|
|
231
|
+
targetPrice = reservePriceBefore.sub(priceGapScaled);
|
|
232
232
|
direction = PositionDirection.SHORT;
|
|
233
233
|
}
|
|
234
234
|
|
|
@@ -265,14 +265,14 @@ export function calculateTargetPriceTrade(
|
|
|
265
265
|
targetPrice.gt(bidPriceBefore)
|
|
266
266
|
) {
|
|
267
267
|
// no trade, market is at target
|
|
268
|
-
if (
|
|
268
|
+
if (reservePriceBefore.gt(targetPrice)) {
|
|
269
269
|
direction = PositionDirection.SHORT;
|
|
270
270
|
} else {
|
|
271
271
|
direction = PositionDirection.LONG;
|
|
272
272
|
}
|
|
273
273
|
tradeSize = ZERO;
|
|
274
274
|
return [direction, tradeSize, targetPrice, targetPrice];
|
|
275
|
-
} else if (
|
|
275
|
+
} else if (reservePriceBefore.gt(targetPrice)) {
|
|
276
276
|
// overestimate y2
|
|
277
277
|
baseAssetReserveAfter = squareRootBN(
|
|
278
278
|
k.div(targetPrice).mul(peg).div(PEG_PRECISION).sub(biasModifier)
|
|
@@ -291,7 +291,7 @@ export function calculateTargetPriceTrade(
|
|
|
291
291
|
.div(PEG_PRECISION)
|
|
292
292
|
.div(AMM_TO_QUOTE_PRECISION_RATIO);
|
|
293
293
|
baseSize = baseAssetReserveAfter.sub(baseAssetReserveBefore);
|
|
294
|
-
} else if (
|
|
294
|
+
} else if (reservePriceBefore.lt(targetPrice)) {
|
|
295
295
|
// underestimate y2
|
|
296
296
|
baseAssetReserveAfter = squareRootBN(
|
|
297
297
|
k.div(targetPrice).mul(peg).div(PEG_PRECISION).add(biasModifier)
|
|
@@ -320,12 +320,12 @@ export function calculateTargetPriceTrade(
|
|
|
320
320
|
|
|
321
321
|
let tp1 = targetPrice;
|
|
322
322
|
let tp2 = markPriceAfter;
|
|
323
|
-
let originalDiff = targetPrice.sub(
|
|
323
|
+
let originalDiff = targetPrice.sub(reservePriceBefore);
|
|
324
324
|
|
|
325
325
|
if (direction == PositionDirection.SHORT) {
|
|
326
326
|
tp1 = markPriceAfter;
|
|
327
327
|
tp2 = targetPrice;
|
|
328
|
-
originalDiff =
|
|
328
|
+
originalDiff = reservePriceBefore.sub(targetPrice);
|
|
329
329
|
}
|
|
330
330
|
|
|
331
331
|
const entryPrice = tradeSize
|
package/src/types.ts
CHANGED
|
@@ -3,8 +3,23 @@ import { BN, ZERO } from '.';
|
|
|
3
3
|
|
|
4
4
|
// # Utility Types / Enums / Constants
|
|
5
5
|
|
|
6
|
+
export class ExchangeStatus {
|
|
7
|
+
static readonly ACTIVE = { active: {} };
|
|
8
|
+
static readonly FUNDINGPAUSED = { fundingpaused: {} };
|
|
9
|
+
static readonly AMMPAUSED = { ammpaused: {} };
|
|
10
|
+
static readonly FILLPAUSED = { fillpaused: {} };
|
|
11
|
+
static readonly LIQPAUSED = { liqpaused: {} };
|
|
12
|
+
static readonly WITHDRAWPAUSED = { withdrawpaused: {} };
|
|
13
|
+
static readonly PAUSED = { paused: {} };
|
|
14
|
+
}
|
|
15
|
+
|
|
6
16
|
export class MarketStatus {
|
|
7
17
|
static readonly INITIALIZED = { initialized: {} };
|
|
18
|
+
static readonly ACTIVE = { active: {} };
|
|
19
|
+
static readonly FUNDINGPAUSED = { fundingpaused: {} };
|
|
20
|
+
static readonly AMMPAUSED = { ammpaused: {} };
|
|
21
|
+
static readonly FILLPAUSED = { fillpaused: {} };
|
|
22
|
+
static readonly WITHDRAWPAUSED = { withdrawpaused: {} };
|
|
8
23
|
static readonly REDUCEONLY = { reduceonly: {} };
|
|
9
24
|
static readonly SETTLEMENT = { settlement: {} };
|
|
10
25
|
static readonly DELISTED = { delisted: {} };
|
|
@@ -15,6 +30,21 @@ export class ContractType {
|
|
|
15
30
|
static readonly FUTURE = { future: {} };
|
|
16
31
|
}
|
|
17
32
|
|
|
33
|
+
export class ContractTier {
|
|
34
|
+
static readonly A = { a: {} };
|
|
35
|
+
static readonly B = { b: {} };
|
|
36
|
+
static readonly C = { c: {} };
|
|
37
|
+
static readonly Speculative = { speculative: {} };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class AssetTier {
|
|
41
|
+
static readonly COLLATERAL = { collateral: {} };
|
|
42
|
+
static readonly PROTECTED = { protected: {} };
|
|
43
|
+
static readonly CROSS = { cross: {} };
|
|
44
|
+
static readonly ISOLATED = { isolated: {} };
|
|
45
|
+
static readonly UNLISTED = { unlisted: {} };
|
|
46
|
+
}
|
|
47
|
+
|
|
18
48
|
export class SwapDirection {
|
|
19
49
|
static readonly ADD = { add: {} };
|
|
20
50
|
static readonly REMOVE = { remove: {} };
|
|
@@ -155,9 +185,23 @@ export type DepositRecord = {
|
|
|
155
185
|
marketIndex: number;
|
|
156
186
|
amount: BN;
|
|
157
187
|
oraclePrice: BN;
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
188
|
+
marketDepositBalance: BN;
|
|
189
|
+
marketWithdrawBalance: BN;
|
|
190
|
+
marketCumulativeDepositInterest: BN;
|
|
191
|
+
marketCumulativeBorrowInterest: BN;
|
|
192
|
+
transferUser?: PublicKey;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export type SpotInterestRecord = {
|
|
196
|
+
ts: BN;
|
|
197
|
+
marketIndex: number;
|
|
198
|
+
depositBalance: BN;
|
|
199
|
+
cumulativeDepositInterest: BN;
|
|
200
|
+
borrowBalance: BN;
|
|
201
|
+
cumulativeBorrowInterest: BN;
|
|
202
|
+
optimalUtilization: number;
|
|
203
|
+
optimalBorrowRate: number;
|
|
204
|
+
maxBorrowRate: number;
|
|
161
205
|
};
|
|
162
206
|
|
|
163
207
|
export type CurveRecord = {
|
|
@@ -184,8 +228,8 @@ export declare type InsuranceFundRecord = {
|
|
|
184
228
|
ts: BN;
|
|
185
229
|
bankIndex: BN;
|
|
186
230
|
marketIndex: number;
|
|
187
|
-
userIfFactor:
|
|
188
|
-
totalIfFactor:
|
|
231
|
+
userIfFactor: number;
|
|
232
|
+
totalIfFactor: number;
|
|
189
233
|
vaultAmountBefore: BN;
|
|
190
234
|
insuranceVaultAmountBefore: BN;
|
|
191
235
|
amount: BN;
|
|
@@ -234,7 +278,6 @@ export type FundingPaymentRecord = {
|
|
|
234
278
|
fundingPayment: BN;
|
|
235
279
|
baseAssetAmount: BN;
|
|
236
280
|
userLastCumulativeFunding: BN;
|
|
237
|
-
userLastFundingRateTs: BN;
|
|
238
281
|
ammCumulativeFundingLong: BN;
|
|
239
282
|
ammCumulativeFundingShort: BN;
|
|
240
283
|
};
|
|
@@ -249,11 +292,11 @@ export type LiquidationRecord = {
|
|
|
249
292
|
liquidationId: number;
|
|
250
293
|
canceledOrderIds: BN[];
|
|
251
294
|
liquidatePerp: LiquidatePerpRecord;
|
|
252
|
-
|
|
295
|
+
liquidateSpot: LiquidateSpotRecord;
|
|
253
296
|
liquidateBorrowForPerpPnl: LiquidateBorrowForPerpPnlRecord;
|
|
254
297
|
liquidatePerpPnlForDeposit: LiquidatePerpPnlForDepositRecord;
|
|
255
298
|
perpBankruptcy: PerpBankruptcyRecord;
|
|
256
|
-
|
|
299
|
+
spotBankruptcy: SpotBankruptcyRecord;
|
|
257
300
|
};
|
|
258
301
|
|
|
259
302
|
export class LiquidationType {
|
|
@@ -279,15 +322,13 @@ export type LiquidatePerpRecord = {
|
|
|
279
322
|
baseAssetAmount: BN;
|
|
280
323
|
quoteAssetAmount: BN;
|
|
281
324
|
lpShares: BN;
|
|
282
|
-
userPnl: BN;
|
|
283
|
-
liquidatorPnl: BN;
|
|
284
325
|
userOrderId: BN;
|
|
285
326
|
liquidatorOrderId: BN;
|
|
286
327
|
fillRecordId: BN;
|
|
287
328
|
ifFee: BN;
|
|
288
329
|
};
|
|
289
330
|
|
|
290
|
-
export type
|
|
331
|
+
export type LiquidateSpotRecord = {
|
|
291
332
|
assetMarketIndex: number;
|
|
292
333
|
assetPrice: BN;
|
|
293
334
|
assetTransfer: BN;
|
|
@@ -321,7 +362,7 @@ export type PerpBankruptcyRecord = {
|
|
|
321
362
|
cumulativeFundingRateDelta: BN;
|
|
322
363
|
};
|
|
323
364
|
|
|
324
|
-
export type
|
|
365
|
+
export type SpotBankruptcyRecord = {
|
|
325
366
|
marketIndex: number;
|
|
326
367
|
borrowAmount: BN;
|
|
327
368
|
cumulativeDepositInterestDelta: BN;
|
|
@@ -360,14 +401,14 @@ export type OrderActionRecord = {
|
|
|
360
401
|
referrerReward: number | null;
|
|
361
402
|
quoteAssetAmountSurplus: BN | null;
|
|
362
403
|
taker: PublicKey | null;
|
|
363
|
-
takerOrderId:
|
|
404
|
+
takerOrderId: number | null;
|
|
364
405
|
takerOrderDirection: PositionDirection | null;
|
|
365
406
|
takerOrderBaseAssetAmount: BN | null;
|
|
366
407
|
takerOrderCumulativeBaseAssetAmountFilled: BN | null;
|
|
367
408
|
takerOrderCumulativeQuoteAssetAmountFilled: BN | null;
|
|
368
409
|
takerOrderFee: BN | null;
|
|
369
410
|
maker: PublicKey | null;
|
|
370
|
-
makerOrderId:
|
|
411
|
+
makerOrderId: number | null;
|
|
371
412
|
makerOrderDirection: PositionDirection | null;
|
|
372
413
|
makerOrderBaseAssetAmount: BN | null;
|
|
373
414
|
makerOrderCumulativeBaseAssetAmountFilled: BN | null;
|
|
@@ -378,24 +419,20 @@ export type OrderActionRecord = {
|
|
|
378
419
|
|
|
379
420
|
export type StateAccount = {
|
|
380
421
|
admin: PublicKey;
|
|
381
|
-
|
|
382
|
-
exchangePaused: boolean;
|
|
383
|
-
adminControlsPrices: boolean;
|
|
384
|
-
totalFee: BN;
|
|
385
|
-
totalFeeWithdrawn: BN;
|
|
422
|
+
exchangeStatus: ExchangeStatus;
|
|
386
423
|
whitelistMint: PublicKey;
|
|
387
424
|
discountMint: PublicKey;
|
|
388
425
|
oracleGuardRails: OracleGuardRails;
|
|
389
|
-
maxDeposit: BN;
|
|
390
426
|
numberOfMarkets: number;
|
|
391
427
|
numberOfSpotMarkets: number;
|
|
392
428
|
minOrderQuoteAssetAmount: BN;
|
|
393
|
-
signer: PublicKey;
|
|
394
|
-
signerNonce: number;
|
|
395
|
-
defaultMarketOrderTimeInForce: number;
|
|
396
429
|
minPerpAuctionDuration: number;
|
|
430
|
+
defaultMarketOrderTimeInForce: number;
|
|
397
431
|
defaultSpotAuctionDuration: number;
|
|
398
432
|
liquidationMarginBufferRatio: number;
|
|
433
|
+
settlementDuration: number;
|
|
434
|
+
signer: PublicKey;
|
|
435
|
+
signerNonce: number;
|
|
399
436
|
srmVault: PublicKey;
|
|
400
437
|
perpFeeStructure: FeeStructure;
|
|
401
438
|
spotFeeStructure: FeeStructure;
|
|
@@ -449,6 +486,9 @@ export type HistoricalIndexData = {
|
|
|
449
486
|
};
|
|
450
487
|
|
|
451
488
|
export type SpotMarketAccount = {
|
|
489
|
+
status: MarketStatus;
|
|
490
|
+
assetTier: AssetTier;
|
|
491
|
+
|
|
452
492
|
marketIndex: number;
|
|
453
493
|
pubkey: PublicKey;
|
|
454
494
|
mint: PublicKey;
|
|
@@ -466,18 +506,20 @@ export type SpotMarketAccount = {
|
|
|
466
506
|
totalIfShares: BN;
|
|
467
507
|
userIfShares: BN;
|
|
468
508
|
|
|
469
|
-
userIfFactor:
|
|
470
|
-
totalIfFactor:
|
|
509
|
+
userIfFactor: number;
|
|
510
|
+
totalIfFactor: number;
|
|
471
511
|
ifLiquidationFee: BN;
|
|
472
512
|
|
|
473
513
|
decimals: number;
|
|
474
|
-
optimalUtilization:
|
|
475
|
-
optimalBorrowRate:
|
|
476
|
-
maxBorrowRate:
|
|
514
|
+
optimalUtilization: number;
|
|
515
|
+
optimalBorrowRate: number;
|
|
516
|
+
maxBorrowRate: number;
|
|
477
517
|
cumulativeDepositInterest: BN;
|
|
478
518
|
cumulativeBorrowInterest: BN;
|
|
479
519
|
depositBalance: BN;
|
|
480
520
|
borrowBalance: BN;
|
|
521
|
+
maxTokenDeposits: BN;
|
|
522
|
+
|
|
481
523
|
lastInterestTs: BN;
|
|
482
524
|
lastTwapTs: BN;
|
|
483
525
|
initialAssetWeight: BN;
|
|
@@ -494,14 +536,13 @@ export type SpotMarketAccount = {
|
|
|
494
536
|
|
|
495
537
|
orderStepSize: BN;
|
|
496
538
|
nextFillRecordId: BN;
|
|
497
|
-
spotFeePool:
|
|
498
|
-
balance: BN;
|
|
499
|
-
};
|
|
539
|
+
spotFeePool: PoolBalance;
|
|
500
540
|
totalSpotFee: BN;
|
|
501
541
|
};
|
|
502
542
|
|
|
503
543
|
export type PoolBalance = {
|
|
504
544
|
balance: BN;
|
|
545
|
+
marketIndex: number;
|
|
505
546
|
};
|
|
506
547
|
|
|
507
548
|
export type AMM = {
|
|
@@ -518,7 +559,7 @@ export type AMM = {
|
|
|
518
559
|
oracleSource: OracleSource;
|
|
519
560
|
historicalOracleData: HistoricalOracleData;
|
|
520
561
|
|
|
521
|
-
|
|
562
|
+
lastOracleReservePriceSpreadPct: BN;
|
|
522
563
|
lastOracleConfPct: BN;
|
|
523
564
|
|
|
524
565
|
fundingPeriod: BN;
|
|
@@ -567,7 +608,6 @@ export type AMM = {
|
|
|
567
608
|
// # User Account Types
|
|
568
609
|
export type PerpPosition = {
|
|
569
610
|
baseAssetAmount: BN;
|
|
570
|
-
remainderBaseAssetAmount: BN;
|
|
571
611
|
lastCumulativeFundingRate: BN;
|
|
572
612
|
marketIndex: number;
|
|
573
613
|
quoteAssetAmount: BN;
|
|
@@ -577,7 +617,7 @@ export type PerpPosition = {
|
|
|
577
617
|
openAsks: BN;
|
|
578
618
|
settledPnl: BN;
|
|
579
619
|
lpShares: BN;
|
|
580
|
-
|
|
620
|
+
remainderBaseAssetAmount: number;
|
|
581
621
|
lastNetBaseAssetAmountPerLp: BN;
|
|
582
622
|
lastNetQuoteAssetAmountPerLp: BN;
|
|
583
623
|
};
|
|
@@ -614,7 +654,7 @@ export type UserAccount = {
|
|
|
614
654
|
beingLiquidated: boolean;
|
|
615
655
|
bankrupt: boolean;
|
|
616
656
|
nextLiquidationId: number;
|
|
617
|
-
nextOrderId:
|
|
657
|
+
nextOrderId: number;
|
|
618
658
|
customMarginRatio: number;
|
|
619
659
|
};
|
|
620
660
|
|
|
@@ -634,7 +674,7 @@ export type Order = {
|
|
|
634
674
|
marketType: MarketType;
|
|
635
675
|
ts: BN;
|
|
636
676
|
slot: BN;
|
|
637
|
-
orderId:
|
|
677
|
+
orderId: number;
|
|
638
678
|
userOrderId: number;
|
|
639
679
|
marketIndex: number;
|
|
640
680
|
price: BN;
|
|
@@ -669,10 +709,10 @@ export type OrderParams = {
|
|
|
669
709
|
reduceOnly: boolean;
|
|
670
710
|
postOnly: boolean;
|
|
671
711
|
immediateOrCancel: boolean;
|
|
672
|
-
triggerPrice: BN;
|
|
712
|
+
triggerPrice: BN | null;
|
|
673
713
|
triggerCondition: OrderTriggerCondition;
|
|
674
714
|
positionLimit: BN;
|
|
675
|
-
oraclePriceOffset: BN;
|
|
715
|
+
oraclePriceOffset: BN | null;
|
|
676
716
|
auctionDuration: number | null;
|
|
677
717
|
timeInForce: number | null;
|
|
678
718
|
auctionStartPrice: BN | null;
|
|
@@ -700,10 +740,10 @@ export const DefaultOrderParams = {
|
|
|
700
740
|
reduceOnly: false,
|
|
701
741
|
postOnly: false,
|
|
702
742
|
immediateOrCancel: false,
|
|
703
|
-
triggerPrice:
|
|
743
|
+
triggerPrice: null,
|
|
704
744
|
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
705
745
|
positionLimit: ZERO,
|
|
706
|
-
oraclePriceOffset:
|
|
746
|
+
oraclePriceOffset: null,
|
|
707
747
|
auctionDuration: null,
|
|
708
748
|
timeInForce: null,
|
|
709
749
|
auctionStartPrice: null,
|
|
@@ -712,6 +752,7 @@ export const DefaultOrderParams = {
|
|
|
712
752
|
export type MakerInfo = {
|
|
713
753
|
maker: PublicKey;
|
|
714
754
|
makerStats: PublicKey;
|
|
755
|
+
makerUserAccount: UserAccount;
|
|
715
756
|
order: Order;
|
|
716
757
|
};
|
|
717
758
|
|
package/tests/bn/test.ts
CHANGED
|
@@ -77,6 +77,8 @@ describe('BigNum Tests', () => {
|
|
|
77
77
|
expect(val.toPrecision(5)).to.equal('1234.5');
|
|
78
78
|
expect(val.toPrecision(11)).to.equal('1234.5678900');
|
|
79
79
|
|
|
80
|
+
expect(BigNum.from('1234').toPrecision(5)).to.equal('1234.0');
|
|
81
|
+
|
|
80
82
|
// Case 2
|
|
81
83
|
const val2 = BigNum.from(1, 5);
|
|
82
84
|
|
|
@@ -99,8 +101,8 @@ describe('BigNum Tests', () => {
|
|
|
99
101
|
.shift(AMM_RESERVE_PRECISION_EXP)
|
|
100
102
|
.div(BigNum.from(entryPriceNum * 10 ** 8));
|
|
101
103
|
|
|
102
|
-
expect(val4.toString()).to.equal('
|
|
103
|
-
expect(val4.print()).to.equal('0.
|
|
104
|
+
expect(val4.toString()).to.equal('25000000');
|
|
105
|
+
expect(val4.print()).to.equal('0.025000000');
|
|
104
106
|
expect(val4.toNum().toFixed(3)).to.equal('0.025');
|
|
105
107
|
expect(val4.toPrecision(4)).to.equal('0.025');
|
|
106
108
|
|
|
@@ -118,8 +120,8 @@ describe('BigNum Tests', () => {
|
|
|
118
120
|
expect(BigNum.fromPrint('1').toMillified(5)).to.equal('1.0000');
|
|
119
121
|
expect(BigNum.fromPrint('12').toMillified(5)).to.equal('12.000');
|
|
120
122
|
expect(BigNum.fromPrint('123').toMillified(5)).to.equal('123.00');
|
|
121
|
-
expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('
|
|
122
|
-
expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('
|
|
123
|
+
expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('1234.0');
|
|
124
|
+
expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('12345');
|
|
123
125
|
expect(BigNum.fromPrint('123456').toMillified(5)).to.equal('123.45K');
|
|
124
126
|
expect(BigNum.fromPrint('1234567').toMillified(5)).to.equal('1.2345M');
|
|
125
127
|
expect(BigNum.fromPrint('12345678').toMillified(5)).to.equal('12.345M');
|
|
@@ -142,8 +144,8 @@ describe('BigNum Tests', () => {
|
|
|
142
144
|
const baseAmountVal1 = '14.33';
|
|
143
145
|
const val1 = BigNum.fromPrint(baseAmountVal1, BASE_PRECISION_EXP);
|
|
144
146
|
|
|
145
|
-
expect(val1.toString()).to.equal('
|
|
146
|
-
expect(val1.print()).to.equal('14.
|
|
147
|
+
expect(val1.toString()).to.equal('14330000000');
|
|
148
|
+
expect(val1.print()).to.equal('14.330000000');
|
|
147
149
|
|
|
148
150
|
const baseAmountVal2 = '34.1';
|
|
149
151
|
const val2 = BigNum.fromPrint(baseAmountVal2, BASE_PRECISION_EXP);
|
|
@@ -225,7 +227,7 @@ describe('BigNum Tests', () => {
|
|
|
225
227
|
.shift(AMM_RESERVE_PRECISION_EXP)
|
|
226
228
|
.div(BigNum.from(entryPriceNum * 10 ** 8));
|
|
227
229
|
|
|
228
|
-
expect(val.toString()).to.equal('
|
|
230
|
+
expect(val.toString()).to.equal('25000000');
|
|
229
231
|
expect(val.printShort()).to.equal('0.025');
|
|
230
232
|
|
|
231
233
|
const val2 = BigNum.from(10000, 4);
|
|
@@ -260,5 +262,8 @@ describe('BigNum Tests', () => {
|
|
|
260
262
|
|
|
261
263
|
const val9 = BigNum.from('1000000000123', 6);
|
|
262
264
|
expect(val9.prettyPrint()).to.equal('1,000,000.000123');
|
|
265
|
+
|
|
266
|
+
const val10 = BigNum.from('100000000000', 6);
|
|
267
|
+
expect(val10.prettyPrint(true)).to.equal('100,000');
|
|
263
268
|
});
|
|
264
269
|
});
|