1 people like it.

Get Bitcoin wallet account balance by public key

Get Bitcoin wallet account balance by public key

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
// 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"
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
type JsonProvider

Full name: FSharp.Data.JsonProvider


<summary>Typed representation of a JSON document.</summary>
       <param name='Sample'>Location of a JSON sample file or a string containing a sample JSON document.</param>
       <param name='SampleIsList'>If true, sample should be a list of individual samples for the inference.</param>
       <param name='RootName'>The name to be used to the root type. Defaults to `Root`.</param>
       <param name='Culture'>The culture used for parsing numbers and dates. Defaults to the invariant culture.</param>
       <param name='Encoding'>The encoding used to read the sample. You can specify either the character set name or the codepage number. Defaults to UTF8 for files, and to ISO-8859-1 the for HTTP requests, unless `charset` is specified in the `Content-Type` response header.</param>
       <param name='ResolutionFolder'>A directory that is used when resolving relative file references (at design time and in hosted execution).</param>
       <param name='EmbeddedResource'>When specified, the type provider first attempts to load the sample from the specified resource
          (e.g. 'MyCompany.MyAssembly, resource_name.json'). This is useful when exposing types generated by the type provider.</param>
       <param name='InferTypesFromValues'>If true, turns on additional type inference from values.
          (e.g. type inference infers string values such as "123" as ints and values constrained to 0 and 1 as booleans.)</param>
val getBalance : publicKey:string -> decimal

Full name: Script.getBalance
val publicKey : string
val balance : int64
type WalletData = FSharp.Data.JsonProvider<...>

Full name: Script.WalletData
FSharp.Data.JsonProvider<...>.Load(uri: string) : FSharp.Data.JsonProvider<...>.Root


Loads JSON from the specified uri

FSharp.Data.JsonProvider<...>.Load(reader: System.IO.TextReader) : FSharp.Data.JsonProvider<...>.Root


Loads JSON from the specified reader

FSharp.Data.JsonProvider<...>.Load(stream: System.IO.Stream) : FSharp.Data.JsonProvider<...>.Root


Loads JSON from the specified stream
module Array

from Microsoft.FSharp.Collections
val sumBy : projection:('T -> 'U) -> array:'T [] -> 'U (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Array.sumBy
val t : FSharp.Data.JsonProvider<...>.UnspentOutput
property FSharp.Data.JsonProvider<...>.UnspentOutput.Value: int64
namespace System
namespace System.Net
Multiple items
type WebException =
  inherit InvalidOperationException
  new : unit -> WebException + 4 overloads
  member GetObjectData : serializationInfo:SerializationInfo * streamingContext:StreamingContext -> unit
  member Response : WebResponse
  member Status : WebExceptionStatus

Full name: System.Net.WebException

--------------------
System.Net.WebException() : unit
System.Net.WebException(message: string) : unit
System.Net.WebException(message: string, innerException: exn) : unit
System.Net.WebException(message: string, status: System.Net.WebExceptionStatus) : unit
System.Net.WebException(message: string, innerException: exn, status: System.Net.WebExceptionStatus, response: System.Net.WebResponse) : unit
val ex : System.Net.WebException
val stream : System.IO.Stream
property System.Net.WebException.Response: System.Net.WebResponse
System.Net.WebResponse.GetResponseStream() : System.IO.Stream
val reader : System.IO.StreamReader
namespace System.IO
Multiple items
type StreamReader =
  inherit TextReader
  new : stream:Stream -> StreamReader + 9 overloads
  member BaseStream : Stream
  member Close : unit -> unit
  member CurrentEncoding : Encoding
  member DiscardBufferedData : unit -> unit
  member EndOfStream : bool
  member Peek : unit -> int
  member Read : unit -> int + 1 overload
  member ReadLine : unit -> string
  member ReadToEnd : unit -> string
  ...

Full name: System.IO.StreamReader

--------------------
System.IO.StreamReader(stream: System.IO.Stream) : unit
System.IO.StreamReader(path: string) : unit
System.IO.StreamReader(stream: System.IO.Stream, detectEncodingFromByteOrderMarks: bool) : unit
System.IO.StreamReader(stream: System.IO.Stream, encoding: System.Text.Encoding) : unit
System.IO.StreamReader(path: string, detectEncodingFromByteOrderMarks: bool) : unit
System.IO.StreamReader(path: string, encoding: System.Text.Encoding) : unit
System.IO.StreamReader(stream: System.IO.Stream, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
System.IO.StreamReader(path: string, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
System.IO.StreamReader(stream: System.IO.Stream, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
System.IO.StreamReader(path: string, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
val err : string
System.IO.StreamReader.ReadToEnd() : string
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
System.Console.WriteLine() : unit
   (+0 other overloads)
System.Console.WriteLine(value: string) : unit
   (+0 other overloads)
System.Console.WriteLine(value: obj) : unit
   (+0 other overloads)
System.Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
System.Console.WriteLine(value: int64) : unit
   (+0 other overloads)
System.Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
System.Console.WriteLine(value: int) : unit
   (+0 other overloads)
System.Console.WriteLine(value: float32) : unit
   (+0 other overloads)
System.Console.WriteLine(value: float) : unit
   (+0 other overloads)
System.Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
System.Convert.ToDecimal(value: System.DateTime) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: bool) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: decimal) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: string) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: float) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: float32) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: uint64) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: int64) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: uint32) : decimal
   (+0 other overloads)
System.Convert.ToDecimal(value: int) : decimal
   (+0 other overloads)
Raw view New version

More information

Link:http://fssnip.net/7S1
Posted:1 months ago
Author:Tuomas Hietanen
Tags: bitcoin , blockchain , btc