goBlockchainDataAnalysis/exploreBlocks.go
exploreBlocks.goRaw
1package main
2
3import (
4 "fmt"
5
6 "github.com/btcsuite/btcd/chaincfg/chainhash"
7 "github.com/btcsuite/btcrpcclient"
8)
9
10func explore(client *btcrpcclient.Client, blockHash string) {
11 for blockHash != "" {
12 //generate hash from string
13 bh, err := chainhash.NewHashFromStr(blockHash)
14 check(err)
15 fmt.Print("blockHash: ")
16 fmt.Println(bh)
17 block, err := client.GetBlockVerbose(bh)
18 check(err)
19 fmt.Print("height: ")
20 fmt.Println(block.Height)
21 fmt.Print("rawTx: ")
22 fmt.Println(block.RawTx)
23 fmt.Print("Tx: ")
24 fmt.Println(block.Tx)
25 fmt.Print("Time: ")
26 fmt.Println(block.Time)
27 fmt.Print("Confirmations: ")
28 fmt.Println(block.Confirmations)
29
30 fmt.Print("Fee: ")
31 th, err := chainhash.NewHashFromStr(block.Tx[0])
32 check(err)
33 tx, err := client.GetRawTransactionVerbose(th)
34 check(err)
35
36 var totalFee float64
37 for _, Vo := range tx.Vout {
38 totalFee = totalFee + Vo.Value
39 }
40 fmt.Print("totalFee: ")
41 fmt.Print(totalFee)
42 fmt.Println(" FAIR")
43
44 //for each Tx, get the Tx value
45 var totalAmount float64
46 for k, txHash := range block.Tx {
47 //first Tx is the Fee
48 //after first Tx is the Sent Amount
49 if k > 0 {
50 th, err := chainhash.NewHashFromStr(txHash)
51 check(err)
52 fmt.Print("tx hash: ")
53 fmt.Println(th)
54 tx, err := client.GetRawTransactionVerbose(th)
55 check(err)
56 for _, Vo := range tx.Vout {
57 totalAmount = totalAmount + Vo.Value
58 }
59 fmt.Print("totalAmount: ")
60 fmt.Print(totalAmount)
61 fmt.Println(" FAIR")
62 }
63 }
64 fmt.Println("-----")
65
66 //set the next block
67 blockHash = block.NextHash
68 }
69 fmt.Println("reached the end of blockchain")
70}