UNPKG

lisk-framework

Version:

Lisk blockchain application platform

59 lines 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EscrowStore = exports.escrowStoreSchema = void 0; const lisk_db_1 = require("@liskhq/lisk-db"); const base_store_1 = require("../../base_store"); exports.escrowStoreSchema = { $id: '/token/store/escrow', type: 'object', required: ['amount'], properties: { amount: { dataType: 'uint64', fieldNumber: 1, }, }, }; class EscrowStore extends base_store_1.BaseStore { constructor() { super(...arguments); this.schema = exports.escrowStoreSchema; } getKey(escrowChainID, tokenID) { return Buffer.concat([escrowChainID, tokenID]); } async getOrDefault(ctx, key) { let escrowData; try { escrowData = await this.get(ctx, key); } catch (error) { if (!(error instanceof lisk_db_1.NotFoundError)) { throw error; } escrowData = { amount: BigInt(0) }; } return escrowData; } async createDefaultAccount(context, chainID, tokenID) { await this.set(context, this.getKey(chainID, tokenID), { amount: BigInt(0) }); } async addAmount(context, chainID, tokenID, amount) { const escrowKey = this.getKey(chainID, tokenID); const escrowData = await this.getOrDefault(context, escrowKey); escrowData.amount += amount; await this.set(context, escrowKey, escrowData); } async deductEscrowAmountWithTerminate(context, interopMethod, sendingChainID, tokenID, amount) { const escrowKey = this.getKey(sendingChainID, tokenID); const escrowData = await this.getOrDefault(context, escrowKey); if (escrowData.amount < amount) { await interopMethod.terminateChain(context, sendingChainID); return; } escrowData.amount -= amount; await this.set(context, escrowKey, escrowData); } } exports.EscrowStore = EscrowStore; //# sourceMappingURL=escrow.js.map