-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbridge.bats
More file actions
276 lines (214 loc) · 14 KB
/
bridge.bats
File metadata and controls
276 lines (214 loc) · 14 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env bats
# bats file_tags=pos
setup() {
# Load libraries.
load "../../core/helpers/pos-setup.bash"
load "../../core/helpers/scripts/eventually.bash"
pos_setup
# Define state sync count commands.
HEIMDALL_STATE_SYNC_COUNT_CMD='curl "${L2_CL_API_URL}/clerk/event-records/count" | jq -r ".count"'
BOR_STATE_SYNC_COUNT_CMD='cast call --gas-limit 15000000 --rpc-url "${L2_RPC_URL}" "${L2_STATE_RECEIVER_ADDRESS}" "lastStateId()(uint)"'
# Define timeout and interval for eventually commands.
timeout_seconds=${TIMEOUT_SECONDS:-"180"}
interval_seconds=${INTERVAL_SECONDS:-"10"}
}
function wait_for_heimdall_state_sync() {
state_sync_count="$1"
if [[ -z "${state_sync_count}" ]]; then
echo "Error: state_sync_count is not set."
exit 1
fi
if [[ -z "${HEIMDALL_STATE_SYNC_COUNT_CMD}" ]]; then
echo "Error: HEIMDALL_STATE_SYNC_COUNT_CMD environment variable is not set."
exit 1
fi
echo "Monitoring state syncs on Heimdall..."
assert_command_eventually_greater_or_equal "${HEIMDALL_STATE_SYNC_COUNT_CMD}" $((state_sync_count + 1)) "${timeout_seconds}" "${interval_seconds}"
}
function wait_for_bor_state_sync() {
state_sync_count="$1"
if [[ -z "${state_sync_count}" ]]; then
echo "Error: state_sync_count is not set."
exit 1
fi
if [[ -z "${BOR_STATE_SYNC_COUNT_CMD}" ]]; then
echo "Error: BOR_STATE_SYNC_COUNT_CMD environment variable is not set."
exit 1
fi
echo "Monitoring state syncs on Bor..."
assert_command_eventually_greater_or_equal "${BOR_STATE_SYNC_COUNT_CMD}" $((state_sync_count + 1)) "${timeout_seconds}" "${interval_seconds}"
}
# bats test_tags=bridge,transaction-pol
@test "bridge MATIC/POL from L1 to L2 and confirm L2 MATIC/POL balance increased" {
address=$(cast wallet address --private-key "${PRIVATE_KEY}")
# Get the initial balances.
initial_l1_balance=$(cast call --rpc-url "${L1_RPC_URL}" --json "${L1_MATIC_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
initial_l2_balance=$(cast balance --rpc-url "${L2_RPC_URL}" "${address}")
echo "Initial balances:"
echo "- L1 balance: ${initial_l1_balance} MATIC"
echo "- L2 balance: ${initial_l2_balance} wei"
heimdall_state_sync_count=$(eval "${HEIMDALL_STATE_SYNC_COUNT_CMD}")
bor_state_sync_count=$(eval "${BOR_STATE_SYNC_COUNT_CMD}")
# Bridge some MATIC/POL tokens from L1 to L2 to trigger a state sync.
# 1 MATIC/POL token = 1000000000000000000 wei.
bridge_amount=$(cast to-unit 1ether wei)
echo "Approving the DepositManager contract to spend MATIC/POL tokens on our behalf..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_MATIC_TOKEN_ADDRESS}" "approve(address,uint)" "${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "${bridge_amount}"
echo "Depositing MATIC/POL tokens to trigger a state sync..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "depositERC20(address,uint)" "${L1_MATIC_TOKEN_ADDRESS}" "${bridge_amount}"
# Wait for Heimdall and Bor to process the bridge event.
wait_for_heimdall_state_sync "${heimdall_state_sync_count}"
wait_for_bor_state_sync "${bor_state_sync_count}"
# Monitor the balances on L1 and L2.
echo "Monitoring MATIC/POL balance on L1..."
assert_token_balance_eventually_lower_or_equal "${L1_MATIC_TOKEN_ADDRESS}" "${address}" $((initial_l1_balance - bridge_amount)) "${L1_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "Monitoring MATIC/POL balance on L2..."
assert_ether_balance_eventually_greater_or_equal "${address}" $((initial_l2_balance + bridge_amount)) "${L2_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
}
# bats test_tags=bridge,transaction-pol
# @test "bridge MATIC/POL from L2 to L1 and confirm L1 MATIC/POL balance increased" {
# echo TODO
# }
# bats test_tags=bridge,transaction-erc20
@test "bridge some ERC20 tokens from L1 to L2 and confirm L2 ERC20 balance increased" {
address=$(cast wallet address --private-key "${PRIVATE_KEY}")
# Get the initial balances.
initial_l1_balance=$(cast call --rpc-url "${L1_RPC_URL}" --json "${L1_ERC20_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
initial_l2_balance=$(cast call --rpc-url "${L2_RPC_URL}" --json "${L2_ERC20_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
echo "Initial ERC20 balances:"
echo "- L1: ${initial_l1_balance}"
echo "- L2: ${initial_l2_balance}"
heimdall_state_sync_count=$(eval "${HEIMDALL_STATE_SYNC_COUNT_CMD}")
bor_state_sync_count=$(eval "${BOR_STATE_SYNC_COUNT_CMD}")
# Bridge some ERC20 tokens from L1 to L2.
# 1 ERC20 token = 1000000000000000000 wei.
bridge_amount=$(cast to-unit 1ether wei)
echo "Approving the DepositManager contract to spend ERC20 tokens on our behalf..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_ERC20_TOKEN_ADDRESS}" "approve(address,uint)" "${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "${bridge_amount}"
echo "Depositing ERC20 tokens..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "depositERC20(address,uint)" "${L1_ERC20_TOKEN_ADDRESS}" "${bridge_amount}"
# Wait for Heimdall and Bor to process the bridge event.
wait_for_heimdall_state_sync "${heimdall_state_sync_count}"
wait_for_bor_state_sync "${bor_state_sync_count}"
# Monitor the balances on L1 and L2.
echo "Monitoring ERC20 balance on L1..."
assert_token_balance_eventually_lower_or_equal "${L1_ERC20_TOKEN_ADDRESS}" "${address}" $((initial_l1_balance - bridge_amount)) "${L1_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "Monitoring ERC20 balance on L2..."
assert_token_balance_eventually_greater_or_equal "${L2_ERC20_TOKEN_ADDRESS}" "${address}" $((initial_l2_balance + bridge_amount)) "${L2_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
}
# bats test_tags=bridge,transaction-erc20
# @test "bridge some ERC20 tokens from L2 to L1 and confirm L1 ERC20 balance increased" {
# echo TODO
# }
# bats test_tags=bridge,transaction-erc721
@test "bridge an ERC721 token from L1 to L2 and confirm L2 ERC721 balance increased" {
address=$(cast wallet address --private-key "${PRIVATE_KEY}")
# Mint an ERC721 token.
total_supply=$(cast call --rpc-url "${L1_RPC_URL}" --json "${L1_ERC721_TOKEN_ADDRESS}" "totalSupply()(uint)" | jq --raw-output '.[0]')
token_id=$((total_supply + 1))
echo "Minting the ERC721 token (id: ${token_id})..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_ERC721_TOKEN_ADDRESS}" "mint(uint)" "${token_id}"
# Get the initial values.
initial_l1_balance=$(cast call --rpc-url "${L1_RPC_URL}" --json "${L1_ERC721_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
initial_l2_balance=$(cast call --rpc-url "${L2_RPC_URL}" --json "${L2_ERC721_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
echo "Initial ERC721 balances:"
echo "- L1: ${initial_l1_balance}"
echo "- L2: ${initial_l2_balance}"
heimdall_state_sync_count=$(eval "${HEIMDALL_STATE_SYNC_COUNT_CMD}")
bor_state_sync_count=$(eval "${BOR_STATE_SYNC_COUNT_CMD}")
# Bridge the ERC721 token from L1 to L2.
echo "Approving the DepositManager contract to spend ERC721 tokens on our behalf..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_ERC721_TOKEN_ADDRESS}" "approve(address,uint)" "${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "${token_id}"
echo "Depositing ERC721 tokens..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "depositERC721(address,uint)" "${L1_ERC721_TOKEN_ADDRESS}" "${token_id}"
# Wait for Heimdall and Bor to process the bridge event.
wait_for_heimdall_state_sync "${heimdall_state_sync_count}"
wait_for_bor_state_sync "${bor_state_sync_count}"
# Monitor the balances on L1 and L2.
echo "Monitoring ERC721 balance on L1..."
assert_token_balance_eventually_lower_or_equal "${L1_ERC721_TOKEN_ADDRESS}" "${address}" $((initial_l1_balance - 1)) "${L1_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "Monitoring ERC721 balance on L2..."
assert_token_balance_eventually_greater_or_equal "${L2_ERC721_TOKEN_ADDRESS}" "${address}" $((initial_l2_balance + 1)) "${L2_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
}
# bats test_tags=bridge,transaction-erc721
# @test "bridge an ERC721 token from L2 to L1 and confirm L1 ERC721 balance increased" {
# echo TODO
# }
# bats test_tags=bridge,transaction-pol,transaction-erc20,transaction-erc721
@test "bridge MATIC/POL, ERC20, and ERC721 from L1 to L2 and confirm L2 balances increased" {
address=$(cast wallet address --private-key "${PRIVATE_KEY}")
# Get the initial balances.
initial_l1_matic_balance=$(cast call --rpc-url "${L1_RPC_URL}" --json "${L1_MATIC_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
initial_l2_native_balance=$(cast balance --rpc-url "${L2_RPC_URL}" "${address}")
initial_l1_erc20_balance=$(cast call --rpc-url "${L1_RPC_URL}" --json "${L1_ERC20_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
initial_l2_erc20_balance=$(cast call --rpc-url "${L2_RPC_URL}" --json "${L2_ERC20_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
# Mint a new ERC721 token.
total_supply=$(cast call --rpc-url "${L1_RPC_URL}" --json "${L1_ERC721_TOKEN_ADDRESS}" "totalSupply()(uint)" | jq --raw-output '.[0]')
token_id=$((total_supply + 1))
echo "Minting ERC721 token (id: ${token_id})..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_ERC721_TOKEN_ADDRESS}" "mint(uint)" "${token_id}"
initial_l1_erc721_balance=$(cast call --rpc-url "${L1_RPC_URL}" --json "${L1_ERC721_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
initial_l2_erc721_balance=$(cast call --rpc-url "${L2_RPC_URL}" --json "${L2_ERC721_TOKEN_ADDRESS}" "balanceOf(address)(uint)" "${address}" | jq --raw-output '.[0]')
echo "Initial balances:"
echo "- L1 MATIC/POL: ${initial_l1_matic_balance}"
echo "- L2 MATIC/POL: ${initial_l2_native_balance} wei"
echo "- L1 ERC20: ${initial_l1_erc20_balance}"
echo "- L2 ERC20: ${initial_l2_erc20_balance}"
echo "- L1 ERC721: ${initial_l1_erc721_balance}"
echo "- L2 ERC721: ${initial_l2_erc721_balance}"
# Get the initial state sync count.
heimdall_state_sync_count=$(eval "${HEIMDALL_STATE_SYNC_COUNT_CMD}")
bor_state_sync_count=$(eval "${BOR_STATE_SYNC_COUNT_CMD}")
echo "Initial state sync counts:"
echo "- Heimdall: ${heimdall_state_sync_count}"
echo "- Bor: ${bor_state_sync_count}"
# Bridge amount.
bridge_amount=$(cast to-unit 1ether wei)
# Bridge MATIC/POL.
echo "Bridging MATIC/POL from L1 to L2..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_MATIC_TOKEN_ADDRESS}" "approve(address,uint)" "${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "${bridge_amount}"
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "depositERC20(address,uint)" "${L1_MATIC_TOKEN_ADDRESS}" "${bridge_amount}"
# Bridge ERC20.
echo "Bridging ERC20 from L1 to L2..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_ERC20_TOKEN_ADDRESS}" "approve(address,uint)" "${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "${bridge_amount}"
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "depositERC20(address,uint)" "${L1_ERC20_TOKEN_ADDRESS}" "${bridge_amount}"
# Bridge ERC721.
echo "Bridging ERC721 from L1 to L2..."
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_ERC721_TOKEN_ADDRESS}" "approve(address,uint)" "${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "${token_id}"
cast send --rpc-url "${L1_RPC_URL}" --private-key "${PRIVATE_KEY}" \
"${L1_DEPOSIT_MANAGER_PROXY_ADDRESS}" "depositERC721(address,uint)" "${L1_ERC721_TOKEN_ADDRESS}" "${token_id}"
# Wait for Heimdall and Bor to process the bridge events.
assert_command_eventually_greater_or_equal "${HEIMDALL_STATE_SYNC_COUNT_CMD}" $((heimdall_state_sync_count + 3)) "${timeout_seconds}" "${interval_seconds}"
echo "Waiting for Bor to process all bridge events..."
assert_command_eventually_greater_or_equal "${BOR_STATE_SYNC_COUNT_CMD}" $((bor_state_sync_count + 3)) "${timeout_seconds}" "${interval_seconds}"
echo "Verifying L1 MATIC/POL balance decreased..."
assert_token_balance_eventually_lower_or_equal "${L1_MATIC_TOKEN_ADDRESS}" "${address}" $((initial_l1_matic_balance - bridge_amount)) "${L1_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "Verifying L1 ERC20 balance decreased..."
assert_token_balance_eventually_lower_or_equal "${L1_ERC20_TOKEN_ADDRESS}" "${address}" $((initial_l1_erc20_balance - bridge_amount)) "${L1_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "Verifying L1 ERC721 balance decreased..."
assert_token_balance_eventually_lower_or_equal "${L1_ERC721_TOKEN_ADDRESS}" "${address}" $((initial_l1_erc721_balance - 1)) "${L1_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "Verifying L2 native balance increased..."
assert_ether_balance_eventually_greater_or_equal "${address}" $((initial_l2_native_balance + bridge_amount)) "${L2_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "Verifying L2 ERC20 balance increased..."
assert_token_balance_eventually_greater_or_equal "${L2_ERC20_TOKEN_ADDRESS}" "${address}" $((initial_l2_erc20_balance + bridge_amount)) "${L2_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "Verifying L2 ERC721 balance increased..."
assert_token_balance_eventually_greater_or_equal "${L2_ERC721_TOKEN_ADDRESS}" "${address}" $((initial_l2_erc721_balance + 1)) "${L2_RPC_URL}" "${timeout_seconds}" "${interval_seconds}"
echo "✅ MATIC/POL, ERC20, and ERC721 bridge operations completed successfully!"
echo "Summary:"
echo "- 1 MATIC/POL bridged from L1 to L2"
echo "- 1 ERC20 token bridged from L1 to L2"
echo "- 1 ERC721 token (id: ${token_id}) bridged from L1 to L2"
}