@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/src/user.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
isOneOfVariant,
|
|
13
13
|
PerpMarketAccount,
|
|
14
14
|
HealthComponents,
|
|
15
|
+
UserStatsAccount,
|
|
15
16
|
} from './types';
|
|
16
17
|
import { calculateEntryPrice, positionIsAvailable } from './math/position';
|
|
17
18
|
import {
|
|
@@ -77,7 +78,6 @@ import {
|
|
|
77
78
|
getWorstCaseTokenAmounts,
|
|
78
79
|
isSpotPositionAvailable,
|
|
79
80
|
} from './math/spotPosition';
|
|
80
|
-
|
|
81
81
|
import { calculateLiveOracleTwap } from './math/oracles';
|
|
82
82
|
import { getPerpMarketTierNumber, getSpotMarketTierNumber } from './math/tiers';
|
|
83
83
|
import { StrictOraclePrice } from './oracles/strictOraclePrice';
|
|
@@ -631,7 +631,8 @@ export class User {
|
|
|
631
631
|
const marginRatio = calculateMarketMarginRatio(
|
|
632
632
|
this.driftClient.getPerpMarketAccount(marketIndex),
|
|
633
633
|
baseAssetAmount,
|
|
634
|
-
'Initial'
|
|
634
|
+
'Initial',
|
|
635
|
+
this.getUserAccount().maxMarginRatio
|
|
635
636
|
);
|
|
636
637
|
|
|
637
638
|
return freeCollateral.mul(MARGIN_PRECISION).div(new BN(marginRatio));
|
|
@@ -967,7 +968,8 @@ export class User {
|
|
|
967
968
|
spotPosition,
|
|
968
969
|
spotMarketAccount,
|
|
969
970
|
strictOraclePrice,
|
|
970
|
-
marginCategory
|
|
971
|
+
marginCategory,
|
|
972
|
+
this.getUserAccount().maxMarginRatio
|
|
971
973
|
);
|
|
972
974
|
|
|
973
975
|
if (worstCaseTokenAmount.gt(ZERO) && countForBase) {
|
|
@@ -1066,8 +1068,16 @@ export class User {
|
|
|
1066
1068
|
marginCategory
|
|
1067
1069
|
);
|
|
1068
1070
|
|
|
1069
|
-
if (
|
|
1070
|
-
|
|
1071
|
+
if (
|
|
1072
|
+
marginCategory === 'Initial' &&
|
|
1073
|
+
spotMarketAccount.marketIndex !== QUOTE_SPOT_MARKET_INDEX
|
|
1074
|
+
) {
|
|
1075
|
+
weight = BN.max(
|
|
1076
|
+
weight,
|
|
1077
|
+
SPOT_MARKET_WEIGHT_PRECISION.addn(
|
|
1078
|
+
this.getUserAccount().maxMarginRatio
|
|
1079
|
+
)
|
|
1080
|
+
);
|
|
1071
1081
|
}
|
|
1072
1082
|
|
|
1073
1083
|
if (liquidationBuffer !== undefined) {
|
|
@@ -1113,13 +1123,26 @@ export class User {
|
|
|
1113
1123
|
);
|
|
1114
1124
|
|
|
1115
1125
|
if (marginCategory !== undefined) {
|
|
1116
|
-
|
|
1126
|
+
let weight = calculateAssetWeight(
|
|
1117
1127
|
tokenAmount,
|
|
1118
1128
|
strictOraclePrice.current,
|
|
1119
1129
|
spotMarketAccount,
|
|
1120
1130
|
marginCategory
|
|
1121
1131
|
);
|
|
1122
1132
|
|
|
1133
|
+
if (
|
|
1134
|
+
marginCategory === 'Initial' &&
|
|
1135
|
+
spotMarketAccount.marketIndex !== QUOTE_SPOT_MARKET_INDEX
|
|
1136
|
+
) {
|
|
1137
|
+
const userCustomAssetWeight = BN.max(
|
|
1138
|
+
ZERO,
|
|
1139
|
+
SPOT_MARKET_WEIGHT_PRECISION.subn(
|
|
1140
|
+
this.getUserAccount().maxMarginRatio
|
|
1141
|
+
)
|
|
1142
|
+
);
|
|
1143
|
+
weight = BN.min(weight, userCustomAssetWeight);
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1123
1146
|
assetValue = assetValue.mul(weight).div(SPOT_MARKET_WEIGHT_PRECISION);
|
|
1124
1147
|
}
|
|
1125
1148
|
|
|
@@ -1253,17 +1276,11 @@ export class User {
|
|
|
1253
1276
|
calculateMarketMarginRatio(
|
|
1254
1277
|
market,
|
|
1255
1278
|
baseAssetAmount.abs(),
|
|
1256
|
-
marginCategory
|
|
1279
|
+
marginCategory,
|
|
1280
|
+
this.getUserAccount().maxMarginRatio
|
|
1257
1281
|
)
|
|
1258
1282
|
);
|
|
1259
1283
|
|
|
1260
|
-
if (marginCategory === 'Initial') {
|
|
1261
|
-
marginRatio = BN.max(
|
|
1262
|
-
marginRatio,
|
|
1263
|
-
new BN(this.getUserAccount().maxMarginRatio)
|
|
1264
|
-
);
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
1284
|
if (liquidationBuffer !== undefined) {
|
|
1268
1285
|
marginRatio = marginRatio.add(liquidationBuffer);
|
|
1269
1286
|
}
|
|
@@ -1493,25 +1510,28 @@ export class User {
|
|
|
1493
1510
|
return totalLiabilityValue.mul(TEN_THOUSAND).div(netAssetValue);
|
|
1494
1511
|
}
|
|
1495
1512
|
|
|
1496
|
-
getLeverageComponents(
|
|
1513
|
+
getLeverageComponents(
|
|
1514
|
+
includeOpenOrders = true,
|
|
1515
|
+
marginCategory: MarginCategory = undefined
|
|
1516
|
+
): {
|
|
1497
1517
|
perpLiabilityValue: BN;
|
|
1498
1518
|
perpPnl: BN;
|
|
1499
1519
|
spotAssetValue: BN;
|
|
1500
1520
|
spotLiabilityValue: BN;
|
|
1501
1521
|
} {
|
|
1502
1522
|
const perpLiability = this.getTotalPerpPositionValue(
|
|
1503
|
-
|
|
1523
|
+
marginCategory,
|
|
1504
1524
|
undefined,
|
|
1505
1525
|
includeOpenOrders
|
|
1506
1526
|
);
|
|
1507
|
-
const perpPnl = this.getUnrealizedPNL(true);
|
|
1527
|
+
const perpPnl = this.getUnrealizedPNL(true, undefined, marginCategory);
|
|
1508
1528
|
|
|
1509
1529
|
const {
|
|
1510
1530
|
totalAssetValue: spotAssetValue,
|
|
1511
1531
|
totalLiabilityValue: spotLiabilityValue,
|
|
1512
1532
|
} = this.getSpotMarketAssetAndLiabilityValue(
|
|
1513
1533
|
undefined,
|
|
1514
|
-
|
|
1534
|
+
marginCategory,
|
|
1515
1535
|
undefined,
|
|
1516
1536
|
includeOpenOrders
|
|
1517
1537
|
);
|
|
@@ -1598,7 +1618,10 @@ export class User {
|
|
|
1598
1618
|
|
|
1599
1619
|
switch (marginCategory) {
|
|
1600
1620
|
case 'Initial':
|
|
1601
|
-
rawMarginRatio =
|
|
1621
|
+
rawMarginRatio = Math.max(
|
|
1622
|
+
market.marginRatioInitial,
|
|
1623
|
+
this.getUserAccount().maxMarginRatio
|
|
1624
|
+
);
|
|
1602
1625
|
break;
|
|
1603
1626
|
case 'Maintenance':
|
|
1604
1627
|
rawMarginRatio = market.marginRatioMaintenance;
|
|
@@ -1622,7 +1645,8 @@ export class User {
|
|
|
1622
1645
|
let marginRatio = calculateMarketMarginRatio(
|
|
1623
1646
|
market,
|
|
1624
1647
|
maxSize,
|
|
1625
|
-
marginCategory
|
|
1648
|
+
marginCategory,
|
|
1649
|
+
this.getUserAccount().maxMarginRatio
|
|
1626
1650
|
);
|
|
1627
1651
|
|
|
1628
1652
|
// use more fesible size since imf factor activated
|
|
@@ -1642,7 +1666,8 @@ export class User {
|
|
|
1642
1666
|
marginRatio = calculateMarketMarginRatio(
|
|
1643
1667
|
market,
|
|
1644
1668
|
targetSize,
|
|
1645
|
-
marginCategory
|
|
1669
|
+
marginCategory,
|
|
1670
|
+
this.getUserAccount().maxMarginRatio
|
|
1646
1671
|
);
|
|
1647
1672
|
attempts += 1;
|
|
1648
1673
|
}
|
|
@@ -2176,6 +2201,7 @@ export class User {
|
|
|
2176
2201
|
: this.getPerpPositionValue(targetMarketIndex, oracleData);
|
|
2177
2202
|
|
|
2178
2203
|
let maxPositionSize = this.getPerpBuyingPower(targetMarketIndex, lpBuffer);
|
|
2204
|
+
|
|
2179
2205
|
if (maxPositionSize.gte(ZERO)) {
|
|
2180
2206
|
if (oppositeSizeValueUSDC.eq(ZERO)) {
|
|
2181
2207
|
// case 1 : Regular trade where current total position less than max, and no opposite position to account for
|
|
@@ -2261,7 +2287,8 @@ export class User {
|
|
|
2261
2287
|
ZERO,
|
|
2262
2288
|
isVariant(direction, 'long')
|
|
2263
2289
|
? SpotBalanceType.DEPOSIT
|
|
2264
|
-
: SpotBalanceType.BORROW
|
|
2290
|
+
: SpotBalanceType.BORROW,
|
|
2291
|
+
this.getUserAccount().maxMarginRatio
|
|
2265
2292
|
);
|
|
2266
2293
|
|
|
2267
2294
|
let tradeAmount = ZERO;
|
|
@@ -2274,7 +2301,8 @@ export class User {
|
|
|
2274
2301
|
oraclePrice,
|
|
2275
2302
|
'Initial',
|
|
2276
2303
|
this.getTokenAmount(targetMarketIndex).abs(),
|
|
2277
|
-
SpotBalanceType.BORROW
|
|
2304
|
+
SpotBalanceType.BORROW,
|
|
2305
|
+
this.getUserAccount().maxMarginRatio
|
|
2278
2306
|
);
|
|
2279
2307
|
freeCollateral = freeCollateral.add(
|
|
2280
2308
|
tradeAmount.mul(new BN(marginRatio)).div(MARGIN_PRECISION)
|
|
@@ -2289,7 +2317,8 @@ export class User {
|
|
|
2289
2317
|
oraclePrice,
|
|
2290
2318
|
'Initial',
|
|
2291
2319
|
this.getTokenAmount(targetMarketIndex),
|
|
2292
|
-
SpotBalanceType.DEPOSIT
|
|
2320
|
+
SpotBalanceType.DEPOSIT,
|
|
2321
|
+
this.getUserAccount().maxMarginRatio
|
|
2293
2322
|
);
|
|
2294
2323
|
freeCollateral = freeCollateral.add(
|
|
2295
2324
|
tradeAmount.mul(new BN(marginRatio)).div(MARGIN_PRECISION)
|
|
@@ -2383,7 +2412,7 @@ export class User {
|
|
|
2383
2412
|
);
|
|
2384
2413
|
|
|
2385
2414
|
const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } =
|
|
2386
|
-
this.getLeverageComponents();
|
|
2415
|
+
this.getLeverageComponents(undefined, 'Initial');
|
|
2387
2416
|
|
|
2388
2417
|
if (!calculateSwap) {
|
|
2389
2418
|
calculateSwap = (inSwap: BN) => {
|
|
@@ -2582,7 +2611,8 @@ export class User {
|
|
|
2582
2611
|
spotPosition,
|
|
2583
2612
|
spotMarketAccount,
|
|
2584
2613
|
strictOraclePrice,
|
|
2585
|
-
marginCategory
|
|
2614
|
+
marginCategory,
|
|
2615
|
+
this.getUserAccount().maxMarginRatio
|
|
2586
2616
|
);
|
|
2587
2617
|
|
|
2588
2618
|
return freeCollateralContribution;
|
|
@@ -2605,7 +2635,8 @@ export class User {
|
|
|
2605
2635
|
spotPosition,
|
|
2606
2636
|
spotMarketAccount,
|
|
2607
2637
|
strictOraclePrice,
|
|
2608
|
-
'Initial'
|
|
2638
|
+
'Initial',
|
|
2639
|
+
this.getUserAccount().maxMarginRatio
|
|
2609
2640
|
);
|
|
2610
2641
|
|
|
2611
2642
|
if (tokenValue.gte(ZERO)) {
|
|
@@ -2677,7 +2708,7 @@ export class User {
|
|
|
2677
2708
|
);
|
|
2678
2709
|
|
|
2679
2710
|
const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } =
|
|
2680
|
-
this.getLeverageComponents();
|
|
2711
|
+
this.getLeverageComponents(undefined, 'Initial');
|
|
2681
2712
|
|
|
2682
2713
|
const inPositionAfter = this.cloneAndUpdateSpotPosition(
|
|
2683
2714
|
inSpotPosition,
|
|
@@ -2909,14 +2940,82 @@ export class User {
|
|
|
2909
2940
|
return newLeverage;
|
|
2910
2941
|
}
|
|
2911
2942
|
|
|
2943
|
+
public getUserFeeTier(marketType: MarketType) {
|
|
2944
|
+
const state = this.driftClient.getStateAccount();
|
|
2945
|
+
|
|
2946
|
+
let feeTierIndex = 0;
|
|
2947
|
+
if (isVariant(marketType, 'perp')) {
|
|
2948
|
+
const userStatsAccount: UserStatsAccount = this.driftClient
|
|
2949
|
+
.getUserStats()
|
|
2950
|
+
.getAccount();
|
|
2951
|
+
|
|
2952
|
+
const total30dVolume = userStatsAccount.takerVolume30D.add(
|
|
2953
|
+
userStatsAccount.makerVolume30D
|
|
2954
|
+
); // todo: update using now and lastTs?
|
|
2955
|
+
|
|
2956
|
+
const stakedQuoteAssetAmount = userStatsAccount.ifStakedQuoteAssetAmount;
|
|
2957
|
+
const volumeTiers = [
|
|
2958
|
+
new BN(100_000_000).mul(QUOTE_PRECISION),
|
|
2959
|
+
new BN(50_000_000).mul(QUOTE_PRECISION),
|
|
2960
|
+
new BN(10_000_000).mul(QUOTE_PRECISION),
|
|
2961
|
+
new BN(5_000_000).mul(QUOTE_PRECISION),
|
|
2962
|
+
new BN(1_000_000).mul(QUOTE_PRECISION),
|
|
2963
|
+
];
|
|
2964
|
+
const stakedTiers = [
|
|
2965
|
+
new BN(10000).mul(QUOTE_PRECISION),
|
|
2966
|
+
new BN(5000).mul(QUOTE_PRECISION),
|
|
2967
|
+
new BN(2000).mul(QUOTE_PRECISION),
|
|
2968
|
+
new BN(1000).mul(QUOTE_PRECISION),
|
|
2969
|
+
new BN(500).mul(QUOTE_PRECISION),
|
|
2970
|
+
];
|
|
2971
|
+
|
|
2972
|
+
for (let i = 0; i < volumeTiers.length; i++) {
|
|
2973
|
+
if (
|
|
2974
|
+
total30dVolume.gte(volumeTiers[i]) ||
|
|
2975
|
+
stakedQuoteAssetAmount.gte(stakedTiers[i])
|
|
2976
|
+
) {
|
|
2977
|
+
feeTierIndex = 5 - i;
|
|
2978
|
+
break;
|
|
2979
|
+
}
|
|
2980
|
+
}
|
|
2981
|
+
|
|
2982
|
+
return state.perpFeeStructure.feeTiers[feeTierIndex];
|
|
2983
|
+
}
|
|
2984
|
+
|
|
2985
|
+
return state.spotFeeStructure.feeTiers[feeTierIndex];
|
|
2986
|
+
}
|
|
2987
|
+
|
|
2912
2988
|
/**
|
|
2913
|
-
* Calculates
|
|
2989
|
+
* Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
|
|
2990
|
+
* @param marketType
|
|
2991
|
+
* @param positionMarketIndex
|
|
2992
|
+
* @returns : {takerFee: number, makerFee: number} Precision None
|
|
2993
|
+
*/
|
|
2994
|
+
public getMarketFees(marketType: MarketType, marketIndex?: number) {
|
|
2995
|
+
const feeTier = this.getUserFeeTier(marketType);
|
|
2996
|
+
let takerFee = feeTier.feeNumerator / feeTier.feeDenominator;
|
|
2997
|
+
let makerFee =
|
|
2998
|
+
feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
|
|
2999
|
+
|
|
3000
|
+
if (marketIndex && isVariant(marketType, 'perp')) {
|
|
3001
|
+
const marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
3002
|
+
takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
|
|
3003
|
+
makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
|
|
3004
|
+
}
|
|
3005
|
+
|
|
3006
|
+
return {
|
|
3007
|
+
takerFee,
|
|
3008
|
+
makerFee,
|
|
3009
|
+
};
|
|
3010
|
+
}
|
|
3011
|
+
|
|
3012
|
+
/**
|
|
3013
|
+
* Calculates how much perp fee will be taken for a given sized trade
|
|
2914
3014
|
* @param quoteAmount
|
|
2915
3015
|
* @returns feeForQuote : Precision QUOTE_PRECISION
|
|
2916
3016
|
*/
|
|
2917
3017
|
public calculateFeeForQuoteAmount(quoteAmount: BN): BN {
|
|
2918
|
-
const feeTier =
|
|
2919
|
-
this.driftClient.getStateAccount().perpFeeStructure.feeTiers[0];
|
|
3018
|
+
const feeTier = this.getUserFeeTier(MarketType.PERP);
|
|
2920
3019
|
return quoteAmount
|
|
2921
3020
|
.mul(new BN(feeTier.feeNumerator))
|
|
2922
3021
|
.div(new BN(feeTier.feeDenominator));
|
|
@@ -3156,7 +3255,8 @@ export class User {
|
|
|
3156
3255
|
calculateMarketMarginRatio(
|
|
3157
3256
|
perpMarket,
|
|
3158
3257
|
worstCaseBaseAmount.abs(),
|
|
3159
|
-
marginCategory
|
|
3258
|
+
marginCategory,
|
|
3259
|
+
this.getUserAccount().maxMarginRatio
|
|
3160
3260
|
)
|
|
3161
3261
|
);
|
|
3162
3262
|
|
|
@@ -3280,7 +3380,8 @@ export class User {
|
|
|
3280
3380
|
spotPosition,
|
|
3281
3381
|
spotMarketAccount,
|
|
3282
3382
|
strictOraclePrice,
|
|
3283
|
-
marginCategory
|
|
3383
|
+
marginCategory,
|
|
3384
|
+
this.getUserAccount().maxMarginRatio
|
|
3284
3385
|
);
|
|
3285
3386
|
|
|
3286
3387
|
netQuoteValue = netQuoteValue.add(ordersValue);
|
|
@@ -3324,7 +3425,8 @@ export class User {
|
|
|
3324
3425
|
baseAssetValue,
|
|
3325
3426
|
oraclePriceData.price,
|
|
3326
3427
|
spotMarketAccount,
|
|
3327
|
-
marginCategory
|
|
3428
|
+
marginCategory,
|
|
3429
|
+
this.getUserAccount().maxMarginRatio
|
|
3328
3430
|
);
|
|
3329
3431
|
|
|
3330
3432
|
if (netQuoteValue.lt(ZERO)) {
|
package/tests/amm/test.ts
CHANGED
|
@@ -568,7 +568,8 @@ describe('AMM Tests', () => {
|
|
|
568
568
|
mockMarket1.amm.maxBaseAssetReserve = mockMarket1.amm.baseAssetReserve.add(
|
|
569
569
|
new BN(1234835)
|
|
570
570
|
);
|
|
571
|
-
mockMarket1.amm.minBaseAssetReserve =
|
|
571
|
+
mockMarket1.amm.minBaseAssetReserve =
|
|
572
|
+
mockMarket1.amm.baseAssetReserve.sub(BASE_PRECISION);
|
|
572
573
|
mockMarket1.amm.quoteAssetReserve = new BN(cc).mul(BASE_PRECISION);
|
|
573
574
|
mockMarket1.amm.pegMultiplier = new BN(18.32 * PEG_PRECISION.toNumber());
|
|
574
575
|
mockMarket1.amm.sqrtK = new BN(cc).mul(BASE_PRECISION);
|
|
@@ -790,14 +791,14 @@ describe('AMM Tests', () => {
|
|
|
790
791
|
assert(totalAskSize.sub(openAsks.abs()).lte(new BN(5))); // only tiny rounding errors
|
|
791
792
|
});
|
|
792
793
|
|
|
793
|
-
|
|
794
794
|
it('orderbook L2 gen (4 topOfBookQuoteAmounts, 10 numOrders, low bid liquidity)', async () => {
|
|
795
795
|
const myMockPerpMarkets = _.cloneDeep(mockPerpMarkets);
|
|
796
796
|
|
|
797
797
|
const mockMarket1: PerpMarketAccount = myMockPerpMarkets[0];
|
|
798
798
|
const cc = 38104569;
|
|
799
799
|
mockMarket1.amm.baseAssetReserve = new BN(cc).mul(BASE_PRECISION);
|
|
800
|
-
mockMarket1.amm.maxBaseAssetReserve =
|
|
800
|
+
mockMarket1.amm.maxBaseAssetReserve =
|
|
801
|
+
mockMarket1.amm.baseAssetReserve.add(BASE_PRECISION); // only 1 base
|
|
801
802
|
mockMarket1.amm.minBaseAssetReserve = mockMarket1.amm.baseAssetReserve.div(
|
|
802
803
|
new BN(2)
|
|
803
804
|
);
|
|
@@ -873,14 +874,15 @@ describe('AMM Tests', () => {
|
|
|
873
874
|
assert(totalAskSize.sub(openAsks.abs()).lte(new BN(5))); // only tiny rounding errors
|
|
874
875
|
});
|
|
875
876
|
|
|
876
|
-
|
|
877
877
|
it('orderbook L2 gen (4 topOfBookQuoteAmounts, 10 numOrders, low ask liquidity)', async () => {
|
|
878
878
|
const myMockPerpMarkets = _.cloneDeep(mockPerpMarkets);
|
|
879
879
|
|
|
880
880
|
const mockMarket1: PerpMarketAccount = myMockPerpMarkets[0];
|
|
881
881
|
const cc = 38104569;
|
|
882
882
|
mockMarket1.amm.baseAssetReserve = new BN(cc).mul(BASE_PRECISION);
|
|
883
|
-
mockMarket1.amm.maxBaseAssetReserve = mockMarket1.amm.baseAssetReserve.add(
|
|
883
|
+
mockMarket1.amm.maxBaseAssetReserve = mockMarket1.amm.baseAssetReserve.add(
|
|
884
|
+
BASE_PRECISION.mul(new BN(1000))
|
|
885
|
+
); // 1000 base
|
|
884
886
|
mockMarket1.amm.minBaseAssetReserve = mockMarket1.amm.baseAssetReserve.sub(
|
|
885
887
|
BASE_PRECISION.div(new BN(2))
|
|
886
888
|
); // only .5 base
|
package/tests/auctions/test.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {PRICE_PRECISION, BN, deriveOracleAuctionParams} from
|
|
2
|
-
import {assert} from
|
|
3
|
-
import {PositionDirection} from
|
|
1
|
+
import { PRICE_PRECISION, BN, deriveOracleAuctionParams } from '../../src';
|
|
2
|
+
import { assert } from 'chai';
|
|
3
|
+
import { PositionDirection } from '../../lib';
|
|
4
4
|
|
|
5
5
|
describe('Auction Tests', () => {
|
|
6
|
-
|
|
7
6
|
it('deriveOracleAuctionParams', async () => {
|
|
8
7
|
let oraclePrice = new BN(100).mul(PRICE_PRECISION);
|
|
9
8
|
let auctionStartPrice = new BN(90).mul(PRICE_PRECISION);
|
|
@@ -18,9 +17,15 @@ describe('Auction Tests', () => {
|
|
|
18
17
|
limitPrice,
|
|
19
18
|
});
|
|
20
19
|
|
|
21
|
-
assert(
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
assert(
|
|
21
|
+
oracleOrderParams.auctionStartPrice.eq(new BN(-10).mul(PRICE_PRECISION))
|
|
22
|
+
);
|
|
23
|
+
assert(
|
|
24
|
+
oracleOrderParams.auctionEndPrice.eq(new BN(10).mul(PRICE_PRECISION))
|
|
25
|
+
);
|
|
26
|
+
assert(
|
|
27
|
+
oracleOrderParams.oraclePriceOffset === 20 * PRICE_PRECISION.toNumber()
|
|
28
|
+
);
|
|
24
29
|
|
|
25
30
|
oracleOrderParams = deriveOracleAuctionParams({
|
|
26
31
|
direction: PositionDirection.LONG,
|
|
@@ -47,9 +52,15 @@ describe('Auction Tests', () => {
|
|
|
47
52
|
limitPrice,
|
|
48
53
|
});
|
|
49
54
|
|
|
50
|
-
assert(
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
assert(
|
|
56
|
+
oracleOrderParams.auctionStartPrice.eq(new BN(10).mul(PRICE_PRECISION))
|
|
57
|
+
);
|
|
58
|
+
assert(
|
|
59
|
+
oracleOrderParams.auctionEndPrice.eq(new BN(-10).mul(PRICE_PRECISION))
|
|
60
|
+
);
|
|
61
|
+
assert(
|
|
62
|
+
oracleOrderParams.oraclePriceOffset === -20 * PRICE_PRECISION.toNumber()
|
|
63
|
+
);
|
|
53
64
|
|
|
54
65
|
oracleOrderParams = deriveOracleAuctionParams({
|
|
55
66
|
direction: PositionDirection.SHORT,
|
|
@@ -63,4 +74,4 @@ describe('Auction Tests', () => {
|
|
|
63
74
|
assert(oracleOrderParams.auctionEndPrice.eq(new BN(0)));
|
|
64
75
|
assert(oracleOrderParams.oraclePriceOffset === -1);
|
|
65
76
|
});
|
|
66
|
-
});
|
|
77
|
+
});
|
package/tests/dlob/helpers.ts
CHANGED
|
@@ -21,7 +21,9 @@ import {
|
|
|
21
21
|
OrderRecord,
|
|
22
22
|
ZERO,
|
|
23
23
|
ContractTier,
|
|
24
|
-
SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION
|
|
24
|
+
SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
|
|
25
|
+
SPOT_MARKET_WEIGHT_PRECISION,
|
|
26
|
+
PRICE_PRECISION,
|
|
25
27
|
} from '../../src';
|
|
26
28
|
|
|
27
29
|
export const mockPerpPosition: PerpPosition = {
|
|
@@ -263,7 +265,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
263
265
|
status: MarketStatus.ACTIVE,
|
|
264
266
|
assetTier: AssetTier.COLLATERAL,
|
|
265
267
|
name: [],
|
|
266
|
-
maxTokenDeposits: new BN(1000000),
|
|
268
|
+
maxTokenDeposits: new BN(1000000 * QUOTE_PRECISION.toNumber()),
|
|
267
269
|
marketIndex: 0,
|
|
268
270
|
pubkey: PublicKey.default,
|
|
269
271
|
mint: DevnetSpotMarkets[0].mint,
|
|
@@ -300,10 +302,10 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
300
302
|
lastInterestTs: new BN(0),
|
|
301
303
|
lastTwapTs: new BN(0),
|
|
302
304
|
oracle: PublicKey.default,
|
|
303
|
-
initialAssetWeight:
|
|
304
|
-
maintenanceAssetWeight:
|
|
305
|
-
initialLiabilityWeight:
|
|
306
|
-
maintenanceLiabilityWeight:
|
|
305
|
+
initialAssetWeight: SPOT_MARKET_WEIGHT_PRECISION.toNumber(),
|
|
306
|
+
maintenanceAssetWeight: SPOT_MARKET_WEIGHT_PRECISION.toNumber(),
|
|
307
|
+
initialLiabilityWeight: SPOT_MARKET_WEIGHT_PRECISION.toNumber(),
|
|
308
|
+
maintenanceLiabilityWeight: SPOT_MARKET_WEIGHT_PRECISION.toNumber(),
|
|
307
309
|
scaleInitialAssetWeightStart: new BN(0),
|
|
308
310
|
imfFactor: 0,
|
|
309
311
|
withdrawGuardThreshold: new BN(0),
|
|
@@ -325,18 +327,18 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
325
327
|
flashLoanInitialTokenAmount: new BN(0),
|
|
326
328
|
oracleSource: OracleSource.PYTH,
|
|
327
329
|
historicalOracleData: {
|
|
328
|
-
lastOraclePrice:
|
|
330
|
+
lastOraclePrice: PRICE_PRECISION,
|
|
329
331
|
lastOracleConf: new BN(0),
|
|
330
332
|
lastOracleDelay: new BN(0),
|
|
331
|
-
lastOraclePriceTwap:
|
|
332
|
-
lastOraclePriceTwap5Min:
|
|
333
|
+
lastOraclePriceTwap: PRICE_PRECISION,
|
|
334
|
+
lastOraclePriceTwap5Min: PRICE_PRECISION,
|
|
333
335
|
lastOraclePriceTwapTs: new BN(0),
|
|
334
336
|
},
|
|
335
337
|
historicalIndexData: {
|
|
336
|
-
lastIndexBidPrice:
|
|
337
|
-
lastIndexAskPrice:
|
|
338
|
-
lastIndexPriceTwap:
|
|
339
|
-
lastIndexPriceTwap5Min:
|
|
338
|
+
lastIndexBidPrice: PRICE_PRECISION,
|
|
339
|
+
lastIndexAskPrice: PRICE_PRECISION,
|
|
340
|
+
lastIndexPriceTwap: PRICE_PRECISION,
|
|
341
|
+
lastIndexPriceTwap5Min: PRICE_PRECISION,
|
|
340
342
|
lastIndexPriceTwapTs: new BN(0),
|
|
341
343
|
},
|
|
342
344
|
},
|