-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenmeta.go
145 lines (121 loc) · 3.77 KB
/
tokenmeta.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
133
134
135
136
137
138
139
140
141
142
143
144
145
package tools
import (
"encoding/json"
"errors"
"fmt"
"os"
zsw "github.com/zhongshuwen/zswchain-go"
"github.com/spf13/viper"
"github.com/zhongshuwen/historyexp/tokenmeta/cache"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var tokenmetaCmd = &cobra.Command{Use: "tokenmeta", Short: "Tokenmeta helper functions"}
var toJsonCmd = &cobra.Command{
Use: "to_json",
Short: "Outputs gob file in json",
Args: cobra.ExactArgs(0),
RunE: toJSONE,
}
var findCmd = &cobra.Command{Use: "find", Short: "Find accounts and contracts"}
var findAccountCmd = &cobra.Command{
Use: "account {owner}",
Short: "Returns assets owned by given account",
Args: cobra.ExactArgs(1),
RunE: findAccountE,
}
var findContractCmd = &cobra.Command{
Use: "contract {contract}",
Short: "Returns contract's asset holders",
Args: cobra.ExactArgs(1),
RunE: findContractE,
}
var findAccountContractCmd = &cobra.Command{
Use: "account-contract {owner} {contract}",
Short: "Returns assets owned by a given account for a given contract",
Args: cobra.ExactArgs(2),
RunE: findAccountContractE,
}
func init() {
Cmd.AddCommand(tokenmetaCmd)
tokenmetaCmd.AddCommand(toJsonCmd)
tokenmetaCmd.AddCommand(findCmd)
findCmd.AddCommand(findAccountCmd)
findCmd.AddCommand(findContractCmd)
findCmd.AddCommand(findAccountContractCmd)
tokenmetaCmd.PersistentFlags().String("tokenmeta-cache", "/data/token-cache-v1.gob", "Path to tokenmeta cache GOB file")
}
func findAccountE(cmd *cobra.Command, args []string) (err error) {
account := zsw.AccountName(args[0])
tokenCache, err := loadgob()
if err != nil {
return fmt.Errorf("unable to load tokenmeta cache GOB file: %w", err)
}
assets := tokenCache.AccountBalances(account, cache.EOSIncludeStakedAccOpt)
fmt.Printf("%q's assets:\n", account)
for _, a := range assets {
fmt.Printf("contract %q -> %s\n", a.Asset.Contract, a.Asset.Asset.String())
}
return nil
}
func findContractE(cmd *cobra.Command, args []string) (err error) {
contract := zsw.AccountName(args[0])
tokenCache, err := loadgob()
if err != nil {
return fmt.Errorf("unable to load tokenmeta cache GOB file: %w", err)
}
assets := tokenCache.TokenBalances(contract, cache.EOSIncludeStakedTokOpt)
fmt.Printf("Assets managed by contract %q:\n", contract)
for _, a := range assets {
fmt.Printf("owner %q -> %s\n", a.Owner, a.Asset.Asset.String())
}
return nil
}
func findAccountContractE(cmd *cobra.Command, args []string) (err error) {
account := zsw.AccountName(args[0])
contract := zsw.AccountName(args[1])
tokenCache, err := loadgob()
if err != nil {
return fmt.Errorf("unable to load tokenmeta cache GOB file: %w", err)
}
assets := tokenCache.TokenBalances(contract, cache.EOSIncludeStakedTokOpt)
fmt.Printf("%q's assets in contract %q:\n", account, contract)
for _, a := range assets {
if a.Owner == account {
fmt.Printf("%s\n", a.Asset.Asset.String())
}
}
return nil
}
func loadgob() (*cache.DefaultCache, error) {
filename := viper.GetString("tokenmeta-cache")
zlog.Info("trying to load from token cache file",
zap.String("filename", filename),
)
fmt.Printf("Loading tokenmeta cache file %q (...this may take a minute or two)\n", filename)
return cache.LoadDefaultCacheFromFile(filename)
}
func toJSONE(cmd *cobra.Command, args []string) (err error) {
tokenCache, err := loadgob()
if err != nil {
return fmt.Errorf("unable to load tokenmeta cache GOB file: %w", err)
}
cnt, err := json.Marshal(tokenCache)
if err != nil {
zlog.Error("unable to JSON marshall token cache content", zap.Error(err))
return fmt.Errorf("unable to JSON marshall token cache content : %w", err)
}
fmt.Println(string(cnt))
return nil
}
func isNotExits(err error) bool {
for {
if os.IsNotExist(err) {
return true
}
err = errors.Unwrap(err)
if err == nil {
return false
}
}
}