コード例 #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));
コード例 #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);
コード例 #3
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));