Ejemplo n.º 1
0
        public async Task <Transaction> GetTransactionFromNodeAsync(string transactionHash)
        {
            var body = new Models.Node.JsonRpcBody("eth_getTransactionByHash", 1);

            body.AddParam(transactionHash);

            var client = new RestClient(GetNodeUrl());

            var request = new RestRequest(Method.POST);

            request.AddParameter("application/json; charset=utf-8", Newtonsoft.Json.JsonConvert.SerializeObject(body), ParameterType.RequestBody);
            request.RequestFormat = DataFormat.Json;

            var cancellationTokenSource = new CancellationTokenSource();
            var restResponse            = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);

            if (restResponse.StatusCode == HttpStatusCode.OK)
            {
                var response = Newtonsoft.Json.JsonConvert.DeserializeObject <Models.Node.GetTransactionResponse>(restResponse.Content);
                return(response.result != null?ConversionService.Convert(response.result) : null);
            }
            else
            {
                if (string.IsNullOrEmpty(restResponse.Content))
                {
                    throw new Exception($"Error posting to node: {restResponse.ErrorException}");
                }
                else
                {
                    throw new Exception($"Error posting to node: {restResponse.Content}");
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <Models.DTOs.Block> GetBlockFromNodeAsync(ulong blockNumber)
        {
            var body = new Models.Node.JsonRpcBody("eth_getBlockByNumber", 1);

            body.AddParam(blockNumber.ToHexString());
            body.AddParam(true);

            var client = new RestClient(GetNodeUrl());

            var request = new RestRequest(Method.POST);

            request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(body), ParameterType.RequestBody);
            request.RequestFormat = DataFormat.Json;

            var cancellationTokenSource = new CancellationTokenSource();
            var restResponse            = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);

            if (restResponse.StatusCode == HttpStatusCode.OK)
            {
                var response = JsonConvert.DeserializeObject <Models.Node.GetBlockResponse>(restResponse.Content);
                return(ConversionService.Convert(response.result));
            }
            else
            {
                if (string.IsNullOrEmpty(restResponse.Content))
                {
                    throw new Exception($"Error posting to node: {restResponse.ErrorException}");
                }
                else
                {
                    throw new Exception($"Error posting to node: {restResponse.Content}");
                }
            }
        }
Ejemplo n.º 3
0
        public async Task <List <Transaction> > GetPendingTransactionsFromNodeAsync()
        {
            var body = new Models.Node.JsonRpcBody("eth_getBlockByNumber", 1);

            body.AddParam("pending");
            body.AddParam(true);

            var client = new RestClient(GetNodeUrl());

            var request = new RestRequest(Method.POST);

            request.AddParameter("application/json; charset=utf-8", Newtonsoft.Json.JsonConvert.SerializeObject(body), ParameterType.RequestBody);
            request.RequestFormat = DataFormat.Json;

            var cancellationTokenSource = new CancellationTokenSource();
            var restResponse            = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);

            if (restResponse.StatusCode == HttpStatusCode.OK)
            {
                var response = Newtonsoft.Json.JsonConvert.DeserializeObject <Models.Node.GetBlockResponse>(restResponse.Content);

                var transactions = new List <Transaction>();
                if (response.result != null)
                {
                    foreach (var transaction in response.result.transactions)
                    {
                        transactions.Add(ConversionService.Convert(transaction));
                    }
                }
                return(transactions);
            }
            else
            {
                if (string.IsNullOrEmpty(restResponse.Content))
                {
                    throw new Exception($"Error posting to node: {restResponse.ErrorException}");
                }
                else
                {
                    throw new Exception($"Error posting to node: {restResponse.Content}");
                }
            }
        }