This repository was archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaccount.go
More file actions
124 lines (99 loc) · 2.57 KB
/
account.go
File metadata and controls
124 lines (99 loc) · 2.57 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
package ethertest
import (
"context"
"crypto/ecdsa"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
var ErrTransactionFailed = errors.New("Transaction Failed")
func NewAccount() *Account {
k, err := crypto.GenerateKey()
// will only happen if RNG fails big time
if err != nil {
panic(err)
}
return &Account{k}
}
func NewAccountFromPrivKey(pk *ecdsa.PrivateKey) *Account {
return &Account{pk}
}
type Account struct {
pk *ecdsa.PrivateKey
}
func (a *Account) Address() common.Address {
return crypto.PubkeyToAddress(a.pk.PublicKey)
}
func (a *Account) PrivKey() *ecdsa.PrivateKey {
return a.pk
}
func (a *Account) MustTransfer(be TestBackend, to common.Address, amount *big.Int) {
err := a.Transfer(be, to, amount)
if err != nil {
panic(err)
}
}
func (a *Account) SignTransaction(be TestBackend, tx *types.Transaction) (*types.Transaction, error) {
return types.SignTx(tx, types.HomesteadSigner{}, a.pk)
}
func (a *Account) Transfer(be TestBackend, to common.Address, amount *big.Int) error {
n, err := be.PendingNonceAt(context.Background(), a.Address())
if err != nil {
return err
}
gasPrice, err := be.SuggestGasPrice(context.Background())
if err != nil {
return err
}
tx := types.NewTransaction(n, to, amount, 41000, gasPrice, nil)
signed, err := types.SignTx(tx, types.HomesteadSigner{}, a.pk)
if err != nil {
return err
}
err = be.SendTransaction(context.Background(), signed)
if err != nil {
return err
}
be.Commit()
rcpt, err := be.TransactionReceipt(context.Background(), signed.Hash())
if err != nil {
return err
}
if rcpt.Status != types.ReceiptStatusSuccessful {
return ErrTransactionFailed
}
return nil
}
type TransactionOptionModifier func(*bind.TransactOpts)
func WithGasLimit(gasLimit uint64) func(*bind.TransactOpts) {
return func(to *bind.TransactOpts) {
to.GasLimit = gasLimit
}
}
func WithGasPrice(gasPrice *big.Int) func(*bind.TransactOpts) {
return func(to *bind.TransactOpts) {
to.GasPrice = gasPrice
}
}
func WithValue(value *big.Int) func(*bind.TransactOpts) {
return func(to *bind.TransactOpts) {
to.Value = value
}
}
func (a *Account) TransactOpts(modifiers ...TransactionOptionModifier) *bind.TransactOpts {
to := bind.NewKeyedTransactor(a.pk)
for _, m := range modifiers {
m(to)
}
return to
}
func (a *Account) Balance(be TestBackend) *big.Int {
b, err := be.BalanceAt(context.Background(), a.Address(), nil)
if err != nil {
panic(err)
}
return b
}