Skip to content

Instantly share code, notes, and snippets.

@Thorium
Created February 2, 2017 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thorium/c81bd3e3b0834ca872b42310913d5a86 to your computer and use it in GitHub Desktop.
Save Thorium/c81bd3e3b0834ca872b42310913d5a86 to your computer and use it in GitHub Desktop.
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