コード例 #1
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";

           

        }
コード例 #2
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);
 }
コード例 #3
0
        public async Task <TransactionReceipt> PollForReceiptAsync(string transaction, CancellationTokenSource tokenSource = null)
        {
            var getTransactionReceipt = new EthGetTransactionReceipt(_transactionManager.Client);
            var receipt = await getTransactionReceipt.SendRequestAsync(transaction).ConfigureAwait(false);

            while (receipt == null)
            {
                await Task.Delay(_retryMiliseconds);

                tokenSource?.Token.ThrowIfCancellationRequested();
                receipt = await getTransactionReceipt.SendRequestAsync(transaction).ConfigureAwait(false);
            }
            return(receipt);
        }
コード例 #4
0
        /// <summary>
        /// Get Receipt Proof
        /// </summary>
        /// <param name="transactionId"></param>
        /// <returns></returns>
        public async Task <TransactionReceiptResponse> GetReceiptProof(string transactionId)
        {
            if (!String.IsNullOrEmpty(SyncerUrl))
            {
                try
                {
                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri(SyncerUrl);
                    HttpResponseMessage response = await client.GetAsync($"tx/{transactionId}/receipt/proof");

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string contentString = await response.Content.ReadAsStringAsync();

                        TransactionReceiptResponse receiptProofResponse = JsonConvert.DeserializeObject <TransactionReceiptResponse>(contentString);
                        return(receiptProofResponse);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"Could not fetch the Receipt Proof from the syncer URL because {ex.Message}");
                }
            }
            else
            {
                try
                {
                    EthGetTransactionReceipt transactionByHash = ParentWeb3.Eth.Transactions.GetTransactionReceipt;
                    TransactionReceipt       tx = await transactionByHash.SendRequestAsync(transactionId);

                    TransactionReceiptResponse transactionReceiptResponse = new TransactionReceiptResponse()
                    {
                        Value = tx
                    };

                    return(transactionReceiptResponse);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Could not fetch te receipt proof because {ex.Message}");
                }
            }


            return(null);
        }
コード例 #5
0
        /// <summary>
        /// Continuously checks the transaction receipt until it is found, and calls the respective event.
        /// </summary>
        /// <param name="sender"> Sender object. </param>
        /// <param name="eventArgs"> Timer event arguments. </param>
        private async void CheckTransactionReceipt(object sender, ElapsedEventArgs eventArgs)
        {
            EthGetTransactionReceipt ethGetTransactionReceipt = new EthGetTransactionReceipt(NetworkProvider.GetWeb3().Client);
            TransactionReceipt       receipt = await ethGetTransactionReceipt.SendRequestAsync(txHash);

            if (receipt == null)
            {
                return;
            }

            if (receipt.HasErrors() == true || receipt.Status.Value == 0)
            {
                OnTransactionFail?.Invoke();
            }
            else
            {
                OnTransactionSuccess?.Invoke();
            }

            timer.Stop();
        }
