-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
162 lines (156 loc) · 3.48 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"tools/log"
"tools/util"
"fmt"
"go/token"
"go/types"
"math/big"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/urfave/cli/v2"
)
func main() {
app := cli.NewApp()
app.Name = filepath.Base(os.Args[0])
app.HideHelp = true
app.Copyright = "Copyright 2021-2023 Asuka, jeancky"
app.Usage = "very useful tool kits for TRON and ethereum"
app.CustomAppHelpTemplate = ""
app.Action = func(c *cli.Context) error {
if c.NArg() != 1 {
return cli.ShowAppHelp(c)
}
arg0 := c.Args().Get(0)
// input can be address
_ = hexAddrCommand.Action(c)
// input is in hex
if data, ok := utils.FromHex(arg0); ok {
// 0 ~ 31 bytes, pad it to 32 bytes
if len(data) < 32 {
_ = abiPadCommand.Action(c)
} else if len(data) > 32 {
// > 32 bytes, can be call data
_ = abiSplitCommand.Action(c)
}
// if 32bytes, output its value in decimal
if len(data) <= 32 {
_ = nowCommand.Action(c)
_ = hexIntCommand.Action(c)
}
// try to display its readable string
_ = hexStrCommand.Action(c)
} else {
// input is not hex
// check if input is in decimal
if num, ok := new(big.Int).SetString(arg0, 10); ok {
if num.Cmp(big.NewInt(256)) <= 0 {
_ = hexMaxCommand.Action(c)
}
// arg is decimal num
_ = nowCommand.Action(c)
_ = hexIntCommand.Action(c)
// try to pad it to 32bytes
_ = abiPadCommand.Action(c)
} else {
// it may be a string
// check if arg is date
if strings.ContainsAny(arg0, "-,:") {
_ = nowCommand.Action(c)
}
// check if arg is function or event
if matched, _ := regexp.MatchString(`^\w.*\(.*\)$`, arg0); matched {
_ = abi4bytesCommand.Action(c)
return nil
}
// try to eval arg
ee := regexp.MustCompile(`10*e\d+`)
for _, e := range ee.FindAllString(arg0, -1) {
bigfloat := new(big.Float)
bigfloat.SetString(e)
bigint := new(big.Int)
bigfloat.Int(bigint)
arg0 = strings.ReplaceAll(arg0, e, bigint.String())
}
if res, err := types.Eval(token.NewFileSet(), nil, token.NoPos, arg0); err == nil {
log.NewLog("eval result", res.Value.String())
return nil
}
// otherwise, convert str to hex
return hexStrCommand.Action(c)
}
}
return nil
}
app.Commands = []*cli.Command{
&callCommand,
&hashCommand,
&nowCommand,
{
Name: "abi",
Usage: "ABI related commands",
Subcommands: []*cli.Command{
&abiPadCommand,
&abiSplitCommand,
&abiUnpackCommand,
&abi4bytesCommand,
},
},
{
Name: "db",
Usage: "Database related commands",
Subcommands: []*cli.Command{
&dbCountCommand,
&dbGetCommand,
&dbRootCommand,
&dbPrintCommand,
&dbDiffCommand,
},
},
{
Name: "eth",
Usage: "ETH JSON-RPC related commands",
Subcommands: []*cli.Command{
&logsCommand,
},
},
{
Name: "hex",
Usage: "Hex related commands",
Subcommands: []*cli.Command{
&hexAddrCommand,
&hexIntCommand,
&hexMaxCommand,
&hexStrCommand,
},
},
{
Name: "scan",
Usage: "TronScan related commands",
Subcommands: []*cli.Command{
&txsCommand,
&txCommand,
},
},
{
Name: "tx",
Usage: "Transaction related commands",
Subcommands: []*cli.Command{
&recoverCommand,
},
},
}
app.After = func(c *cli.Context) error {
log.FlushLogsToConsole()
return nil
}
for _, cmd := range app.Commands {
cmd.HideHelpCommand = true
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}