Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Bitcoin wallet account balance by public key
// Using FSharp.Data
type WalletData =
FSharp.Data.JsonProvider<
"""{"unspent_outputs":[{"value":9000000000},{"value":10}]}""">
let getBalance publicKey =
let balance =
try
WalletData.Load(
"https://blockchain.info/unspent?active=" +
publicKey).UnspentOutputs
|> Array.sumBy(fun t -> t.Value)
with // Can response e.g. "No free outputs to spend"
| :? System.Net.WebException as ex ->
use stream = ex.Response.GetResponseStream()
use reader = new System.IO.StreamReader(stream)
let err = reader.ReadToEnd()
System.Console.WriteLine err
0L
//``balance in BTC``:
(System.Convert.ToDecimal balance) / 100000000m
//Fetch with public Bitcoin address:
//getBalance "19fMhMwxyX6zK9ajPijwzRX7wTcjyunXuz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.