コード例 #6
0
        public async Task <object> ExecuteTestAsync(IClient client)
        {
            /* This is the example contract containing an event raised every time we call multiply
             * contract test {
             *
             *  event Multiplied(uint indexed a, address sender);
             *
             *  function multiply(uint a) returns(uint d)
             *  {
             *      Multiplied(a, msg.sender);
             *      return a * 7;
             *
             *  }
             *
             * }*/

            //The contract byte code already compiled
            var contractByteCode =
                "606060405260c08060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b604b60048080359060200190919050506061565b6040518082815260200191505060405180910390f35b6000817f10f82b5dc139f3677a16d7bfb70c65252e78143313768d2c52e07db775e1c7ab33604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390a260078202905060bb565b91905056";

            //Create a new Eth Send Transanction RPC Handler
            var ethSendTransation = new EthSendTransaction(client);

            //Create the transaction input for the new contract

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

            transactionInput.Data = contractByteCode;
            transactionInput.From = "0x12890d2cce102216644c59dae5baed380d84830c";
            // retrieve the transaction hash, as we need to get a transaction sreceipt with the contract address
            var transactionHash = await ethSendTransation.SendRequestAsync(transactionInput);

            //the contract should be mining now

            //Get the transaction receipt using the transactionHash
            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);
            }

            //sha3 the event call, we can use this to validate our topics

            var eventCallSh3 = await new Web3Sha3(client).SendRequestAsync(new HexUTF8String("Multiplied(uint256,address)"));
            //create a filter
            //just listen to anything no more filter topics (ie int indexed number)
            var ethFilterInput = new NewFilterInput();

            ethFilterInput.FromBlock.SetValue(receipt.BlockNumber);
            ethFilterInput.ToBlock = BlockParameter.CreateLatest();
            ethFilterInput.Address = new[] { receipt.ContractAddress };
            //no topics
            //ethFilterInput.Topics = new object[]{};

            var newEthFilter = new EthNewFilter(client);
            var filterId     = await newEthFilter.SendRequestAsync(ethFilterInput);


            //create a transaction which will raise the event
            await SendTransaction(client, transactionInput.From, receipt.ContractAddress);

            //get filter changes
            var ethGetFilterChangesForEthNewFilter = new EthGetFilterChangesForEthNewFilter(client);

            FilterLog[] logs = null;

            while (logs == null || logs.Length < 1)
            {
                //Get the filter changes logs
                logs = await ethGetFilterChangesForEthNewFilter.SendRequestAsync(filterId);

                if (logs.Length > 0)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine("Topic 0: " + logs[0].Topics[0] +
                                  " should be the same as the SH3 encoded event signature " + eventCallSh3);
                    Assert.Equal(logs[0].Topics[0], eventCallSh3);
                    sb.AppendLine("Topic 1: " + logs[0].Topics[1] + " should be 69 hex  0x45, padded");

                    sb.AppendLine("Data " + logs[0].Data + " should be the same as the address padded 32 bytes " +
                                  transactionInput.From);

                    return(sb.ToString());
                }
            }
            throw new Exception("Execution failed");
        }
コード例 #7
0
        public async Task <object> ExecuteTestAsync(IClient 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"));
            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);
        }
        public async Task<object> ExecuteTestAsync(IClient client)
        {
            /* This is the example contract containing an event raised every time we call multiply
            contract test { 
    
                event Multiplied(uint indexed a, address sender);
    
                function multiply(uint a) returns(uint d) 
                { 
                    Multiplied(a, msg.sender);
                    return a * 7; 
                    
                } 
    
            }*/

            //The contract byte code already compiled
            var contractByteCode =
                "606060405260c08060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b604b60048080359060200190919050506061565b6040518082815260200191505060405180910390f35b6000817f10f82b5dc139f3677a16d7bfb70c65252e78143313768d2c52e07db775e1c7ab33604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390a260078202905060bb565b91905056";

            //Create a new Eth Send Transanction RPC Handler
            var ethSendTransation = new EthSendTransaction(client);

            //Create the transaction input for the new contract

            //On transaction input the compiled contract is the Data, together with our sender address 
            var transactionInput = new TransactionInput();
            transactionInput.Data = contractByteCode;
            transactionInput.From = "0x12890d2cce102216644c59dae5baed380d84830c";
            // retrieve the transaction hash, as we need to get a transaction sreceipt with the contract address
            var transactionHash = await ethSendTransation.SendRequestAsync(transactionInput);

            //the contract should be mining now

            //Get the transaction receipt using the transactionHash
            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);
            }

            //sha3 the event call, we can use this to validate our topics 

            var eventCallSh3 = await new Web3Sha3(client).SendRequestAsync(new HexUTF8String("Multiplied(uint256,address)"));
            //create a filter 
            //just listen to anything no more filter topics (ie int indexed number)
            var ethFilterInput = new NewFilterInput();
            ethFilterInput.FromBlock.SetValue(receipt.BlockNumber);
            ethFilterInput.ToBlock = BlockParameter.CreateLatest();
            ethFilterInput.Address = new[] {receipt.ContractAddress};
            //no topics
            //ethFilterInput.Topics = new object[]{};

            var newEthFilter = new EthNewFilter(client);
            var filterId = await newEthFilter.SendRequestAsync(ethFilterInput);


            //create a transaction which will raise the event
            await SendTransaction(client, transactionInput.From, receipt.ContractAddress);

            //get filter changes
            var ethGetFilterChangesForEthNewFilter = new EthGetFilterChangesForEthNewFilter(client);
            FilterLog[] logs = null;

            while (logs == null || logs.Length < 1)
            {
                //Get the filter changes logs
                logs = await ethGetFilterChangesForEthNewFilter.SendRequestAsync(filterId);

                if (logs.Length > 0)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine("Topic 0: " + logs[0].Topics[0] +
                                  " should be the same as the SH3 encoded event signature " + eventCallSh3);
                    Assert.Equal(logs[0].Topics[0], eventCallSh3);
                    sb.AppendLine("Topic 1: " + logs[0].Topics[1] + " should be 69 hex  0x45, padded");
                   
                    sb.AppendLine("Data " + logs[0].Data + " should be the same as the address padded 32 bytes " +
                                  transactionInput.From);
                  
                    return sb.ToString();
                }
                
            }
           throw new Exception("Execution failed");
        }
