Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nethereum C# how can I get balance in address?

Tags:

c#

ethereum

I try to get balance in address. It is my code:

`using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Nethereum.Web3;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
        class Program
        {

            static void Main(string[] args)
            {
                 Bananas().Wait();
            }

            static private async Task Bananas()
            {
                var publicKey = "0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B";
                var web3 = new Nethereum.Web3.Web3("https://ropsten.infura.io/myInfura");
                //var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(publicKey);
                var balance = await web3.Eth.GetBalance.SendRequestAsync(publicKey);
                var etherAmount = Web3.Convert.FromWei(balance.Value);

                Console.WriteLine(web3);
                Console.WriteLine("Get txCount ", etherAmount);
                Console.ReadLine();
            }
        }
    }`

I installed Nethereum via PM console: Nethereum. I use a normal link of infura. Why don`t I get a next result without the balance in address? enter image description here

like image 511
Stepan Poperechnyi Avatar asked Oct 17 '25 22:10

Stepan Poperechnyi


1 Answers

I have just made a console application with your code and all the data is coming back fine from Nethereum.

Your issue is with your Console.WriteLine(... You are passing the etherAmount as a arg0 property into the Console.WriteLine which will not output correctly on the console when you run it.

Try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Nethereum.Web3;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
        class Program
        {

            static void Main(string[] args)
            {
                 Bananas().Wait();
            }

            static private async Task Bananas()
            {
                var publicKey = "0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B";
                var web3 = new Nethereum.Web3.Web3("https://ropsten.infura.io/myInfura");
                //var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(publicKey);
                var balance = await web3.Eth.GetBalance.SendRequestAsync(publicKey);
                var etherAmount = Web3.Convert.FromWei(balance.Value);

                Console.WriteLine(web3);
                Console.WriteLine("Get txCount " + etherAmount);
                Console.ReadLine();
            }
        }
    }

ps nice 1110.337873197299357846 ETH ;) (i know it is only test ETH but we can dream)

like image 68
Josh Stevens Avatar answered Oct 20 '25 11:10

Josh Stevens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!