You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

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