forked from umbracle/ethgo
/
erc20.go
168 lines (133 loc) · 3.72 KB
/
erc20.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
package erc20
import (
"fmt"
"math/big"
web3 "github.com/Energy8Token/go-web3"
"github.com/Energy8Token/go-web3/contract"
"github.com/Energy8Token/go-web3/jsonrpc"
)
var (
_ = big.NewInt
)
// ERC20 is a solidity contract
type ERC20 struct {
c *contract.Contract
}
// NewERC20 creates a new instance of the contract at a specific address
func NewERC20(addr web3.Address, provider *jsonrpc.Client) *ERC20 {
return &ERC20{c: contract.NewContract(addr, abiERC20, provider)}
}
// Contract returns the contract object
func (a *ERC20) Contract() *contract.Contract {
return a.c
}
// calls
// Allowance calls the allowance method in the solidity contract
func (a *ERC20) Allowance(owner web3.Address, spender web3.Address, block ...web3.BlockNumber) (val0 *big.Int, err error) {
var out map[string]interface{}
var ok bool
out, err = a.c.Call("allowance", web3.EncodeBlock(block...), owner, spender)
if err != nil {
return
}
// decode outputs
val0, ok = out["0"].(*big.Int)
if !ok {
err = fmt.Errorf("failed to encode output at index 0")
return
}
return
}
// BalanceOf calls the balanceOf method in the solidity contract
func (a *ERC20) BalanceOf(owner web3.Address, block ...web3.BlockNumber) (val0 *big.Int, err error) {
var out map[string]interface{}
var ok bool
out, err = a.c.Call("balanceOf", web3.EncodeBlock(block...), owner)
if err != nil {
return
}
// decode outputs
val0, ok = out["balance"].(*big.Int)
if !ok {
err = fmt.Errorf("failed to encode output at index 0")
return
}
return
}
// Decimals calls the decimals method in the solidity contract
func (a *ERC20) Decimals(block ...web3.BlockNumber) (val0 uint8, err error) {
var out map[string]interface{}
var ok bool
out, err = a.c.Call("decimals", web3.EncodeBlock(block...))
if err != nil {
return
}
// decode outputs
val0, ok = out["0"].(uint8)
if !ok {
err = fmt.Errorf("failed to encode output at index 0")
return
}
return
}
// Name calls the name method in the solidity contract
func (a *ERC20) Name(block ...web3.BlockNumber) (val0 string, err error) {
var out map[string]interface{}
var ok bool
out, err = a.c.Call("name", web3.EncodeBlock(block...))
if err != nil {
return
}
// decode outputs
val0, ok = out["0"].(string)
if !ok {
err = fmt.Errorf("failed to encode output at index 0")
return
}
return
}
// Symbol calls the symbol method in the solidity contract
func (a *ERC20) Symbol(block ...web3.BlockNumber) (val0 string, err error) {
var out map[string]interface{}
var ok bool
out, err = a.c.Call("symbol", web3.EncodeBlock(block...))
if err != nil {
return
}
// decode outputs
val0, ok = out["0"].(string)
if !ok {
err = fmt.Errorf("failed to encode output at index 0")
return
}
return
}
// TotalSupply calls the totalSupply method in the solidity contract
func (a *ERC20) TotalSupply(block ...web3.BlockNumber) (val0 *big.Int, err error) {
var out map[string]interface{}
var ok bool
out, err = a.c.Call("totalSupply", web3.EncodeBlock(block...))
if err != nil {
return
}
// decode outputs
val0, ok = out["0"].(*big.Int)
if !ok {
err = fmt.Errorf("failed to encode output at index 0")
return
}
return
}
// txns
// Approve sends a approve transaction in the solidity contract
func (a *ERC20) Approve(spender web3.Address, value *big.Int) *contract.Txn {
return a.c.Txn("approve", spender, value)
}
// Transfer sends a transfer transaction in the solidity contract
func (a *ERC20) Transfer(to web3.Address, value *big.Int) *contract.Txn {
return a.c.Txn("transfer", to, value)
}
// TransferFrom sends a transferFrom transaction in the solidity contract
func (a *ERC20) TransferFrom(from web3.Address, to web3.Address, value *big.Int) *contract.Txn {
return a.c.Txn("transferFrom", from, to, value)
}