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

3

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#?

emcor

Posted 2016-06-06T22:18:58.863

Reputation: 131

That looks like an address to me, not a public key. Edited accordingly. Related unanswered question: C# parse bitcoin blockchain to get balance of an address

Murch 2016-06-07T13:26:54.313

Answers

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);

Andras

Posted 2016-06-06T22:18:58.863

Reputation: 121

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 2016-06-07T18:05:50.413

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 2016-06-07T20:58:37.887

@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 (https://api.blockchain.info/v2/apikey/request/ or https://blockchain.info/api/api_create_code). Sorry for the outdated information, my bad!

Andras 2016-06-08T08:52:21.587

1

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

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

dontmindme

Posted 2016-06-06T22:18:58.863

Reputation: 487

1Thank you. This package requires C++ and Unix, is there a solution in C# on windows?emcor 2016-06-07T07:33:33.610

@emcor There may be but I am not familiar with one, sorry. Perhaps someone else can make a better suggestion.dontmindme 2016-06-07T07:37:13.847

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;
 }

Iman Abidi

Posted 2016-06-06T22:18:58.863

Reputation: 101