Пример #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;
            }
        }
Пример #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");
        }
Пример #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";

           

        }
Пример #5
0
        protected async Task <TReturn> CallAsync <TReturn>(CallInput callInput, BlockParameter block)
        {
            var result =
                await
                EthCall.SendRequestAsync(callInput, block)
                .ConfigureAwait(false);

            return(DecodeSimpleTypeOutput <TReturn>(result));
        }
Пример #6
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));
        }
Пример #7
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));
        }
Пример #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));
        }
Пример #9
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));
        }
Пример #10
0
        protected async Task <TReturn> CallAsync <TReturn>(string encodedFunctionCall, BlockParameter block)
        {
            var result =
                await
                EthCall.SendRequestAsync(new CallInput(encodedFunctionCall, ContractAddress), block)
                .ConfigureAwait(false);

            return
                (FunctionCallDecoder.DecodeSimpleTypeOutput <TReturn>(
                     GetFirstParameterOrNull(FunctionABI.OutputParameters), result));
        }
Пример #11
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);
     }));
 }
Пример #12
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);
     }));
 }
Пример #13
0
        protected FunctionBase(IClient rpcClient, Contract contract, FunctionABI functionABI)
        {
            FunctionABI             = functionABI;
            this.rpcClient          = rpcClient;
            this.contract           = contract;
            this.ethCall            = new EthCall(rpcClient);
            this.ethSendTransaction = new EthSendTransaction(rpcClient);

            this.FunctionCallDecoder = new FunctionCallDecoder();
            this.FunctionCallEncoder = new FunctionCallEncoder();
        }
Пример #14
0
 protected FunctionBase(IClient rpcClient, Contract contract, FunctionABI functionABI)
 {
     FunctionABI                    = functionABI;
     this.rpcClient                 = rpcClient;
     this.contract                  = contract;
     ethCall                        = new EthCall(rpcClient);
     ethSendTransaction             = new EthSendTransaction(rpcClient);
     ethEstimateGas                 = new EthEstimateGas(rpcClient);
     personalSignAndSendTransaction = new PersonalSignAndSendTransaction(rpcClient);
     FunctionCallDecoder            = new FunctionCallDecoder();
     FunctionCallEncoder            = new FunctionCallEncoder();
 }
Пример #15
0
        protected async Task <TReturn> CallAsync <TReturn>(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.DecodeSimpleTypeOutput <TReturn>(
                     GetFirstParameterOrNull(FunctionABI.OutputParameters), result));
        }
 public EthApiTransactionsService(IClient client) : base(client)
 {
     Call        = new EthCall(client);
     EstimateGas = new EthEstimateGas(client);
     GetTransactionByBlockHashAndIndex   = new EthGetTransactionByBlockHashAndIndex(client);
     GetTransactionByBlockNumberAndIndex = new EthGetTransactionByBlockNumberAndIndex(client);
     GetTransactionByHash  = new EthGetTransactionByHash(client);
     GetTransactionCount   = new EthGetTransactionCount(client);
     GetTransactionReceipt = new EthGetTransactionReceipt(client);
     SendRawTransaction    = new EthSendRawTransaction(client);
     SendTransaction       = new EthSendTransaction(client);
 }
Пример #17
0
 public EthTransactionsService(IClient client) : base(client)
 {
     Call = new EthCall(client);
     EstimateGas = new EthEstimateGas(client);
     GetTransactionByBlockHashAndIndex = new EthGetTransactionByBlockHashAndIndex(client);
     GetTransactionByBlockNumberAndIndex = new EthGetTransactionByBlockNumberAndIndex(client);
     GetTransactionByHash = new EthGetTransactionByHash(client);
     GetTransactionCount = new EthGetTransactionCount(client);
     GetTransactionReceipt = new EthGetTransactionReceipt(client);
     SendRawTransaction = new EthSendRawTransaction(client);
     SendTransaction = new EthSendTransaction(client);
 }
Пример #18
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
 }
Пример #19
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
        }
Пример #20
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 QueryToSimpleTypeHandler(EthCall ethCall, string defaultAddressFrom = null, BlockParameter defaultBlockParameter = null) : base(ethCall, defaultAddressFrom, defaultBlockParameter)
 {
     QueryRawHandler = new QueryRawHandler <TFunctionMessage>(ethCall, defaultAddressFrom, defaultBlockParameter);
 }
 public QueryDecoderBaseHandler(EthCall ethCall, string defaultAddressFrom = null, BlockParameter defaultBlockParameter = null)
 {
     QueryRawHandler = new QueryRawHandler <TFunctionMessage>(ethCall, defaultAddressFrom, defaultBlockParameter);
 }
 public ContractQueryEthCallHandler(EthCall ethCall, string defaultAddressFrom = null, BlockParameter defaultBlockParameter = null)
 {
     DefaultAddressFrom = defaultAddressFrom;
     DefaultBlockParameter = defaultBlockParameter;
     EthCall = ethCall;
 }
Пример #24
0
 protected QueryHandlerBase(EthCall ethCall, string defaultAddressFrom = null, BlockParameter defaultBlockParameter = null)
 {
     EthCall               = ethCall;
     DefaultAddressFrom    = defaultAddressFrom;
     DefaultBlockParameter = defaultBlockParameter ?? BlockParameter.CreateLatest();
 }
        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));
        }
Пример #26
0
 public QueryRawHandler(EthCall ethCall, string defaultAddressFrom = null, BlockParameter defaultBlockParameter = null) : base(ethCall, defaultAddressFrom, defaultBlockParameter)
 {
 }
        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);
        }
Пример #28
0
 public GethEthApiService(IClient client) : base(client)
 {
     PendingTransactions = new EthPendingTransactions(client);
     Call = new EthCall(client);
 }