Snippets

Tuomas Hietanen Get Bitcoin wallet account balance by public key

Created by Tuomas Hietanen
// 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"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.