-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathETH_RPC_Requester.go
More file actions
300 lines (275 loc) · 7.71 KB
/
ETH_RPC_Requester.go
File metadata and controls
300 lines (275 loc) · 7.71 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"errors"
"eth-relay/model"
"eth-relay/tool"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
)
type ETHRPCRequester struct {
nonceManager *NonceManager
client *ETHRPCClient
}
type ERC20BalanceRpcReq struct {
ContractAddress string // 合约的以太坊地址
UserAddress string // 用户的以太坊地址
ContractDecimal int // 合约所对应代币的数位
}
func NewETHRPCRequester(nodeUrl string) *ETHRPCRequester {
requester := ÐRPCRequester{}
requester.client = NewETHRPCClient(nodeUrl)
requester.nonceManager = NewNonceManager()
return requester
}
func NewETHWalletRequester() *ETHRPCRequester {
requester := ÐRPCRequester{}
return requester
}
func (r *ETHRPCRequester) GetTransactionByHash(txHash string) (model.Transaction, error) {
name := "eth_getTransactionByHash"
res := model.Transaction{}
err := r.client.GetRpc().Call(&res, name, txHash)
return res, err
}
func (r *ETHRPCRequester) GetTransactions(txHashArr []string) ([]*model.Transaction, error) {
name := "eth_getTransactionByHash"
var resArr []*model.Transaction
var reqs []rpc.BatchElem
for _, txHash := range txHashArr {
res := model.Transaction{}
req := rpc.BatchElem{
Method: name,
Args: []interface{}{txHash},
Result: &res,
}
reqs = append(reqs, req)
resArr = append(resArr, &res)
}
err := r.client.GetRpc().BatchCall(reqs)
return resArr, err
}
func (r *ETHRPCRequester) GetETHBalance(address string) (string, error) {
name := "eth_getBalance"
res := ""
err := r.client.GetRpc().Call(&res, name, address, "latest")
if err != nil {
return "", err
}
ten, _ := new(big.Int).SetString(res[2:], 16)
return ten.String(), nil
}
func (r *ETHRPCRequester) GetEthBalances(addressArr []string) ([]string, error) {
name := "eth_getBalance"
var resArr []*string
var reqs []rpc.BatchElem
for _, addr := range addressArr {
res := ""
req := rpc.BatchElem{
Method: name,
Args: []interface{}{addr, "latest"},
Result: &res,
}
reqs = append(reqs, req)
resArr = append(resArr, &res)
}
err := r.client.GetRpc().BatchCall(reqs)
if err != nil {
return nil, err
}
for _, req := range reqs {
if req.Error != nil {
return nil, req.Error
}
}
var finalRes []string
for _, item := range resArr {
ten, _ := new(big.Int).SetString((*item)[2:], 16)
finalRes = append(finalRes, ten.String())
}
return finalRes, err
}
func (r *ETHRPCRequester) GetERC20Balances(paramArr []ERC20BalanceRpcReq) ([]string, error) {
name := "eth_call"
methodId := "0x70a08231"
var resArr []*string
var reqs []rpc.BatchElem
for _, param := range paramArr {
res := ""
arg := &model.CallArg{}
userAddress := param.UserAddress
arg.Gas = hexutil.EncodeUint64(30000)
arg.To = common.HexToAddress(param.ContractAddress)
arg.Data = methodId + "000000000000000000000000" + userAddress[2:]
req := rpc.BatchElem{
Method: name,
Args: []interface{}{arg, "latest"},
Result: &res,
}
reqs = append(reqs, req)
resArr = append(resArr, &res)
}
err := r.client.GetRpc().BatchCall(reqs)
if err != nil {
return nil, err
}
for _, req := range reqs {
if req.Error != nil {
return nil, req.Error
}
}
var finalRes []string
for _, item := range resArr {
if *item == "" {
continue
}
ten, _ := new(big.Int).SetString((*item)[2:], 16)
finalRes = append(finalRes, ten.String())
}
return finalRes, err
}
func (r *ETHRPCRequester) GetLastestBlockNumber() (*big.Int, error) {
name := "eth_blockNumber"
number := ""
err := r.client.GetRpc().Call(&number, name)
if err != nil {
return nil, err
}
ten, _ := new(big.Int).SetString(number[2:], 16)
return ten, nil
}
func (r *ETHRPCRequester) GetBlockInfoByNumber(blockNumber *big.Int) (*model.FullBlock, error) {
number := fmt.Sprintf("%#x", blockNumber)
name := "eth_getBlockByNumber"
fullBlock := &model.FullBlock{}
err := r.client.GetRpc().Call(fullBlock, name, number, true)
if err != nil {
return nil, err
}
if fullBlock.Number == "" {
return nil, errors.New("block info is empty")
}
return fullBlock, nil
}
func (r *ETHRPCRequester) GetBlockInfoByHash(blockHash string) (*model.FullBlock, error) {
name := "eth_getBlockByHash"
fullBlock := &model.FullBlock{}
err := r.client.GetRpc().Call(fullBlock, name, blockHash, true)
if err != nil {
return nil, err
}
if fullBlock.Number == "" {
return nil, errors.New("block info is empty")
}
return fullBlock, nil
}
func (r *ETHRPCRequester) ETHCall(request interface{}, arg model.CallArg) error {
name := "eth_call"
err := r.client.GetRpc().Call(request, name, arg, "latest")
if err != nil {
return err
}
return nil
}
func (r *ETHRPCRequester) CreateETHWallet(password string) (string, error) {
if password == "" {
return "", errors.New("password is empty")
}
if len(password) < 6 {
return "", errors.New("password is too short")
}
keysDir := "./keystores"
ks := keystore.NewKeyStore(keysDir, keystore.StandardScryptN, keystore.StandardScryptP)
wallet, err := ks.NewAccount(password)
if err != nil {
return "0x", err
}
return wallet.Address.String(), nil
}
func (r *ETHRPCRequester) SendTransaction(address string, transaction *types.Transaction) (string, error) {
signTx, err := tool.SignETHTransaction(address, transaction)
if err != nil {
return "", err
}
txRlpData, err := rlp.EncodeToBytes(&signTx)
if err != nil {
return "", err
}
txHash := ""
name := "eth_sendRawTransaction"
err = r.client.GetRpc().Call(&txHash, name, hexutil.Encode(txRlpData))
if err != nil {
return "", err
}
oldNonce := r.nonceManager.nonceMemCache[address]
if oldNonce == nil {
r.nonceManager.SetNonce(address, new(big.Int).SetUint64(transaction.Nonce()))
}
r.nonceManager.PlusNonce(address)
return txHash, nil
}
func (r *ETHRPCRequester) GetNonce(address string) (uint64, error) {
name := "eth_getTransactionCount"
nonce := ""
err := r.client.GetRpc().Call(&nonce, name, address, "pending")
if err != nil {
return 0, err
}
n, _ := new(big.Int).SetString(nonce[2:], 16)
return n.Uint64(), nil
}
func (r *ETHRPCRequester) SendETHTransaction(fromStr, toStr, value string, gasLimit, gasPrice uint64) (string, error) {
_to := common.HexToAddress(toStr)
_gasPrice := new(big.Int).SetUint64(gasPrice)
_value := tool.GetRealDecimalValue(value, 18)
_amount, _ := new(big.Int).SetString(_value, 10)
nonce := r.nonceManager.GetNonce(fromStr)
if nonce == nil {
n, err := r.GetNonce(fromStr)
if err != nil {
return "", err
}
nonce = new(big.Int).SetUint64(n)
r.nonceManager.SetNonce(fromStr, nonce)
}
transaction := types.NewTx(&types.LegacyTx{
Nonce: nonce.Uint64(),
GasPrice: _gasPrice,
Gas: gasLimit,
To: &_to,
Value: _amount,
Data: []byte(""),
})
return r.SendTransaction(fromStr, transaction)
}
func (r *ETHRPCRequester) SendERC20Transaction(fromStr, contract, receiver, valueStr string,
gasLimit, gasPrice uint64, decimal int) (string, error) {
_to := common.HexToAddress(contract)
_gasPrice := new(big.Int).SetUint64(gasPrice)
_amount := new(big.Int).SetInt64(0)
nonce := r.nonceManager.GetNonce(fromStr)
if nonce == nil {
n, err := r.GetNonce(fromStr)
if err != nil {
return "", err
}
nonce = new(big.Int).SetUint64(n)
r.nonceManager.SetNonce(fromStr, nonce)
}
data := tool.BuildERC20TransferData(valueStr, receiver, decimal)
dataBytes := common.FromHex(data)
transaction := types.NewTx(&types.LegacyTx{
Nonce: nonce.Uint64(),
GasPrice: _gasPrice,
Gas: gasLimit,
To: &_to,
Value: _amount,
Data: dataBytes,
})
return r.SendTransaction(fromStr, transaction)
}