-
Notifications
You must be signed in to change notification settings - Fork 6
/
wallet.go
179 lines (147 loc) · 3.41 KB
/
wallet.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
package goether
import (
"fmt"
"io/ioutil"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/everFinance/ethrpc"
)
type TxOpts struct {
Nonce *int
GasLimit *int
GasPrice *big.Int
GasTipCap *big.Int
GasFeeCap *big.Int
}
type Wallet struct {
Address common.Address
ChainID *big.Int
Signer *Signer
Client *ethrpc.EthRPC
}
func NewWallet(prvHex, rpc string) (*Wallet, error) {
signer, err := NewSigner(prvHex)
if err != nil {
return nil, err
}
client := ethrpc.New(rpc)
version, err := client.NetVersion()
if err != nil {
return nil, err
}
chainID, ok := new(big.Int).SetString(version, 10)
if !ok {
return nil, fmt.Errorf("wrong chainID: %s", version)
}
return &Wallet{
Address: signer.Address,
ChainID: chainID,
Signer: signer,
Client: client,
}, nil
}
func NewWalletFromPath(prvPath, rpc string) (*Wallet, error) {
b, err := ioutil.ReadFile(prvPath)
if err != nil {
return nil, err
}
return NewWallet(strings.TrimSpace(string(b)), rpc)
}
func (w *Wallet) SendTx(to common.Address, amount *big.Int, data []byte, opts *TxOpts) (txHash string, err error) {
opts, err = w.initTxOpts(to, amount, data, opts)
if err != nil {
return
}
if amount == nil {
amount = big.NewInt(0)
}
tx, err := w.Signer.SignTx(
*opts.Nonce, to, amount,
*opts.GasLimit, opts.GasTipCap, opts.GasFeeCap,
data, w.ChainID)
if err != nil {
return
}
raw, err := tx.MarshalBinary()
if err != nil {
return
}
return w.Client.EthSendRawTransaction(hexutil.Encode(raw))
}
func (w *Wallet) SendLegacyTx(to common.Address, amount *big.Int, data []byte, opts *TxOpts) (txHash string, err error) {
opts, err = w.initTxOpts(to, amount, data, opts)
if err != nil {
return
}
if amount == nil {
amount = big.NewInt(0)
}
tx, err := w.Signer.SignLegacyTx(
*opts.Nonce, to, amount,
*opts.GasLimit, opts.GasPrice,
data, w.ChainID)
if err != nil {
return
}
raw, err := tx.MarshalBinary()
if err != nil {
return
}
return w.Client.EthSendRawTransaction(hexutil.Encode(raw))
}
func (w *Wallet) initTxOpts(to common.Address, amount *big.Int, data []byte, opts *TxOpts) (*TxOpts, error) {
var (
nonce, gasLimit int
gasPrice big.Int
err error
)
if opts == nil {
opts = &TxOpts{}
}
if opts.Nonce == nil {
nonce, err = w.GetPendingNonce()
if err != nil {
return nil, err
}
opts.Nonce = &nonce
}
if opts.GasLimit == nil {
ethrpcTx := ethrpc.T{
From: w.Address.String(),
To: to.String(),
Value: amount,
Data: hexutil.Encode(data),
}
gasLimit, err = w.Client.EthEstimateGas(ethrpcTx)
if err != nil {
return nil, err
}
opts.GasLimit = &gasLimit
}
if opts.GasPrice == nil {
gasPrice, err = w.Client.EthGasPrice()
if err != nil {
return nil, err
}
opts.GasPrice = &gasPrice
}
if opts.GasTipCap == nil || opts.GasFeeCap == nil {
opts.GasTipCap = opts.GasPrice
opts.GasFeeCap = opts.GasPrice
}
return opts, nil
}
func (w *Wallet) GetAddress() string {
return w.Address.String()
}
func (w *Wallet) GetNonce() (nonce int, err error) {
return w.Client.EthGetTransactionCount(w.GetAddress(), "latest")
}
func (w *Wallet) GetPendingNonce() (nonce int, err error) {
return w.Client.EthGetTransactionCount(w.GetAddress(), "pending")
}
func (w *Wallet) GetBalance() (balance big.Int, err error) {
return w.Client.EthGetBalance(w.GetAddress(), "latest")
}