@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
85 lines • 5.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tickSpacingToMaxLiquidityPerTick = tickSpacingToMaxLiquidityPerTick;
exports.getFeeGrowthInside = getFeeGrowthInside;
exports.update = update;
exports.clear = clear;
exports.cross = cross;
const int_1 = require("../int");
const TickMath = require("./tick_math");
const LiquidityMath = require("./liquidity_math");
function tickSpacingToMaxLiquidityPerTick(tickSpacing) {
const minTick = int_1.Int24.from(TickMath.MIN_TICK).div(tickSpacing).mul(tickSpacing);
const maxTick = int_1.Int24.from(TickMath.MAX_TICK).div(tickSpacing).mul(tickSpacing);
const numTicks = int_1.Uint24.from(maxTick).sub(minTick).div(tickSpacing).add(1);
return int_1.Uint128.from(int_1.Uint128.MAX).div(numTicks).toString();
}
async function getFeeGrowthInside(tickInfoStore, context, poolAddress, tickLower, tickUpper, tickCurrent, feeGrowthGlobal0X128, feeGrowthGlobal1X128) {
const lower = await tickInfoStore.getOrDefault(context, tickInfoStore.getKey(poolAddress, tickLower));
const upper = await tickInfoStore.getOrDefault(context, tickInfoStore.getKey(poolAddress, tickUpper));
let feeGrowthBelow0X128;
let feeGrowthBelow1X128;
if (int_1.Int24.from(tickCurrent).gte(tickLower)) {
feeGrowthBelow0X128 = int_1.Uint256.from(lower.feeGrowthOutside0X128);
feeGrowthBelow1X128 = int_1.Uint256.from(lower.feeGrowthOutside1X128);
}
else {
feeGrowthBelow0X128 = int_1.Uint256.from(feeGrowthGlobal0X128).sub(lower.feeGrowthOutside0X128);
feeGrowthBelow1X128 = int_1.Uint256.from(feeGrowthGlobal1X128).sub(lower.feeGrowthOutside1X128);
}
let feeGrowthAbove0X128;
let feeGrowthAbove1X128;
if (int_1.Int24.from(tickCurrent).lt(tickUpper)) {
feeGrowthAbove0X128 = int_1.Uint256.from(upper.feeGrowthOutside0X128);
feeGrowthAbove1X128 = int_1.Uint256.from(upper.feeGrowthOutside1X128);
}
else {
feeGrowthAbove0X128 = int_1.Uint256.from(feeGrowthGlobal0X128).sub(upper.feeGrowthOutside0X128);
feeGrowthAbove1X128 = int_1.Uint256.from(feeGrowthGlobal1X128).sub(upper.feeGrowthOutside1X128);
}
const feeGrowthInside0X128 = int_1.Uint256.from(feeGrowthGlobal0X128).sub(feeGrowthBelow0X128).sub(feeGrowthAbove0X128).toString();
const feeGrowthInside1X128 = int_1.Uint256.from(feeGrowthGlobal1X128).sub(feeGrowthBelow1X128).sub(feeGrowthAbove1X128).toString();
return [feeGrowthInside0X128, feeGrowthInside1X128];
}
async function update(tickInfoStore, context, poolAddress, tick, tickCurrent, liquidityDelta, feeGrowthGlobal0X128, feeGrowthGlobal1X128, secondsPerLiquidityCumulativeX128, tickCumulative, time, upper, maxLiquidity, simulation = false) {
const info = await tickInfoStore.getOrDefault(context, tickInfoStore.getKey(poolAddress, tick));
const liquidityGrossBefore = int_1.Uint128.from(info.liquidityGross);
const liquidityGrossAfter = int_1.Uint128.from(LiquidityMath.addDelta(liquidityGrossBefore.toString(), liquidityDelta));
if (liquidityGrossAfter.gt(maxLiquidity))
throw new Error('LO');
const flipped = liquidityGrossAfter.eq(0) !== liquidityGrossBefore.eq(0);
if (liquidityGrossBefore.eq(0)) {
if (int_1.Int24.from(tick).lte(tickCurrent)) {
info.feeGrowthOutside0X128 = feeGrowthGlobal0X128;
info.feeGrowthOutside1X128 = feeGrowthGlobal1X128;
info.secondsPerLiquidityOutsideX128 = secondsPerLiquidityCumulativeX128;
info.tickCumulativeOutside = tickCumulative;
info.secondsOutside = time;
}
info.initialized = true;
}
info.liquidityGross = liquidityGrossAfter.toString();
info.liquidityNet = upper ? int_1.Int128.from(int_1.Int256.from(info.liquidityNet).sub(liquidityDelta)).toString() : int_1.Int128.from(int_1.Int256.from(info.liquidityNet).add(liquidityDelta)).toString();
if (!simulation) {
await tickInfoStore.set(context, tickInfoStore.getKey(poolAddress, tick), info);
}
return [flipped, info];
}
async function clear(tickInfoStore, context, poolAddress, tick, simulation = false) {
if (!simulation) {
await tickInfoStore.del(context, tickInfoStore.getKey(poolAddress, tick));
}
}
async function cross(tickInfoStore, context, poolAddress, tick, feeGrowthGlobal0X128, feeGrowthGlobal1X128, secondsPerLiquidityCumulativeX128, tickCumulative, time, simulation = false) {
const info = await tickInfoStore.getOrDefault(context, tickInfoStore.getKey(poolAddress, tick));
info.feeGrowthOutside0X128 = int_1.Uint256.from(feeGrowthGlobal0X128).sub(info.feeGrowthOutside0X128).toString();
info.feeGrowthOutside1X128 = int_1.Uint256.from(feeGrowthGlobal1X128).sub(info.feeGrowthOutside1X128).toString();
info.secondsPerLiquidityOutsideX128 = int_1.Uint160.from(secondsPerLiquidityCumulativeX128).sub(info.secondsPerLiquidityOutsideX128).toString();
info.tickCumulativeOutside = int_1.Int56.from(tickCumulative).sub(info.tickCumulativeOutside).toString();
info.secondsOutside = int_1.Uint32.from(time).sub(info.secondsOutside).toString();
if (!simulation) {
await tickInfoStore.set(context, tickInfoStore.getKey(poolAddress, tick), info);
}
return info.liquidityNet;
}
//# sourceMappingURL=tick.js.map