@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
43 lines • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.positionKey = positionKey;
exports.get = get;
exports.set = set;
exports.update = update;
const cryptography = require("@klayr/cryptography");
const int_1 = require("../int");
const LiquidityMath = require("./liquidity_math");
const FullMath = require("./full_math");
const FixedPoint128 = require("./fixed_point_128");
function positionKey(owner, tickLower, tickUpper) {
return cryptography.utils.hash(`${owner.toString('hex')}${tickLower}${tickUpper}`, 'utf8');
}
async function get(positionInfoStore, context, poolAddress, owner, tickLower, tickUpper) {
return positionInfoStore.getOrDefault(context, positionInfoStore.getKey(poolAddress, positionKey(owner, tickLower, tickUpper)));
}
async function set(positionInfoStore, context, poolAddress, owner, tickLower, tickUpper, position) {
await positionInfoStore.set(context, positionInfoStore.getKey(poolAddress, positionKey(owner, tickLower, tickUpper)), position);
}
function update(self, liquidityDelta, feeGrowthInside0X128, feeGrowthInside1X128) {
const _self = self;
let liquidityNext;
if (int_1.Int128.from(liquidityDelta).eq(0)) {
if (int_1.Uint128.from(_self.liquidity).lte(0))
throw new Error('NP');
liquidityNext = int_1.Uint128.from(_self.liquidity);
}
else {
liquidityNext = int_1.Uint128.from(LiquidityMath.addDelta(_self.liquidity, liquidityDelta));
}
const tokensOwed0 = int_1.Uint128.from(0).add(FullMath.mulDiv(int_1.Uint256.from(feeGrowthInside0X128).sub(_self.feeGrowthInside0LastX128).toString(), _self.liquidity, FixedPoint128.Q128));
const tokensOwed1 = int_1.Uint128.from(0).add(FullMath.mulDiv(int_1.Uint256.from(feeGrowthInside1X128).sub(_self.feeGrowthInside1LastX128).toString(), _self.liquidity, FixedPoint128.Q128));
if (!int_1.Int128.from(liquidityDelta).eq(0))
self.liquidity = liquidityNext.toString();
self.feeGrowthInside0LastX128 = feeGrowthInside0X128;
self.feeGrowthInside1LastX128 = feeGrowthInside1X128;
if (tokensOwed0.gt(0) || tokensOwed1.gt(0)) {
self.tokensOwed0 = int_1.Uint128.from(self.tokensOwed0).add(tokensOwed0).toString();
self.tokensOwed1 = int_1.Uint128.from(self.tokensOwed1).add(tokensOwed1).toString();
}
}
//# sourceMappingURL=position.js.map