@stabilis/c9-shape-liquidity-getter
Version:
A library for calculating redemption values of concentrated liquidity positions for C9 shape liquidity.
75 lines (74 loc) • 2.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateTick = calculateTick;
exports.calculatePrice = calculatePrice;
exports.calculateBinStartTick = calculateBinStartTick;
exports.calculateBinFraction = calculateBinFraction;
const decimal_js_1 = require("decimal.js");
const MIN_PRICE = new decimal_js_1.Decimal("0.000000000001892254");
const MAX_TICK = new decimal_js_1.Decimal("4294967295");
const TICK_SIZE = new decimal_js_1.Decimal("1.00100025");
/**
* Calculates the tick for a given price
* @param price The price to calculate the tick for
* @returns The calculated tick
*/
function calculateTick(price) {
const priceDecimal = new decimal_js_1.Decimal(price);
// Check for invalid inputs
if (priceDecimal.lte(0)) {
return 0;
}
const priceRatio = priceDecimal.div(MIN_PRICE);
const tick = priceRatio.ln().div(TICK_SIZE.ln());
return Math.round(tick.toNumber());
}
/**
* Calculates the price for a given tick
* @param tick The tick to calculate the price for
* @returns The calculated price
*/
function calculatePrice(tick) {
return MIN_PRICE.mul(TICK_SIZE.pow(tick)).toString();
}
/**
* Calculates the bin start tick for a given tick and bin span
* @param tick The tick to calculate the bin start for
* @param binSpan The bin span
* @returns The bin start tick
*/
function calculateBinStartTick(tick, binSpan) {
if (binSpan <= 0) {
return tick;
}
return Math.floor(tick / binSpan) * binSpan;
}
/**
* Calculates the fraction of a bin that is within the price bounds
* @param binStartTick The start tick of the bin
* @param binSpan The bin span
* @param lowerBoundTick The lower bound tick
* @param upperBoundTick The upper bound tick
* @returns The fraction of the bin that is within bounds (0-1)
*/
function calculateBinFraction(binStartTick, binSpan, lowerBoundTick, upperBoundTick) {
if (binSpan <= 0) {
return 0;
}
if (lowerBoundTick >= upperBoundTick) {
return 0;
}
const binEndTick = binStartTick + binSpan;
// If bin is completely outside bounds, return 0
if (binEndTick <= lowerBoundTick || binStartTick >= upperBoundTick) {
return 0;
}
// If bin is completely inside bounds, return 1
if (binStartTick >= lowerBoundTick && binEndTick <= upperBoundTick) {
return 1;
}
// Calculate partial fraction
const effectiveStart = Math.max(binStartTick, lowerBoundTick);
const effectiveEnd = Math.min(binEndTick, upperBoundTick);
return (effectiveEnd - effectiveStart) / binSpan;
}