/
cli.go
132 lines (113 loc) · 3.38 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"log"
"strconv"
"time"
api "github.com/gophergala2016/be/insightapi"
)
var dateTimeFormat = "2006-01-02 15:04:05"
func cliLatestBlocks() {
var miner string
latestBlocks, err := api.GetLatestBlocks()
if err != nil {
log.Fatal(err)
}
for _, b := range latestBlocks.Blocks[0:20] {
blockDatetimeUnix, _ := strconv.Atoi(b.Time)
if err != nil {
log.Fatal(err)
}
blockDatetime := time.Unix(int64(blockDatetimeUnix), 0)
blockDatetimeHuman := blockDatetime.Format(dateTimeFormat)
if b.PoolInfo.PoolName != "" {
miner = "[" + b.PoolInfo.PoolName + "]"
} else {
miner = ""
}
size := b.Size / 1024
fmt.Printf("#%d (%s) %dtxs %dKb %s\n", b.Height, blockDatetimeHuman, b.Txlength, size, miner)
}
}
func cliBlock(block string) {
var b api.Block
var err error
// check if user provided height or hash
if len(block) == 64 {
b, err = api.GetBlockByHash(block)
if err != nil {
log.Fatal(err)
}
} else {
b, err = api.GetBlockByHeight(block)
if err != nil {
log.Fatal(err)
}
}
blockDatetime := time.Unix(b.Time, 0)
blockDatetimeHuman := blockDatetime.Format(dateTimeFormat)
size := b.Size / 1024
fmt.Printf("Block #%d\n-------------\n", b.Height)
fmt.Printf("Confirmations: %d\n", b.Confirmations)
fmt.Printf("Number Of Transactions: %d\n", len(b.Tx))
fmt.Printf("Height: %d\n", b.Height)
fmt.Printf("Block Reward: %f\n", b.Reward)
fmt.Printf("Timestamp: %s\n", blockDatetimeHuman)
if b.PoolInfo.PoolName != "" {
fmt.Printf("Mined by: %s\n", b.PoolInfo.PoolName)
}
fmt.Printf("Merkle Root: %s\n", b.Merkleroot)
fmt.Printf("Difficulty: %f\n", b.Difficulty)
fmt.Printf("Size (Kilobytes): %d\n", size)
fmt.Printf("Nonce: %d\n", b.Nonce)
fmt.Println("\nTransactions:\n-------------")
for _, tx := range b.Tx {
fmt.Println(tx)
}
}
func cliTransaction(transaction string) {
tx, err := api.GetTx(transaction)
if err != nil {
log.Fatal(err)
}
receivedDatetime := time.Unix(tx.Blocktime, 0)
receivedDatetimeHuman := receivedDatetime.Format(dateTimeFormat)
minedDatetime := time.Unix(tx.Time, 0)
minedDatetimeHuman := minedDatetime.Format(dateTimeFormat)
fmt.Printf("Transaction ID: %s\n", tx.Txid)
fmt.Printf("Received Time: %s\n", receivedDatetimeHuman)
fmt.Printf("Mined Time: %s\n", minedDatetimeHuman)
fmt.Printf("Value Out: %f\n", tx.ValueOut)
fmt.Printf("Value In: %f\n", tx.ValueIn)
fmt.Printf("Size (bytes): %d\n", tx.Size)
fmt.Printf("Fees: %f\n", tx.Fees)
fmt.Printf("Mined in block: %s\n", tx.Blockhash)
fmt.Printf("Confirmations: %d\n", tx.Confirmations)
fmt.Println("\nInputs:\n-------")
for _, i := range tx.Vin {
// print only if not coinbase transaction
if i.Coinbase == "" {
fmt.Println(i.Addr, formatBtc(i.Value))
}
}
fmt.Println("\nOutputs:\n--------")
for _, o := range tx.Vout {
fmt.Println(o.ScriptPubKey.Addresses[0], o.Value)
}
}
func cliAddress(address string) {
a, err := api.GetAddr(address)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Address: %s\n", a.AddrStr)
fmt.Printf("Total Received: %s\n", formatBtc(a.TotalReceived))
fmt.Printf("Total Sent: %s\n", formatBtc(a.TotalSent))
fmt.Printf("Final Balance: %s\n", formatBtc(a.Balance))
fmt.Printf("Unconfirmed Balance: %s\n", formatBtc(a.UnconfirmedBalance))
fmt.Printf("Total Transactions: %d\n", len(a.Transactions))
fmt.Println("\nTransactions:\n-------------")
for _, tx := range a.Transactions {
fmt.Println(tx)
}
}