UNPKG

@swaptoshi/dex-module

Version:

Klayr decentralized exchange (dex) on-chain module

54 lines 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TickInfoStore = exports.defaultTickInfo = void 0; const klayr_framework_1 = require("klayr-framework"); const utils = require("@klayr/utils"); const db = require("@liskhq/lisk-db"); const schema_1 = require("../schema"); const limit_1 = require("./library/int/base/limit"); exports.defaultTickInfo = Object.freeze({ liquidityGross: '0', liquidityNet: '0', feeGrowthOutside0X128: '0', feeGrowthOutside1X128: '0', tickCumulativeOutside: '0', secondsPerLiquidityOutsideX128: '0', secondsOutside: '0', initialized: false, }); class TickInfoStore extends klayr_framework_1.Modules.BaseStore { constructor() { super(...arguments); this.schema = schema_1.tickInfoStoreSchema; this.default = { ...exports.defaultTickInfo }; } getKey(poolAddress, tick) { const tickBuf = Buffer.allocUnsafe(3); tickBuf.writeUIntBE(this.hof(tick, 24), 0, 3); return Buffer.concat([poolAddress, tickBuf]); } async getOrDefault(context, key) { try { const positionInfo = await this.get(context, key); return positionInfo; } catch (error) { if (!(error instanceof db.NotFoundError)) { throw error; } return utils.objects.cloneDeep(exports.defaultTickInfo); } } hof(value, bitSize) { const max = parseInt(limit_1.maxUnsigned[bitSize], 10); const min = 0; let result = parseInt(value, 10); if (result < min || result > max) { const range = max - min + 1; result = ((((result - min) % range) + range) % range) + min; } return result; } } exports.TickInfoStore = TickInfoStore; //# sourceMappingURL=tick_info.js.map