swapable
Version:
Swapable: Automated Liquidity Pools
71 lines (70 loc) • 2.28 kB
JavaScript
;
/**
* This file is part of Swapable shared under AGPL-3.0
* Copyright (C) 2021 Using Blockchain Ltd, Reg No.: 12658136, United Kingdom
*
* @package Swapable
* @author Grégory Saive for Using Blockchain Ltd <greg@ubc.digital>
* @license AGPL-3.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCommand = void 0;
// internal dependencies
const index_1 = require("../../index");
const FailureMissingArgument_1 = require("../errors/FailureMissingArgument");
/**
* @class BaseCommand
* @package Swapable
* @subpackage Contracts
* @since v1.0.0
* @description Abstract class that describes a command interface for executing
* commands that involve swapable digital assets.
*/
class BaseCommand {
/**
* Construct a command object around `context`.
*
* @param {Context} context
*/
constructor(
/**
* @description Execution context
*/
context) {
this.context = context;
}
/// end-region abstract methods
/**
* Asserts the allowance of `actor` to execute the command.
*
* @param {PublicAccount} actor
* @param {CommandOption[]} argv
* @throws {FailureOperationForbidden} On denial of authorization
*/
assertExecutionAllowance(actor, argv) {
// check that `actor` is allowed to execute
const authResult = this.canExecute(actor, argv);
if (!authResult.status) {
throw new index_1.FailureOperationForbidden('Operation forbidden (' + this.name + ')');
}
return true;
}
/**
* Asserts the presence of `fields` in `argv`.
*
* @param {CommandOption[]} argv
* @param {string[]} fields
* @throws {FailureMissingArgument} On missing mandatory argument(s).
*/
assertHasMandatoryArguments(argv, fields) {
// check that all `fields` are present in context
for (let i = 0, m = fields.length; i < m; i++) {
const value = this.context.getInput(fields[i], null);
if (null === value) {
throw new FailureMissingArgument_1.FailureMissingArgument('Missing argument "' + fields[i] + '"');
}
}
return true;
}
}
exports.BaseCommand = BaseCommand;