コード例 #9
0
        public async Task <string> WithdrawLocally(string transactionId, MaticTransactionOptions options)
        {
            EthGetTransactionByHash ethGetTransactionByHash = web3.Eth.Transactions.GetTransactionByHash;
            Transaction             withdrawTransaction     = await ethGetTransactionByHash.SendRequestAsync(transactionId);

            EthGetTransactionReceipt ethGetTransactionReceipt = web3.Eth.Transactions.GetTransactionReceipt;
            TransactionReceipt       withdrawReceipt          = await ethGetTransactionReceipt.SendRequestAsync(transactionId);

            EthGetBlockWithTransactionsByNumber ethGetBlockWithTransactionsByNumber = web3.Eth.Blocks.GetBlockWithTransactionsByNumber;
            BlockWithTransactions withdrawBlock = await ethGetBlockWithTransactionsByNumber.SendRequestAsync(withdrawReceipt.BlockNumber);


            //Draft Withdraw Object
            DraftWithdrawObject withdrawObject = new DraftWithdrawObject()
            {
                TxId    = transactionId,
                Block   = withdrawBlock,
                Tx      = withdrawTransaction,
                Receipt = withdrawReceipt
            };

            //Get Transaction Proof
            TransactionProofResponse transactionProof = await  GetTxProof(withdrawObject.Tx, withdrawObject.Block);

            //Get Receipt Proof
            TransactionProofResponse receiptProof = await GetReceiptProof(withdrawObject.Receipt, withdrawObject.Block, web3);

            //Get Current Header Block
            HexBigInteger currentHeaderBlock = await maticRootChainContract.CurrenctHeaderBlock();

            //Get the Header
            Header header = await maticRootChainContract.HeaderBlock(new HexBigInteger(currentHeaderBlock.Value - 1));

            HexBigInteger headerNumber = new HexBigInteger(currentHeaderBlock.Value - 1);
            int           start        = header.Start;
            int           end          = header.End;

            Header headers = await GetHeaders(start, end, web3);

            MerkleTree tree = new MerkleTree(headers);

            string blockHeader = GetBlockHeader(withdrawObject.Block);

            string headerProof = await tree.GetProof(blockHeader);

            WithdrawBurntTokensModel withdrawBurntTokensModel = new WithdrawBurntTokensModel()
            {
                HeaderNumber   = headerNumber,
                HeaderProof    = "",
                BlockNumber    = withdrawObject.Block.Number,
                BlockTimeStamp = withdrawObject.Block.Timestamp,
                TxRoot         = withdrawObject.Block.TransactionsRoot,
                ReceiptRoot    = withdrawObject.Block.ReceiptsRoot,
                Path           = receiptProof.Path,
                TxBytes        = withdrawObject.Tx.ToString(),
                TxProof        = transactionProof.ParentNodes,
                ReceiptBytes   = withdrawObject.Receipt.ToString(),
                ReceiptProof   = receiptProof.ParentNodes
            };

            string withdrawTxObject = await maticWithrawalManagerContract.WithdrawBurntTokens(withdrawBurntTokensModel, options);

            return(withdrawTxObject);
        }