-
Notifications
You must be signed in to change notification settings - Fork 175
/
accounts.go
149 lines (124 loc) · 3.19 KB
/
accounts.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
package testutils
import (
"bytes"
"crypto/ecdsa"
"io"
"math/big"
"sync"
"testing"
gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
"github.com/onflow/atree"
"github.com/onflow/flow-go/fvm/evm/emulator"
"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/model/flow"
)
// address: 658bdf435d810c91414ec09147daa6db62406379
const EOATestAccount1KeyHex = "9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c"
type EOATestAccount struct {
address gethCommon.Address
key *ecdsa.PrivateKey
nonce uint64
signer gethTypes.Signer
lock sync.Mutex
}
func (a *EOATestAccount) Address() types.Address {
return types.Address(a.address)
}
func (a *EOATestAccount) PrepareSignAndEncodeTx(
t testing.TB,
to gethCommon.Address,
data []byte,
amount *big.Int,
gasLimit uint64,
gasPrice *big.Int,
) []byte {
tx := a.PrepareAndSignTx(t, to, data, amount, gasLimit, gasPrice)
var b bytes.Buffer
writer := io.Writer(&b)
err := tx.EncodeRLP(writer)
require.NoError(t, err)
return b.Bytes()
}
func (a *EOATestAccount) PrepareAndSignTx(
t testing.TB,
to gethCommon.Address,
data []byte,
amount *big.Int,
gasLimit uint64,
gasPrice *big.Int,
) *gethTypes.Transaction {
a.lock.Lock()
defer a.lock.Unlock()
tx := a.signTx(
t,
gethTypes.NewTransaction(
a.nonce,
to,
amount,
gasLimit,
gasPrice,
data,
),
)
a.nonce++
return tx
}
func (a *EOATestAccount) SignTx(
t testing.TB,
tx *gethTypes.Transaction,
) *gethTypes.Transaction {
a.lock.Lock()
defer a.lock.Unlock()
return a.signTx(t, tx)
}
func (a *EOATestAccount) signTx(
t testing.TB,
tx *gethTypes.Transaction,
) *gethTypes.Transaction {
tx, err := gethTypes.SignTx(tx, a.signer, a.key)
require.NoError(t, err)
return tx
}
func (a *EOATestAccount) SetNonce(nonce uint64) {
a.lock.Lock()
defer a.lock.Unlock()
a.nonce = nonce
}
func GetTestEOAAccount(t testing.TB, keyHex string) *EOATestAccount {
key, _ := gethCrypto.HexToECDSA(keyHex)
address := gethCrypto.PubkeyToAddress(key.PublicKey)
signer := emulator.GetDefaultSigner()
return &EOATestAccount{
address: address,
key: key,
signer: signer,
lock: sync.Mutex{},
}
}
func RunWithEOATestAccount(t testing.TB, led atree.Ledger, flowEVMRootAddress flow.Address, f func(*EOATestAccount)) {
account := FundAndGetEOATestAccount(t, led, flowEVMRootAddress)
f(account)
}
func FundAndGetEOATestAccount(t testing.TB, led atree.Ledger, flowEVMRootAddress flow.Address) *EOATestAccount {
account := GetTestEOAAccount(t, EOATestAccount1KeyHex)
// fund account
e := emulator.NewEmulator(led, flowEVMRootAddress)
blk, err := e.NewBlockView(types.NewDefaultBlockContext(2))
require.NoError(t, err)
_, err = blk.DirectCall(
types.NewDepositCall(
account.Address(),
new(big.Int).Mul(big.NewInt(1e18), big.NewInt(1000)),
),
)
require.NoError(t, err)
blk2, err := e.NewReadOnlyBlockView(types.NewDefaultBlockContext(2))
require.NoError(t, err)
bal, err := blk2.BalanceOf(account.Address())
require.NoError(t, err)
require.Greater(t, bal.Uint64(), uint64(0))
return account
}