UNPKG

@hiero-ledger/sdk

Version:
269 lines (245 loc) 9.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _Hbar = _interopRequireDefault(require("../Hbar.cjs")); var _Transaction = _interopRequireWildcard(require("../transaction/Transaction.cjs")); var _ContractId = _interopRequireDefault(require("./ContractId.cjs")); var _ContractFunctionParameters = _interopRequireDefault(require("./ContractFunctionParameters.cjs")); var _long = _interopRequireDefault(require("long")); var _HbarUnit = _interopRequireDefault(require("../HbarUnit.cjs")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } // SPDX-License-Identifier: Apache-2.0 /** * @namespace proto * @typedef {import("@hashgraph/proto").proto.ITransaction} HieroProto.proto.ITransaction * @typedef {import("@hashgraph/proto").proto.ISignedTransaction} HieroProto.proto.ISignedTransaction * @typedef {import("@hashgraph/proto").proto.TransactionBody} HieroProto.proto.TransactionBody * @typedef {import("@hashgraph/proto").proto.ITransactionBody} HieroProto.proto.ITransactionBody * @typedef {import("@hashgraph/proto").proto.ITransactionResponse} HieroProto.proto.ITransactionResponse * @typedef {import("@hashgraph/proto").proto.IContractCallTransactionBody} HieroProto.proto.IContractCallTransactionBody * @typedef {import("@hashgraph/proto").proto.IAccountID} HieroProto.proto.IAccountID * @typedef {import("@hashgraph/proto").proto.IContractID} HieroProto.proto.IContractID * @typedef {import("@hashgraph/proto").proto.IFileID} HieroProto.proto.IFileID */ /** * @typedef {import("bignumber.js").default} BigNumber * @typedef {import("../channel/Channel.js").default} Channel * @typedef {import("../client/Client.js").default<*, *>} Client * @typedef {import("../account/AccountId.js").default} AccountId * @typedef {import("../transaction/TransactionId.js").default} TransactionId */ /** * @typedef {object} FunctionParameters * @property {string} name * @property {ContractFunctionParameters} parameters */ /** * Call a function of a given smart contract, providing function parameter * inputs as needed. * <p> * Resource ("gas") charges SHALL include all relevant fees incurred by * the contract execution, including any storage required.<br/> * The total transaction fee SHALL incorporate all of the "gas" actually * consumed as well as the standard fees for transaction handling, * data transfers, signature verification, etc... */ class ContractExecuteTransaction extends _Transaction.default { /** * @param {object} [props] * @param {ContractId | string} [props.contractId] * @param {number | Long} [props.gas] * @param {number | string | Long | BigNumber | Hbar} [props.amount] * @param {Uint8Array} [props.functionParameters] * @param {FunctionParameters} [props.function] */ constructor(props = {}) { super(); /** * @private * @type {?ContractId} */ this._contractId = null; /** * @private * @type {?Long} */ this._gas = null; /** * @private * @type {?Hbar} */ this._amount = null; /** * @private * @type {?Uint8Array} */ this._functionParameters = null; if (props.contractId != null) { this.setContractId(props.contractId); } if (props.gas != null) { this.setGas(props.gas); } if (props.amount != null) { this.setPayableAmount(props.amount); } if (props.functionParameters != null) { this.setFunctionParameters(props.functionParameters); } else if (props.function != null) { this.setFunction(props.function.name, props.function.parameters); } } /** * @internal * @param {HieroProto.proto.ITransaction[]} transactions * @param {HieroProto.proto.ISignedTransaction[]} signedTransactions * @param {TransactionId[]} transactionIds * @param {AccountId[]} nodeIds * @param {HieroProto.proto.ITransactionBody[]} bodies * @returns {ContractExecuteTransaction} */ static _fromProtobuf(transactions, signedTransactions, transactionIds, nodeIds, bodies) { const body = bodies[0]; const call = /** @type {HieroProto.proto.IContractCallTransactionBody} */ body.contractCall; return _Transaction.default._fromProtobufTransactions(new ContractExecuteTransaction({ contractId: call.contractID != null ? _ContractId.default._fromProtobuf(/** @type {HieroProto.proto.IContractID} */ call.contractID) : undefined, gas: call.gas != null ? call.gas : undefined, amount: call.amount != null ? _Hbar.default.fromTinybars(call.amount) : undefined, functionParameters: call.functionParameters != null ? call.functionParameters : undefined }), transactions, signedTransactions, transactionIds, nodeIds, bodies); } /** * @returns {?ContractId} */ get contractId() { return this._contractId; } /** * Sets the contract ID which is being executed in this transaction. * * @param {ContractId | string} contractId * @returns {ContractExecuteTransaction} */ setContractId(contractId) { this._requireNotFrozen(); this._contractId = typeof contractId === "string" ? _ContractId.default.fromString(contractId) : contractId.clone(); return this; } /** * @returns {?Long} */ get gas() { return this._gas; } /** * Sets the amount of gas to use for the call. * * @param {number | Long} gas * @returns {ContractExecuteTransaction} */ setGas(gas) { this._requireNotFrozen(); this._gas = gas instanceof _long.default ? gas : _long.default.fromValue(gas); return this; } /** * @returns {?Hbar} */ get payableAmount() { return this._amount; } /** * Sets the number of hbars to be sent with this function call. * * @param {number | string | Long | BigNumber | Hbar} amount * @param {HbarUnit} unit * @returns {ContractExecuteTransaction} */ setPayableAmount(amount, unit = _HbarUnit.default.Hbar) { this._requireNotFrozen(); this._amount = amount instanceof _Hbar.default ? amount : _Hbar.default.from(amount, unit); return this; } /** * @returns {?Uint8Array} */ get functionParameters() { return this._functionParameters; } /** * @param {Uint8Array} functionParameters * @returns {this} */ setFunctionParameters(functionParameters) { this._requireNotFrozen(); this._functionParameters = functionParameters; return this; } /** * @param {string} name * @param {ContractFunctionParameters} [functionParameters] * @returns {this} */ setFunction(name, functionParameters) { this._requireNotFrozen(); this._functionParameters = functionParameters != null ? functionParameters._build(name) : new _ContractFunctionParameters.default()._build(name); return this; } /** * @param {Client} client */ _validateChecksums(client) { if (this._contractId != null) { this._contractId.validateChecksum(client); } } /** * @override * @internal * @param {Channel} channel * @param {HieroProto.proto.ITransaction} request * @returns {Promise<HieroProto.proto.ITransactionResponse>} */ _execute(channel, request) { return channel.smartContract.contractCallMethod(request); } /** * @override * @protected * @returns {NonNullable<HieroProto.proto.TransactionBody["data"]>} */ _getTransactionDataCase() { return "contractCall"; } /** * @override * @protected * @returns {HieroProto.proto.IContractCallTransactionBody} */ _makeTransactionData() { return { contractID: this._contractId != null ? this._contractId._toProtobuf() : null, gas: this._gas, amount: this._amount != null ? this._amount.toTinybars() : null, functionParameters: this._functionParameters }; } /** * @returns {string} */ _getLogId() { const timestamp = /** @type {import("../Timestamp.js").default} */ this._transactionIds.current.validStart; return `ContractExecuteTransaction:${timestamp.toString()}`; } } exports.default = ContractExecuteTransaction; _Transaction.TRANSACTION_REGISTRY.set("contractCall", // eslint-disable-next-line @typescript-eslint/unbound-method ContractExecuteTransaction._fromProtobuf);