@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/lib/math/amm.d.ts
CHANGED
|
@@ -35,9 +35,9 @@ export declare type AssetType = 'quote' | 'base';
|
|
|
35
35
|
export declare function calculateAmmReservesAfterSwap(amm: Pick<AMM, 'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'>, inputAssetType: AssetType, swapAmount: BN, swapDirection: SwapDirection): [BN, BN];
|
|
36
36
|
export declare function calculateMarketOpenBidAsk(baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [BN, BN];
|
|
37
37
|
export declare function calculateInventoryScale(netBaseAssetAmount: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): number;
|
|
38
|
-
export declare function calculateEffectiveLeverage(baseSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN,
|
|
38
|
+
export declare function calculateEffectiveLeverage(baseSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, reservePrice: BN, totalFeeMinusDistributions: BN): number;
|
|
39
39
|
export declare function calculateMaxSpread(marginRatioInitial: number): number;
|
|
40
|
-
export declare function calculateSpreadBN(baseSpread: number,
|
|
40
|
+
export declare function calculateSpreadBN(baseSpread: number, lastOracleReservePriceSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, reservePrice: BN, totalFeeMinusDistributions: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [number, number];
|
|
41
41
|
export declare function calculateSpread(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): number;
|
|
42
42
|
export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): {
|
|
43
43
|
baseAssetReserve: BN;
|
package/lib/math/amm.js
CHANGED
|
@@ -16,7 +16,7 @@ function calculatePegFromTargetPrice(targetPrice, baseAssetReserve, quoteAssetRe
|
|
|
16
16
|
}
|
|
17
17
|
exports.calculatePegFromTargetPrice = calculatePegFromTargetPrice;
|
|
18
18
|
function calculateOptimalPegAndBudget(amm, oraclePriceData) {
|
|
19
|
-
const
|
|
19
|
+
const reservePriceBefore = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
20
20
|
const targetPrice = oraclePriceData.price;
|
|
21
21
|
const newPeg = calculatePegFromTargetPrice(targetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
22
22
|
const prePegCost = (0, repeg_1.calculateRepegCost)(amm, newPeg);
|
|
@@ -29,14 +29,14 @@ function calculateOptimalPegAndBudget(amm, oraclePriceData) {
|
|
|
29
29
|
let newTargetPrice;
|
|
30
30
|
let newOptimalPeg;
|
|
31
31
|
let newBudget;
|
|
32
|
-
const targetPriceGap =
|
|
32
|
+
const targetPriceGap = reservePriceBefore.sub(targetPrice);
|
|
33
33
|
if (targetPriceGap.abs().gt(maxPriceSpread)) {
|
|
34
34
|
const markAdj = targetPriceGap.abs().sub(maxPriceSpread);
|
|
35
35
|
if (targetPriceGap.lt(new anchor_1.BN(0))) {
|
|
36
|
-
newTargetPrice =
|
|
36
|
+
newTargetPrice = reservePriceBefore.add(markAdj);
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
39
|
-
newTargetPrice =
|
|
39
|
+
newTargetPrice = reservePriceBefore.sub(markAdj);
|
|
40
40
|
}
|
|
41
41
|
newOptimalPeg = calculatePegFromTargetPrice(newTargetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
42
42
|
newBudget = (0, repeg_1.calculateRepegCost)(amm, newOptimalPeg);
|
|
@@ -185,24 +185,22 @@ function calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBas
|
|
|
185
185
|
}
|
|
186
186
|
exports.calculateMarketOpenBidAsk = calculateMarketOpenBidAsk;
|
|
187
187
|
function calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
188
|
+
const maxScale = numericConstants_1.BID_ASK_SPREAD_PRECISION.mul(new anchor_1.BN(10));
|
|
188
189
|
// inventory skew
|
|
189
190
|
const [openBids, openAsks] = calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
190
191
|
const minSideLiquidity = anchor_1.BN.max(new anchor_1.BN(1), anchor_1.BN.min(openBids.abs(), openAsks.abs()));
|
|
191
|
-
const inventoryScale = anchor_1.BN.min(netBaseAssetAmount.abs()
|
|
192
|
-
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION.mul(new anchor_1.BN(10)))
|
|
193
|
-
.div(minSideLiquidity)
|
|
194
|
-
.toNumber() / numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber();
|
|
192
|
+
const inventoryScale = anchor_1.BN.min(maxScale, netBaseAssetAmount.mul(maxScale).div(minSideLiquidity).abs()).toNumber() / numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber();
|
|
195
193
|
return inventoryScale;
|
|
196
194
|
}
|
|
197
195
|
exports.calculateInventoryScale = calculateInventoryScale;
|
|
198
|
-
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount,
|
|
196
|
+
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, reservePrice, totalFeeMinusDistributions) {
|
|
199
197
|
// inventory skew
|
|
200
198
|
const netBaseAssetValue = quoteAssetReserve
|
|
201
199
|
.sub(terminalQuoteAssetReserve)
|
|
202
200
|
.mul(pegMultiplier)
|
|
203
201
|
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
204
202
|
const localBaseAssetValue = netBaseAssetAmount
|
|
205
|
-
.mul(
|
|
203
|
+
.mul(reservePrice)
|
|
206
204
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.PRICE_PRECISION));
|
|
207
205
|
const effectiveLeverage = localBaseAssetValue.sub(netBaseAssetValue).toNumber() /
|
|
208
206
|
(Math.max(0, totalFeeMinusDistributions.toNumber()) + 1) +
|
|
@@ -217,28 +215,30 @@ function calculateMaxSpread(marginRatioInitial) {
|
|
|
217
215
|
return maxTargetSpread;
|
|
218
216
|
}
|
|
219
217
|
exports.calculateMaxSpread = calculateMaxSpread;
|
|
220
|
-
function calculateSpreadBN(baseSpread,
|
|
218
|
+
function calculateSpreadBN(baseSpread, lastOracleReservePriceSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, reservePrice, totalFeeMinusDistributions, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
221
219
|
let longSpread = baseSpread / 2;
|
|
222
220
|
let shortSpread = baseSpread / 2;
|
|
223
|
-
if (
|
|
224
|
-
shortSpread = Math.max(shortSpread,
|
|
221
|
+
if (lastOracleReservePriceSpreadPct.gt(numericConstants_1.ZERO)) {
|
|
222
|
+
shortSpread = Math.max(shortSpread, lastOracleReservePriceSpreadPct.abs().toNumber() +
|
|
223
|
+
lastOracleConfPct.toNumber());
|
|
225
224
|
}
|
|
226
|
-
else if (
|
|
227
|
-
longSpread = Math.max(longSpread,
|
|
225
|
+
else if (lastOracleReservePriceSpreadPct.lt(numericConstants_1.ZERO)) {
|
|
226
|
+
longSpread = Math.max(longSpread, lastOracleReservePriceSpreadPct.abs().toNumber() +
|
|
227
|
+
lastOracleConfPct.toNumber());
|
|
228
228
|
}
|
|
229
|
-
const maxTargetSpread = Math.max(maxSpread,
|
|
230
|
-
const
|
|
229
|
+
const maxTargetSpread = Math.max(maxSpread, lastOracleReservePriceSpreadPct.abs().toNumber());
|
|
230
|
+
const MAX_BID_ASK_INVENTORY_SKEW_FACTOR = 10;
|
|
231
231
|
const inventoryScale = calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
232
|
-
const inventorySpreadScale = Math.min(
|
|
232
|
+
const inventorySpreadScale = Math.min(MAX_BID_ASK_INVENTORY_SKEW_FACTOR, 1 + inventoryScale);
|
|
233
233
|
if (netBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
234
234
|
longSpread *= inventorySpreadScale;
|
|
235
235
|
}
|
|
236
236
|
else if (netBaseAssetAmount.lt(numericConstants_1.ZERO)) {
|
|
237
237
|
shortSpread *= inventorySpreadScale;
|
|
238
238
|
}
|
|
239
|
-
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount,
|
|
239
|
+
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, reservePrice, totalFeeMinusDistributions);
|
|
240
240
|
if (totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
241
|
-
const spreadScale = Math.min(
|
|
241
|
+
const spreadScale = Math.min(MAX_BID_ASK_INVENTORY_SKEW_FACTOR, 1 + effectiveLeverage);
|
|
242
242
|
if (netBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
243
243
|
longSpread *= spreadScale;
|
|
244
244
|
}
|
|
@@ -247,8 +247,8 @@ function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPc
|
|
|
247
247
|
}
|
|
248
248
|
}
|
|
249
249
|
else {
|
|
250
|
-
longSpread *=
|
|
251
|
-
shortSpread *=
|
|
250
|
+
longSpread *= MAX_BID_ASK_INVENTORY_SKEW_FACTOR;
|
|
251
|
+
shortSpread *= MAX_BID_ASK_INVENTORY_SKEW_FACTOR;
|
|
252
252
|
}
|
|
253
253
|
const totalSpread = longSpread + shortSpread;
|
|
254
254
|
if (totalSpread > maxTargetSpread) {
|
|
@@ -268,17 +268,17 @@ function calculateSpread(amm, direction, oraclePriceData) {
|
|
|
268
268
|
if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
|
|
269
269
|
return amm.baseSpread / 2;
|
|
270
270
|
}
|
|
271
|
-
const
|
|
272
|
-
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) ||
|
|
271
|
+
const reservePrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
272
|
+
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || reservePrice;
|
|
273
273
|
const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
|
|
274
|
-
const targetMarkSpreadPct =
|
|
274
|
+
const targetMarkSpreadPct = reservePrice
|
|
275
275
|
.sub(targetPrice)
|
|
276
276
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
277
|
-
.div(
|
|
277
|
+
.div(reservePrice);
|
|
278
278
|
const confIntervalPct = confInterval
|
|
279
279
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
280
|
-
.div(
|
|
281
|
-
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount,
|
|
280
|
+
.div(reservePrice);
|
|
281
|
+
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, reservePrice, amm.totalFeeMinusDistributions, amm.baseAssetReserve, amm.minBaseAssetReserve, amm.maxBaseAssetReserve);
|
|
282
282
|
let spread;
|
|
283
283
|
if ((0, types_1.isVariant)(direction, 'long')) {
|
|
284
284
|
spread = longSpread;
|
|
@@ -396,6 +396,9 @@ function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oracleP
|
|
|
396
396
|
}
|
|
397
397
|
exports.calculateMaxBaseAssetAmountToTrade = calculateMaxBaseAssetAmountToTrade;
|
|
398
398
|
function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swapDirection) {
|
|
399
|
+
if ((0, types_1.isVariant)(swapDirection, 'remove')) {
|
|
400
|
+
quoteAssetReserves = quoteAssetReserves.add(numericConstants_1.ONE);
|
|
401
|
+
}
|
|
399
402
|
let quoteAssetAmount = quoteAssetReserves
|
|
400
403
|
.mul(pegMultiplier)
|
|
401
404
|
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
package/lib/math/market.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export declare function calculateBidPrice(market: PerpMarketAccount, oraclePrice
|
|
|
24
24
|
*/
|
|
25
25
|
export declare function calculateAskPrice(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
26
26
|
export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market: PerpMarketAccount): PerpMarketAccount;
|
|
27
|
-
export declare function
|
|
27
|
+
export declare function calculateOracleReserveSpread(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
28
28
|
export declare function calculateOracleSpread(price: BN, oraclePriceData: OraclePriceData): BN;
|
|
29
29
|
export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory): number;
|
|
30
30
|
export declare function calculateUnrealizedAssetWeight(market: PerpMarketAccount, quoteSpotMarket: SpotMarketAccount, unrealizedPnl: BN, marginCategory: MarginCategory, oraclePriceData: OraclePriceData): BN;
|
package/lib/math/market.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateNetUserPnlImbalance = exports.calculateNetUserPnl = exports.calculateMarketAvailablePNL = exports.calculateUnrealizedAssetWeight = exports.calculateMarketMarginRatio = exports.calculateOracleSpread = exports.
|
|
3
|
+
exports.calculateNetUserPnlImbalance = exports.calculateNetUserPnl = exports.calculateMarketAvailablePNL = exports.calculateUnrealizedAssetWeight = exports.calculateMarketMarginRatio = exports.calculateOracleSpread = exports.calculateOracleReserveSpread = exports.calculateNewMarketAfterTrade = exports.calculateAskPrice = exports.calculateBidPrice = exports.calculateReservePrice = void 0;
|
|
4
4
|
const anchor_1 = require("@project-serum/anchor");
|
|
5
5
|
const types_1 = require("../types");
|
|
6
6
|
const amm_1 = require("./amm");
|
|
@@ -50,11 +50,11 @@ function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
|
|
|
50
50
|
return newMarket;
|
|
51
51
|
}
|
|
52
52
|
exports.calculateNewMarketAfterTrade = calculateNewMarketAfterTrade;
|
|
53
|
-
function
|
|
54
|
-
const
|
|
55
|
-
return calculateOracleSpread(
|
|
53
|
+
function calculateOracleReserveSpread(market, oraclePriceData) {
|
|
54
|
+
const reservePrice = calculateReservePrice(market, oraclePriceData);
|
|
55
|
+
return calculateOracleSpread(reservePrice, oraclePriceData);
|
|
56
56
|
}
|
|
57
|
-
exports.
|
|
57
|
+
exports.calculateOracleReserveSpread = calculateOracleReserveSpread;
|
|
58
58
|
function calculateOracleSpread(price, oraclePriceData) {
|
|
59
59
|
return price.sub(oraclePriceData.price);
|
|
60
60
|
}
|
|
@@ -66,7 +66,7 @@ function calculateMarketMarginRatio(market, size, marginCategory) {
|
|
|
66
66
|
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, market.imfFactor, new anchor_1.BN(market.marginRatioInitial), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
67
67
|
break;
|
|
68
68
|
case 'Maintenance':
|
|
69
|
-
marginRatio = market.marginRatioMaintenance;
|
|
69
|
+
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, market.imfFactor, new anchor_1.BN(market.marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
70
70
|
break;
|
|
71
71
|
}
|
|
72
72
|
return marginRatio;
|
package/lib/math/spotBalance.js
CHANGED
|
@@ -115,20 +115,19 @@ exports.calculateUtilization = calculateUtilization;
|
|
|
115
115
|
function calculateInterestRate(bank) {
|
|
116
116
|
const utilization = calculateUtilization(bank);
|
|
117
117
|
let interestRate;
|
|
118
|
-
if (utilization.gt(bank.optimalUtilization)) {
|
|
119
|
-
const surplusUtilization = utilization.sub(bank.optimalUtilization);
|
|
120
|
-
const borrowRateSlope = bank.maxBorrowRate
|
|
121
|
-
.sub(bank.optimalBorrowRate)
|
|
118
|
+
if (utilization.gt(new anchor_1.BN(bank.optimalUtilization))) {
|
|
119
|
+
const surplusUtilization = utilization.sub(new anchor_1.BN(bank.optimalUtilization));
|
|
120
|
+
const borrowRateSlope = new anchor_1.BN(bank.maxBorrowRate - bank.optimalBorrowRate)
|
|
122
121
|
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
123
|
-
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
124
|
-
interestRate = bank.optimalBorrowRate.add(surplusUtilization
|
|
122
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(new anchor_1.BN(bank.optimalUtilization)));
|
|
123
|
+
interestRate = new anchor_1.BN(bank.optimalBorrowRate).add(surplusUtilization
|
|
125
124
|
.mul(borrowRateSlope)
|
|
126
125
|
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION));
|
|
127
126
|
}
|
|
128
127
|
else {
|
|
129
|
-
const borrowRateSlope = bank.optimalBorrowRate
|
|
128
|
+
const borrowRateSlope = new anchor_1.BN(bank.optimalBorrowRate)
|
|
130
129
|
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
131
|
-
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
130
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(new anchor_1.BN(bank.optimalUtilization)));
|
|
132
131
|
interestRate = utilization
|
|
133
132
|
.mul(borrowRateSlope)
|
|
134
133
|
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION);
|
package/lib/math/trade.js
CHANGED
|
@@ -138,20 +138,20 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
138
138
|
(0, assert_1.assert)(market.amm.baseAssetReserve.gt(numericConstants_1.ZERO));
|
|
139
139
|
(0, assert_1.assert)(targetPrice.gt(numericConstants_1.ZERO));
|
|
140
140
|
(0, assert_1.assert)(pct.lte(MAXPCT) && pct.gt(numericConstants_1.ZERO));
|
|
141
|
-
const
|
|
141
|
+
const reservePriceBefore = (0, market_1.calculateReservePrice)(market, oraclePriceData);
|
|
142
142
|
const bidPriceBefore = (0, market_1.calculateBidPrice)(market, oraclePriceData);
|
|
143
143
|
const askPriceBefore = (0, market_1.calculateAskPrice)(market, oraclePriceData);
|
|
144
144
|
let direction;
|
|
145
|
-
if (targetPrice.gt(
|
|
146
|
-
const priceGap = targetPrice.sub(
|
|
145
|
+
if (targetPrice.gt(reservePriceBefore)) {
|
|
146
|
+
const priceGap = targetPrice.sub(reservePriceBefore);
|
|
147
147
|
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
148
|
-
targetPrice =
|
|
148
|
+
targetPrice = reservePriceBefore.add(priceGapScaled);
|
|
149
149
|
direction = types_1.PositionDirection.LONG;
|
|
150
150
|
}
|
|
151
151
|
else {
|
|
152
|
-
const priceGap =
|
|
152
|
+
const priceGap = reservePriceBefore.sub(targetPrice);
|
|
153
153
|
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
154
|
-
targetPrice =
|
|
154
|
+
targetPrice = reservePriceBefore.sub(priceGapScaled);
|
|
155
155
|
direction = types_1.PositionDirection.SHORT;
|
|
156
156
|
}
|
|
157
157
|
let tradeSize;
|
|
@@ -179,7 +179,7 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
179
179
|
targetPrice.lt(askPriceBefore) &&
|
|
180
180
|
targetPrice.gt(bidPriceBefore)) {
|
|
181
181
|
// no trade, market is at target
|
|
182
|
-
if (
|
|
182
|
+
if (reservePriceBefore.gt(targetPrice)) {
|
|
183
183
|
direction = types_1.PositionDirection.SHORT;
|
|
184
184
|
}
|
|
185
185
|
else {
|
|
@@ -188,7 +188,7 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
188
188
|
tradeSize = numericConstants_1.ZERO;
|
|
189
189
|
return [direction, tradeSize, targetPrice, targetPrice];
|
|
190
190
|
}
|
|
191
|
-
else if (
|
|
191
|
+
else if (reservePriceBefore.gt(targetPrice)) {
|
|
192
192
|
// overestimate y2
|
|
193
193
|
baseAssetReserveAfter = (0, utils_1.squareRootBN)(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).sub(biasModifier)).sub(new anchor_1.BN(1));
|
|
194
194
|
quoteAssetReserveAfter = k.div(numericConstants_1.PRICE_PRECISION).div(baseAssetReserveAfter);
|
|
@@ -201,7 +201,7 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
201
201
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
202
202
|
baseSize = baseAssetReserveAfter.sub(baseAssetReserveBefore);
|
|
203
203
|
}
|
|
204
|
-
else if (
|
|
204
|
+
else if (reservePriceBefore.lt(targetPrice)) {
|
|
205
205
|
// underestimate y2
|
|
206
206
|
baseAssetReserveAfter = (0, utils_1.squareRootBN)(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).add(biasModifier)).add(new anchor_1.BN(1));
|
|
207
207
|
quoteAssetReserveAfter = k.div(numericConstants_1.PRICE_PRECISION).div(baseAssetReserveAfter);
|
|
@@ -222,11 +222,11 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
222
222
|
}
|
|
223
223
|
let tp1 = targetPrice;
|
|
224
224
|
let tp2 = markPriceAfter;
|
|
225
|
-
let originalDiff = targetPrice.sub(
|
|
225
|
+
let originalDiff = targetPrice.sub(reservePriceBefore);
|
|
226
226
|
if (direction == types_1.PositionDirection.SHORT) {
|
|
227
227
|
tp1 = markPriceAfter;
|
|
228
228
|
tp2 = targetPrice;
|
|
229
|
-
originalDiff =
|
|
229
|
+
originalDiff = reservePriceBefore.sub(targetPrice);
|
|
230
230
|
}
|
|
231
231
|
const entryPrice = tradeSize
|
|
232
232
|
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
package/lib/types.d.ts
CHANGED
|
@@ -1,10 +1,48 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { PublicKey, Transaction } from '@solana/web3.js';
|
|
3
3
|
import { BN } from '.';
|
|
4
|
+
export declare class ExchangeStatus {
|
|
5
|
+
static readonly ACTIVE: {
|
|
6
|
+
active: {};
|
|
7
|
+
};
|
|
8
|
+
static readonly FUNDINGPAUSED: {
|
|
9
|
+
fundingpaused: {};
|
|
10
|
+
};
|
|
11
|
+
static readonly AMMPAUSED: {
|
|
12
|
+
ammpaused: {};
|
|
13
|
+
};
|
|
14
|
+
static readonly FILLPAUSED: {
|
|
15
|
+
fillpaused: {};
|
|
16
|
+
};
|
|
17
|
+
static readonly LIQPAUSED: {
|
|
18
|
+
liqpaused: {};
|
|
19
|
+
};
|
|
20
|
+
static readonly WITHDRAWPAUSED: {
|
|
21
|
+
withdrawpaused: {};
|
|
22
|
+
};
|
|
23
|
+
static readonly PAUSED: {
|
|
24
|
+
paused: {};
|
|
25
|
+
};
|
|
26
|
+
}
|
|
4
27
|
export declare class MarketStatus {
|
|
5
28
|
static readonly INITIALIZED: {
|
|
6
29
|
initialized: {};
|
|
7
30
|
};
|
|
31
|
+
static readonly ACTIVE: {
|
|
32
|
+
active: {};
|
|
33
|
+
};
|
|
34
|
+
static readonly FUNDINGPAUSED: {
|
|
35
|
+
fundingpaused: {};
|
|
36
|
+
};
|
|
37
|
+
static readonly AMMPAUSED: {
|
|
38
|
+
ammpaused: {};
|
|
39
|
+
};
|
|
40
|
+
static readonly FILLPAUSED: {
|
|
41
|
+
fillpaused: {};
|
|
42
|
+
};
|
|
43
|
+
static readonly WITHDRAWPAUSED: {
|
|
44
|
+
withdrawpaused: {};
|
|
45
|
+
};
|
|
8
46
|
static readonly REDUCEONLY: {
|
|
9
47
|
reduceonly: {};
|
|
10
48
|
};
|
|
@@ -23,6 +61,37 @@ export declare class ContractType {
|
|
|
23
61
|
future: {};
|
|
24
62
|
};
|
|
25
63
|
}
|
|
64
|
+
export declare class ContractTier {
|
|
65
|
+
static readonly A: {
|
|
66
|
+
a: {};
|
|
67
|
+
};
|
|
68
|
+
static readonly B: {
|
|
69
|
+
b: {};
|
|
70
|
+
};
|
|
71
|
+
static readonly C: {
|
|
72
|
+
c: {};
|
|
73
|
+
};
|
|
74
|
+
static readonly Speculative: {
|
|
75
|
+
speculative: {};
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export declare class AssetTier {
|
|
79
|
+
static readonly COLLATERAL: {
|
|
80
|
+
collateral: {};
|
|
81
|
+
};
|
|
82
|
+
static readonly PROTECTED: {
|
|
83
|
+
protected: {};
|
|
84
|
+
};
|
|
85
|
+
static readonly CROSS: {
|
|
86
|
+
cross: {};
|
|
87
|
+
};
|
|
88
|
+
static readonly ISOLATED: {
|
|
89
|
+
isolated: {};
|
|
90
|
+
};
|
|
91
|
+
static readonly UNLISTED: {
|
|
92
|
+
unlisted: {};
|
|
93
|
+
};
|
|
94
|
+
}
|
|
26
95
|
export declare class SwapDirection {
|
|
27
96
|
static readonly ADD: {
|
|
28
97
|
add: {};
|
|
@@ -197,9 +266,22 @@ export declare type DepositRecord = {
|
|
|
197
266
|
marketIndex: number;
|
|
198
267
|
amount: BN;
|
|
199
268
|
oraclePrice: BN;
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
269
|
+
marketDepositBalance: BN;
|
|
270
|
+
marketWithdrawBalance: BN;
|
|
271
|
+
marketCumulativeDepositInterest: BN;
|
|
272
|
+
marketCumulativeBorrowInterest: BN;
|
|
273
|
+
transferUser?: PublicKey;
|
|
274
|
+
};
|
|
275
|
+
export declare type SpotInterestRecord = {
|
|
276
|
+
ts: BN;
|
|
277
|
+
marketIndex: number;
|
|
278
|
+
depositBalance: BN;
|
|
279
|
+
cumulativeDepositInterest: BN;
|
|
280
|
+
borrowBalance: BN;
|
|
281
|
+
cumulativeBorrowInterest: BN;
|
|
282
|
+
optimalUtilization: number;
|
|
283
|
+
optimalBorrowRate: number;
|
|
284
|
+
maxBorrowRate: number;
|
|
203
285
|
};
|
|
204
286
|
export declare type CurveRecord = {
|
|
205
287
|
ts: BN;
|
|
@@ -224,8 +306,8 @@ export declare type InsuranceFundRecord = {
|
|
|
224
306
|
ts: BN;
|
|
225
307
|
bankIndex: BN;
|
|
226
308
|
marketIndex: number;
|
|
227
|
-
userIfFactor:
|
|
228
|
-
totalIfFactor:
|
|
309
|
+
userIfFactor: number;
|
|
310
|
+
totalIfFactor: number;
|
|
229
311
|
vaultAmountBefore: BN;
|
|
230
312
|
insuranceVaultAmountBefore: BN;
|
|
231
313
|
amount: BN;
|
|
@@ -276,7 +358,6 @@ export declare type FundingPaymentRecord = {
|
|
|
276
358
|
fundingPayment: BN;
|
|
277
359
|
baseAssetAmount: BN;
|
|
278
360
|
userLastCumulativeFunding: BN;
|
|
279
|
-
userLastFundingRateTs: BN;
|
|
280
361
|
ammCumulativeFundingLong: BN;
|
|
281
362
|
ammCumulativeFundingShort: BN;
|
|
282
363
|
};
|
|
@@ -290,11 +371,11 @@ export declare type LiquidationRecord = {
|
|
|
290
371
|
liquidationId: number;
|
|
291
372
|
canceledOrderIds: BN[];
|
|
292
373
|
liquidatePerp: LiquidatePerpRecord;
|
|
293
|
-
|
|
374
|
+
liquidateSpot: LiquidateSpotRecord;
|
|
294
375
|
liquidateBorrowForPerpPnl: LiquidateBorrowForPerpPnlRecord;
|
|
295
376
|
liquidatePerpPnlForDeposit: LiquidatePerpPnlForDepositRecord;
|
|
296
377
|
perpBankruptcy: PerpBankruptcyRecord;
|
|
297
|
-
|
|
378
|
+
spotBankruptcy: SpotBankruptcyRecord;
|
|
298
379
|
};
|
|
299
380
|
export declare class LiquidationType {
|
|
300
381
|
static readonly LIQUIDATE_PERP: {
|
|
@@ -322,14 +403,12 @@ export declare type LiquidatePerpRecord = {
|
|
|
322
403
|
baseAssetAmount: BN;
|
|
323
404
|
quoteAssetAmount: BN;
|
|
324
405
|
lpShares: BN;
|
|
325
|
-
userPnl: BN;
|
|
326
|
-
liquidatorPnl: BN;
|
|
327
406
|
userOrderId: BN;
|
|
328
407
|
liquidatorOrderId: BN;
|
|
329
408
|
fillRecordId: BN;
|
|
330
409
|
ifFee: BN;
|
|
331
410
|
};
|
|
332
|
-
export declare type
|
|
411
|
+
export declare type LiquidateSpotRecord = {
|
|
333
412
|
assetMarketIndex: number;
|
|
334
413
|
assetPrice: BN;
|
|
335
414
|
assetTransfer: BN;
|
|
@@ -359,7 +438,7 @@ export declare type PerpBankruptcyRecord = {
|
|
|
359
438
|
pnl: BN;
|
|
360
439
|
cumulativeFundingRateDelta: BN;
|
|
361
440
|
};
|
|
362
|
-
export declare type
|
|
441
|
+
export declare type SpotBankruptcyRecord = {
|
|
363
442
|
marketIndex: number;
|
|
364
443
|
borrowAmount: BN;
|
|
365
444
|
cumulativeDepositInterestDelta: BN;
|
|
@@ -395,14 +474,14 @@ export declare type OrderActionRecord = {
|
|
|
395
474
|
referrerReward: number | null;
|
|
396
475
|
quoteAssetAmountSurplus: BN | null;
|
|
397
476
|
taker: PublicKey | null;
|
|
398
|
-
takerOrderId:
|
|
477
|
+
takerOrderId: number | null;
|
|
399
478
|
takerOrderDirection: PositionDirection | null;
|
|
400
479
|
takerOrderBaseAssetAmount: BN | null;
|
|
401
480
|
takerOrderCumulativeBaseAssetAmountFilled: BN | null;
|
|
402
481
|
takerOrderCumulativeQuoteAssetAmountFilled: BN | null;
|
|
403
482
|
takerOrderFee: BN | null;
|
|
404
483
|
maker: PublicKey | null;
|
|
405
|
-
makerOrderId:
|
|
484
|
+
makerOrderId: number | null;
|
|
406
485
|
makerOrderDirection: PositionDirection | null;
|
|
407
486
|
makerOrderBaseAssetAmount: BN | null;
|
|
408
487
|
makerOrderCumulativeBaseAssetAmountFilled: BN | null;
|
|
@@ -412,24 +491,20 @@ export declare type OrderActionRecord = {
|
|
|
412
491
|
};
|
|
413
492
|
export declare type StateAccount = {
|
|
414
493
|
admin: PublicKey;
|
|
415
|
-
|
|
416
|
-
exchangePaused: boolean;
|
|
417
|
-
adminControlsPrices: boolean;
|
|
418
|
-
totalFee: BN;
|
|
419
|
-
totalFeeWithdrawn: BN;
|
|
494
|
+
exchangeStatus: ExchangeStatus;
|
|
420
495
|
whitelistMint: PublicKey;
|
|
421
496
|
discountMint: PublicKey;
|
|
422
497
|
oracleGuardRails: OracleGuardRails;
|
|
423
|
-
maxDeposit: BN;
|
|
424
498
|
numberOfMarkets: number;
|
|
425
499
|
numberOfSpotMarkets: number;
|
|
426
500
|
minOrderQuoteAssetAmount: BN;
|
|
427
|
-
signer: PublicKey;
|
|
428
|
-
signerNonce: number;
|
|
429
|
-
defaultMarketOrderTimeInForce: number;
|
|
430
501
|
minPerpAuctionDuration: number;
|
|
502
|
+
defaultMarketOrderTimeInForce: number;
|
|
431
503
|
defaultSpotAuctionDuration: number;
|
|
432
504
|
liquidationMarginBufferRatio: number;
|
|
505
|
+
settlementDuration: number;
|
|
506
|
+
signer: PublicKey;
|
|
507
|
+
signerNonce: number;
|
|
433
508
|
srmVault: PublicKey;
|
|
434
509
|
perpFeeStructure: FeeStructure;
|
|
435
510
|
spotFeeStructure: FeeStructure;
|
|
@@ -479,6 +554,8 @@ export declare type HistoricalIndexData = {
|
|
|
479
554
|
lastIndexPriceTwapTs: BN;
|
|
480
555
|
};
|
|
481
556
|
export declare type SpotMarketAccount = {
|
|
557
|
+
status: MarketStatus;
|
|
558
|
+
assetTier: AssetTier;
|
|
482
559
|
marketIndex: number;
|
|
483
560
|
pubkey: PublicKey;
|
|
484
561
|
mint: PublicKey;
|
|
@@ -492,17 +569,18 @@ export declare type SpotMarketAccount = {
|
|
|
492
569
|
revenuePool: PoolBalance;
|
|
493
570
|
totalIfShares: BN;
|
|
494
571
|
userIfShares: BN;
|
|
495
|
-
userIfFactor:
|
|
496
|
-
totalIfFactor:
|
|
572
|
+
userIfFactor: number;
|
|
573
|
+
totalIfFactor: number;
|
|
497
574
|
ifLiquidationFee: BN;
|
|
498
575
|
decimals: number;
|
|
499
|
-
optimalUtilization:
|
|
500
|
-
optimalBorrowRate:
|
|
501
|
-
maxBorrowRate:
|
|
576
|
+
optimalUtilization: number;
|
|
577
|
+
optimalBorrowRate: number;
|
|
578
|
+
maxBorrowRate: number;
|
|
502
579
|
cumulativeDepositInterest: BN;
|
|
503
580
|
cumulativeBorrowInterest: BN;
|
|
504
581
|
depositBalance: BN;
|
|
505
582
|
borrowBalance: BN;
|
|
583
|
+
maxTokenDeposits: BN;
|
|
506
584
|
lastInterestTs: BN;
|
|
507
585
|
lastTwapTs: BN;
|
|
508
586
|
initialAssetWeight: BN;
|
|
@@ -517,13 +595,12 @@ export declare type SpotMarketAccount = {
|
|
|
517
595
|
utilizationTwap: BN;
|
|
518
596
|
orderStepSize: BN;
|
|
519
597
|
nextFillRecordId: BN;
|
|
520
|
-
spotFeePool:
|
|
521
|
-
balance: BN;
|
|
522
|
-
};
|
|
598
|
+
spotFeePool: PoolBalance;
|
|
523
599
|
totalSpotFee: BN;
|
|
524
600
|
};
|
|
525
601
|
export declare type PoolBalance = {
|
|
526
602
|
balance: BN;
|
|
603
|
+
marketIndex: number;
|
|
527
604
|
};
|
|
528
605
|
export declare type AMM = {
|
|
529
606
|
baseAssetReserve: BN;
|
|
@@ -537,7 +614,7 @@ export declare type AMM = {
|
|
|
537
614
|
oracle: PublicKey;
|
|
538
615
|
oracleSource: OracleSource;
|
|
539
616
|
historicalOracleData: HistoricalOracleData;
|
|
540
|
-
|
|
617
|
+
lastOracleReservePriceSpreadPct: BN;
|
|
541
618
|
lastOracleConfPct: BN;
|
|
542
619
|
fundingPeriod: BN;
|
|
543
620
|
quoteAssetReserve: BN;
|
|
@@ -583,7 +660,6 @@ export declare type AMM = {
|
|
|
583
660
|
};
|
|
584
661
|
export declare type PerpPosition = {
|
|
585
662
|
baseAssetAmount: BN;
|
|
586
|
-
remainderBaseAssetAmount: BN;
|
|
587
663
|
lastCumulativeFundingRate: BN;
|
|
588
664
|
marketIndex: number;
|
|
589
665
|
quoteAssetAmount: BN;
|
|
@@ -593,7 +669,7 @@ export declare type PerpPosition = {
|
|
|
593
669
|
openAsks: BN;
|
|
594
670
|
settledPnl: BN;
|
|
595
671
|
lpShares: BN;
|
|
596
|
-
|
|
672
|
+
remainderBaseAssetAmount: number;
|
|
597
673
|
lastNetBaseAssetAmountPerLp: BN;
|
|
598
674
|
lastNetQuoteAssetAmountPerLp: BN;
|
|
599
675
|
};
|
|
@@ -628,7 +704,7 @@ export declare type UserAccount = {
|
|
|
628
704
|
beingLiquidated: boolean;
|
|
629
705
|
bankrupt: boolean;
|
|
630
706
|
nextLiquidationId: number;
|
|
631
|
-
nextOrderId:
|
|
707
|
+
nextOrderId: number;
|
|
632
708
|
customMarginRatio: number;
|
|
633
709
|
};
|
|
634
710
|
export declare type SpotPosition = {
|
|
@@ -646,7 +722,7 @@ export declare type Order = {
|
|
|
646
722
|
marketType: MarketType;
|
|
647
723
|
ts: BN;
|
|
648
724
|
slot: BN;
|
|
649
|
-
orderId:
|
|
725
|
+
orderId: number;
|
|
650
726
|
userOrderId: number;
|
|
651
727
|
marketIndex: number;
|
|
652
728
|
price: BN;
|
|
@@ -680,10 +756,10 @@ export declare type OrderParams = {
|
|
|
680
756
|
reduceOnly: boolean;
|
|
681
757
|
postOnly: boolean;
|
|
682
758
|
immediateOrCancel: boolean;
|
|
683
|
-
triggerPrice: BN;
|
|
759
|
+
triggerPrice: BN | null;
|
|
684
760
|
triggerCondition: OrderTriggerCondition;
|
|
685
761
|
positionLimit: BN;
|
|
686
|
-
oraclePriceOffset: BN;
|
|
762
|
+
oraclePriceOffset: BN | null;
|
|
687
763
|
auctionDuration: number | null;
|
|
688
764
|
timeInForce: number | null;
|
|
689
765
|
auctionStartPrice: BN | null;
|
|
@@ -714,12 +790,12 @@ export declare const DefaultOrderParams: {
|
|
|
714
790
|
reduceOnly: boolean;
|
|
715
791
|
postOnly: boolean;
|
|
716
792
|
immediateOrCancel: boolean;
|
|
717
|
-
triggerPrice:
|
|
793
|
+
triggerPrice: any;
|
|
718
794
|
triggerCondition: {
|
|
719
795
|
above: {};
|
|
720
796
|
};
|
|
721
797
|
positionLimit: BN;
|
|
722
|
-
oraclePriceOffset:
|
|
798
|
+
oraclePriceOffset: any;
|
|
723
799
|
auctionDuration: any;
|
|
724
800
|
timeInForce: any;
|
|
725
801
|
auctionStartPrice: any;
|
|
@@ -727,6 +803,7 @@ export declare const DefaultOrderParams: {
|
|
|
727
803
|
export declare type MakerInfo = {
|
|
728
804
|
maker: PublicKey;
|
|
729
805
|
makerStats: PublicKey;
|
|
806
|
+
makerUserAccount: UserAccount;
|
|
730
807
|
order: Order;
|
|
731
808
|
};
|
|
732
809
|
export declare type TakerInfo = {
|