-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathreadme_test.go
136 lines (114 loc) · 5.25 KB
/
readme_test.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
package supersim
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"os/exec"
"strings"
"testing"
"time"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/wait"
"github.com/ethereum-optimism/supersim/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type L2Addresses struct {
L1StandardBridgeProxy string `json:"L1StandardBridgeProxy"`
}
//go:embed genesis/generated/901-l2-addresses.json
var l2AddressesJSON []byte
func runCmd(command string) (string, error) {
cmd := exec.Command("bash", "-c", command)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error executing command: %w\nOutput: %s", err, output)
}
// Trim any trailing whitespace (including newlines)
return strings.TrimSpace(string(output)), nil
}
func TestL1ToL2Deposit(t *testing.T) {
_ = createTestSuite(t, func(cfg *config.CLIConfig) *config.CLIConfig {
cfg.L1Port = 8545
cfg.L2StartingPort = 9545
return cfg
})
// Get the initial balance on L2
initialBalanceCmd := "cast balance 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url http://127.0.0.1:9545"
initialBalance, err := runCmd(initialBalanceCmd)
assert.NoError(t, err)
assert.Equal(t, "10000000000000000000000", initialBalance, "Initial balance check failed")
// Read the L1StandardBridgeProxy address from the JSON file
var addresses L2Addresses
err = json.Unmarshal(l2AddressesJSON, &addresses)
require.NoError(t, err, "Failed to parse addresses file")
// Initiate the bridge transaction on L1
bridgeCmd := fmt.Sprintf(`cast send %s "bridgeETH(uint32 _minGasLimit, bytes calldata _extraData)" 50000 0x --value 0.1ether --rpc-url http://127.0.0.1:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80`, addresses.L1StandardBridgeProxy)
_, err = runCmd(bridgeCmd)
assert.NoError(t, err, "Failed to bridge ETH")
// Wait for bridge transaction to be processed
require.NoError(t, wait.For(context.Background(), 500*time.Millisecond, func() (bool, error) {
finalBalanceCmd := "cast balance 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url http://127.0.0.1:9545"
finalBalance, err := runCmd(finalBalanceCmd)
if err != nil {
return false, err
}
return finalBalance == "10000100000000000000000", nil
}))
}
func TestL2ToL2Transfer(t *testing.T) {
_ = createTestSuite(t, func(cfg *config.CLIConfig) *config.CLIConfig {
cfg.L1Port = 8545
cfg.L2StartingPort = 9545
cfg.InteropAutoRelay = true
return cfg
})
// Mint tokens on chain 901
mintCmd := `cast send 0x420beeF000000000000000000000000000000001 "mint(address _to, uint256 _amount)" 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 1000 --rpc-url http://127.0.0.1:9545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80`
_, err := runCmd(mintCmd)
assert.NoError(t, err)
// Check initial balance on chain 902
initialBalanceCmd := `cast balance --erc20 0x420beeF000000000000000000000000000000001 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url http://127.0.0.1:9546`
initialBalance, err := runCmd(initialBalanceCmd)
assert.NoError(t, err)
assert.Equal(t, "0", initialBalance, "Initial balance check failed")
// Initiate the send transaction on chain 901
sendCmd := `cast send 0x4200000000000000000000000000000000000028 "sendERC20(address _token, address _to, uint256 _amount, uint256 _chainId)" 0x420beeF000000000000000000000000000000001 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 1000 902 --rpc-url http://127.0.0.1:9545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80`
_, err = runCmd(sendCmd)
assert.NoError(t, err)
// Check the final balance on chain 902
require.NoError(t, wait.For(context.Background(), 500*time.Millisecond, func() (bool, error) {
finalBalanceCmd := `cast balance --erc20 0x420beeF000000000000000000000000000000001 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url http://127.0.0.1:9546`
finalBalance, err := runCmd(finalBalanceCmd)
if err != nil {
return false, err
}
return finalBalance == "1000", nil
}))
}
func TestSuperchainETHTransfer(t *testing.T) {
_ = createTestSuite(t, func(cfg *config.CLIConfig) *config.CLIConfig {
cfg.L1Port = 8545
cfg.L2StartingPort = 9545
cfg.InteropAutoRelay = true
return cfg
})
// Check initial balance on chain 902
initialBalanceCmd := `cast balance 0xCE35738E4bC96bB0a194F71B3d184809F3727f56 --rpc-url http://127.0.0.1:9546`
initialBalance, err := runCmd(initialBalanceCmd)
assert.NoError(t, err)
assert.Equal(t, "0", initialBalance, "Initial balance check failed")
// Initiate the send transaction on chain 901
sendCmd := `cast send 0x4200000000000000000000000000000000000024 "sendETH(address _to, uint256 _chainId)" 0xCE35738E4bC96bB0a194F71B3d184809F3727f56 902 --value 10ether --rpc-url http://127.0.0.1:9545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80`
_, err = runCmd(sendCmd)
assert.NoError(t, err)
// Check the final balance on chain 902
require.NoError(t, wait.For(context.Background(), 500*time.Millisecond, func() (bool, error) {
finalBalanceCmd := `cast balance 0xCE35738E4bC96bB0a194F71B3d184809F3727f56 --rpc-url http://127.0.0.1:9546`
finalBalance, err := runCmd(finalBalanceCmd)
if err != nil {
return false, err
}
return finalBalance == "10000000000000000000", nil
}))
}