Ejemplo n.º 1
0
 internal ContractCallTransactionBody(Hashgraph.CallContractParams callParameters) : this()
 {
     if (callParameters is null)
     {
         throw new ArgumentNullException(nameof(callParameters), "The call parameters are missing. Please check that the argument is not null.");
     }
     ContractID         = new ContractID(callParameters.Contract);
     Gas                = callParameters.Gas;
     Amount             = callParameters.PayableAmount;
     FunctionParameters = ByteString.CopyFrom(Abi.EncodeFunctionWithArguments(callParameters.FunctionName, callParameters.FunctionArgs).Span);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Internal implementation of the contract call method.
        /// </summary>
        private async Task <TResult> CallContractImplementationAsync <TResult>(CallContractParams callParmeters, Action <IContext>?configure) where TResult : new()
        {
            callParmeters = RequireInputParameter.CallContractParameters(callParmeters);
            var context         = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Call Contract");

            transactionBody.ContractCall = new ContractCallTransactionBody
            {
                ContractID         = Protobuf.ToContractID(callParmeters.Contract),
                Gas                = callParmeters.Gas,
                Amount             = callParmeters.PayableAmount,
                FunctionParameters = Abi.EncodeFunctionWithArguments(callParmeters.FunctionName, callParmeters.FunctionArgs).ToByteString()
            };
            var request  = Transactions.SignTransaction(transactionBody, payer);
            var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Contract call failed, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is CallContractRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
                rec.Contract   = Protobuf.FromContractID(record.Receipt.ContractID);
                rec.CallResult = Protobuf.FromContractCallResult(record.ContractCallResult);
            }
            else if (result is ContractReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
                rcpt.Contract = Protobuf.FromContractID(receipt.ContractID);
            }
            return(result);
Ejemplo n.º 3
0
        /// <summary>
        /// Internal implementation of the contract call method.
        /// </summary>
        private async Task <TResult> CallContractImplementationAsync <TResult>(CallContractParams callParmeters, Action <IContext>?configure) where TResult : new()
        {
            callParmeters           = RequireInputParameter.CallContractParameters(callParmeters);
            await using var context = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var signatory       = Transactions.GatherSignatories(context, callParmeters.Signatory);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = new TransactionBody(context, transactionId);

            transactionBody.ContractCall = new ContractCallTransactionBody
            {
                ContractID         = new ContractID(callParmeters.Contract),
                Gas                = callParmeters.Gas,
                Amount             = callParmeters.PayableAmount,
                FunctionParameters = Abi.EncodeFunctionWithArguments(callParmeters.FunctionName, callParmeters.FunctionArgs).ToByteString()
            };
            var receipt = await transactionBody.SignAndExecuteWithRetryAsync(signatory, context);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Contract call failed, status: {receipt.Status}", transactionId.ToTxId(), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is CallContractRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                record.FillProperties(rec);
            }
            else if (result is TransactionReceipt rcpt)
            {
                receipt.FillProperties(transactionId, rcpt);
            }
            return(result);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Calls a smart contract returning if successful.  The CallContractReceipt
 /// will include the details of the results from the call, including the
 /// output parameters returned by the function call.
 /// </summary>
 /// <param name="callParameters">
 /// An object identifying the function to call, any input parameters and the
 /// amount of gas that may be used to execute the request.
 /// </param>
 /// <param name="configure">
 /// Optional callback method providing an opportunity to modify
 /// the execution configuration for just this method call.
 /// It is executed prior to submitting the request to the network.
 /// </param>
 /// <returns>
 /// A contract transaction record indicating success, it also
 /// any output parameters sent from the contract function.
 /// </returns>
 /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
 /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
 /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
 /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
 /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
 public Task <CallContractRecord> CallContractWithRecordAsync(CallContractParams callParameters, Action <IContext>?configure = null)
 {
     return(CallContractImplementationAsync <CallContractRecord>(callParameters, configure));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Calls a smart contract returning a receipt indicating success.
 /// This can be used for contract calls that do not return data.
 /// If the contract returns data, call the <see cref="CallContractWithRecordAsync"/>
 /// call instead to retrieve the information returned from the function call.
 /// </summary>
 /// <param name="callParameters">
 /// An object identifying the function to call, any input parameters and the
 /// amount of gas that may be used to execute the request.
 /// </param>
 /// <param name="configure">
 /// Optional callback method providing an opportunity to modify
 /// the execution configuration for just this method call.
 /// It is executed prior to submitting the request to the network.
 /// </param>
 /// <returns>
 /// A contract transaction receipt indicating success, it does not
 /// include any output parameters sent from the contract.
 /// </returns>
 /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
 /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
 /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
 /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
 /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
 public Task <TransactionReceipt> CallContractAsync(CallContractParams callParameters, Action <IContext>?configure = null)
 {
     return(CallContractImplementationAsync <TransactionReceipt>(callParameters, configure));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Calls a smart contract returning if successful.  The CallContractReceipt
 /// will include the details of the results from the call, including the
 /// output parameters returned by the function call.
 /// </summary>
 /// <param name="callParameters">
 /// An object identifying the function to call, any input parameters and the
 /// amount of gas that may be used to execute the request.
 /// </param>
 /// <param name="configure">
 /// Optional callback method providing an opportunity to modify
 /// the execution configuration for just this method call.
 /// It is executed prior to submitting the request to the network.
 /// </param>
 /// <returns>
 /// A contract transaction record indicating success, it also
 /// any output parameters sent from the contract function.
 /// </returns>
 /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
 /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
 /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
 /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
 /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
 public async Task <CallContractRecord> CallContractWithRecordAsync(CallContractParams callParameters, Action <IContext>?configure = null)
 {
     return(new CallContractRecord(await ExecuteTransactionAsync(new ContractCallTransactionBody(callParameters), configure, true, callParameters.Signatory).ConfigureAwait(false)));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Calls a smart contract returning a receipt indicating success.
 /// This can be used for contract calls that do not return data.
 /// If the contract returns data, call the <see cref="CallContractWithRecordAsync"/>
 /// call instead to retrieve the information returned from the function call.
 /// </summary>
 /// <param name="callParameters">
 /// An object identifying the function to call, any input parameters and the
 /// amount of gas that may be used to execute the request.
 /// </param>
 /// <param name="configure">
 /// Optional callback method providing an opportunity to modify
 /// the execution configuration for just this method call.
 /// It is executed prior to submitting the request to the network.
 /// </param>
 /// <returns>
 /// A contract transaction receipt indicating success, it does not
 /// include any output parameters sent from the contract.
 /// </returns>
 /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
 /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
 /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
 /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
 /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
 public async Task <TransactionReceipt> CallContractAsync(CallContractParams callParameters, Action <IContext>?configure = null)
 {
     return(new TransactionReceipt(await ExecuteTransactionAsync(new ContractCallTransactionBody(callParameters), configure, false, callParameters.Signatory).ConfigureAwait(false)));
 }