-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.go
246 lines (210 loc) · 8.21 KB
/
contract.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package tokentype
import (
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ob-blocker/apitypes/utils"
)
const DEFAULT_ABI = "[{\"inputs\":[],\"name\":\"DOMAIN_SEPARATOR\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"DOMAIN_TYPEHASH\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"PERMIT_TYPEHASH\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"}],\"name\":\"_nonces\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"}],\"name\":\"nonces\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"deadline\",\"type\":\"uint256\"},{\"internalType\":\"uint8\",\"name\":\"v\",\"type\":\"uint8\"},{\"internalType\":\"bytes32\",\"name\":\"r\",\"type\":\"bytes32\"},{\"internalType\":\"bytes32\",\"name\":\"s\",\"type\":\"bytes32\"}],\"name\":\"permit\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"version\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]"
type Contract struct {
address common.Address
abi *abi.ABI
contract *bind.BoundContract
}
type ContractABIResp struct {
Status string `json:"status"`
Message string `json:"message"`
Result string `json:"result"`
}
func (client *Client) requestABI(url string, contractAddress string) (*abi.ABI, error) {
var resp ContractABIResp
httpUrl := fmt.Sprintf("%s?module=contract&action=getabi&address=%v&apikey=%v", url, contractAddress, client.apiKey)
if err := utils.HTTPGetJSON(httpUrl, &resp); err != nil {
return nil, fmt.Errorf("request abi: %v failed: %v", httpUrl, err.Error())
}
if resp.Status == "1" && len(resp.Result) != 0 {
contractAbi, abiErr := abi.JSON(strings.NewReader(resp.Result))
if abiErr != nil {
return nil, fmt.Errorf("get abi failed: %v", abiErr.Error())
}
return &contractAbi, nil
}
return nil, fmt.Errorf("request abi failed: %v", resp.Message)
}
func (client *Client) getDefaultContract(address common.Address) (*Contract, error) {
contractAbi, err := abi.JSON(strings.NewReader(DEFAULT_ABI))
if err != nil {
return nil, err
}
return &Contract{
address: address,
abi: &contractAbi,
contract: bind.NewBoundContract(address, contractAbi, client.ethClient, nil, nil),
}, nil
}
func (client *Client) getImplementationBySlot(address common.Address) (common.Address, error) {
var impl string
err := fmt.Errorf("can not get impl by slot")
for _, slot := range IMPLEMENTATION_SLOTS {
bytes, _ := client.ethClient.StorageAt(client.ctx, address, common.HexToHash(slot), nil)
impl = hexutil.Encode(bytes)
if impl != ZERO_ADDRESS {
err = nil
break
}
}
return common.HexToAddress(impl), err
}
func (client *Client) NewContract(address string) (*Contract, error) {
addr := common.HexToAddress(address)
url, ok := APIMap[client.chainId.String()]
if !ok {
return client.getDefaultContract(addr)
}
contractAbi, err := client.requestABI(url, address)
if err != nil {
client.logger.WithError(err).Error("requestGetABI")
return client.getDefaultContract(addr)
}
contract := &Contract{
address: addr,
abi: contractAbi,
contract: bind.NewBoundContract(addr, *contractAbi, client.ethClient, nil, nil),
}
// check is proxy
if !contract.HasMethod("implementation") {
return contract, nil
}
// get implement address
impl, err := contract.Implementation()
if err != nil {
var err1 error
impl, err1 = client.getImplementationBySlot(addr)
if err1 != nil {
client.logger.WithError(err).Error("get Implementation failed: %v", err1.Error())
return contract, err1
}
}
// fetch implement abi
implAbi, err := client.requestABI(url, impl.String())
if err != nil {
client.logger.WithError(err).Error("requestImplABI")
return contract, err
}
return &Contract{
address: addr,
abi: implAbi,
contract: bind.NewBoundContract(addr, *implAbi, client.ethClient, nil, nil),
}, nil
}
func (c *Contract) Address() string {
return c.address.String()
}
func (c *Contract) HasMethod(method string) bool {
_, found := c.abi.Methods[method]
return found
}
func (c *Contract) Implementation() (common.Address, error) {
var out []interface{}
err := c.contract.Call(nil, &out, "implementation")
if err != nil {
return *new(common.Address), err
}
return *abi.ConvertType(out[0], new(common.Address)).(*common.Address), nil
}
func (c *Contract) getTYPEHASH(method string) (string, error) {
if !c.HasMethod(method) {
return "", fmt.Errorf("contract has no method %v", method)
}
var out []interface{}
err := c.contract.Call(nil, &out, method)
if err != nil {
return "", err
}
out0 := *abi.ConvertType(out[0], new([32]byte)).(*[32]byte)
return hexutil.Encode(out0[:]), nil
}
func (c *Contract) DOMAIN_SEPARATOR() (string, error) {
for _, method := range []string{
"DOMAIN_SEPARATOR",
"getDomainSeperator",
} {
sep, err := c.getTYPEHASH(method)
if err == nil {
return sep, err
}
}
return "", fmt.Errorf("can not get DOMAIN_SEPARATOR")
}
func (c *Contract) DOMAIN_TYPEHASH() (string, error) {
return c.getTYPEHASH("DOMAIN_TYPEHASH")
}
func (c *Contract) PERMIT_TYPEHASH() (string, error) {
return c.getTYPEHASH("PERMIT_TYPEHASH")
}
func (c *Contract) getNonces(method string, sender string) (*big.Int, error) {
if !c.HasMethod(method) {
return nil, fmt.Errorf("contract has no method %v", method)
}
var out []interface{}
err := c.contract.Call(nil, &out, method, common.HexToAddress(sender))
if err != nil {
return *new(*big.Int), err
}
return *abi.ConvertType(out[0], new(*big.Int)).(**big.Int), nil
}
func (c *Contract) Nonces(sender string) (*big.Int, error) {
for _, method := range []string{
"nonces",
"getNonce", // polygon.dai
"_nonces", // eth.aaveToken
} {
n, err := c.getNonces(method, sender)
if err == nil {
return n, err
}
}
return nil, fmt.Errorf("can not get Nonces")
}
func (c *Contract) getString(method string) (string, error) {
if !c.HasMethod(method) {
return "", fmt.Errorf("contract has no method %v", method)
}
var out []interface{}
err := c.contract.Call(nil, &out, method)
if err != nil {
return *new(string), err
}
out0 := *abi.ConvertType(out[0], new(string)).(*string)
return out0, err
}
func (c *Contract) Name() (string, error) {
return c.getString("name")
}
func (c *Contract) Decimals() (uint8, error) {
var out []interface{}
err := c.contract.Call(nil, &out, "decimals")
if err != nil {
return *new(uint8), err
}
out0 := *abi.ConvertType(out[0], new(uint8)).(*uint8)
return out0, err
}
func (c *Contract) Version() (string, error) {
for _, method := range []string{
"version",
"EIP712_VERSION", // polygon.USDC
"ERC712_VERSION", // polygon.DAI
} {
r, err := c.getString(method)
if err == nil {
return r, err
}
}
// default is 1
return "1", nil
}