Exemple #1
0
        public async Task <HexBigInteger> EstimateGasAsync(string contractAddress, TFunctionMessage functionMessage = null)
        {
            if (functionMessage == null)
            {
                functionMessage = new TFunctionMessage();
            }
            SetEncoderContractAddress(contractAddress);
            var callInput = FunctionMessageEncodingService.CreateCallInput(functionMessage);

            try
            {
                if (TransactionManager.EstimateOrSetDefaultGasIfNotSet)
                {
                    return(await TransactionManager.EstimateGasAsync(callInput).ConfigureAwait(false));
                }

                return(null);
            }
            catch (RpcResponseException rpcException)
            {
                ContractRevertExceptionHandler.HandleContractRevertException(rpcException);
                throw;
            }
            catch (Exception)
            {
                var ethCall = new EthCall(TransactionManager.Client);
                var result  = await ethCall.SendRequestAsync(callInput).ConfigureAwait(false);

                new FunctionCallDecoder().ThrowIfErrorOnOutput(result);
                throw;
            }
        }
Exemple #2
0
        public async Task <dynamic> ExecuteTestAsync(RpcClient client)
        {
            //The compiled solidity contract to be deployed
            //contract test { function multiply(uint a) returns(uint d) { return a * 7; } }
            var contractByteCode =
                "0x606060405260728060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b604b60048080359060200190919050506061565b6040518082815260200191505060405180910390f35b6000600782029050606d565b91905056";

            //Create a new Eth Send Transanction RPC Handler
            var ethSendTransation = new EthSendTransaction(client);
            //As the input the compiled contract is the Data, together with our address
            var transactionInput = new TransactionInput();

            transactionInput.Data = contractByteCode;
            transactionInput.From = "0x12890d2cce102216644c59dae5baed380d84830c";
            // retrieve the hash
            var transactionHash = await ethSendTransation.SendRequestAsync(transactionInput);

            //the contract should be mining now

            //get contract address
            var ethGetTransactionReceipt = new EthGetTransactionReceipt(client);
            TransactionReceipt receipt   = null;

            //wait for the contract to be mined to the address
            while (receipt == null)
            {
                receipt = await ethGetTransactionReceipt.SendRequestAsync(transactionHash);
            }

            //Encode and build function parameters
            var function = new FunctionCallEncoder();

            //Input the function method Sha3Encoded (4 bytes)
            var sha3Signature = "c6888fa1";
            //Define input parameters
            var inputParameters = new[] { new Parameter("uint", "a") };
            //encode the function call (function + parameter input)

            //using 69 as the input
            var functionCall = function.EncodeRequest(sha3Signature, inputParameters, 69);

            //reuse the transaction input, (just the address)
            //the destination address is the contract address
            transactionInput.To = receipt.ContractAddress;
            //use as data the function call
            transactionInput.Data = functionCall;
            // rpc method to do the call
            var ethCall = new EthCall(client);
            // call and get the result
            var resultFunction = await ethCall.SendRequestAsync(transactionInput);

            // decode the output
            var functionDecoder = new FunctionCallDecoder();

            var output = functionDecoder.DecodeOutput <int>(resultFunction, new Parameter("uint", "d"));

            //visual test
            return("The result of deploying a contract and calling a function to multiply 7 by 69 is: " + output +
                   " and should be 483");
        }
