UNPKG

@swaptoshi/dex-module

Version:

Klayr decentralized exchange (dex) on-chain module

71 lines 3.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenSymbolStore = void 0; const klayr_framework_1 = require("klayr-framework"); const schema_1 = require("../schema"); const token_registered_1 = require("../events/token_registered"); const utils_1 = require("../utils"); class TokenSymbolStore extends klayr_framework_1.Modules.BaseStore { constructor(moduleName, index, events) { super(moduleName, index); this.schema = schema_1.tokenSymbolStoreSchema; this.dexConfig = undefined; this.genesisConfig = undefined; this.dependencyReady = false; this.events = events; } init(genesisConfig, dexConfig) { this.genesisConfig = genesisConfig; this.dexConfig = dexConfig; this.dependencyReady = true; } getKey(tokenId) { return tokenId; } async registerSymbol(ctx, tokenId, symbol, decimal) { this._checkDependencies(); if (await this.has(ctx, this.getKey(tokenId))) return; let _symbol = symbol; let _decimal = decimal; const config = await this.dexConfig.getConfig(ctx); if (this._isInvalidMainchainToken(tokenId, symbol, decimal, config)) throw new Error('invalid mainchain token parameter'); if (this._isInvalidDEXToken(tokenId, symbol, decimal, config)) throw new Error('invalid dex token parameter'); const mainchain = (0, utils_1.getMainchainToken)(this.genesisConfig, config); const dex = (0, utils_1.getDEXToken)(this.genesisConfig, config); if (this.getKey(tokenId).compare(mainchain.tokenId) === 0) { _symbol = mainchain.symbol; _decimal = mainchain.decimal; } else if (this.getKey(tokenId).compare(dex.tokenId) === 0) { _symbol = dex.symbol; _decimal = dex.decimal; } await this.set(ctx, this.getKey(tokenId), { symbol: _symbol, decimal: _decimal }); const events = this.events.get(token_registered_1.TokenRegisteredEvent); events.add(ctx, { tokenId, symbol: _symbol, decimal: _decimal, }, [tokenId]); } _isInvalidMainchainToken(tokenId, symbol, decimal, config) { const mainchain = (0, utils_1.getMainchainToken)(this.genesisConfig, config); const isMainchainToken = this.getKey(tokenId).compare(mainchain.tokenId) === 0; return (isMainchainToken && (symbol !== mainchain.symbol || decimal !== mainchain.decimal)) || (symbol === mainchain.symbol && (!isMainchainToken || decimal !== mainchain.decimal)); } _isInvalidDEXToken(tokenId, symbol, decimal, config) { const dex = (0, utils_1.getDEXToken)(this.genesisConfig, config); const isDEXToken = this.getKey(tokenId).compare(dex.tokenId) === 0; return (isDEXToken && (symbol !== dex.symbol || decimal !== dex.decimal)) || (symbol === dex.symbol && (!isDEXToken || decimal !== dex.decimal)); } _checkDependencies() { if (!this.dependencyReady) { throw new Error('dependencies not configured'); } } } exports.TokenSymbolStore = TokenSymbolStore; //# sourceMappingURL=token_symbol.js.map