5

I'm using this simple code to get the balances with NEthereum:

using System;
using System.Numerics;

namespace TestConsole
{
    class Program
    {
        static readonly BigInteger ethToWeiRatio = new BigInteger(1000000000000000000m);

        static void Main(string[] args)
        {
            var web3 = new Nethereum.Web3.Web3();
            var accountsTask = web3.Eth.Accounts.SendRequestAsync();
            accountsTask.Wait();
            var accounts = accountsTask.Result;
            foreach (var account in accounts)
            {
                Console.WriteLine("account " + account);
            }
            foreach (var account in accounts)
            {
                var balanceTask = web3.Eth.GetBalance.SendRequestAsync(account);
                balanceTask.Wait();
                var balance = balanceTask.Result;
                Console.WriteLine("account " + account + "; balance in wei: "
                                  + balance.Value);
            }
            foreach (var account in accounts)
            {
                var balanceTask = web3.Eth.GetBalance.SendRequestAsync(account);
                balanceTask.Wait();
                var balance = balanceTask.Result;
                Console.WriteLine("account " + account + "; balance in eth: "
                      + BigInteger.Divide(balance.Value, ethToWeiRatio));
            }
        }
    }
}

The first foreach loops work, but the 3rd doesn't. My guess was that the balances being printed in the second foreach are in Wei units, so I had to convert them to Eth. But when doing that, the result is 0 for both cases, which is wrong because the balances are different between the accounts.

This is the full output:

account 0xd74c7d19e3bff6b150f76cac754a65df3b23b755
account 0xa9f4a7d4d7a163bc9f45e2e1e216b7f40ce79af5
account 0xd74c7d19e3bff6b150f76cac754a65df3b23b755; balance in wei: 542316352000000000
account 0xa9f4a7d4d7a163bc9f45e2e1e216b7f40ce79af5; balance in wei: 300000000000000000
account 0xd74c7d19e3bff6b150f76cac754a65df3b23b755; balance in eth: 0
account 0xa9f4a7d4d7a163bc9f45e2e1e216b7f40ce79af5; balance in eth: 0

UPDATE 1: Just found an ugly workaround: decimal.Parse(balance.Value.ToString())/10000000000000000m.

UPDATE 2: Author of library recommended a new API: https://github.com/Nethereum/Nethereum/blob/master/src/Nethereum.Util/UnitConversion.cs (haven't tested it yet).

4

There is a helper converter function in Web3, similar to one implemented in web3.js.

It can be used as following:

web3.Convert.ToWei() or web3.Convert.FromWei()

or as a static method Nethereum.Web3.Web3.Convert.ToWei() or Nethereum.Web3.Web3.Convert.FromWei()

please have a look at the code for the different overloads

https://github.com/Nethereum/Nethereum/blob/master/src/Nethereum.Util/UnitConversion.cs

7
  • Just tried your FromWei() method and it gives me same problem, returns 0. So I'll still need to use my workaround :( – knocte May 25 '16 at 7:35
  • Interesting that bigInteger does not do a proper division, ill test it later – Juan Blanco May 25 '16 at 12:27
  • Just realised converting from and to Wei does not handle the decimal places due to BigInteger. I have an implementation already done for decimals. github.com/Nethereum/Netherum.Maker/blob/master/Nethereum.Maker/… – Juan Blanco May 25 '16 at 12:36
  • this is in the TODO list.. to add to the convertor – Juan Blanco May 25 '16 at 12:37
  • 1
    Thanks @ThorkilVærge edited to add more info. – Juan Blanco Jan 27 '20 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.