-
Notifications
You must be signed in to change notification settings - Fork 1
/
trc20.go
105 lines (79 loc) · 2.54 KB
/
trc20.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
package tronWallet
import (
"fmt"
"github.com/Amirilidan78/tron-wallet/enums"
"github.com/Amirilidan78/tron-wallet/grpcClient"
"github.com/Amirilidan78/tron-wallet/util"
"math/big"
)
const (
trc20NameSignature = "0x06fdde03"
trc20SymbolSignature = "0x95d89b41"
trc20DecimalsSignature = "0x313ce567"
trc20BalanceOf = "0x70a08231"
trc20TransferMethodSignature = "0xa9059cbb"
trc20FeeLimit = 10000000
)
type Token struct {
ContractAddress enums.ContractAddress
}
func (t *Token) GetName(node enums.Node, addressBase58 string) (string, error) {
c, err := grpcClient.GetGrpcClient(node)
if err != nil {
return "", err
}
result, err := c.TRC20Call(addressBase58, t.ContractAddress.Base58(), trc20NameSignature, true, 0)
if err != nil {
return "", err
}
data := util.ToHex(result.GetConstantResult()[0])
return c.ParseTRC20StringProperty(data)
}
func (t *Token) GetSymbol(node enums.Node, addressBase58 string) (string, error) {
c, err := grpcClient.GetGrpcClient(node)
if err != nil {
return "", err
}
result, err := c.TRC20Call(addressBase58, t.ContractAddress.Base58(), trc20SymbolSignature, true, 0)
if err != nil {
return "", err
}
data := util.ToHex(result.GetConstantResult()[0])
return c.ParseTRC20StringProperty(data)
}
func (t *Token) GetDecimals(node enums.Node, addressBase58 string) (*big.Int, error) {
c, err := grpcClient.GetGrpcClient(node)
if err != nil {
return nil, err
}
result, err := c.TRC20Call(addressBase58, t.ContractAddress.Base58(), trc20DecimalsSignature, true, 0)
if err != nil {
return nil, err
}
data := util.ToHex(result.GetConstantResult()[0])
return c.ParseTRC20NumericProperty(data)
}
func (t *Token) GetBalance(node enums.Node, addressBase58 string) (*big.Int, error) {
address, err := util.Base58ToAddress(addressBase58)
if err != nil {
return nil, err
}
c, err := grpcClient.GetGrpcClient(node)
if err != nil {
return nil, err
}
req := trc20BalanceOf + "0000000000000000000000000000000000000000000000000000000000000000"[len(address.Hex())-2:] + address.Hex()[2:]
result, err := c.TRC20Call(addressBase58, t.ContractAddress.Base58(), req, true, 0)
if err != nil {
return nil, err
}
data := util.ToHex(result.GetConstantResult()[0])
r, err := c.ParseTRC20NumericProperty(data)
if err != nil {
return nil, fmt.Errorf("contract address %s: %v", t.ContractAddress.Base58(), err)
}
if r == nil {
return nil, fmt.Errorf("contract address %s: invalid balance of %s", t.ContractAddress.Base58(), addressBase58)
}
return r, nil
}