@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
62 lines • 2.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PositionManagerStore = void 0;
const klayr_framework_1 = require("klayr-framework");
const cryptography = require("@klayr/cryptography");
const schema_1 = require("../schema");
const factory_1 = require("./factory");
class PositionManagerStore extends klayr_framework_1.Modules.BaseStore {
constructor(moduleName, index, stores, events) {
super(moduleName, index);
this.schema = schema_1.positionManagerStoreSchema;
this.genesisConfig = undefined;
this.dexConfig = undefined;
this.dependencyReady = false;
this.stores = stores;
this.events = events;
this.moduleName = moduleName;
}
addDependencies(tokenMethod, nftMethod) {
this.tokenMethod = tokenMethod;
this.nftMethod = nftMethod;
if (this.genesisConfig !== undefined && this.dexConfig !== undefined)
this.dependencyReady = true;
}
init(genesisConfig, dexConfig) {
this.genesisConfig = genesisConfig;
this.dexConfig = dexConfig;
if (this.tokenMethod !== undefined && this.nftMethod !== undefined)
this.dependencyReady = true;
}
getKey(poolAddress) {
const hash = cryptography.utils.hash(poolAddress);
return hash.subarray(0, 4);
}
async getImmutablePositionManager(ctx, poolAddress) {
this._checkDependencies();
if (!this.schema) {
throw new Error('Schema is not set');
}
const subStore = ctx.context.getStore(this.storePrefix, this.subStorePrefix);
const positionManager = await subStore.getWithSchema(this.getKey(poolAddress), this.schema);
const config = await this.dexConfig.getConfig(ctx.context);
return (0, factory_1.createImmutablePositionManagerinstance)(positionManager, ctx, this.stores, this.events, this.tokenMethod, this.nftMethod, this.genesisConfig, config, this.moduleName);
}
async getMutablePositionManager(ctx, poolAddress) {
this._checkDependencies();
if (!this.schema) {
throw new Error('Schema is not set');
}
const subStore = ctx.context.getStore(this.storePrefix, this.subStorePrefix);
const positionManager = await subStore.getWithSchema(this.getKey(poolAddress), this.schema);
const config = await this.dexConfig.getConfig(ctx.context);
return (0, factory_1.createMutablePositionManagerinstance)(positionManager, ctx, this.stores, this.events, this.tokenMethod, this.nftMethod, this.genesisConfig, config, this.moduleName);
}
_checkDependencies() {
if (!this.dependencyReady) {
throw new Error('dependencies not configured');
}
}
}
exports.PositionManagerStore = PositionManagerStore;
//# sourceMappingURL=position_manager.js.map