-
Notifications
You must be signed in to change notification settings - Fork 649
/
shared.go
125 lines (112 loc) · 3.19 KB
/
shared.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
//nolint:deadcode,unused,varcheck
package e2e
import (
"context"
"fmt"
"math/big"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
const (
invalidParamsErrorCode = -32602
toAddressHex = "0x4d5Cf5032B2a844602278b01199ED191A86c93ff"
gerFinalityBlocks = uint64(250)
)
var (
toAddress = common.HexToAddress(toAddressHex)
)
var networks = []struct {
Name string
URL string
WebSocketURL string
ChainID uint64
PrivateKey string
}{
{
Name: "Local L1",
URL: operations.DefaultL1NetworkURL,
WebSocketURL: operations.DefaultL1NetworkWebSocketURL,
ChainID: operations.DefaultL1ChainID,
PrivateKey: operations.DefaultSequencerPrivateKey,
},
{
Name: "Local L2",
URL: operations.DefaultL2NetworkURL,
WebSocketURL: operations.DefaultL2NetworkWebSocketURL,
ChainID: operations.DefaultL2ChainID,
PrivateKey: operations.DefaultSequencerPrivateKey,
},
}
func setup() {
var err error
ctx := context.Background()
err = operations.Teardown()
if err != nil {
panic(err)
}
opsCfg := operations.GetDefaultOperationsConfig()
opsMan, err := operations.NewManager(ctx, opsCfg)
if err != nil {
panic(err)
}
err = opsMan.Setup()
if err != nil {
panic(err)
}
}
func teardown() {
err := operations.Teardown()
if err != nil {
panic(err)
}
}
func createTX(client *ethclient.Client, auth *bind.TransactOpts, to common.Address, amount *big.Int) (*ethTypes.Transaction, error) {
nonce, err := client.NonceAt(context.Background(), auth.From, nil)
if err != nil {
return nil, err
}
gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{From: auth.From, To: &to, Value: amount})
if err != nil {
return nil, err
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}
log.Infof("\nTX details:\n\tNonce: %d\n\tGasLimit: %d\n\tGasPrice: %d", nonce, gasLimit, gasPrice)
if gasLimit != uint64(21000) { //nolint:gomnd
return nil, fmt.Errorf("gasLimit %d != 21000", gasLimit)
}
tx := ethTypes.NewTransaction(nonce, to, amount, gasLimit, gasPrice, nil)
signedTx, err := auth.Signer(auth.From, tx)
if err != nil {
return nil, err
}
log.Infof("Sending Tx %v Nonce %v", signedTx.Hash(), signedTx.Nonce())
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
return nil, err
}
return signedTx, nil
}
func logTx(tx *ethTypes.Transaction) {
sender, _ := state.GetSender(*tx)
log.Debugf("********************")
log.Debugf("Hash: %v", tx.Hash())
log.Debugf("From: %v", sender)
log.Debugf("Nonce: %v", tx.Nonce())
log.Debugf("ChainId: %v", tx.ChainId())
log.Debugf("To: %v", tx.To())
log.Debugf("Gas: %v", tx.Gas())
log.Debugf("GasPrice: %v", tx.GasPrice())
log.Debugf("Cost: %v", tx.Cost())
// b, _ := tx.MarshalBinary()
//log.Debugf("RLP: ", hex.EncodeToHex(b))
log.Debugf("********************")
}