Browse Source

gets blocks and tx values

master
arnaucode 6 years ago
parent
commit
d296958ff3
6 changed files with 196 additions and 0 deletions
  1. +18
    -0
      .gitignore
  2. +9
    -0
      errors.go
  3. +70
    -0
      exploreBlocks.go
  4. +32
    -0
      instructions.md
  5. +43
    -0
      main.go
  6. +24
    -0
      readConfig.go

+ 18
- 0
.gitignore

@ -0,0 +1,18 @@
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
*.conf
*.json

+ 9
- 0
errors.go

@ -0,0 +1,9 @@
package main
import "fmt"
func check(err error) {
if err != nil {
fmt.Println(err)
}
}

+ 70
- 0
exploreBlocks.go

@ -0,0 +1,70 @@
package main
import (
"fmt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcrpcclient"
)
func explore(client *btcrpcclient.Client, blockHash string) {
for blockHash != "" {
//generate hash from string
bh, err := chainhash.NewHashFromStr(blockHash)
check(err)
fmt.Print("blockHash: ")
fmt.Println(bh)
block, err := client.GetBlockVerbose(bh)
check(err)
fmt.Print("height: ")
fmt.Println(block.Height)
fmt.Print("rawTx: ")
fmt.Println(block.RawTx)
fmt.Print("Tx: ")
fmt.Println(block.Tx)
fmt.Print("Time: ")
fmt.Println(block.Time)
fmt.Print("Confirmations: ")
fmt.Println(block.Confirmations)
fmt.Print("Fee: ")
th, err := chainhash.NewHashFromStr(block.Tx[0])
check(err)
tx, err := client.GetRawTransactionVerbose(th)
check(err)
var totalFee float64
for _, Vo := range tx.Vout {
totalFee = totalFee + Vo.Value
}
fmt.Print("totalFee: ")
fmt.Print(totalFee)
fmt.Println(" FAIR")
//for each Tx, get the Tx value
var totalAmount float64
for k, txHash := range block.Tx {
//first Tx is the Fee
//after first Tx is the Sent Amount
if k > 0 {
th, err := chainhash.NewHashFromStr(txHash)
check(err)
fmt.Print("tx hash: ")
fmt.Println(th)
tx, err := client.GetRawTransactionVerbose(th)
check(err)
for _, Vo := range tx.Vout {
totalAmount = totalAmount + Vo.Value
}
fmt.Print("totalAmount: ")
fmt.Print(totalAmount)
fmt.Println(" FAIR")
}
}
fmt.Println("-----")
//set the next block
blockHash = block.NextHash
}
fmt.Println("reached the end of blockchain")
}

+ 32
- 0
instructions.md

@ -0,0 +1,32 @@
# Instructions
#### Install FairCoin Wallet and configure
now, need to configure wallet:
.FairCoin/
FairCoin.conf
```
rpcuser=usernamerpc
rpcpassword=password
rpcport=3021
rpcworkqueue=2000
server=1
rpcbind=127.0.0.1
rpcallowip=127.0.0.1
```
execute wallet:
./FairCoind -txindex -reindex-chainstate
### Configure
```json
{
"user": "faircoinrpc",
"pass": "password",
"host": "127.0.0.1",
"port": "3021",
"genesisTx": "7c27ade2c28e67ed3077f8f77b8ea6d36d4f5eba04c099be3c9faa9a4a04c046",
"genesisBlock": "beed44fa5e96150d95d56ebd5d2625781825a9407a5215dd7eda723373a0a1d7"
}
```

+ 43
- 0
main.go

@ -0,0 +1,43 @@
package main
import (
"log"
"github.com/btcsuite/btcrpcclient"
)
func main() {
readConfig("config.json")
// create new client instance
client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
HTTPPostMode: true,
DisableTLS: true,
Host: config.Host + ":" + config.Port,
User: config.User,
Pass: config.Pass,
}, nil)
if err != nil {
log.Fatalf("error creating new btc client: %v", err)
}
// list accounts
accounts, err := client.ListAccounts()
if err != nil {
log.Fatalf("error listing accounts: %v", err)
}
// iterate over accounts (map[string]btcutil.Amount) and write to stdout
for label, amount := range accounts {
log.Printf("%s: %s", label, amount)
}
explore(client, config.GenesisBlock)
// Get the current block count.
blockCount, err := client.GetBlockCount()
if err != nil {
log.Fatal(err)
}
log.Printf("Block count: %d", blockCount)
}

+ 24
- 0
readConfig.go

@ -0,0 +1,24 @@
package main
import (
"encoding/json"
"io/ioutil"
)
type Config struct {
User string `json:"user"`
Pass string `json:"pass"`
Host string `json:"host"`
Port string `json:"port"`
GenesisTx string `json:"genesisTx"`
GenesisBlock string `json:"genesisBlock"`
}
var config Config
func readConfig(path string) {
file, err := ioutil.ReadFile(path)
check(err)
content := string(file)
json.Unmarshal([]byte(content), &config)
}

Loading…
Cancel
Save