diff --git a/test/bats/helpers/lxly-bridge.bash b/test/bats/helpers/lxly-bridge.bash index e44cfd7b..7acdc84a 100644 --- a/test/bats/helpers/lxly-bridge.bash +++ b/test/bats/helpers/lxly-bridge.bash @@ -280,16 +280,20 @@ function wait_for_expected_token() { local expected_origin_token="$1" local max_attempts="$2" local poll_frequency="$3" + local network_id="$4" + local aggkit_node_url=$(kurtosis port print $enclave cdk-node-001 rpc) local attempt=0 local token_mappings_result local origin_token_address + echo "Waiting for expected origin_token_address $aggkit_node_url @ $enclave..." >&3 + while true; do ((attempt++)) # Fetch token mappings from the RPC - token_mappings_result=$(cast rpc --rpc-url "$aggkit_node_url" "bridge_getTokenMappings" "$l2_rpc_network_id") + token_mappings_result=$(cast rpc --rpc-url "$aggkit_node_url" "bridge_getTokenMappings" "$network_id") # Extract the first origin_token_address (if available) origin_token_address=$(echo "$token_mappings_result" | jq -r '.tokenMappings[0].origin_token_address') diff --git a/test/bats/pp/bridge-e2e.bats b/test/bats/pp/bridge-e2e.bats index 5ff389b9..29dd8b65 100644 --- a/test/bats/pp/bridge-e2e.bats +++ b/test/bats/pp/bridge-e2e.bats @@ -49,12 +49,12 @@ setup() { readonly erc20_artifact_path=${ERC20_ARTIFACT_PATH:-"./contracts/erc20mock/ERC20Mock.json"} } -@test "Native gas token deposit to WETH" { - destination_addr=$sender_addr +@test "Native gas token bridge L1 -> L2" { run cast call --rpc-url $l2_rpc_url $bridge_addr 'WETHToken() (address)' assert_success readonly weth_token_addr=$output + destination_addr=$sender_addr local initial_receiver_balance=$(cast call --rpc-url "$l2_rpc_url" "$weth_token_addr" "$balance_of_fn_sig" "$destination_addr" | awk '{print $1}') echo "Initial receiver balance of native token on L2 $initial_receiver_balance" >&3 @@ -121,7 +121,7 @@ setup() { assert_success assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)" - # DEPOSIT + # Deposit token (on L1) destination_addr=$receiver destination_net=$l2_rpc_network_id amount=$wei_amount @@ -130,7 +130,7 @@ setup() { assert_success local bridge_tx_hash=$output - # Claim deposits (settle them on the L2) + # Claim deposit (settle it on the L2) timeout="180" claim_frequency="10" run claim_tx_hash "$timeout" "$bridge_tx_hash" "$destination_addr" "$l2_rpc_url" "$bridge_api_url" @@ -141,19 +141,20 @@ setup() { assert_success } -@test "Custom gas token withdrawal L2 -> L1" { - echo "Custom gas token withdrawal (gas token addr: $gas_token_addr, L1 RPC: $l1_rpc_url, L2 RPC: $l2_rpc_url)" >&3 +@test "Custom gas token deposit L2 -> L1" { + echo "Custom gas token deposit (gas token addr: $gas_token_addr, L1 RPC: $l1_rpc_url, L2 RPC: $l2_rpc_url)" >&3 local initial_receiver_balance=$(cast call --rpc-url "$l1_rpc_url" "$gas_token_addr" "$balance_of_fn_sig" "$destination_addr" | awk '{print $1}') assert_success echo "Receiver balance of gas token on L1 $initial_receiver_balance" >&3 + # Deposit token (on L2) destination_net=$l1_rpc_network_id run bridge_asset "$native_token_addr" "$l2_rpc_url" assert_success local bridge_tx_hash=$output - # Claim withdrawals (settle them on the L1) + # Claim L2 deposit (settle it on the L1) timeout="180" claim_frequency="10" destination_net=$l1_rpc_network_id @@ -170,7 +171,6 @@ setup() { @test "ERC20 token deposit L1 -> L2" { echo "Retrieving ERC20 contract artifact from $erc20_artifact_path" >&3 - # local erc20_bytecode=$(cat "$erc20_artifact_path" | jq -r '.bytecode') run jq -r '.bytecode' "$erc20_artifact_path" assert_success @@ -206,7 +206,7 @@ setup() { assert_success assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)" - # DEPOSIT ON L1 + # Deposit on L1 local receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"} destination_addr=$receiver destination_net=$l2_rpc_network_id @@ -216,24 +216,114 @@ setup() { assert_success local bridge_tx_hash=$output - # Claim deposits (settle them on the L2) + # Claim deposit (settle it on the L2) timeout="180" claim_frequency="10" run claim_tx_hash "$timeout" "$bridge_tx_hash" "$destination_addr" "$l2_rpc_url" "$bridge_api_url" assert_success - local aggkit_node_url=$(kurtosis port print $enclave cdk-node-001 rpc) - - run wait_for_expected_token "$l1_erc20_addr" 10 2 + run wait_for_expected_token "$l1_erc20_addr" 10 2 "$l1_rpc_network_id" assert_success local token_mappings_result=$output local origin_token_addr=$(echo "$token_mappings_result" | jq -r '.tokenMappings[0].origin_token_address') assert_equal "$l1_erc20_addr" "$origin_token_addr" - local l2_token_addr=$(echo "$token_mappings_result" | jq -r '.tokenMappings[0].wrapped_token_address') - echo "L2 token addr $l2_token_addr" >&3 + local l2_wrapped_token=$(echo "$token_mappings_result" | jq -r '.tokenMappings[0].wrapped_token_address') + echo "L2 wrapped token address $l2_wrapped_token" >&3 + + run verify_balance "$l2_rpc_url" "$l2_wrapped_token" "$receiver" 0 "$tokens_amount" + assert_success +} + +@test "ERC20 token deposit L2 -> L1" { + echo "Retrieving ERC20 contract artifact from $erc20_artifact_path" >&3 + + run jq -r '.bytecode' "$erc20_artifact_path" + assert_success + local erc20_bytecode="$output" + + echo "Deploying ERC20 contract on L2 ($l2_rpc_url)" >&3 + run cast send --rpc-url "$l2_rpc_url" --private-key "$sender_private_key" --legacy --create "$erc20_bytecode" + assert_success + local erc20_deploy_output=$output + + local l2_erc20_addr=$(echo "$erc20_deploy_output" | + grep 'contractAddress' | + awk '{print $2}' | + tr '[:upper:]' '[:lower:]') + echo "ERC20 contract address: $l2_erc20_addr" >&3 + + # Mint ERC20 tokens on L2 + local tokens_amount="0.1ether" + local wei_amount=$(cast --to-unit $tokens_amount wei) + run mint_erc20_tokens "$l2_rpc_url" "$l2_erc20_addr" "$sender_private_key" "$sender_addr" "$tokens_amount" + assert_success + + # Assert that balance of ERC20 token (on the L2) is correct + run query_contract "$l2_rpc_url" "$l2_erc20_addr" "$balance_of_fn_sig" "$sender_addr" + assert_success + local l2_erc20_token_sender_balance=$(echo "$output" | + tail -n 1 | + awk '{print $1}') + echo "Sender balance ($sender_addr) (ERC20 token L2): $l2_erc20_token_sender_balance [weis]" >&3 + + # Send approve transaction to the ERC20 token on L2 + run send_tx "$l2_rpc_url" "$sender_private_key" "$l2_erc20_addr" "$approve_fn_sig" "$bridge_addr" "$tokens_amount" + assert_success + assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)" + + # Deposit on L2 + destination_addr=$sender_addr + destination_net=$l1_rpc_network_id + amount=$(cast --to-unit $tokens_amount wei) + meta_bytes="0x" + run bridge_asset "$l2_erc20_addr" "$l2_rpc_url" + assert_success + local bridge_tx_hash=$output + + # Claim deposit (settle it on the L1) + timeout="180" + claim_frequency="10" + run claim_tx_hash "$timeout" "$bridge_tx_hash" "$destination_addr" "$l1_rpc_url" "$bridge_api_url" + assert_success + + run wait_for_expected_token "$l2_erc20_addr" 15 2 "$l1_rpc_network_id" + assert_success + local token_mappings_result=$output + + local origin_token_addr=$(echo "$token_mappings_result" | jq -r '.tokenMappings[0].origin_token_address') + assert_equal "$l2_erc20_addr" "$origin_token_addr" + + local l1_wrapped_token_addr=$(echo "$token_mappings_result" | jq -r '.tokenMappings[0].wrapped_token_address') + echo "L1 wrapped token address $l1_wrapped_token_addr" >&3 + + run verify_balance "$l1_rpc_url" "$l1_wrapped_token_addr" "$receiver" 0 "$tokens_amount" + assert_success + + # TODO: @Stefan-Ethernal + # Do another exit to see if this causes any issues (L1 -> L2) + + # Send approve transaction to the ERC20 token on L1 + run send_tx "$l1_rpc_url" "$sender_private_key" "$l1_wrapped_token_addr" "$approve_fn_sig" "$bridge_addr" "$tokens_amount" + assert_success + assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)" + + # Deposit the L1 wrapped token (bridge L1 -> L2) + destination_addr=$receiver + destination_net=$l2_rpc_network_id + amount=$(cast --to-unit $tokens_amount wei) + meta_bytes="0x" + run bridge_asset "$l1_wrapped_token_addr" "$l1_rpc_url" + assert_success + local bridge_tx_hash=$output + + # Claim deposit (settle it on the L2) + timeout="180" + claim_frequency="10" + run claim_tx_hash "$timeout" "$bridge_tx_hash" "$destination_addr" "$l2_rpc_url" "$bridge_api_url" + assert_success - run verify_balance "$l2_rpc_url" "$l2_token_addr" "$receiver" 0 "$tokens_amount" + run verify_balance "$l2_rpc_url" "$l2_erc20_addr" "$receiver" 0 "$tokens_amount" assert_success }