Exemple #3
0
        protected async Task <TReturn> CallAsync <TReturn>(TReturn functionOuput, string encodedFunctionCall,
                                                           CallInput callInput, BlockParameter block)
        {
            callInput.Data = encodedFunctionCall;
            var result = await EthCall.SendRequestAsync(callInput, block).ConfigureAwait(false);

            return(FunctionCallDecoder.DecodeFunctionOutput(functionOuput, result));
        }
        public async Task<dynamic> ExecuteTestAsync(RpcClient client)
        {

            
            //The compiled solidity contract to be deployed
            //contract test { function multiply(uint a) returns(uint d) { return a * 7; } }
            var contractByteCode = "0x606060405260728060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b604b60048080359060200190919050506061565b6040518082815260200191505060405180910390f35b6000600782029050606d565b91905056";

            //Create a new Eth Send Transanction RPC Handler
            var ethSendTransation = new EthSendTransaction(client);
            //As the input the compiled contract is the Data, together with our address
            var transactionInput = new TransactionInput();
            transactionInput.Data = contractByteCode;
            transactionInput.From = "0x12890d2cce102216644c59dae5baed380d84830c";
            // retrieve the hash
            var transactionHash =  await ethSendTransation.SendRequestAsync( transactionInput);
            
            //the contract should be mining now

            //get contract address 
            var ethGetTransactionReceipt = new EthGetTransactionReceipt(client);
            TransactionReceipt receipt = null;
            //wait for the contract to be mined to the address
            while (receipt == null)
            {
                receipt = await ethGetTransactionReceipt.SendRequestAsync( transactionHash);
            }

            //Encode and build function parameters 
            var function = new FunctionCallEncoder();

            //Input the function method Sha3Encoded (4 bytes) 
            var sha3Signature = "c6888fa1";
            //Define input parameters
            var inputParameters = new[] { new Parameter("uint", "a") };
            //encode the function call (function + parameter input)
           
            //using 69 as the input
            var functionCall = function.EncodeRequest(sha3Signature, inputParameters, 69);
            //reuse the transaction input, (just the address) 
            //the destination address is the contract address
            transactionInput.To = receipt.ContractAddress;
            //use as data the function call
            transactionInput.Data = functionCall;
            // rpc method to do the call
            var ethCall = new EthCall(client);
            // call and get the result
            var resultFunction = await ethCall.SendRequestAsync( transactionInput);
            // decode the output
            var functionDecoder = new FunctionCallDecoder();

            var output = functionDecoder.DecodeOutput<int>(resultFunction, new Parameter("uint", "d"));
            //visual test 
            return "The result of deploying a contract and calling a function to multiply 7 by 69 is: " + output  + " and should be 483";

           

        }
Exemple #5
0
        protected async Task <TReturn> CallAsync <TReturn>(TReturn functionOuput, CallInput callInput)
        {
            var result =
                await
                EthCall.SendRequestAsync(callInput, DefaultBlock)
                .ConfigureAwait(false);

            return(FunctionCallDecoder.DecodeFunctionOutput(functionOuput, result));
        }
Exemple #6
0
        protected async Task <TReturn> CallAsync <TReturn>(TReturn functionOuput, string encodedFunctionCall, BlockParameter block)
        {
            var result =
                await
                EthCall.SendRequestAsync(new CallInput(encodedFunctionCall, ContractAddress), block)
                .ConfigureAwait(false);

            return(FunctionCallDecoder.DecodeFunctionOutput(functionOuput, result));
        }
Exemple #7
0
        protected async Task <TReturn> CallAsync <TReturn>(CallInput callInput, BlockParameter block)
        {
            var result =
                await
                EthCall.SendRequestAsync(callInput, block)
                .ConfigureAwait(false);

            return(DecodeSimpleTypeOutput <TReturn>(result));
        }
Exemple #8
0
        protected async Task <TReturn> CallAsync <TReturn>(string encodedFunctionCall, CallInput callInput)
        {
            callInput.Data = encodedFunctionCall;
            var result = await EthCall.SendRequestAsync(callInput, DefaultBlock).ConfigureAwait(false);

            return
                (FunctionCallDecoder.DecodeSimpleTypeOutput <TReturn>(
                     GetFirstParameterOrNull(FunctionABI.OutputParameters), result));
        }
        protected async Task <TReturn> CallAsync <TReturn>(string encodedFunctionCall)
        {
            var result =
                await
                ethCall.SendRequestAsync(new CallInput(encodedFunctionCall, ContractAddress), DefaultBlock)
                .ConfigureAwait(false);

            return
                (FunctionCallDecoder.DecodeSimpleTypeOutput <TReturn>(
                     GetFirstParameterOrNull(FunctionABI.OutputParameters), result));
        }
