forked from fbsobreira/gotron-sdk
/
trc20.go
191 lines (176 loc) · 5.57 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package client
import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"unicode/utf8"
"github.com/bizvip/gotron/pkg/address"
"github.com/bizvip/gotron/pkg/common"
"github.com/bizvip/gotron/pkg/proto/api"
"github.com/bizvip/gotron/pkg/proto/core"
)
const (
trc20TransferMethodSignature = "0xa9059cbb"
trc20ApproveMethodSignature = "0x095ea7b3"
trc20TransferEventSignature = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
trc20NameSignature = "0x06fdde03"
trc20SymbolSignature = "0x95d89b41"
trc20DecimalsSignature = "0x313ce567"
trc20BalanceOf = "0x70a08231"
)
// TRC20Call make cosntant calll
func (g *GrpcClient) TRC20Call(from, contractAddress, data string, constant bool, feeLimit int64) (*api.TransactionExtention, error) {
var err error
fromDesc := address.HexToAddress("410000000000000000000000000000000000000000")
if len(from) > 0 {
fromDesc, err = address.Base58ToAddress(from)
if err != nil {
return nil, err
}
}
contractDesc, err := address.Base58ToAddress(contractAddress)
if err != nil {
return nil, err
}
dataBytes, err := common.FromHex(data)
if err != nil {
return nil, err
}
ct := &core.TriggerSmartContract{
OwnerAddress: fromDesc.Bytes(),
ContractAddress: contractDesc.Bytes(),
Data: dataBytes,
}
var result *api.TransactionExtention
if constant {
result, err = g.triggerConstantContract(ct)
} else {
result, err = g.triggerContract(ct, feeLimit)
}
if err != nil {
return nil, err
}
if result.Result.Code > 0 {
return result, fmt.Errorf(string(result.Result.Message))
}
return result, nil
}
// TRC20GetName get token name
func (g *GrpcClient) TRC20GetName(contractAddress string) (string, error) {
result, err := g.TRC20Call("", contractAddress, trc20NameSignature, true, 0)
if err != nil {
return "", err
}
data := common.BytesToHexString(result.GetConstantResult()[0])
return g.ParseTRC20StringProperty(data)
}
// TRC20GetSymbol get contract symbol
func (g *GrpcClient) TRC20GetSymbol(contractAddress string) (string, error) {
result, err := g.TRC20Call("", contractAddress, trc20SymbolSignature, true, 0)
if err != nil {
return "", err
}
data := common.BytesToHexString(result.GetConstantResult()[0])
return g.ParseTRC20StringProperty(data)
}
// TRC20GetDecimals get contract decimals
func (g *GrpcClient) TRC20GetDecimals(contractAddress string) (*big.Int, error) {
result, err := g.TRC20Call("", contractAddress, trc20DecimalsSignature, true, 0)
if err != nil {
return nil, err
}
data := common.BytesToHexString(result.GetConstantResult()[0])
return g.ParseTRC20NumericProperty(data)
}
// ParseTRC20NumericProperty get number from data
func (g *GrpcClient) ParseTRC20NumericProperty(data string) (*big.Int, error) {
if common.Has0xPrefix(data) {
data = data[2:]
}
if len(data) == 64 {
var n big.Int
_, ok := n.SetString(data, 16)
if ok {
return &n, nil
}
}
if len(data) == 0 {
return big.NewInt(0), nil
}
return nil, fmt.Errorf("cannot parse %s", data)
}
// ParseTRC20StringProperty get string from data
func (g *GrpcClient) ParseTRC20StringProperty(data string) (string, error) {
if common.Has0xPrefix(data) {
data = data[2:]
}
if len(data) > 128 {
n, _ := g.ParseTRC20NumericProperty(data[64:128])
if n != nil {
l := n.Uint64()
if 2*int(l) <= len(data)-128 {
b, err := hex.DecodeString(data[128 : 128+2*l])
if err == nil {
return string(b), nil
}
}
}
} else if len(data) == 64 {
// allow string properties as 32 bytes of UTF-8 data
b, err := hex.DecodeString(data)
if err == nil {
i := bytes.Index(b, []byte{0})
if i > 0 {
b = b[:i]
}
if utf8.Valid(b) {
return string(b), nil
}
}
}
return "", fmt.Errorf("cannot parse %s,", data)
}
// TRC20ContractBalance get Address balance
func (g *GrpcClient) TRC20ContractBalance(addr, contractAddress string) (*big.Int, error) {
addrB, err := address.Base58ToAddress(addr)
if err != nil {
return nil, fmt.Errorf("invalid address %s: %v", addr, addr)
}
req := trc20BalanceOf + "0000000000000000000000000000000000000000000000000000000000000000"[len(addrB.Hex())-2:] + addrB.Hex()[2:]
result, err := g.TRC20Call("", contractAddress, req, true, 0)
if err != nil {
return nil, err
}
data := common.BytesToHexString(result.GetConstantResult()[0])
r, err := g.ParseTRC20NumericProperty(data)
if err != nil {
return nil, fmt.Errorf("contract address %s: %v", contractAddress, err)
}
if r == nil {
return nil, fmt.Errorf("contract address %s: invalid balance of %s", contractAddress, addr)
}
return r, nil
}
// TRC20Send send token to address
func (g *GrpcClient) TRC20Send(from, to, contract string, amount *big.Int, feeLimit int64) (*api.TransactionExtention, error) {
addrB, err := address.Base58ToAddress(to)
if err != nil {
return nil, err
}
ab := common.LeftPadBytes(amount.Bytes(), 32)
req := trc20TransferMethodSignature + "0000000000000000000000000000000000000000000000000000000000000000"[len(addrB.Hex())-4:] + addrB.Hex()[4:]
req += common.Bytes2Hex(ab)
return g.TRC20Call(from, contract, req, false, feeLimit)
}
// TRC20Approve approve token to address
func (g *GrpcClient) TRC20Approve(from, to, contract string, amount *big.Int, feeLimit int64) (*api.TransactionExtention, error) {
addrB, err := address.Base58ToAddress(to)
if err != nil {
return nil, err
}
ab := common.LeftPadBytes(amount.Bytes(), 32)
req := trc20ApproveMethodSignature + "0000000000000000000000000000000000000000000000000000000000000000"[len(addrB.Hex())-4:] + addrB.Hex()[4:]
req += common.Bytes2Hex(ab)
return g.TRC20Call(from, contract, req, false, feeLimit)
}