-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtx.go
104 lines (87 loc) · 3.1 KB
/
tx.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
package mocks
import (
"context"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/ethergo/backends"
"github.com/synapsecns/sanguine/ethergo/backends/simulated"
"math/big"
"testing"
)
// MockBlocksOnBackend mocks count blocks on a backend.
func MockBlocksOnBackend(ctx context.Context, t *testing.T, simulatedBackend backends.SimulatedTestBackend, count int) (blocks []*types.Block) {
t.Helper()
acct := simulatedBackend.GetFundedAccount(ctx, big.NewInt(params.Ether))
lastBlock, err := simulatedBackend.BlockByNumber(ctx, nil)
assert.Nil(t, err)
// there should be done tx per block on simulated backend but we want to make sure
defer func() {
assert.Equal(t, count, len(blocks))
}()
for len(blocks) < count {
MockTx(ctx, t, simulatedBackend, acct, types.LegacyTxType)
newBlock, err := simulatedBackend.BlockByNumber(ctx, nil)
assert.Nil(t, err)
if newBlock.Number().Uint64() > lastBlock.Number().Uint64() {
blocks = append(blocks, newBlock)
lastBlock = newBlock
}
}
return blocks
}
// GetMockTxes gets count txes from a new mock account. These are real txes ona simulated chain
// useful for testing db interactions/adding noise to listeners.
func GetMockTxes(ctx context.Context, t *testing.T, count int, txType uint8) (txes []*types.Transaction) {
t.Helper()
simulatedBackend := simulated.NewSimulatedBackend(ctx, t)
acct := simulatedBackend.GetFundedAccount(ctx, big.NewInt(params.Ether))
for i := 0; i < count; i++ {
txes = append(txes, MockTx(ctx, t, simulatedBackend, acct, txType))
}
return txes
}
// MockTx mocks a transaction on a simulated backend.
func MockTx(ctx context.Context, t *testing.T, simulatedBackend backends.SimulatedTestBackend, acct *keystore.Key, txType uint8) *types.Transaction {
t.Helper()
nonce, err := simulatedBackend.PendingNonceAt(ctx, acct.Address)
assert.Nil(t, err)
to := common.BigToAddress(big.NewInt(0))
value := big.NewInt(params.GWei)
var rawTx *types.Transaction
switch txType {
case types.LegacyTxType:
gasPrice, err := simulatedBackend.SuggestGasPrice(ctx)
assert.Nil(t, err)
rawTx = types.NewTx(&types.LegacyTx{
Nonce: nonce,
GasPrice: gasPrice,
Gas: 21000,
To: &to,
Value: value,
})
case types.DynamicFeeTxType:
gasTipCap, err := simulatedBackend.SuggestGasTipCap(ctx)
assert.Nil(t, err)
latestBlock, err := simulatedBackend.BlockByNumber(ctx, nil)
assert.Nil(t, err)
rawTx = types.NewTx(&types.DynamicFeeTx{
ChainID: simulatedBackend.GetBigChainID(),
Nonce: nonce,
GasTipCap: gasTipCap,
GasFeeCap: big.NewInt(0).Add(latestBlock.BaseFee(), gasTipCap),
Gas: 21000,
To: &to,
Value: value,
})
default:
t.Errorf("tx type %d unspoorted", txType)
}
signedTx, err := simulatedBackend.SignTx(rawTx, simulatedBackend.Signer(), acct.PrivateKey)
assert.Nil(t, err)
err = simulatedBackend.SendTransaction(ctx, signedTx)
assert.Nil(t, err)
return signedTx
}