forked from umbracle/ethgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth.go
213 lines (188 loc) · 5.83 KB
/
eth.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
package jsonrpc
import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"github.com/umbracle/go-web3"
)
// Eth is the eth namespace
type Eth struct {
c *Client
}
// Eth returns the reference to the eth namespace
func (c *Client) Eth() *Eth {
return c.endpoints.e
}
// Accounts returns a list of addresses owned by client.
func (e *Eth) Accounts() ([]web3.Address, error) {
var out []web3.Address
if err := e.c.Call("eth_accounts", &out); err != nil {
return nil, err
}
return out, nil
}
// BlockNumber returns the number of most recent block.
func (e *Eth) BlockNumber() (uint64, error) {
var out string
if err := e.c.Call("eth_blockNumber", &out); err != nil {
return 0, err
}
return parseUint64orHex(out)
}
// GetBlockByNumber returns information about a block by block number.
func (e *Eth) GetBlockByNumber(i web3.BlockNumber, full bool) (*web3.Block, error) {
var b *web3.Block
if err := e.c.Call("eth_getBlockByNumber", &b, i.String(), full); err != nil {
return nil, err
}
return b, nil
}
// GetBlockByHash returns information about a block by hash.
func (e *Eth) GetBlockByHash(hash web3.Hash, full bool) (*web3.Block, error) {
var b *web3.Block
if err := e.c.Call("eth_getBlockByHash", &b, hash, full); err != nil {
return nil, err
}
return b, nil
}
// GetFilterChanges returns the filter changes for log filters
func (e *Eth) GetFilterChanges(id string) ([]*web3.Log, error) {
var raw string
err := e.c.Call("eth_getFilterChanges", &raw, id)
if err != nil {
return nil, err
}
var res []*web3.Log
if err := json.Unmarshal([]byte(raw), &res); err != nil {
return nil, err
}
return res, nil
}
// GetTransactionByHash returns a transaction by his hash
func (e *Eth) GetTransactionByHash(hash web3.Hash) (*web3.Transaction, error) {
var txn *web3.Transaction
err := e.c.Call("eth_getTransactionByHash", &txn, hash)
return txn, err
}
// GetFilterChangesBlock returns the filter changes for block filters
func (e *Eth) GetFilterChangesBlock(id string) ([]web3.Hash, error) {
var raw string
err := e.c.Call("eth_getFilterChanges", &raw, id)
if err != nil {
return nil, err
}
var res []web3.Hash
if err := json.Unmarshal([]byte(raw), &res); err != nil {
return nil, err
}
return res, nil
}
// NewFilter creates a new log filter
func (e *Eth) NewFilter(filter *web3.LogFilter) (string, error) {
var id string
err := e.c.Call("eth_newFilter", &id, filter)
return id, err
}
// NewBlockFilter creates a new block filter
func (e *Eth) NewBlockFilter() (string, error) {
var id string
err := e.c.Call("eth_newBlockFilter", &id, nil)
return id, err
}
// UninstallFilter uninstalls a filter
func (e *Eth) UninstallFilter(id string) (bool, error) {
var res bool
err := e.c.Call("eth_uninstallFilter", &res, id)
return res, err
}
// SendRawTransaction sends a signed transaction in rlp format.
func (e *Eth) SendRawTransaction(data []byte) (web3.Hash, error) {
var hash web3.Hash
hexData := "0x" + hex.EncodeToString(data)
err := e.c.Call("eth_sendRawTransaction", &hash, hexData)
return hash, err
}
// SendTransaction creates new message call transaction or a contract creation.
func (e *Eth) SendTransaction(txn *web3.Transaction) (web3.Hash, error) {
var hash web3.Hash
err := e.c.Call("eth_sendTransaction", &hash, txn)
return hash, err
}
// GetTransactionReceipt returns the receipt of a transaction by transaction hash.
func (e *Eth) GetTransactionReceipt(hash web3.Hash) (*web3.Receipt, error) {
var receipt *web3.Receipt
err := e.c.Call("eth_getTransactionReceipt", &receipt, hash)
return receipt, err
}
// GetNonce returns the nonce of the account
func (e *Eth) GetNonce(addr web3.Address, blockNumber web3.BlockNumber) (uint64, error) {
var nonce string
if err := e.c.Call("eth_getTransactionCount", &nonce, addr, blockNumber.String()); err != nil {
return 0, err
}
return parseUint64orHex(nonce)
}
// GetBalance returns the balance of the account of given address.
func (e *Eth) GetBalance(addr web3.Address, blockNumber web3.BlockNumber) (*big.Int, error) {
var out string
if err := e.c.Call("eth_getBalance", &out, addr, blockNumber.String()); err != nil {
return nil, err
}
b, ok := new(big.Int).SetString(out[2:], 16)
if !ok {
return nil, fmt.Errorf("failed to convert to big.int")
}
return b, nil
}
// GasPrice returns the current price per gas in wei.
func (e *Eth) GasPrice() (uint64, error) {
var out string
if err := e.c.Call("eth_gasPrice", &out); err != nil {
return 0, err
}
return parseUint64orHex(out)
}
// Call executes a new message call immediately without creating a transaction on the block chain.
func (e *Eth) Call(msg *web3.CallMsg, block web3.BlockNumber) (string, error) {
var out string
if err := e.c.Call("eth_call", &out, msg, block.String()); err != nil {
return "", err
}
return out, nil
}
// EstimateGasContract estimates the gas to deploy a contract
func (e *Eth) EstimateGasContract(bin []byte) (uint64, error) {
var out string
msg := map[string]interface{}{
"data": "0x" + hex.EncodeToString(bin),
}
if err := e.c.Call("eth_estimateGas", &out, msg); err != nil {
return 0, err
}
return parseUint64orHex(out)
}
// EstimateGas generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
func (e *Eth) EstimateGas(msg *web3.CallMsg) (uint64, error) {
var out string
if err := e.c.Call("eth_estimateGas", &out, msg); err != nil {
return 0, err
}
return parseUint64orHex(out)
}
// GetLogs returns an array of all logs matching a given filter object
func (e *Eth) GetLogs(filter *web3.LogFilter) ([]*web3.Log, error) {
var out []*web3.Log
if err := e.c.Call("eth_getLogs", &out, filter); err != nil {
return nil, err
}
return out, nil
}
// ChainID returns the id of the chain
func (e *Eth) ChainID() (*big.Int, error) {
var out string
if err := e.c.Call("eth_chainId", &out); err != nil {
return nil, err
}
return parseBigInt(out), nil
}