@uniswap/v4-sdk
Version:
⚒️ An SDK for building applications on top of Uniswap V4
55 lines • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.priceToClosestTick = exports.tickToPrice = void 0;
const tslib_1 = require("tslib");
const sdk_core_1 = require("@uniswap/sdk-core");
const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
const internalConstants_1 = require("../internalConstants");
const v3_sdk_1 = require("@uniswap/v3-sdk");
const sortsBefore_1 = require("../utils/sortsBefore");
/**
* This library is the almost the same as v3-sdk priceTickConversion except
* that it accepts a Currency type instead of a Token type,
* and thus uses some helper functions defined for the Currency type over the Token type.
*/
/**
* Returns a price object corresponding to the input tick and the base/quote token
* Inputs must be tokens because the address order is used to interpret the price represented by the tick
* @param baseToken the base token of the price
* @param quoteToken the quote token of the price
* @param tick the tick for which to return the price
*/
function tickToPrice(baseCurrency, quoteCurrency, tick) {
const sqrtRatioX96 = v3_sdk_1.TickMath.getSqrtRatioAtTick(tick);
const ratioX192 = jsbi_1.default.multiply(sqrtRatioX96, sqrtRatioX96);
return (0, sortsBefore_1.sortsBefore)(baseCurrency, quoteCurrency)
? new sdk_core_1.Price(baseCurrency, quoteCurrency, internalConstants_1.Q192, ratioX192)
: new sdk_core_1.Price(baseCurrency, quoteCurrency, ratioX192, internalConstants_1.Q192);
}
exports.tickToPrice = tickToPrice;
/**
* Returns the first tick for which the given price is greater than or equal to the tick price
* @param price for which to return the closest tick that represents a price less than or equal to the input price,
* i.e. the price of the returned tick is less than or equal to the input price
*/
function priceToClosestTick(price) {
const sorted = (0, sortsBefore_1.sortsBefore)(price.baseCurrency, price.quoteCurrency);
const sqrtRatioX96 = sorted
? (0, v3_sdk_1.encodeSqrtRatioX96)(price.numerator, price.denominator)
: (0, v3_sdk_1.encodeSqrtRatioX96)(price.denominator, price.numerator);
let tick = v3_sdk_1.TickMath.getTickAtSqrtRatio(sqrtRatioX96);
const nextTickPrice = tickToPrice(price.baseCurrency, price.quoteCurrency, tick + 1);
if (sorted) {
if (!price.lessThan(nextTickPrice)) {
tick++;
}
}
else {
if (!price.greaterThan(nextTickPrice)) {
tick++;
}
}
return tick;
}
exports.priceToClosestTick = priceToClosestTick;
//# sourceMappingURL=priceTickConversions.js.map