@hiero-ledger/sdk
Version:
318 lines (282 loc) • 9.24 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import Hbar from "../Hbar.js";
import Transaction, {
TRANSACTION_REGISTRY,
} from "../transaction/Transaction.js";
import ContractId from "./ContractId.js";
import ContractFunctionParameters from "./ContractFunctionParameters.js";
import Long from "long";
import HbarUnit from "../HbarUnit.js";
/**
* @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...
*/
export default class ContractExecuteTransaction extends Transaction {
/**
* @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._fromProtobufTransactions(
new ContractExecuteTransaction({
contractId:
call.contractID != null
? ContractId._fromProtobuf(
/** @type {HieroProto.proto.IContractID} */ (
call.contractID
),
)
: undefined,
gas: call.gas != null ? call.gas : undefined,
amount:
call.amount != null
? Hbar.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.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();
const gasLong = gas instanceof Long ? gas : Long.fromValue(gas);
if (gasLong.lt(0)) {
throw new Error("Gas must be greater than 0");
}
this._gas = gasLong;
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.Hbar) {
this._requireNotFrozen();
this._amount =
amount instanceof Hbar ? amount : Hbar.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()._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()}`;
}
}
TRANSACTION_REGISTRY.set(
"contractCall",
// eslint-disable-next-line @typescript-eslint/unbound-method
ContractExecuteTransaction._fromProtobuf,
);