From d296958ff3bdccf7e4f758d60ee519eea74c54b6 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Fri, 21 Jul 2017 16:20:48 +0200 Subject: [PATCH] gets blocks and tx values --- .gitignore | 18 +++++++++++++ errors.go | 9 +++++++ exploreBlocks.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ instructions.md | 32 ++++++++++++++++++++++ main.go | 43 +++++++++++++++++++++++++++++ readConfig.go | 24 +++++++++++++++++ 6 files changed, 196 insertions(+) create mode 100644 .gitignore create mode 100644 errors.go create mode 100644 exploreBlocks.go create mode 100644 instructions.md create mode 100644 main.go create mode 100644 readConfig.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..db726fa --- /dev/null +++ b/.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 diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..9e83a92 --- /dev/null +++ b/errors.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func check(err error) { + if err != nil { + fmt.Println(err) + } +} diff --git a/exploreBlocks.go b/exploreBlocks.go new file mode 100644 index 0000000..43c2944 --- /dev/null +++ b/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") +} diff --git a/instructions.md b/instructions.md new file mode 100644 index 0000000..4271b5f --- /dev/null +++ b/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" +} +``` \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..8eae80e --- /dev/null +++ b/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) +} diff --git a/readConfig.go b/readConfig.go new file mode 100644 index 0000000..938714b --- /dev/null +++ b/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) +}