Exemple #10
0
        protected async Task <TReturn> CallAsync <TReturn>(TReturn functionOuput, string encodedFunctionCall, string from,
                                                           HexBigInteger gas, HexBigInteger value)
        {
            var result =
                await
                EthCall.SendRequestAsync(new CallInput(encodedFunctionCall, ContractAddress, @from, gas, value),
                                         DefaultBlock).ConfigureAwait(false);

            return(FunctionCallDecoder.DecodeFunctionOutput(functionOuput, result));
        }
Exemple #11
0
 protected Task <TReturn> CallAsync <TReturn>(TReturn functionOuput, CallInput callInput, BlockParameter block)
 {
     return(EthCall.SendRequestAsync(callInput, block).ContinueWith(result =>
     {
         if (result.Exception != null)
         {
             throw result.Exception;
         }
         return FunctionCallDecoder.DecodeFunctionOutput(functionOuput, result.Result);
     }));
 }
Exemple #12
0
 protected Task <TReturn> CallAsync <TReturn>(CallInput callInput, BlockParameter block)
 {
     return(EthCall.SendRequestAsync(callInput, block).ContinueWith(result =>
     {
         if (result.Exception != null)
         {
             throw result.Exception;
         }
         return DecodeSimpleTypeOutput <TReturn>(result.Result);
     }));
 }
Exemple #13
0
        protected async Task <TReturn> CallAsync <TReturn>(string encodedFunctionCall, string from, HexBigInteger gas,
                                                           HexBigInteger value, BlockParameter block)
        {
            var result =
                await
                EthCall.SendRequestAsync(new CallInput(encodedFunctionCall, ContractAddress, @from, gas, value),
                                         block).ConfigureAwait(false);

            return
                (FunctionCallDecoder.DecodeSimpleTypeOutput <TReturn>(
                     GetFirstParameterOrNull(FunctionABI.OutputParameters), result));
        }
Exemple #14
0
 /*
  curl -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"from":"0x65180b8c813457b21dad6cc6363d195231b4d2e9","data":"0x606060405260728060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b604b60048080359060200190919050506061565b6040518082815260200191505060405180910390f35b6000600782029050606d565b91905056"}],"id":1}' http://localhost:8545
  */
 public async Task<dynamic> ExecuteTestAsync(RpcClient client)
 {
     //function multiply , input 69
     var contractByteCode = "0xc6888fa10000000000000000000000000000000000000000000000000000000000000045";
     var to = "0x32eb97b8ad202b072fd9066c03878892426320ed";
     var ethSendTransation = new EthCall(client);
     var transactionInput = new CallInput();
     transactionInput.Data = contractByteCode;
     transactionInput.To = to;
     transactionInput.From = "0x12890d2cce102216644c59dae5baed380d84830c";
     return await ethSendTransation.SendRequestAsync( transactionInput);
     //result
     // 0x00000000000000000000000000000000000000000000000000000000000001e3
     //483
 }
Exemple #15
0
        /*
         *   curl -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"from":"0x65180b8c813457b21dad6cc6363d195231b4d2e9","data":"0x606060405260728060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b604b60048080359060200190919050506061565b6040518082815260200191505060405180910390f35b6000600782029050606d565b91905056"}],"id":1}' http://localhost:8545
         */

        public async Task <dynamic> ExecuteTestAsync(RpcClient client)
        {
            //function multiply , input 69
            var contractByteCode = "0xc6888fa10000000000000000000000000000000000000000000000000000000000000045";
            var to = "0x32eb97b8ad202b072fd9066c03878892426320ed";
            var ethSendTransation = new EthCall(client);
            var transactionInput  = new CallInput();

            transactionInput.Data = contractByteCode;
            transactionInput.To   = to;
            transactionInput.From = "0x12890d2cce102216644c59dae5baed380d84830c";
            return(await ethSendTransation.SendRequestAsync(transactionInput));

            //result
            // 0x00000000000000000000000000000000000000000000000000000000000001e3
            //483
        }
