UNPKG

@mavrykdynamics/taquito

Version:

High level functionality that builds upon the other packages in the Mavryk Typescript Library Suite.

93 lines (92 loc) 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContractMethod = void 0; const wallet_1 = require("../../wallet"); const contract_1 = require("../contract"); const errors_1 = require("../errors"); /** * @description Utility class to send smart contract operation * The format for the arguments is the flattened representation */ class ContractMethod { constructor(provider, address, parameterSchema, name, args, isMultipleEntrypoint = true, isAnonymous = false) { this.provider = provider; this.address = address; this.parameterSchema = parameterSchema; this.name = name; this.args = args; this.isMultipleEntrypoint = isMultipleEntrypoint; this.isAnonymous = isAnonymous; } validateArgs(args, schema, name) { const sigs = schema.ExtractSignatures(); if (!sigs.find((x) => x.length === args.length)) { throw new errors_1.InvalidParameterError(name, sigs, args); } } /** * @description Get the schema of the smart contract method */ get schema() { return this.isAnonymous ? this.parameterSchema.ExtractSchema()[this.name] : this.parameterSchema.ExtractSchema(); } /** * @description Get the signature of the smart contract method */ getSignature() { if (this.isAnonymous) { const sig = this.parameterSchema.ExtractSignatures().find((x) => x[0] === this.name); if (sig) { sig.shift(); return sig; } } else { const sig = this.parameterSchema.ExtractSignatures(); return sig.length == 1 ? sig[0] : sig; } } /** * * @description Send the smart contract operation * * @param Options generic operation parameter */ send(params = {}) { if (this.provider instanceof wallet_1.Wallet) { return this.provider .transfer(this.toTransferParams(params)) .send(); } else { return this.provider.transfer(this.toTransferParams(params)); } } /** * * @description Create transfer params to be used with MavrykToolkit.contract.transfer methods * * @param Options generic transfer operation parameters */ toTransferParams({ fee, gasLimit, storageLimit, source, amount = 0, mumav = false, } = {}) { const fullTransferParams = { to: this.address, amount, fee, mumav, source, gasLimit, storageLimit, parameter: { entrypoint: this.isMultipleEntrypoint ? this.name : contract_1.DEFAULT_SMART_CONTRACT_METHOD_NAME, value: this.isAnonymous ? this.parameterSchema.Encode(this.name, ...this.args) : this.parameterSchema.Encode(...this.args), }, }; return fullTransferParams; } } exports.ContractMethod = ContractMethod;