4

How can I get the bitcoin balance of a public key in C#?

E.g. for this public key: 1FqLoEpbQpMxow5bqEPpFbPVnBEbFcsg3J

How can I get its balance in C#?

2

3 Answers 3

2

If you are ok with a third party service check out the Blockchain API.

They have a C# library and it's fairly easy to use.

If you need only the balance of an address you can use the code below as described on their BlockExplorer page:

 // calculate the balanace of an address by fetching a list of all its unspent outputs
 var outs = blockExplorer.GetUnspentOutputs("1EjmmDULiZT2GCbJSeXRbjbJVvAPYkSDBw");
 long totalUnspentValue = outs.Sum(x => x.Value);
3
  • I tried to implement your example, but it gives only various error messages: using System; using System.Collections.Generic; using System.Linq; using Info.Blockchain.API; using Info.Blockchain.API.BlockExplorer; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var blockExplorer = new BlockExplorer(); var outs = blockExplorer.GetUnspentOutputs("1EjmmDULiZT2GCbJSeXRbjbJVvAPYkSDBw"); long totalUnspentValue = outs.Sum(x => x.Value); } } }
    – emcor
    Jun 7, 2016 at 18:05
  • It appears this library is outdated and the new version requires a wallet account on their website. However, I am looking for an independent solution because it is faster.
    – emcor
    Jun 7, 2016 at 20:58
  • @emcor You're right! I've just tested their current version and it seems that they changed a lot and did not update their samples in the documentation. They have their async methods generated and the BlockExplorer must be accessed through the BlockchainApiHelper which needs some parameters we get after applying for an API key (api.blockchain.info/v2/apikey/request or blockchain.info/api/api_create_code). Sorry for the outdated information, my bad!
    – Andras
    Jun 8, 2016 at 8:52
1

Take a look at Blockparser: https://github.com/znort987/blockparser**

It will let you parse blockchain data to verify balances (among other things)

2
  • 1
    Thank you. This package requires C++ and Unix, is there a solution in C# on windows?
    – emcor
    Jun 7, 2016 at 7:33
  • @emcor There may be but I am not familiar with one, sorry. Perhaps someone else can make a better suggestion.
    – dontmindme
    Jun 7, 2016 at 7:37
0
using Info.Blockchain.API.BlockExplorer;
using Info.Blockchain.API.Models;

internal decimal CheckBalance(string[] base58Addresses)  {
    BlockExplorer be = new BlockExplorer();
    IEnumerable<UnspentOutput> outs = be.GetUnspentOutputsAsync(base58Addresses).Result;
    decimal totalUnspentValue = outs.Sum(x => x.Value.GetBtc());

    return totalUnspentValue;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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