Exemple #16
0
        public Task <string> QueryAsync(
            string contractAddress,
            TFunctionMessage contractFunctionMessage = null,
            BlockParameter block = null)
        {
            if (contractFunctionMessage == null)
            {
                contractFunctionMessage = new TFunctionMessage();
            }
            if (block == null)
            {
                block = DefaultBlockParameter;
            }
            FunctionMessageEncodingService.SetContractAddress(contractAddress);
            EnsureInitialiseAddress();
            var callInput = FunctionMessageEncodingService.CreateCallInput(contractFunctionMessage);

            return(EthCall.SendRequestAsync(callInput, block));
        }
        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));
        }
        public override async Task <string> ExecuteAsync(IClient client)
        {
            //The compiled solidity contract to be deployed
            //contract test { function multiply(uint a) returns(uint d) { return a * 7; } }
            var contractByteCode =
                "0x606060405260728060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b604b60048080359060200190919050506061565b6040518082815260200191505060405180910390f35b6000600782029050606d565b91905056";

            //As the input the compiled contract is the Data, together with our address
            var transactionInput = new TransactionInput();

            transactionInput.Data = contractByteCode;
            transactionInput.From = Settings.GetDefaultAccount();
            // retrieve the hash

            var minerStart       = new MinerStart(client);
            var minerStartResult = await minerStart.SendRequestAsync();

            var transactionHash =
                await
                new PersonalSignAndSendTransaction(client).SendRequestAsync(transactionInput,
                                                                            Settings.GetDefaultAccountPassword());

            //get contract address
            var ethGetTransactionReceipt = new EthGetTransactionReceipt(client);
            TransactionReceipt receipt   = null;

            //wait for the contract to be mined to the address
            while (receipt == null)
            {
                await Task.Delay(1000);

                receipt = await ethGetTransactionReceipt.SendRequestAsync(transactionHash);
            }


            var minerStop       = new MinerStop(client);
            var minerStopResult = await minerStop.SendRequestAsync();

            //Encode and build function parameters
            var function = new FunctionCallEncoder();

            //Input the function method Sha3Encoded (4 bytes)
            var sha3Signature = "c6888fa1";
            //Define input parameters
            var inputParameters = new[] { new Parameter("uint", "a") };
            //encode the function call (function + parameter input)

            //using 69 as the input
            var functionCall = function.EncodeRequest(sha3Signature, inputParameters, 69);

            //reuse the transaction input, (just the address)
            //the destination address is the contract address
            transactionInput.To = receipt.ContractAddress;
            //use as data the function call
            transactionInput.Data = functionCall;
            // rpc method to do the call
            var ethCall = new EthCall(client);
            // call and get the result
            var resultFunction = await ethCall.SendRequestAsync(transactionInput);

            // decode the output
            var functionDecoder = new FunctionCallDecoder();

            var output  = functionDecoder.DecodeOutput <int>(resultFunction, new Parameter("uint", "d"));
            var message = "The result of deploying a contract and calling a function to multiply 7 by 69 is: " +
                          output +
                          " and should be 483";

            Assert.Equal(483, output);

            return(message);
        }
Exemple #19
0
        protected async Task <TReturn> CallAsync <TReturn>(string encodedFunctionCall) where TReturn : new()
        {
            var result = await ethCall.SendRequestAsync(new CallInput(encodedFunctionCall, ContractAddress), DefaultBlock);

            return(FunctionCallDecoder.DecodeOutput <TReturn>(result, FunctionABI.OutputParameters));
        }