Skip to content

sdk: add getTriggerAuctionStartPrice #1654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion sdk/src/math/auction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { isOneOfVariant, isVariant, Order, PositionDirection } from '../types';
import { BN, getVariant, ONE, OrderBitFlag, ZERO } from '../.';
import {
BN,
getVariant,
ONE,
OrderBitFlag,
ZERO,
ContractTier,
PerpMarketAccount,
} from '../.';

export function isAuctionComplete(order: Order, slot: number): boolean {
if (order.auctionDuration === 0) {
Expand Down Expand Up @@ -177,3 +185,72 @@ export function deriveOracleAuctionParams({
oraclePriceOffset: oraclePriceOffsetNum,
};
}

export function getTriggerAuctionStartPrice(params: {
perpMarket: PerpMarketAccount;
direction: PositionDirection;
oraclePrice: BN;
startBuffer: number;
}): BN {
const { perpMarket, direction, oraclePrice, startBuffer } = params;

const QUOTE_PRECISION = new BN(1_000_000);
const PRICE_PRECISION = new BN(1_000_000);

const twapMismatch =
perpMarket.amm.historicalOracleData.lastOraclePriceTwapTs
.sub(perpMarket.amm.lastMarkPriceTwapTs)
.abs()
.gte(new BN(60)) ||
perpMarket.amm.volume24H.lte(new BN(100_000).mul(QUOTE_PRECISION));

let baselineStartOffset: BN;

if (twapMismatch) {
const priceDivisor = perpMarket.contractTier <= ContractTier.B ? 500 : 100;
baselineStartOffset =
direction === 'long'
? perpMarket.amm.lastBidPriceTwap.divn(priceDivisor)
: perpMarket.amm.lastAskPriceTwap.divn(priceDivisor).neg();
} else {
const markTwapSlow =
direction === 'long'
? perpMarket.amm.lastBidPriceTwap
: perpMarket.amm.lastAskPriceTwap;

const markTwapFast = perpMarket.amm.lastMarkPriceTwap5Min;
const oracleTwapSlow =
perpMarket.amm.historicalOracleData.lastOraclePriceTwap;
const oracleTwapFast =
perpMarket.amm.historicalOracleData.lastOraclePriceTwap5Min;

const offsetSlow = markTwapSlow.sub(oracleTwapSlow);
const offsetFast = markTwapFast.sub(oracleTwapFast);

const spread =
direction === 'long'
? perpMarket.amm.longSpread
: perpMarket.amm.shortSpread;

const spreadBN = new BN(spread)
.mul(markTwapSlow)
.div(PRICE_PRECISION.muln(10)); // divide by 10x for safety

baselineStartOffset =
direction === PositionDirection.LONG
? BN.min(offsetSlow.add(spreadBN), offsetFast.sub(spreadBN))
: BN.max(offsetSlow.sub(spreadBN), offsetFast.add(spreadBN));
}

// Apply start buffer (in BPS)
const startBufferPrice = oraclePrice
.mul(new BN(startBuffer))
.div(new BN(10_000)); // BPS

const auctionStartPrice =
direction === 'long'
? oraclePrice.add(baselineStartOffset).sub(startBufferPrice)
: oraclePrice.add(baselineStartOffset).add(startBufferPrice);

return auctionStartPrice;
}
Loading