@drift-labs/sdk 2.143.0-beta.7 → 2.143.0-beta.9
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/lib/browser/adminClient.d.ts +2 -2
- package/lib/browser/adminClient.js +5 -5
- package/lib/browser/dlob/DLOB.d.ts +9 -3
- package/lib/browser/dlob/DLOB.js +10 -13
- package/lib/browser/idl/drift.json +18 -22
- package/lib/browser/math/auction.d.ts +3 -2
- package/lib/browser/math/auction.js +23 -4
- package/lib/browser/math/margin.d.ts +2 -1
- package/lib/browser/math/margin.js +31 -3
- package/lib/browser/math/market.js +38 -18
- package/lib/browser/math/oracles.d.ts +2 -1
- package/lib/browser/math/oracles.js +55 -1
- package/lib/browser/math/orders.d.ts +3 -2
- package/lib/browser/math/orders.js +13 -3
- package/lib/browser/types.d.ts +12 -1
- package/lib/browser/types.js +12 -1
- package/lib/node/adminClient.d.ts +2 -2
- package/lib/node/adminClient.d.ts.map +1 -1
- package/lib/node/adminClient.js +5 -5
- package/lib/node/dlob/DLOB.d.ts +9 -3
- package/lib/node/dlob/DLOB.d.ts.map +1 -1
- package/lib/node/dlob/DLOB.js +10 -13
- package/lib/node/idl/drift.json +18 -22
- package/lib/node/math/auction.d.ts +3 -2
- package/lib/node/math/auction.d.ts.map +1 -1
- package/lib/node/math/auction.js +23 -4
- package/lib/node/math/margin.d.ts +2 -1
- package/lib/node/math/margin.d.ts.map +1 -1
- package/lib/node/math/margin.js +31 -3
- package/lib/node/math/market.d.ts.map +1 -1
- package/lib/node/math/market.js +38 -18
- package/lib/node/math/oracles.d.ts +2 -1
- package/lib/node/math/oracles.d.ts.map +1 -1
- package/lib/node/math/oracles.js +55 -1
- package/lib/node/math/orders.d.ts +3 -2
- package/lib/node/math/orders.d.ts.map +1 -1
- package/lib/node/math/orders.js +13 -3
- package/lib/node/types.d.ts +12 -1
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/types.js +12 -1
- package/package.json +1 -1
- package/src/adminClient.ts +10 -10
- package/src/dlob/DLOB.ts +28 -15
- package/src/idl/drift.json +18 -22
- package/src/math/auction.ts +50 -7
- package/src/math/margin.ts +44 -5
- package/src/math/market.ts +61 -31
- package/src/math/oracles.ts +88 -0
- package/src/math/orders.ts +30 -2
- package/src/types.ts +13 -1
- package/tests/dlob/helpers.ts +4 -4
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.143.0-beta.
|
|
1
|
+
2.143.0-beta.9
|
|
@@ -201,8 +201,8 @@ export declare class AdminClient extends DriftClient {
|
|
|
201
201
|
getUpdateSpotMarketFuelIx(spotMarketIndex: number, fuelBoostDeposits?: number, fuelBoostBorrows?: number, fuelBoostTaker?: number, fuelBoostMaker?: number, fuelBoostInsurance?: number): Promise<TransactionInstruction>;
|
|
202
202
|
updatePerpMarketFuel(perpMarketIndex: number, fuelBoostTaker?: number, fuelBoostMaker?: number, fuelBoostPosition?: number): Promise<TransactionSignature>;
|
|
203
203
|
getUpdatePerpMarketFuelIx(perpMarketIndex: number, fuelBoostTaker?: number, fuelBoostMaker?: number, fuelBoostPosition?: number): Promise<TransactionInstruction>;
|
|
204
|
-
|
|
205
|
-
|
|
204
|
+
updatePerpMarketOracleLowRiskSlotDelayOverride(perpMarketIndex: number, oracleLowRiskSlotDelayOverride: number): Promise<TransactionSignature>;
|
|
205
|
+
getUpdatePerpMarketOracleLowRiskSlotDelayOverrideIx(perpMarketIndex: number, oracleLowRiskSlotDelayOverride: number): Promise<TransactionInstruction>;
|
|
206
206
|
updatePerpMarketOracleSlotDelayOverride(perpMarketIndex: number, oracleSlotDelay: number): Promise<TransactionSignature>;
|
|
207
207
|
getUpdatePerpMarketOracleSlotDelayOverrideIx(perpMarketIndex: number, oracleSlotDelay: number): Promise<TransactionInstruction>;
|
|
208
208
|
updatePerpMarketAmmSpreadAdjustment(perpMarketIndex: number, ammSpreadAdjustment: number, ammInventorySpreadAdjustment: number, referencePriceOffset: number): Promise<TransactionSignature>;
|
|
@@ -1856,15 +1856,15 @@ class AdminClient extends driftClient_1.DriftClient {
|
|
|
1856
1856
|
},
|
|
1857
1857
|
});
|
|
1858
1858
|
}
|
|
1859
|
-
async
|
|
1860
|
-
const
|
|
1861
|
-
const tx = await this.buildTransaction(
|
|
1859
|
+
async updatePerpMarketOracleLowRiskSlotDelayOverride(perpMarketIndex, oracleLowRiskSlotDelayOverride) {
|
|
1860
|
+
const updatePerpMarketOracleLowRiskSlotDelayOverrideIx = await this.getUpdatePerpMarketOracleLowRiskSlotDelayOverrideIx(perpMarketIndex, oracleLowRiskSlotDelayOverride);
|
|
1861
|
+
const tx = await this.buildTransaction(updatePerpMarketOracleLowRiskSlotDelayOverrideIx);
|
|
1862
1862
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
1863
1863
|
return txSig;
|
|
1864
1864
|
}
|
|
1865
|
-
async
|
|
1865
|
+
async getUpdatePerpMarketOracleLowRiskSlotDelayOverrideIx(perpMarketIndex, oracleLowRiskSlotDelayOverride) {
|
|
1866
1866
|
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex);
|
|
1867
|
-
return await this.program.instruction.
|
|
1867
|
+
return await this.program.instruction.updatePerpMarketOracleLowRiskSlotDelayOverride(oracleLowRiskSlotDelayOverride, {
|
|
1868
1868
|
accounts: {
|
|
1869
1869
|
admin: this.useHotWalletAdmin
|
|
1870
1870
|
? this.wallet.publicKey
|
|
@@ -95,10 +95,14 @@ export declare class DLOB {
|
|
|
95
95
|
mergeNodesToFill(restingLimitOrderNodesToFill: NodeToFill[], takingOrderNodesToFill: NodeToFill[]): NodeToFill[];
|
|
96
96
|
findRestingLimitOrderNodesToFill<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
97
97
|
spot: unknown;
|
|
98
|
-
} ? OraclePriceData : MMOraclePriceData, isAmmPaused: boolean,
|
|
98
|
+
} ? OraclePriceData : MMOraclePriceData, isAmmPaused: boolean, stateAccount: StateAccount, marketAccount: T extends {
|
|
99
|
+
spot: unknown;
|
|
100
|
+
} ? SpotMarketAccount : PerpMarketAccount, makerRebateNumerator: number, makerRebateDenominator: number, fallbackAsk: BN | undefined, fallbackBid: BN | undefined): NodeToFill[];
|
|
99
101
|
findTakingNodesToFill<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
100
102
|
spot: unknown;
|
|
101
|
-
} ? OraclePriceData : MMOraclePriceData, isAmmPaused: boolean,
|
|
103
|
+
} ? OraclePriceData : MMOraclePriceData, isAmmPaused: boolean, state: StateAccount, marketAccount: T extends {
|
|
104
|
+
spot: unknown;
|
|
105
|
+
} ? SpotMarketAccount : PerpMarketAccount, fallbackAsk: BN | undefined, fallbackBid?: BN | undefined): NodeToFill[];
|
|
102
106
|
findTakingNodesCrossingMakerNodes<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
103
107
|
spot: unknown;
|
|
104
108
|
} ? OraclePriceData : MMOraclePriceData, takerNodeGenerator: Generator<DLOBNode>, makerNodeGeneratorFn: (marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: T extends {
|
|
@@ -106,7 +110,9 @@ export declare class DLOB {
|
|
|
106
110
|
} ? OraclePriceData : MMOraclePriceData) => Generator<DLOBNode>, doesCross: (takerPrice: BN | undefined, makerPrice: BN) => boolean): NodeToFill[];
|
|
107
111
|
findNodesCrossingFallbackLiquidity<T extends MarketType>(marketType: T, slot: number, oraclePriceData: T extends {
|
|
108
112
|
spot: unknown;
|
|
109
|
-
} ? OraclePriceData : MMOraclePriceData, nodeGenerator: Generator<DLOBNode>, doesCross: (nodePrice: BN | undefined) => boolean,
|
|
113
|
+
} ? OraclePriceData : MMOraclePriceData, nodeGenerator: Generator<DLOBNode>, doesCross: (nodePrice: BN | undefined) => boolean, state: StateAccount, marketAccount: T extends {
|
|
114
|
+
spot: unknown;
|
|
115
|
+
} ? SpotMarketAccount : PerpMarketAccount): NodeToFill[];
|
|
110
116
|
findExpiredNodesToFill(marketIndex: number, ts: number, marketType: MarketType, slot?: BN): NodeToFill[];
|
|
111
117
|
findUnfillableReduceOnlyOrdersToCancel(marketIndex: number, marketType: MarketType, stepSize: BN): NodeToFill[];
|
|
112
118
|
getTakingBids<T extends MarketType>(marketIndex: number, marketType: T, slot: number, oraclePriceData: T extends {
|
package/lib/browser/dlob/DLOB.js
CHANGED
|
@@ -250,12 +250,9 @@ class DLOB {
|
|
|
250
250
|
return [];
|
|
251
251
|
}
|
|
252
252
|
const isAmmPaused = (0, exchangeStatus_1.ammPaused)(stateAccount, marketAccount);
|
|
253
|
-
const minAuctionDuration = (0, types_1.isVariant)(marketType, 'perp')
|
|
254
|
-
? stateAccount.minPerpAuctionDuration
|
|
255
|
-
: 0;
|
|
256
253
|
const { makerRebateNumerator, makerRebateDenominator } = this.getMakerRebate(marketType, stateAccount, marketAccount);
|
|
257
|
-
const takingOrderNodesToFill = this.findTakingNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused,
|
|
258
|
-
const restingLimitOrderNodesToFill = this.findRestingLimitOrderNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused,
|
|
254
|
+
const takingOrderNodesToFill = this.findTakingNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, stateAccount, marketAccount, fallbackAsk, fallbackBid);
|
|
255
|
+
const restingLimitOrderNodesToFill = this.findRestingLimitOrderNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, stateAccount, marketAccount, makerRebateNumerator, makerRebateDenominator, fallbackAsk, fallbackBid);
|
|
259
256
|
// get expired market nodes
|
|
260
257
|
const expiredNodesToFill = this.findExpiredNodesToFill(marketIndex, ts, marketType, new anchor_1.BN(slot));
|
|
261
258
|
const stepSize = (0, types_1.isVariant)(marketType, 'perp')
|
|
@@ -310,7 +307,7 @@ class DLOB {
|
|
|
310
307
|
mergeNodesToFillHelper(takingOrderNodesToFill);
|
|
311
308
|
return Array.from(mergedNodesToFill.values());
|
|
312
309
|
}
|
|
313
|
-
findRestingLimitOrderNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused,
|
|
310
|
+
findRestingLimitOrderNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, stateAccount, marketAccount, makerRebateNumerator, makerRebateDenominator, fallbackAsk, fallbackBid) {
|
|
314
311
|
const nodesToFill = new Array();
|
|
315
312
|
const crossingNodes = this.findCrossingRestingLimitOrders(marketIndex, slot, marketType, oraclePriceData);
|
|
316
313
|
for (const crossingNode of crossingNodes) {
|
|
@@ -321,7 +318,7 @@ class DLOB {
|
|
|
321
318
|
const fallbackBidWithBuffer = fallbackBid.sub(fallbackBid.muln(makerRebateNumerator).divn(makerRebateDenominator));
|
|
322
319
|
const asksCrossingFallback = this.findNodesCrossingFallbackLiquidity(marketType, slot, oraclePriceData, askGenerator, (askPrice) => {
|
|
323
320
|
return askPrice.lte(fallbackBidWithBuffer);
|
|
324
|
-
},
|
|
321
|
+
}, stateAccount, marketAccount);
|
|
325
322
|
for (const askCrossingFallback of asksCrossingFallback) {
|
|
326
323
|
nodesToFill.push(askCrossingFallback);
|
|
327
324
|
}
|
|
@@ -331,14 +328,14 @@ class DLOB {
|
|
|
331
328
|
const fallbackAskWithBuffer = fallbackAsk.add(fallbackAsk.muln(makerRebateNumerator).divn(makerRebateDenominator));
|
|
332
329
|
const bidsCrossingFallback = this.findNodesCrossingFallbackLiquidity(marketType, slot, oraclePriceData, bidGenerator, (bidPrice) => {
|
|
333
330
|
return bidPrice.gte(fallbackAskWithBuffer);
|
|
334
|
-
},
|
|
331
|
+
}, stateAccount, marketAccount);
|
|
335
332
|
for (const bidCrossingFallback of bidsCrossingFallback) {
|
|
336
333
|
nodesToFill.push(bidCrossingFallback);
|
|
337
334
|
}
|
|
338
335
|
}
|
|
339
336
|
return nodesToFill;
|
|
340
337
|
}
|
|
341
|
-
findTakingNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused,
|
|
338
|
+
findTakingNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, state, marketAccount, fallbackAsk, fallbackBid) {
|
|
342
339
|
const nodesToFill = new Array();
|
|
343
340
|
let takingOrderGenerator = this.getTakingAsks(marketIndex, marketType, slot, oraclePriceData);
|
|
344
341
|
const takingAsksCrossingBids = this.findTakingNodesCrossingMakerNodes(marketIndex, slot, marketType, oraclePriceData, takingOrderGenerator, this.getRestingLimitBids.bind(this), (takerPrice, makerPrice) => {
|
|
@@ -359,7 +356,7 @@ class DLOB {
|
|
|
359
356
|
takingOrderGenerator = this.getTakingAsks(marketIndex, marketType, slot, oraclePriceData);
|
|
360
357
|
const takingAsksCrossingFallback = this.findNodesCrossingFallbackLiquidity(marketType, slot, oraclePriceData, takingOrderGenerator, (takerPrice) => {
|
|
361
358
|
return takerPrice === undefined || takerPrice.lte(fallbackBid);
|
|
362
|
-
},
|
|
359
|
+
}, state, marketAccount);
|
|
363
360
|
for (const takingAskCrossingFallback of takingAsksCrossingFallback) {
|
|
364
361
|
nodesToFill.push(takingAskCrossingFallback);
|
|
365
362
|
}
|
|
@@ -383,7 +380,7 @@ class DLOB {
|
|
|
383
380
|
takingOrderGenerator = this.getTakingBids(marketIndex, marketType, slot, oraclePriceData);
|
|
384
381
|
const takingBidsCrossingFallback = this.findNodesCrossingFallbackLiquidity(marketType, slot, oraclePriceData, takingOrderGenerator, (takerPrice) => {
|
|
385
382
|
return takerPrice === undefined || takerPrice.gte(fallbackAsk);
|
|
386
|
-
},
|
|
383
|
+
}, state, marketAccount);
|
|
387
384
|
for (const marketBidCrossingFallback of takingBidsCrossingFallback) {
|
|
388
385
|
nodesToFill.push(marketBidCrossingFallback);
|
|
389
386
|
}
|
|
@@ -441,7 +438,7 @@ class DLOB {
|
|
|
441
438
|
}
|
|
442
439
|
return nodesToFill;
|
|
443
440
|
}
|
|
444
|
-
findNodesCrossingFallbackLiquidity(marketType, slot, oraclePriceData, nodeGenerator, doesCross,
|
|
441
|
+
findNodesCrossingFallbackLiquidity(marketType, slot, oraclePriceData, nodeGenerator, doesCross, state, marketAccount) {
|
|
445
442
|
var _a;
|
|
446
443
|
const nodesToFill = new Array();
|
|
447
444
|
let nextNode = nodeGenerator.next();
|
|
@@ -456,7 +453,7 @@ class DLOB {
|
|
|
456
453
|
const crosses = doesCross(nodePrice);
|
|
457
454
|
// fallback is available if auction is complete or it's a spot order
|
|
458
455
|
const fallbackAvailable = (0, types_1.isVariant)(marketType, 'spot') ||
|
|
459
|
-
(0, auction_1.isFallbackAvailableLiquiditySource)(node.order,
|
|
456
|
+
(0, auction_1.isFallbackAvailableLiquiditySource)(node.order, oraclePriceData, slot, state, marketAccount);
|
|
460
457
|
if (crosses && fallbackAvailable) {
|
|
461
458
|
nodesToFill.push({
|
|
462
459
|
node: node,
|
|
@@ -6566,7 +6566,7 @@
|
|
|
6566
6566
|
]
|
|
6567
6567
|
},
|
|
6568
6568
|
{
|
|
6569
|
-
"name": "
|
|
6569
|
+
"name": "updatePerpMarketOracleLowRiskSlotDelayOverride",
|
|
6570
6570
|
"accounts": [
|
|
6571
6571
|
{
|
|
6572
6572
|
"name": "admin",
|
|
@@ -6586,7 +6586,7 @@
|
|
|
6586
6586
|
],
|
|
6587
6587
|
"args": [
|
|
6588
6588
|
{
|
|
6589
|
-
"name": "
|
|
6589
|
+
"name": "oracleLowRiskSlotDelayOverride",
|
|
6590
6590
|
"type": "i8"
|
|
6591
6591
|
}
|
|
6592
6592
|
]
|
|
@@ -11194,7 +11194,7 @@
|
|
|
11194
11194
|
"type": "i8"
|
|
11195
11195
|
},
|
|
11196
11196
|
{
|
|
11197
|
-
"name": "
|
|
11197
|
+
"name": "oracleLowRiskSlotDelayOverride",
|
|
11198
11198
|
"docs": [
|
|
11199
11199
|
"the override for the state.min_perp_auction_duration",
|
|
11200
11200
|
"0 is no override, -1 is disable speed bump, 1-100 is literal speed bump"
|
|
@@ -12278,7 +12278,17 @@
|
|
|
12278
12278
|
"name": "InsufficientDataPoints"
|
|
12279
12279
|
},
|
|
12280
12280
|
{
|
|
12281
|
-
"name": "StaleForAMM"
|
|
12281
|
+
"name": "StaleForAMM",
|
|
12282
|
+
"fields": [
|
|
12283
|
+
{
|
|
12284
|
+
"name": "immediate",
|
|
12285
|
+
"type": "bool"
|
|
12286
|
+
},
|
|
12287
|
+
{
|
|
12288
|
+
"name": "lowRisk",
|
|
12289
|
+
"type": "bool"
|
|
12290
|
+
}
|
|
12291
|
+
]
|
|
12282
12292
|
},
|
|
12283
12293
|
{
|
|
12284
12294
|
"name": "Valid"
|
|
@@ -12304,7 +12314,10 @@
|
|
|
12304
12314
|
"name": "FillOrderMatch"
|
|
12305
12315
|
},
|
|
12306
12316
|
{
|
|
12307
|
-
"name": "
|
|
12317
|
+
"name": "FillOrderAmmLowRisk"
|
|
12318
|
+
},
|
|
12319
|
+
{
|
|
12320
|
+
"name": "FillOrderAmmImmediate"
|
|
12308
12321
|
},
|
|
12309
12322
|
{
|
|
12310
12323
|
"name": "Liquidate"
|
|
@@ -12954,23 +12967,6 @@
|
|
|
12954
12967
|
]
|
|
12955
12968
|
}
|
|
12956
12969
|
},
|
|
12957
|
-
{
|
|
12958
|
-
"name": "AMMAvailability",
|
|
12959
|
-
"type": {
|
|
12960
|
-
"kind": "enum",
|
|
12961
|
-
"variants": [
|
|
12962
|
-
{
|
|
12963
|
-
"name": "Immediate"
|
|
12964
|
-
},
|
|
12965
|
-
{
|
|
12966
|
-
"name": "AfterMinDuration"
|
|
12967
|
-
},
|
|
12968
|
-
{
|
|
12969
|
-
"name": "Unavailable"
|
|
12970
|
-
}
|
|
12971
|
-
]
|
|
12972
|
-
}
|
|
12973
|
-
},
|
|
12974
12970
|
{
|
|
12975
12971
|
"name": "RevenueShareOrderBitFlag",
|
|
12976
12972
|
"type": {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import { Order, PositionDirection } from '../types';
|
|
2
|
+
import { Order, PositionDirection, StateAccount } from '../types';
|
|
3
3
|
import { BN } from '@coral-xyz/anchor';
|
|
4
4
|
import { PerpMarketAccount } from '../types';
|
|
5
|
+
import { MMOraclePriceData } from '../oracles/types';
|
|
5
6
|
export declare function isAuctionComplete(order: Order, slot: number): boolean;
|
|
6
|
-
export declare function isFallbackAvailableLiquiditySource(order: Order,
|
|
7
|
+
export declare function isFallbackAvailableLiquiditySource(order: Order, mmOraclePriceData: MMOraclePriceData, slot: number, state: StateAccount, market: PerpMarketAccount, isLiquidation?: boolean): boolean;
|
|
7
8
|
/**
|
|
8
9
|
*
|
|
9
10
|
* @param order
|
|
@@ -6,6 +6,9 @@ const anchor_1 = require("@coral-xyz/anchor");
|
|
|
6
6
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
7
|
const types_2 = require("../types");
|
|
8
8
|
const tiers_1 = require("./tiers");
|
|
9
|
+
const orders_1 = require("./orders");
|
|
10
|
+
const oracles_1 = require("./oracles");
|
|
11
|
+
const exchangeStatus_1 = require("./exchangeStatus");
|
|
9
12
|
function isAuctionComplete(order, slot) {
|
|
10
13
|
if (order.auctionDuration === 0) {
|
|
11
14
|
return true;
|
|
@@ -13,14 +16,30 @@ function isAuctionComplete(order, slot) {
|
|
|
13
16
|
return new anchor_1.BN(slot).sub(order.slot).gt(new anchor_1.BN(order.auctionDuration));
|
|
14
17
|
}
|
|
15
18
|
exports.isAuctionComplete = isAuctionComplete;
|
|
16
|
-
function isFallbackAvailableLiquiditySource(order,
|
|
17
|
-
if (
|
|
19
|
+
function isFallbackAvailableLiquiditySource(order, mmOraclePriceData, slot, state, market, isLiquidation) {
|
|
20
|
+
if ((0, exchangeStatus_1.isOperationPaused)(market.pausedOperations, types_1.PerpOperation.AMM_FILL)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
// TODO: include too much drawdown check & mm oracle volatility
|
|
24
|
+
const oracleValidity = (0, oracles_1.getOracleValidity)(market, {
|
|
25
|
+
price: mmOraclePriceData.price,
|
|
26
|
+
slot: mmOraclePriceData.slot,
|
|
27
|
+
confidence: mmOraclePriceData.confidence,
|
|
28
|
+
hasSufficientNumberOfDataPoints: mmOraclePriceData.hasSufficientNumberOfDataPoints,
|
|
29
|
+
}, state.oracleGuardRails, new anchor_1.BN(slot));
|
|
30
|
+
if (oracleValidity <= types_1.OracleValidity.StaleForAMMLowRisk) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
if (oracleValidity == types_1.OracleValidity.Valid) {
|
|
18
34
|
return true;
|
|
19
35
|
}
|
|
20
|
-
|
|
36
|
+
const isOrderLowRiskForAmm = (0, orders_1.isLowRiskForAmm)(order, mmOraclePriceData, isLiquidation);
|
|
37
|
+
if (!isOrderLowRiskForAmm) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
21
41
|
return true;
|
|
22
42
|
}
|
|
23
|
-
return new anchor_1.BN(slot).sub(order.slot).gt(new anchor_1.BN(minAuctionDuration));
|
|
24
43
|
}
|
|
25
44
|
exports.isFallbackAvailableLiquiditySource = isFallbackAvailableLiquiditySource;
|
|
26
45
|
/**
|
|
@@ -5,7 +5,7 @@ import { DriftClient } from '../driftClient';
|
|
|
5
5
|
import { PerpMarketAccount, PerpPosition, PositionDirection, UserAccount } from '../types';
|
|
6
6
|
import { PublicKey } from '@solana/web3.js';
|
|
7
7
|
export declare function calculateSizePremiumLiabilityWeight(size: BN, // AMM_RESERVE_PRECISION
|
|
8
|
-
imfFactor: BN, liabilityWeight: BN, precision: BN): BN;
|
|
8
|
+
imfFactor: BN, liabilityWeight: BN, precision: BN, isBounded?: boolean): BN;
|
|
9
9
|
export declare function calculateSizeDiscountAssetWeight(size: BN, // AMM_RESERVE_PRECISION
|
|
10
10
|
imfFactor: BN, assetWeight: BN): BN;
|
|
11
11
|
export declare function calculateOraclePriceForPerpMargin(perpPosition: PerpPosition, market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
@@ -43,3 +43,4 @@ export declare function calculateUserMaxPerpOrderSize(driftClient: DriftClient,
|
|
|
43
43
|
tradeSize: BN;
|
|
44
44
|
oppositeSideTradeSize: BN;
|
|
45
45
|
};
|
|
46
|
+
export declare function calcHighLeverageModeInitialMarginRatioFromSize(preSizeAdjMarginRatio: BN, sizeAdjMarginRatio: BN, defaultMarginRatio: BN): BN;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateUserMaxPerpOrderSize = exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
3
|
+
exports.calcHighLeverageModeInitialMarginRatioFromSize = exports.calculateUserMaxPerpOrderSize = exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
@@ -11,7 +11,7 @@ const user_1 = require("../user");
|
|
|
11
11
|
const types_1 = require("../types");
|
|
12
12
|
const assert_1 = require("../assert/assert");
|
|
13
13
|
function calculateSizePremiumLiabilityWeight(size, // AMM_RESERVE_PRECISION
|
|
14
|
-
imfFactor, liabilityWeight, precision) {
|
|
14
|
+
imfFactor, liabilityWeight, precision, isBounded = true) {
|
|
15
15
|
if (imfFactor.eq(numericConstants_1.ZERO)) {
|
|
16
16
|
return liabilityWeight;
|
|
17
17
|
}
|
|
@@ -23,7 +23,13 @@ imfFactor, liabilityWeight, precision) {
|
|
|
23
23
|
.mul(imfFactor)
|
|
24
24
|
.div(denom) // 1e5
|
|
25
25
|
);
|
|
26
|
-
|
|
26
|
+
let maxLiabilityWeight;
|
|
27
|
+
if (isBounded) {
|
|
28
|
+
maxLiabilityWeight = anchor_1.BN.max(liabilityWeight, sizePremiumLiabilityWeight);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
maxLiabilityWeight = sizePremiumLiabilityWeight;
|
|
32
|
+
}
|
|
27
33
|
return maxLiabilityWeight;
|
|
28
34
|
}
|
|
29
35
|
exports.calculateSizePremiumLiabilityWeight = calculateSizePremiumLiabilityWeight;
|
|
@@ -199,3 +205,25 @@ function calculateUserMaxPerpOrderSize(driftClient, userAccountKey, userAccount,
|
|
|
199
205
|
return user.getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide);
|
|
200
206
|
}
|
|
201
207
|
exports.calculateUserMaxPerpOrderSize = calculateUserMaxPerpOrderSize;
|
|
208
|
+
function calcHighLeverageModeInitialMarginRatioFromSize(preSizeAdjMarginRatio, sizeAdjMarginRatio, defaultMarginRatio) {
|
|
209
|
+
let result;
|
|
210
|
+
if (sizeAdjMarginRatio.lt(preSizeAdjMarginRatio)) {
|
|
211
|
+
const sizePctDiscountFactor = numericConstants_1.PERCENTAGE_PRECISION.sub(preSizeAdjMarginRatio
|
|
212
|
+
.sub(sizeAdjMarginRatio)
|
|
213
|
+
.mul(numericConstants_1.PERCENTAGE_PRECISION)
|
|
214
|
+
.div(preSizeAdjMarginRatio.div(new anchor_1.BN(5))));
|
|
215
|
+
const hlmMarginDelta = anchor_1.BN.max(preSizeAdjMarginRatio.sub(defaultMarginRatio), new anchor_1.BN(1));
|
|
216
|
+
const hlmMarginDeltaProportion = hlmMarginDelta
|
|
217
|
+
.mul(sizePctDiscountFactor)
|
|
218
|
+
.div(numericConstants_1.PERCENTAGE_PRECISION);
|
|
219
|
+
result = hlmMarginDeltaProportion.add(defaultMarginRatio);
|
|
220
|
+
}
|
|
221
|
+
else if (sizeAdjMarginRatio.eq(preSizeAdjMarginRatio)) {
|
|
222
|
+
result = defaultMarginRatio;
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
result = sizeAdjMarginRatio;
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
exports.calcHighLeverageModeInitialMarginRatioFromSize = calcHighLeverageModeInitialMarginRatioFromSize;
|
|
@@ -61,29 +61,49 @@ function calculateOracleSpread(price, oraclePriceData) {
|
|
|
61
61
|
}
|
|
62
62
|
exports.calculateOracleSpread = calculateOracleSpread;
|
|
63
63
|
function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0, userHighLeverageMode = false) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
if (market.status === 'Settlement')
|
|
65
|
+
return 0;
|
|
66
|
+
const isHighLeverageUser = userHighLeverageMode &&
|
|
67
67
|
market.highLeverageMarginRatioInitial > 0 &&
|
|
68
|
-
market.highLeverageMarginRatioMaintenance
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
let marginRatio;
|
|
68
|
+
market.highLeverageMarginRatioMaintenance > 0;
|
|
69
|
+
const marginRatioInitial = isHighLeverageUser
|
|
70
|
+
? market.highLeverageMarginRatioInitial
|
|
71
|
+
: market.marginRatioInitial;
|
|
72
|
+
const marginRatioMaintenance = isHighLeverageUser
|
|
73
|
+
? market.highLeverageMarginRatioMaintenance
|
|
74
|
+
: market.marginRatioMaintenance;
|
|
75
|
+
let defaultMarginRatio;
|
|
77
76
|
switch (marginCategory) {
|
|
78
|
-
case 'Initial':
|
|
79
|
-
|
|
80
|
-
marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRationInitial), numericConstants_1.MARGIN_PRECISION).toNumber(), customMarginRatio);
|
|
77
|
+
case 'Initial':
|
|
78
|
+
defaultMarginRatio = marginRatioInitial;
|
|
81
79
|
break;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
80
|
+
case 'Maintenance':
|
|
81
|
+
defaultMarginRatio = marginRatioMaintenance;
|
|
85
82
|
break;
|
|
83
|
+
default:
|
|
84
|
+
throw new Error('Invalid margin category');
|
|
85
|
+
}
|
|
86
|
+
let marginRatio;
|
|
87
|
+
if (isHighLeverageUser && marginCategory !== 'Maintenance') {
|
|
88
|
+
// Use ordinary-mode initial/fill ratios for size-adjusted calc
|
|
89
|
+
let preSizeAdjMarginRatio;
|
|
90
|
+
switch (marginCategory) {
|
|
91
|
+
case 'Initial':
|
|
92
|
+
preSizeAdjMarginRatio = market.marginRatioInitial;
|
|
93
|
+
break;
|
|
94
|
+
default:
|
|
95
|
+
preSizeAdjMarginRatio = marginRatioMaintenance;
|
|
96
|
+
break;
|
|
86
97
|
}
|
|
98
|
+
const sizeAdjMarginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(preSizeAdjMarginRatio), numericConstants_1.MARGIN_PRECISION, false).toNumber();
|
|
99
|
+
marginRatio = (0, margin_1.calcHighLeverageModeInitialMarginRatioFromSize)(new anchor_1.BN(preSizeAdjMarginRatio), new anchor_1.BN(sizeAdjMarginRatio), new anchor_1.BN(defaultMarginRatio)).toNumber();
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const sizeAdjMarginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(defaultMarginRatio), numericConstants_1.MARGIN_PRECISION, true).toNumber();
|
|
103
|
+
marginRatio = Math.max(defaultMarginRatio, sizeAdjMarginRatio);
|
|
104
|
+
}
|
|
105
|
+
if (marginCategory === 'Initial') {
|
|
106
|
+
marginRatio = Math.max(marginRatio, customMarginRatio);
|
|
87
107
|
}
|
|
88
108
|
return marginRatio;
|
|
89
109
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
|
-
import { AMM, HistoricalOracleData, OracleGuardRails, OracleSource, PerpMarketAccount } from '../types';
|
|
4
|
+
import { AMM, HistoricalOracleData, OracleGuardRails, OracleSource, OracleValidity, PerpMarketAccount } from '../types';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
6
6
|
import { BN } from '@coral-xyz/anchor';
|
|
7
7
|
export declare function oraclePriceBands(market: PerpMarketAccount, oraclePriceData: Pick<OraclePriceData, 'price'>): [BN, BN];
|
|
8
8
|
export declare function getMaxConfidenceIntervalMultiplier(market: PerpMarketAccount): BN;
|
|
9
|
+
export declare function getOracleValidity(market: PerpMarketAccount, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: BN, oracleStalenessBuffer?: BN): OracleValidity;
|
|
9
10
|
export declare function isOracleValid(market: PerpMarketAccount, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: number): boolean;
|
|
10
11
|
export declare function isOracleTooDivergent(amm: AMM, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails): boolean;
|
|
11
12
|
export declare function calculateLiveOracleTwap(histOracleData: HistoricalOracleData, oraclePriceData: OraclePriceData, now: BN, period: BN): BN;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMultipleBetweenOracleSources = exports.trimVaaSignatures = exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.getMaxConfidenceIntervalMultiplier = exports.oraclePriceBands = void 0;
|
|
3
|
+
exports.getMultipleBetweenOracleSources = exports.trimVaaSignatures = exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.getOracleValidity = exports.getMaxConfidenceIntervalMultiplier = exports.oraclePriceBands = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const assert_1 = require("../assert/assert");
|
|
@@ -34,6 +34,60 @@ function getMaxConfidenceIntervalMultiplier(market) {
|
|
|
34
34
|
return maxConfidenceIntervalMultiplier;
|
|
35
35
|
}
|
|
36
36
|
exports.getMaxConfidenceIntervalMultiplier = getMaxConfidenceIntervalMultiplier;
|
|
37
|
+
function getOracleValidity(market, oraclePriceData, oracleGuardRails, slot, oracleStalenessBuffer = numericConstants_1.FIVE) {
|
|
38
|
+
const isNonPositive = oraclePriceData.price.lte(numericConstants_1.ZERO);
|
|
39
|
+
const isTooVolatile = anchor_1.BN.max(oraclePriceData.price, market.amm.historicalOracleData.lastOraclePriceTwap)
|
|
40
|
+
.div(anchor_1.BN.max(numericConstants_1.ONE, anchor_1.BN.min(oraclePriceData.price, market.amm.historicalOracleData.lastOraclePriceTwap)))
|
|
41
|
+
.gt(oracleGuardRails.validity.tooVolatileRatio);
|
|
42
|
+
const confPctOfPrice = oraclePriceData.confidence
|
|
43
|
+
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
44
|
+
.div(oraclePriceData.price);
|
|
45
|
+
const isConfTooLarge = confPctOfPrice.gt(oracleGuardRails.validity.confidenceIntervalMaxSize.mul(getMaxConfidenceIntervalMultiplier(market)));
|
|
46
|
+
const oracleDelay = slot.sub(oraclePriceData.slot).sub(oracleStalenessBuffer);
|
|
47
|
+
let isStaleForAmmImmediate = true;
|
|
48
|
+
if (market.amm.oracleSlotDelayOverride != 0) {
|
|
49
|
+
isStaleForAmmImmediate = oracleDelay.gt(anchor_1.BN.max(new anchor_1.BN(market.amm.oracleSlotDelayOverride), numericConstants_1.ZERO));
|
|
50
|
+
}
|
|
51
|
+
let isStaleForAmmLowRisk = false;
|
|
52
|
+
if (market.amm.oracleLowRiskSlotDelayOverride != 0) {
|
|
53
|
+
isStaleForAmmLowRisk = oracleDelay.gt(anchor_1.BN.max(new anchor_1.BN(market.amm.oracleLowRiskSlotDelayOverride), numericConstants_1.ZERO));
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
isStaleForAmmLowRisk = oracleDelay.gt(oracleGuardRails.validity.slotsBeforeStaleForAmm);
|
|
57
|
+
}
|
|
58
|
+
let isStaleForMargin = oracleDelay.gt(new anchor_1.BN(oracleGuardRails.validity.slotsBeforeStaleForMargin));
|
|
59
|
+
if ((0, types_1.isOneOfVariant)(market.amm.oracleSource, [
|
|
60
|
+
'pythStableCoinPull',
|
|
61
|
+
'pythLazerStableCoin',
|
|
62
|
+
])) {
|
|
63
|
+
isStaleForMargin = oracleDelay.gt(new anchor_1.BN(oracleGuardRails.validity.slotsBeforeStaleForMargin).muln(3));
|
|
64
|
+
}
|
|
65
|
+
if (isNonPositive) {
|
|
66
|
+
return types_1.OracleValidity.NonPositive;
|
|
67
|
+
}
|
|
68
|
+
else if (isTooVolatile) {
|
|
69
|
+
return types_1.OracleValidity.TooVolatile;
|
|
70
|
+
}
|
|
71
|
+
else if (isConfTooLarge) {
|
|
72
|
+
return types_1.OracleValidity.TooUncertain;
|
|
73
|
+
}
|
|
74
|
+
else if (isStaleForMargin) {
|
|
75
|
+
return types_1.OracleValidity.StaleForMargin;
|
|
76
|
+
}
|
|
77
|
+
else if (!oraclePriceData.hasSufficientNumberOfDataPoints) {
|
|
78
|
+
return types_1.OracleValidity.InsufficientDataPoints;
|
|
79
|
+
}
|
|
80
|
+
else if (isStaleForAmmLowRisk) {
|
|
81
|
+
return types_1.OracleValidity.StaleForAMMLowRisk;
|
|
82
|
+
}
|
|
83
|
+
else if (isStaleForAmmImmediate) {
|
|
84
|
+
return types_1.OracleValidity.isStaleForAmmImmediate;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
return types_1.OracleValidity.Valid;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.getOracleValidity = getOracleValidity;
|
|
37
91
|
function isOracleValid(market, oraclePriceData, oracleGuardRails, slot) {
|
|
38
92
|
// checks if oracle is valid for an AMM only fill
|
|
39
93
|
const amm = market.amm;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { User } from '../user';
|
|
3
|
-
import { PerpMarketAccount, AMM, Order, PositionDirection, ProtectedMakerParams, MarketTypeStr } from '../types';
|
|
3
|
+
import { PerpMarketAccount, AMM, Order, PositionDirection, ProtectedMakerParams, MarketTypeStr, StateAccount } from '../types';
|
|
4
4
|
import { BN } from '@coral-xyz/anchor';
|
|
5
5
|
import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
|
|
6
6
|
export declare function isOrderRiskIncreasing(user: User, order: Order): boolean;
|
|
@@ -12,7 +12,8 @@ export declare function getLimitPrice<T extends MarketTypeStr>(order: Order, ora
|
|
|
12
12
|
export declare function applyProtectedMakerParams(limitPrice: BN, direction: PositionDirection, protectedMakerParams: ProtectedMakerParams): BN;
|
|
13
13
|
export declare function hasLimitPrice(order: Order, slot: number): boolean;
|
|
14
14
|
export declare function hasAuctionPrice(order: Order, slot: number): boolean;
|
|
15
|
-
export declare function isFillableByVAMM(order: Order, market: PerpMarketAccount, mmOraclePriceData: MMOraclePriceData, slot: number, ts: number,
|
|
15
|
+
export declare function isFillableByVAMM(order: Order, market: PerpMarketAccount, mmOraclePriceData: MMOraclePriceData, slot: number, ts: number, state: StateAccount): boolean;
|
|
16
|
+
export declare function isLowRiskForAmm(order: Order, mmOraclePriceData: MMOraclePriceData, isLiquidation?: boolean): boolean;
|
|
16
17
|
export declare function calculateBaseAssetAmountForAmmToFulfill(order: Order, market: PerpMarketAccount, mmOraclePriceData: MMOraclePriceData, slot: number): BN;
|
|
17
18
|
export declare function calculateBaseAssetAmountToFillUpToLimitPrice(order: Order, amm: AMM, limitPrice: BN, mmOraclePriceData: MMOraclePriceData): BN;
|
|
18
19
|
export declare function isOrderExpired(order: Order, ts: number, enforceBuffer?: boolean, bufferSeconds?: number): boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.maxSizeForTargetLiabilityWeightBN = exports.calculateOrderBaseAssetAmount = exports.hasBuilder = exports.isSignedMsgOrder = exports.isTakingOrder = exports.isRestingLimitOrder = exports.isTriggered = exports.mustBeTriggered = exports.isLimitOrder = exports.isMarketOrder = exports.isOrderExpired = exports.calculateBaseAssetAmountToFillUpToLimitPrice = exports.calculateBaseAssetAmountForAmmToFulfill = exports.isFillableByVAMM = exports.hasAuctionPrice = exports.hasLimitPrice = exports.applyProtectedMakerParams = exports.getLimitPrice = exports.standardizePrice = exports.standardizeBaseAssetAmount = exports.isOrderReduceOnly = exports.isOrderRiskIncreasingInSameDirection = exports.isOrderRiskIncreasing = void 0;
|
|
3
|
+
exports.maxSizeForTargetLiabilityWeightBN = exports.calculateOrderBaseAssetAmount = exports.hasBuilder = exports.isSignedMsgOrder = exports.isTakingOrder = exports.isRestingLimitOrder = exports.isTriggered = exports.mustBeTriggered = exports.isLimitOrder = exports.isMarketOrder = exports.isOrderExpired = exports.calculateBaseAssetAmountToFillUpToLimitPrice = exports.calculateBaseAssetAmountForAmmToFulfill = exports.isLowRiskForAmm = exports.isFillableByVAMM = exports.hasAuctionPrice = exports.hasLimitPrice = exports.applyProtectedMakerParams = exports.getLimitPrice = exports.standardizePrice = exports.standardizeBaseAssetAmount = exports.isOrderReduceOnly = exports.isOrderRiskIncreasingInSameDirection = exports.isOrderRiskIncreasing = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
@@ -146,12 +146,22 @@ function hasAuctionPrice(order, slot) {
|
|
|
146
146
|
(!order.auctionStartPrice.eq(numericConstants_1.ZERO) || !order.auctionEndPrice.eq(numericConstants_1.ZERO)));
|
|
147
147
|
}
|
|
148
148
|
exports.hasAuctionPrice = hasAuctionPrice;
|
|
149
|
-
function isFillableByVAMM(order, market, mmOraclePriceData, slot, ts,
|
|
150
|
-
return (((0, auction_1.isFallbackAvailableLiquiditySource)(order,
|
|
149
|
+
function isFillableByVAMM(order, market, mmOraclePriceData, slot, ts, state) {
|
|
150
|
+
return (((0, auction_1.isFallbackAvailableLiquiditySource)(order, mmOraclePriceData, slot, state, market) &&
|
|
151
151
|
calculateBaseAssetAmountForAmmToFulfill(order, market, mmOraclePriceData, slot).gt(numericConstants_1.ZERO)) ||
|
|
152
152
|
isOrderExpired(order, ts));
|
|
153
153
|
}
|
|
154
154
|
exports.isFillableByVAMM = isFillableByVAMM;
|
|
155
|
+
function isLowRiskForAmm(order, mmOraclePriceData, isLiquidation) {
|
|
156
|
+
if ((0, types_1.isVariant)(order.marketType, 'spot')) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
const orderOlderThanOracleDelay = new anchor_1.BN(order.slot).lte(mmOraclePriceData.slot);
|
|
160
|
+
return (orderOlderThanOracleDelay ||
|
|
161
|
+
isLiquidation ||
|
|
162
|
+
(order.bitFlags & types_1.OrderBitFlag.SafeTriggerOrder) !== 0);
|
|
163
|
+
}
|
|
164
|
+
exports.isLowRiskForAmm = isLowRiskForAmm;
|
|
155
165
|
function calculateBaseAssetAmountForAmmToFulfill(order, market, mmOraclePriceData, slot) {
|
|
156
166
|
if (mustBeTriggered(order) && !isTriggered(order)) {
|
|
157
167
|
return numericConstants_1.ZERO;
|
package/lib/browser/types.d.ts
CHANGED
|
@@ -1165,7 +1165,8 @@ export type AMM = {
|
|
|
1165
1165
|
netUnsettledFundingPnl: BN;
|
|
1166
1166
|
quoteAssetAmountWithUnsettledLp: BN;
|
|
1167
1167
|
referencePriceOffset: number;
|
|
1168
|
-
|
|
1168
|
+
oracleLowRiskSlotDelayOverride: number;
|
|
1169
|
+
oracleSlotDelayOverride: number;
|
|
1169
1170
|
ammSpreadAdjustment: number;
|
|
1170
1171
|
ammInventorySpreadAdjustment: number;
|
|
1171
1172
|
lastFundingOracleTwap: BN;
|
|
@@ -1480,6 +1481,16 @@ export type OracleGuardRails = {
|
|
|
1480
1481
|
tooVolatileRatio: BN;
|
|
1481
1482
|
};
|
|
1482
1483
|
};
|
|
1484
|
+
export declare enum OracleValidity {
|
|
1485
|
+
NonPositive = 0,
|
|
1486
|
+
TooVolatile = 1,
|
|
1487
|
+
TooUncertain = 2,
|
|
1488
|
+
StaleForMargin = 3,
|
|
1489
|
+
InsufficientDataPoints = 4,
|
|
1490
|
+
StaleForAMMLowRisk = 5,
|
|
1491
|
+
isStaleForAmmImmediate = 6,
|
|
1492
|
+
Valid = 7
|
|
1493
|
+
}
|
|
1483
1494
|
export type PrelaunchOracle = {
|
|
1484
1495
|
price: BN;
|
|
1485
1496
|
maxPrice: BN;
|
package/lib/browser/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SwapReduceOnly = exports.PlaceAndTakeOrderSuccessCondition = exports.FuelOverflowStatus = exports.ReferrerStatus = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.OrderParamsBitFlag = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SettlePnlMode = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderBitFlag = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSourceNum = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.TokenProgramFlag = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.MarginMode = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.FeatureBitFlags = exports.ExchangeStatus = void 0;
|
|
3
|
+
exports.OracleValidity = exports.SwapReduceOnly = exports.PlaceAndTakeOrderSuccessCondition = exports.FuelOverflowStatus = exports.ReferrerStatus = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.OrderParamsBitFlag = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SettlePnlMode = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderBitFlag = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSourceNum = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.TokenProgramFlag = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.MarginMode = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.FeatureBitFlags = exports.ExchangeStatus = void 0;
|
|
4
4
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
5
5
|
// # Utility Types / Enums / Constants
|
|
6
6
|
var ExchangeStatus;
|
|
@@ -407,3 +407,14 @@ class SwapReduceOnly {
|
|
|
407
407
|
exports.SwapReduceOnly = SwapReduceOnly;
|
|
408
408
|
SwapReduceOnly.In = { in: {} };
|
|
409
409
|
SwapReduceOnly.Out = { out: {} };
|
|
410
|
+
var OracleValidity;
|
|
411
|
+
(function (OracleValidity) {
|
|
412
|
+
OracleValidity[OracleValidity["NonPositive"] = 0] = "NonPositive";
|
|
413
|
+
OracleValidity[OracleValidity["TooVolatile"] = 1] = "TooVolatile";
|
|
414
|
+
OracleValidity[OracleValidity["TooUncertain"] = 2] = "TooUncertain";
|
|
415
|
+
OracleValidity[OracleValidity["StaleForMargin"] = 3] = "StaleForMargin";
|
|
416
|
+
OracleValidity[OracleValidity["InsufficientDataPoints"] = 4] = "InsufficientDataPoints";
|
|
417
|
+
OracleValidity[OracleValidity["StaleForAMMLowRisk"] = 5] = "StaleForAMMLowRisk";
|
|
418
|
+
OracleValidity[OracleValidity["isStaleForAmmImmediate"] = 6] = "isStaleForAmmImmediate";
|
|
419
|
+
OracleValidity[OracleValidity["Valid"] = 7] = "Valid";
|
|
420
|
+
})(OracleValidity || (exports.OracleValidity = OracleValidity = {}));
|