@drift-labs/common
Version:
Common functions for Drift
71 lines • 3.73 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculatePriceImpactFromL2 = void 0;
const sdk_1 = require("@drift-labs/sdk");
const UIMarket_1 = require("../types/UIMarket");
const _1 = require(".");
/**
* Calculates the price impact of an order, based on an L2
*/
const calculatePriceImpactFromL2 = (marketId, direction, baseAmount, l2Data, _oraclePrice) => {
var _a, _b, _c;
let [entryPrice, priceImpact, baseFilled, bestPrice, worstPrice] = [
sdk_1.ZERO,
sdk_1.ZERO,
sdk_1.ZERO,
sdk_1.ZERO,
sdk_1.ZERO,
];
let exceedsLiquidity = false;
try {
if (marketId.isPerp) {
const entryResult = (0, sdk_1.calculateEstimatedEntryPriceWithL2)('base', // leadSide
baseAmount, direction, sdk_1.BASE_PRECISION, l2Data);
entryPrice = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.entryPrice) || sdk_1.ZERO;
priceImpact = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.priceImpact) || sdk_1.ZERO;
baseFilled = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.baseFilled) || sdk_1.ZERO;
bestPrice = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.bestPrice) || sdk_1.ZERO;
worstPrice = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.worstPrice) || sdk_1.ZERO;
}
else {
const precisionExp = ((_a = UIMarket_1.UIMarket.spotMarkets[marketId.marketIndex]) === null || _a === void 0 ? void 0 : _a.precisionExp) ||
sdk_1.BASE_PRECISION_EXP;
const entryResult = (0, sdk_1.calculateEstimatedEntryPriceWithL2)('base', // leadSide
baseAmount, direction, precisionExp, l2Data);
entryPrice = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.entryPrice) || sdk_1.ZERO;
priceImpact = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.priceImpact) || sdk_1.ZERO;
baseFilled = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.baseFilled) || sdk_1.ZERO;
bestPrice = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.bestPrice) || sdk_1.ZERO;
worstPrice = (entryResult === null || entryResult === void 0 ? void 0 : entryResult.worstPrice) || sdk_1.ZERO;
}
// Check if we exceeded available liquidity
if (baseFilled.lt(baseAmount)) {
exceedsLiquidity = true;
}
}
catch (e) {
// SDK function may throw error if no liquidity, use fallback values
console.warn('Error calculating price impact from L2:', e);
// Use best bid/ask as fallback
const bestBid = (_b = l2Data.bids) === null || _b === void 0 ? void 0 : _b[0];
const bestAsk = (_c = l2Data.asks) === null || _c === void 0 ? void 0 : _c[0];
if (bestBid && bestAsk) {
const isLong = _1.ENUM_UTILS.match(direction, sdk_1.PositionDirection.LONG);
entryPrice = isLong ? bestAsk.price : bestBid.price;
bestPrice = isLong ? bestAsk.price : bestBid.price;
worstPrice = isLong ? bestAsk.price : bestBid.price;
baseFilled = isLong ? bestAsk.size : bestBid.size;
}
}
return {
entryPrice,
priceImpact,
baseAvailable: baseFilled,
bestPrice,
worstPrice,
exceedsLiquidity,
showPriceEstimateOracleDivergenceWarning: false, // Will be calculated by the caller
};
};
exports.calculatePriceImpactFromL2 = calculatePriceImpactFromL2;
//# sourceMappingURL=priceImpact.js.map
;