Created
February 2, 2017 21:54
-
-
Save Thorium/c81bd3e3b0834ca872b42310913d5a86 to your computer and use it in GitHub Desktop.
Get Bitcoin wallet account balance by public key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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