-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth.go
198 lines (169 loc) · 4.47 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
package eth
import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/jovijovi/web3.go/rpc"
"github.com/jovijovi/web3.go/types"
"github.com/jovijovi/web3.go/utils"
eTypes "github.com/ethereum/go-ethereum/core/types"
)
// Eth is the eth namespace
type Eth struct {
c *rpc.Client
privateKey *ecdsa.PrivateKey
address common.Address
chainId *big.Int
txPollTimeout int
}
func NewEth(c *rpc.Client) *Eth {
return &Eth{
c: c,
}
}
func (e *Eth) SetAccount(privateKey string) error {
if len(privateKey) == 0 {
return fmt.Errorf("private key is empty")
}
privKey, err := crypto.HexToECDSA(privateKey)
if err != nil {
return err
}
e.privateKey = privKey
addr := crypto.PubkeyToAddress(privKey.PublicKey)
// fmt.Println("addr ", addr)
copy(e.address[:], addr[:])
return nil
}
func (e *Eth) SetChainId(chainId int64) {
e.chainId = big.NewInt(chainId)
}
func (e *Eth) SetTxPollTimeout(timeout int) {
if timeout == 0 {
// default tx poll timeout is 720s
e.txPollTimeout = 720
return
}
e.txPollTimeout = timeout
}
func (e *Eth) Accounts() ([]common.Address, error) {
var out []common.Address
if err := e.c.Call("eth_accounts", &out); err != nil {
return nil, err
}
return out, nil
}
func (e *Eth) Address() common.Address {
return e.address
}
func (e *Eth) GetBlockNumber() (uint64, error) {
var out string
if err := e.c.Call("eth_blockNumber", &out); err != nil {
return 0, err
}
return utils.ParseUint64orHex(out)
}
func (e *Eth) GetBlockByNumber(i *big.Int, full bool) (*eTypes.Block, error) {
var b *eTypes.Block
if err := e.c.Call("eth_getBlockByNumber", &b, i.String(), full); err != nil {
return nil, err
}
return b, nil
}
func (e *Eth) GetBlockByHash(hash common.Hash, full bool) (*eTypes.Block, error) {
var b *eTypes.Block
if err := e.c.Call("eth_getBlockByHash", &b, hash, full); err != nil {
return nil, err
}
return b, nil
}
func (e *Eth) SendTransaction(txn *eTypes.Transaction) (common.Hash, error) {
var hash common.Hash
err := e.c.Call("eth_sendTransaction", &hash, txn)
return hash, err
}
func (e *Eth) GetTransactionByHash(hash common.Hash) (*eTypes.Transaction, error) {
var tx *eTypes.Transaction
err := e.c.Call("eth_getTransactionByHash", &tx, hash)
return tx, err
}
func (e *Eth) GetTransactionReceipt(hash common.Hash) (*eTypes.Receipt, error) {
var receipt *eTypes.Receipt
err := e.c.Call("eth_getTransactionReceipt", &receipt, hash)
return receipt, err
}
func (e *Eth) GetNonce(addr common.Address, blockNumber *big.Int) (uint64, error) {
var nonce string
if err := e.c.Call("eth_getTransactionCount", &nonce, addr, toBlockNumArg(blockNumber)); err != nil {
return 0, err
}
return utils.ParseUint64orHex(nonce)
}
func (e *Eth) GetBalance(addr common.Address, blockNumber *big.Int) (*big.Int, error) {
var out string
if err := e.c.Call("eth_getBalance", &out, addr, toBlockNumArg(blockNumber)); 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
}
func (e *Eth) GasPrice() (uint64, error) {
var out string
if err := e.c.Call("eth_gasPrice", &out); err != nil {
return 0, err
}
return utils.ParseUint64orHex(out)
}
func (e *Eth) Call(msg *types.CallMsg, block *big.Int) (string, error) {
var out string
if err := e.c.Call("eth_call", &out, msg, toBlockNumArg(block)); err != nil {
return "", err
}
return out, nil
}
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 utils.ParseUint64orHex(out)
}
func (e *Eth) EstimateGas(msg *types.CallMsg) (uint64, error) {
var out string
if err := e.c.Call("eth_estimateGas", &out, msg); err != nil {
return 0, err
}
return utils.ParseUint64orHex(out)
}
func (e *Eth) ChainID() (*big.Int, error) {
if e.chainId != nil {
return e.chainId, nil
}
var out string
if err := e.c.Call("eth_chainId", &out); err != nil {
return nil, err
}
return utils.ParseBigInt(out), nil
}
func (e *Eth) EncodeParams() {
}
func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
pending := big.NewInt(-1)
if number.Cmp(pending) == 0 {
return "pending"
}
return hexutil.EncodeBig(number)
}