This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
/
main.go
79 lines (66 loc) · 2.06 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"github.com/threefoldfoundation/tfchain/pkg/config"
"github.com/threefoldtech/rivine/pkg/client"
"github.com/threefoldfoundation/rexplorer/pkg/database"
"github.com/threefoldfoundation/rexplorer/pkg/encoding"
"github.com/threefoldfoundation/rexplorer/pkg/types"
"github.com/gomodule/redigo/redis"
)
func main() {
flag.Parse()
encoder, err := encoding.NewEncoder(encodingType)
if err != nil {
panic(err)
}
args := flag.Args()
if len(args) != 1 {
panic("usage: " + os.Args[0] + " <unlockhash>")
}
var uh types.UnlockHash
err = uh.LoadString(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, "usage: "+os.Args[0]+" <unlockhash>")
panic(fmt.Sprintf("invalid uh %q: %v", args[0], err))
}
conn, err := redis.Dial("tcp", dbAddress, redis.DialDatabase(dbSlot))
if err != nil {
panic(err)
}
addressKey, addressField := database.GetAddressKeyAndField(uh)
var wallet types.Wallet
b, err := redis.Bytes(conn.Do("HGET", addressKey, addressField))
if err != nil {
if err != redis.ErrNil {
panic("failed to get wallet " + err.Error())
}
b = nil
}
if len(b) > 0 {
err = encoder.Unmarshal(b, &wallet)
if err != nil {
panic("failed to unmarshal wallet: " + err.Error())
}
}
cfg := config.GetBlockchainInfo()
cc := client.NewCurrencyConvertor(config.GetCurrencyUnits(), cfg.CoinUnit)
fmt.Println("unlocked: " + cc.ToCoinStringWithUnit(wallet.Balance.Unlocked.Total.Currency))
fmt.Println("locked: " + cc.ToCoinStringWithUnit(wallet.Balance.Locked.Total.Currency))
fmt.Println("--------------------")
fmt.Println("total: " + cc.ToCoinStringWithUnit(
wallet.Balance.Locked.Total.Add(wallet.Balance.Unlocked.Total).Currency))
}
var (
dbAddress string
dbSlot int
encodingType encoding.Type
)
func init() {
flag.StringVar(&dbAddress, "redis-addr", ":6379", "(tcp) address of the redis db")
flag.IntVar(&dbSlot, "redis-db", 0, "slot/index of the redis db")
flag.Var(&encodingType, "encoding",
"which encoding protocol to use, one of {json,msgp,protobuf} (default: "+encodingType.String()+")")
}