@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
75 lines • 3.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupportedTokenStore = void 0;
const klayr_framework_1 = require("klayr-framework");
const schema_1 = require("../schema");
class SupportedTokenStore extends klayr_framework_1.Modules.BaseStore {
constructor() {
super(...arguments);
this.schema = schema_1.supportedTokenStoreSchema;
this.dependencyReady = false;
}
init(config) {
this.config = config;
if (this.tokenMethod !== undefined)
this.dependencyReady = true;
}
addDependencies(tokenMethod) {
this.tokenMethod = tokenMethod;
if (this.config !== undefined)
this.dependencyReady = true;
}
async apply(context) {
this._checkDependencies();
const config = await this.config.getConfig(context);
if (await this.has(context, Buffer.alloc(0))) {
const supportManager = await this.get(context, Buffer.alloc(0));
if (supportManager.supportAll !== config.supportAllTokens) {
await this._applyConfig(context);
supportManager.supportAll = config.supportAllTokens;
await this.set(context, Buffer.alloc(0), supportManager);
}
}
else {
await this._applyConfig(context);
const supportManager = {
supportAll: config.supportAllTokens,
supported: [],
};
await this.set(context, Buffer.alloc(0), supportManager);
}
}
async addSupport(context, tokenId) {
var _a;
this._checkDependencies();
const supportManager = await this.get(context, Buffer.alloc(0));
if (!supportManager.supportAll) {
await ((_a = this.tokenMethod) === null || _a === void 0 ? void 0 : _a.supportTokenID(context, tokenId));
}
if (!supportManager.supported.map(t => t.toString('hex')).includes(tokenId.toString('hex'))) {
supportManager.supported.push(tokenId);
}
await this.set(context, Buffer.alloc(0), supportManager);
}
async _applyConfig(context) {
var _a, _b, _c;
const config = await this.config.getConfig(context);
if (config.supportAllTokens) {
await ((_a = this.tokenMethod) === null || _a === void 0 ? void 0 : _a.supportAllTokens(context));
}
else {
await ((_b = this.tokenMethod) === null || _b === void 0 ? void 0 : _b.removeAllTokensSupport(context));
const supportManager = await this.get(context, Buffer.alloc(0));
for (const supportedToken of supportManager.supported) {
await ((_c = this.tokenMethod) === null || _c === void 0 ? void 0 : _c.supportTokenID(context, supportedToken));
}
}
}
_checkDependencies() {
if (!this.dependencyReady) {
throw new Error('dependencies not configured');
}
}
}
exports.SupportedTokenStore = SupportedTokenStore;
//# sourceMappingURL=supported_token.js.map