コード例 #1
0
ファイル: Blockchain.cs プロジェクト: dumians/Conferences
        public static async Task <HttpResponseMessage> CreateRespone(HttpRequestMessage req, string functionName)
        {
            // Get request body
            var jsonBody = await req.Content.ReadAsStringAsync();

            if (string.IsNullOrWhiteSpace(jsonBody))
            {
                jsonBody = @"{}";
            }
            var body = JsonConvert.DeserializeObject <JObject>(jsonBody);

            // Get parameters
            var inputParameters = body.Values();
            var arguments       = new object[inputParameters.Count()];
            var i = 0;

            foreach (var p in inputParameters.Values())
            {
                arguments[i++] = p.Value <string>();
            }

            var web3     = new Nethereum.Web3.Web3(SmartContract.BlockchainRpcEndpoint);
            var contract = web3.Eth.GetContract(SmartContract.Abi, SmartContract.ContractAddress);

            var functionABI = contract.ContractBuilder.ContractABI.Functions
                              .FirstOrDefault(f => f.Name == functionName);

            if (functionABI == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, "Function not found!"));
            }

            var functionParameters = functionABI.InputParameters;

            if (functionParameters?.Count() != inputParameters.Count())
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, "Parameters do not match!"));
            }

            Function function   = contract.GetFunction(functionName);
            Type     returnType = GetFunctionReturnType(functionABI);
            IEthCall ethCall    = contract.Eth.Transactions.Call;
            var      result     = await ethCall.SendRequestAsync(function.CreateCallInput(arguments), contract.Eth.DefaultBlock)
                                  .ConfigureAwait(false);

            FunctionBase functionBase        = function;
            PropertyInfo builderBaseProperty = functionBase.GetType()
                                               .GetProperty("FunctionBuilderBase", BindingFlags.Instance | BindingFlags.NonPublic);

            if (builderBaseProperty != null)
            {
                FunctionBuilderBase builderBase             = (FunctionBuilderBase)builderBaseProperty.GetValue(functionBase);
                PropertyInfo        funcCallDecoderProperty = builderBase.GetType()
                                                              .GetProperty("FunctionCallDecoder", BindingFlags.Instance | BindingFlags.NonPublic);
                if (funcCallDecoderProperty != null)
                {
                    ParameterDecoder decoder = (ParameterDecoder)funcCallDecoderProperty.GetValue(builderBase);
                    var results = decoder.DecodeDefaultData(result, functionABI.OutputParameters);

                    if (results.Count == 1)
                    {
                        var resultValue = JsonConvert.SerializeObject(results[0].Result);
                        return(req.CreateResponse(HttpStatusCode.OK, resultValue));
                    }

                    var resultMultiValue = Activator.CreateInstance(returnType, results.Select(r => r.Result).ToArray());
                    return(req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(resultMultiValue)));
                }
            }

            return(req.CreateResponse(HttpStatusCode.InternalServerError));
        }
        public async Task <HttpResponseMessage> CreateResponse(string parameters, string functionName)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            var body = JsonConvert.DeserializeObject <JObject>(parameters);

            // Get parameters
            var inputParameters = body.Values();
            var arguments       = new object[inputParameters.Count()];
            var i = 0;

            foreach (var p in inputParameters.Values())
            {
                arguments[i++] = p.Value <string>();
            }

            Nethereum.Web3.Web3 web3     = new Nethereum.Web3.Web3(BlockchainRpcEndpoint);
            Contract            contract = web3.Eth.GetContract(Abi, ContractAddress);

            var functionABI = contract.ContractBuilder.ContractABI.Functions
                              .FirstOrDefault(f => f.Name == functionName);


            if (functionABI == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            var functionParameters = functionABI.InputParameters;

            if (functionParameters?.Count() != inputParameters.Count())
            {
                var fp = functionParameters.Count();
                var ip = inputParameters.Count();
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            Function function   = contract.GetFunction(functionName);
            Type     returnType = GetFunctionReturnType(functionABI);
            EthCall  ethCall    = contract.Eth.Transactions.Call;
            var      result     = await ethCall.SendRequestAsync(function.CreateCallInput(arguments), contract.Eth.DefaultBlock).ConfigureAwait(false);

            FunctionBase functionBase        = function;
            PropertyInfo builderBaseProperty = functionBase.GetType()
                                               .GetProperty("FunctionBuilderBase", BindingFlags.Instance | BindingFlags.NonPublic);

            if (builderBaseProperty != null)
            {
                FunctionBuilderBase builderBase             = (FunctionBuilderBase)builderBaseProperty.GetValue(functionBase);
                PropertyInfo        funcCallDecoderProperty = builderBase.GetType()
                                                              .GetProperty("FunctionCallDecoder", BindingFlags.Instance | BindingFlags.NonPublic);
                if (funcCallDecoderProperty != null)
                {
                    ParameterDecoder decoder = (ParameterDecoder)funcCallDecoderProperty.GetValue(builderBase);
                    var results = decoder.DecodeDefaultData(result, functionABI.OutputParameters[0]);

                    if (results.Count == 1)
                    {
                        var resultValue = JsonConvert.SerializeObject(results[0].Result);
                        return(new HttpResponseMessage(HttpStatusCode.OK));
                    }

                    var resultMultiValue = Activator.CreateInstance(returnType, results.Select(r => r.Result).ToArray());
                    return(new HttpResponseMessage(HttpStatusCode.OK));
                }
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }