@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
50 lines • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObservationStore = exports.defaultObservation = 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.defaultObservation = Object.freeze({
blockTimestamp: '0',
tickCumulative: '0',
secondsPerLiquidityCumulativeX128: '0',
initialized: false,
});
class ObservationStore extends klayr_framework_1.Modules.BaseStore {
constructor() {
super(...arguments);
this.schema = schema_1.observationStoreSchema;
this.default = { ...exports.defaultObservation };
}
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 observation = await this.get(context, key);
return observation;
}
catch (error) {
if (!(error instanceof db.NotFoundError)) {
throw error;
}
return utils.objects.cloneDeep(exports.defaultObservation);
}
}
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.ObservationStore = ObservationStore;
//# sourceMappingURL=observation.js.map