Ejemplo n.º 1
0
        /// <summary>
        /// Calls a smart contract function locally on the gateway node.
        /// </summary>
        /// <remarks>
        /// This is performed locally on the gateway node. It cannot change the state of the contract instance
        /// (and so, cannot spend anything from the instance's cryptocurrency account). It will not have a
        /// consensus timestamp nor a record or a receipt. The response will contain the output returned
        /// by the function call.
        /// </remarks>
        /// <param name="queryParameters">
        /// The parameters identifying the contract and function method to call.
        /// </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>
        /// The results from the local contract query call.
        /// </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>
        public async Task <ContractCallResult> QueryContractAsync(QueryContractParams queryParameters, Action <IContext>?configure = null)
        {
            queryParameters         = RequireInputParameter.QueryParameters(queryParameters);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                ContractCallLocal = new ContractCallLocalQuery
                {
                    Header             = Transactions.CreateAskCostHeader(),
                    ContractID         = Protobuf.ToContractID(queryParameters.Contract),
                    Gas                = queryParameters.Gas,
                    FunctionParameters = Abi.EncodeFunctionWithArguments(queryParameters.FunctionName, queryParameters.FunctionArgs).ToByteString(),
                    MaxResultSize      = queryParameters.MaxAllowedReturnSize
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.ContractCallLocal.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.ContractCallLocal.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost + queryParameters.ReturnValueCharge, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(Protobuf.FromContractCallResult(response.ContractCallLocal.FunctionResult));
Ejemplo n.º 2
0
 internal ContractCallLocalQuery(QueryContractParams queryParameters) : this()
 {
     if (queryParameters is null)
     {
         throw new ArgumentNullException(nameof(queryParameters), "The query parameters are missing. Please check that the argument is not null.");
     }
     ContractID         = new ContractID(queryParameters.Contract);
     Gas                = queryParameters.Gas;
     FunctionParameters = ByteString.CopyFrom(Abi.EncodeFunctionWithArguments(queryParameters.FunctionName, queryParameters.FunctionArgs).Span);
     _throwOnFail       = queryParameters.ThrowOnFail;
 }
 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.º 4
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.º 5
0
        /// <summary>
        /// Calls a smart contract function locally on the gateway node.
        /// </summary>
        /// <remarks>
        /// This is performed locally on the gateway node. It cannot change the state of the contract instance
        /// (and so, cannot spend anything from the instance's cryptocurrency account). It will not have a
        /// consensus timestamp nor a record or a receipt. The response will contain the output returned
        /// by the function call.
        /// </remarks>
        /// <param name="queryParameters">
        /// The parameters identifying the contract and function method to call.
        /// </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>
        /// The results from the local contract query call.
        /// </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="ContractException">If the request was accepted by the network but the cotnract failed for
        /// some reason.  Contains additional information returned from the contract virual machine.  Only thrown if
        /// the <see cref="QueryContractParams.ThrowOnFail"/> is set to <code>true</code>, the default, otherwise
        /// the method returns a <see cref="ContractCallResult"/> with the same information.</exception>
        public async Task <ContractCallResult> QueryContractAsync(QueryContractParams queryParameters, Action <IContext>?configure = null)
        {
            queryParameters         = RequireInputParameter.QueryParameters(queryParameters);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                ContractCallLocal = new ContractCallLocalQuery
                {
                    ContractID         = new ContractID(queryParameters.Contract),
                    Gas                = queryParameters.Gas,
                    FunctionParameters = Abi.EncodeFunctionWithArguments(queryParameters.FunctionName, queryParameters.FunctionArgs).ToByteString(),
                    MaxResultSize      = queryParameters.MaxAllowedReturnSize
                }
            };
            var response = await query.SignAndExecuteWithRetryAsync(context, queryParameters.ReturnValueCharge);

            var header = response.ResponseHeader;

            if (header == null)
            {
                throw new PrecheckException($"Transaction Failed to Produce a Response.", query.QueryHeader !.getTransactionId() !.ToTxId(), ResponseCode.Unknown, 0);
            }
            if (response.ContractCallLocal?.FunctionResult == null)
            {
                throw new PrecheckException($"Transaction Failed Pre-Check: {header.NodeTransactionPrecheckCode}", query.QueryHeader !.getTransactionId() !.ToTxId(), (ResponseCode)header.NodeTransactionPrecheckCode, header.Cost);
            }
            if (queryParameters.ThrowOnFail && header.NodeTransactionPrecheckCode != ResponseCodeEnum.Ok)
            {
                throw new ContractException(
                          $"Contract Query Failed with Code: {header.NodeTransactionPrecheckCode}",
                          query.QueryHeader !.getTransactionId() !.ToTxId(),
                          (ResponseCode)header.NodeTransactionPrecheckCode,
                          header.Cost,
                          response.ContractCallLocal.FunctionResult.ToContractCallResult());
            }
            return(response.ContractCallLocal.FunctionResult.ToContractCallResult());
        }
Ejemplo n.º 6
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.º 7
0
        /// <summary>
        /// Calls a smart contract function locally on the gateway node.
        /// </summary>
        /// <remarks>
        /// This is performed locally on the gateway node. It cannot change the state of the contract instance
        /// (and so, cannot spend anything from the instance's cryptocurrency account). It will not have a
        /// consensus timestamp nor a record or a receipt. The response will contain the output returned
        /// by the function call.
        /// </remarks>
        /// <param name="queryParameters">
        /// The parameters identifying the contract and function method to call.
        /// </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>
        /// The results from the local contract query call.
        /// </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>
        public async Task <ContractCallResult> QueryContractAsync(QueryContractParams queryParameters, Action <IContext>?configure = null)
        {
            queryParameters = RequireInputParameter.QueryParameters(queryParameters);
            var context         = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transfers       = Transactions.CreateCryptoTransferList((payer, -context.FeeLimit), (gateway, context.FeeLimit));
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Query Contract Local Call");
            var query           = new Query
            {
                ContractCallLocal = new ContractCallLocalQuery
                {
                    Header             = Transactions.SignQueryHeader(transactionBody, payer),
                    ContractID         = Protobuf.ToContractID(queryParameters.Contract),
                    Gas                = queryParameters.Gas,
                    FunctionParameters = Abi.EncodeFunctionWithArguments(queryParameters.FunctionName, queryParameters.FunctionArgs).ToByteString(),
                    MaxResultSize      = queryParameters.MaxAllowedReturnSize
                }
            };
            var response = await Transactions.ExecuteRequestWithRetryAsync(context, query, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, getResponseCode(response));
            return(Protobuf.FromContractCallResult(response.ContractCallLocal.FunctionResult));