@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
43 lines • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TickBitmapStore = void 0;
const klayr_framework_1 = require("klayr-framework");
const db = require("@liskhq/lisk-db");
const schema_1 = require("../schema");
const limit_1 = require("./library/int/base/limit");
class TickBitmapStore extends klayr_framework_1.Modules.BaseStore {
constructor() {
super(...arguments);
this.schema = schema_1.tickBitmapStoreSchema;
this.default = { bitmap: '0' };
}
getKey(poolAddress, index) {
const indexBuf = Buffer.allocUnsafe(2);
indexBuf.writeUIntBE(this.hof(index, 16), 0, 2);
return Buffer.concat([poolAddress, indexBuf]);
}
async getOrDefault(context, key) {
try {
const tickBitmap = await this.get(context, key);
return tickBitmap;
}
catch (error) {
if (!(error instanceof db.NotFoundError)) {
throw error;
}
return { bitmap: '0' };
}
}
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.TickBitmapStore = TickBitmapStore;
//# sourceMappingURL=tick_bitmap.js.map