BlocktrailSdk


Introduction

Blocktrail does not provide a .NET implementation of their SDK so I decided to build one on my own (feel free to contribute!). This .NET SDK is written in F# and tries to keep it as C# friendly as possible, keep this in mind when contributing as it is a vital requirement. The client implements some of the same methods as the official ones. The idea was to differ in some ways.

E.G.: implementing the request for unconfirmed transactions of an address as a method on the Address object instead of an API call from the client module.

We start the tutorial by setting up the API. The first thing to notice is that it's built in F# modules which are basically statis classes. This might change in the future, but at this moment it is not possible to use multiple API keys.

If you don't have a Blocktrail API key yet go get one at Blocktrail.com, it's free!

Basics

Copy the snippet below and replace the APIKey placeholder with your api key you requested.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
#r "BlocktrailSdk.dll"
open BlocktrailSdk
open BlocktrailSdk.Models

BlocktrailSdk.Config.ApiKey <- "INSERT_YOUR_API_KEY";

let transaction = BlocktrailSdk.Client.GetTransaction "c326105f7fbfa4e8fe971569ef8858f47ee7e4aa5e8e7c458be8002be3d86aad"

Relational properties and methods

This will retrieve the the transaction of that specific hash. After retrieving it you can print and use it's values as shown in the next snippet.

1: 
2: 
printfn "Hash: %s" transaction.Hash
printfn "Confirmations: %i" transaction.Confirmations

Or do more advanced stuff and retrieve the Block where this particular transaction resides in.

1: 
2: 
3: 
let block = transaction.Block
printfn "Block height: %i" block.Height
printfn "Block mined by: %s" block.MiningpoolName

Pagination

And why stop there... let's retrieve the next block and a page of it's transactions. Remember that responses that contains lists are often paginated. That means the reponse is sliced into the size you request (or the default size of 20). But don't worry, retrieving the next page is simple!

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
let nextBlock = BlocktrailSdk.Client.GetBlock block.NextBlock

let transactions = nextBlock.GetTransactions(1, 20, "asc")

// Print out the hashes
transactions |> Seq.iteri (fun i x -> printfn "Transaction[%i]: %s" i x.Hash)

let nextLoadOfTransactions = transactions.NextPage()

// Print out the hashes of the 2nd page of transactions
nextLoadOfTransactions |> Seq.iteri (fun i x -> printfn "Transaction[%i]: %s" i x.Hash)

This was the starting point of using the SDK, there is more to be found within this project. Go explore the blockchain on your own and be sure to file any issues or suggestions.

namespace BlocktrailSdk
namespace BlocktrailSdk.Models
module Config

from BlocktrailSdk
val mutable ApiKey : string

Full name: BlocktrailSdk.Config.ApiKey
val transaction : Transaction

Full name: Tutorial.transaction
module Client

from BlocktrailSdk
val GetTransaction : trans:string -> Transaction

Full name: BlocktrailSdk.Client.GetTransaction
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property Transaction.Hash: string
property Transaction.Confirmations: int
val block : Block

Full name: Tutorial.block
property Transaction.Block: Block
property Block.Height: int
property Block.MiningpoolName: string
val nextBlock : Block

Full name: Tutorial.nextBlock
val GetBlock : block:string -> Block

Full name: BlocktrailSdk.Client.GetBlock
property Block.NextBlock: string
val transactions : PagingResponse<RelatedTransaction>

Full name: Tutorial.transactions
member Block.GetTransactions : page:int * limit:int * sort_dir:string -> PagingResponse<RelatedTransaction>
module Seq

from Microsoft.FSharp.Collections
val iteri : action:(int -> 'T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iteri
val i : int
val x : RelatedTransaction
property RelatedTransaction.Hash: string
val nextLoadOfTransactions : PagingResponse<RelatedTransaction>

Full name: Tutorial.nextLoadOfTransactions
member PagingResponse.NextPage : unit -> PagingResponse<'T>
Fork me on GitHub