arnaucube
arnaucube/goBlockchainDataAnalysisPublic Self-hosted repository
goBlockchainDataAnalysis/exploreBlockchain.go
exploreBlockchain.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	var realBlocks int
12	for blockHash != "" {
13		//generate hash from string
14		bh, err := chainhash.NewHashFromStr(blockHash)
15		check(err)
16		block, err := client.GetBlockVerbose(bh)
17		check(err)
18
19		th, err := chainhash.NewHashFromStr(block.Tx[0])
20		check(err)
21		tx, err := client.GetRawTransactionVerbose(th)
22		check(err)
23
24		var totalFee float64
25		for _, Vo := range tx.Vout {
26			totalFee = totalFee + Vo.Value
27		}
28
29		//for each Tx, get the Tx value
30		var totalAmount float64
31		for k, txHash := range block.Tx {
32			//first Tx is the Fee
33			//after first Tx is the Sent Amount
34			if k > 0 {
35				th, err := chainhash.NewHashFromStr(txHash)
36				check(err)
37				tx, err := client.GetRawTransactionVerbose(th)
38				check(err)
39				for _, Vo := range tx.Vout {
40					totalAmount = totalAmount + Vo.Value
41				}
42			}
43		}
44		if totalAmount > 0 {
45			var newBlock BlockModel
46			newBlock.Hash = block.Hash
47			newBlock.Height = block.Height
48			newBlock.Confirmations = block.Confirmations
49			newBlock.Amount = totalAmount
50			newBlock.Fee = totalFee
51			saveBlock(blockCollection, newBlock)
52			fmt.Println(newBlock.Height)
53			fmt.Println(newBlock.Amount)
54			fmt.Println(newBlock.Fee)
55			fmt.Println("-----")
56			realBlocks++
57		}
58
59		//set the next block
60		blockHash = block.NextHash
61	}
62	fmt.Print("realBlocks (blocks with Fee and Amount values): ")
63	fmt.Println(realBlocks)
64	fmt.Println("reached the end of blockchain")
65}