Skip to content

Commit

Permalink
feat: BridgeMessage e2e test (#184)
Browse files Browse the repository at this point in the history
* feat: e2e

* fix: refactor

* fix: comments

* fix: comments
  • Loading branch information
goran-ethernal authored Nov 21, 2024
1 parent df57e65 commit d2cf7cc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
49 changes: 36 additions & 13 deletions test/bats/fep/bridge-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,62 @@ setup() {
readonly weth_token_addr=$(cast call --rpc-url $l2_rpc_url $bridge_addr 'WETHToken()' | cast parse-bytes32-address)
}

@test "Native gas token deposit to WETH" {
# Helper function to run native gas token deposit to WETH
native_gas_token_deposit_to_WETH() {
local bridge_type="$1"

echo "Bridge_type: $bridge_type" >&3

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

echo "=== Running LxLy deposit on L1 to network: $l2_rpc_network_id native_token: $native_token_addr" >&3
echo "=== Running LxLy deposit $bridge_type on L1 to network: $l2_rpc_network_id native_token: $native_token_addr" >&3

destination_net=$l2_rpc_network_id
run bridgeAsset "$native_token_addr" "$l1_rpc_url"

if [[ $bridge_type == "bridgeMessage" ]]; then
run bridge_message "$native_token_addr" "$l1_rpc_url"
else
run bridge_asset "$native_token_addr" "$l1_rpc_url"
fi
assert_success

echo "=== Running LxLy claim on L2" >&3
echo "=== Claiming on L2..." >&3
timeout="120"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url" "$bridge_type"
assert_success

run verify_balance "$l2_rpc_url" "$weth_token_addr" "$destination_addr" "$initial_receiver_balance" "$ether_value"
assert_success

echo "=== bridgeAsset L2 WETH: $weth_token_addr to L1 ETH" >&3
echo "=== $bridge_type L2 WETH: $weth_token_addr to L1 ETH" >&3
destination_addr=$sender_addr
destination_net=0
run bridgeAsset "$weth_token_addr" "$l2_rpc_url"

if [[ $bridge_type == "bridgeMessage" ]]; then
run bridge_message "$weth_token_addr" "$l2_rpc_url"
else
run bridge_asset "$weth_token_addr" "$l2_rpc_url"
fi
assert_success

echo "=== Claim in L1 ETH" >&3
echo "=== Claiming on L1..." >&3
timeout="400"
claim_frequency="60"
run wait_for_claim "$timeout" "$claim_frequency" "$l1_rpc_url"
run wait_for_claim "$timeout" "$claim_frequency" "$l1_rpc_url" "$bridge_type"
assert_success
}

@test "Native gas token deposit to WETH - BridgeAsset" {
run native_gas_token_deposit_to_WETH "bridgeAsset"
}

@test "Native gas token deposit to WETH - BridgeMessage" {
run native_gas_token_deposit_to_WETH "bridgeMessage"
}

@test "Custom gas token deposit" {
echo "Gas token addr $gas_token_addr, L1 RPC: $l1_rpc_url" >&3

Expand Down Expand Up @@ -127,13 +150,13 @@ setup() {
destination_addr=$receiver
destination_net=$l2_rpc_network_id
amount=$wei_amount
run bridgeAsset "$gas_token_addr" "$l1_rpc_url"
run bridge_asset "$gas_token_addr" "$l1_rpc_url"
assert_success

# Claim deposits (settle them on the L2)
timeout="120"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url" "bridgeAsset"
assert_success

# Validate that the native token of receiver on L2 has increased by the bridge tokens amount
Expand All @@ -150,14 +173,14 @@ setup() {
echo "Receiver balance of gas token on L1 $initial_receiver_balance" >&3

destination_net=$l1_rpc_network_id
run bridgeAsset "$native_token_addr" "$l2_rpc_url"
run bridge_asset "$native_token_addr" "$l2_rpc_url"
assert_success

# Claim withdrawals (settle them on the L1)
timeout="360"
claim_frequency="10"
destination_net=$l1_rpc_network_id
run wait_for_claim "$timeout" "$claim_frequency" "$l1_rpc_url"
run wait_for_claim "$timeout" "$claim_frequency" "$l1_rpc_url" "bridgeAsset"
assert_success

# Validate that the token of receiver on L1 has increased by the bridge tokens amount
Expand Down
48 changes: 42 additions & 6 deletions test/helpers/lxly-bridge-test.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
# Error code reference https://hackmd.io/WwahVBZERJKdfK3BbKxzQQ
function bridgeAsset() {
function bridge_message() {
local token_addr="$1"
local rpc_url="$2"
readonly bridge_sig='bridgeAsset(uint32,address,uint256,address,bool,bytes)'
local bridge_sig='bridgeMessage(uint32,address,bool,bytes)'

if [[ $token_addr == "0x0000000000000000000000000000000000000000" ]]; then
echo "The ETH balance for sender "$sender_addr":" >&3
Expand All @@ -15,7 +15,37 @@ function bridgeAsset() {
echo "$(cast --from-wei "$balance_wei")" >&3
fi

echo "Attempting to deposit $amount [wei] to $destination_addr, token $token_addr (sender=$sender_addr, network id=$destination_net, rpc url=$rpc_url)" >&3
echo "Attempting to deposit $amount [wei] using bridgeMessage to $destination_addr, token $token_addr (sender=$sender_addr, network id=$destination_net, rpc url=$rpc_url)" >&3

if [[ $dry_run == "true" ]]; then
cast calldata $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
else
if [[ $token_addr == "0x0000000000000000000000000000000000000000" ]]; then
echo "cast send --legacy --private-key $sender_private_key --value $amount --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $is_forced $meta_bytes" >&3
cast send --legacy --private-key $sender_private_key --value $amount --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $is_forced $meta_bytes
else
echo "cast send --legacy --private-key $sender_private_key --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $is_forced $meta_bytes"
cast send --legacy --private-key $sender_private_key --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $is_forced $meta_bytes
fi
fi
}

function bridge_asset() {
local token_addr="$1"
local rpc_url="$2"
local bridge_sig='bridgeAsset(uint32,address,uint256,address,bool,bytes)'

if [[ $token_addr == "0x0000000000000000000000000000000000000000" ]]; then
echo "The ETH balance for sender "$sender_addr":" >&3
cast balance -e --rpc-url $rpc_url $sender_addr >&3
else
echo "The "$token_addr" token balance for sender "$sender_addr":" >&3
echo "cast call --rpc-url $rpc_url $token_addr \"$balance_of_fn_sig\" $sender_addr" >&3
balance_wei=$(cast call --rpc-url "$rpc_url" "$token_addr" "$balance_of_fn_sig" "$sender_addr" | awk '{print $1}')
echo "$(cast --from-wei "$balance_wei")" >&3
fi

echo "Attempting to deposit $amount [wei] using bridgeAsset to $destination_addr, token $token_addr (sender=$sender_addr, network id=$destination_net, rpc url=$rpc_url)" >&3

if [[ $dry_run == "true" ]]; then
cast calldata $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
Expand All @@ -24,15 +54,20 @@ function bridgeAsset() {
echo "cast send --legacy --private-key $sender_private_key --value $amount --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes" >&3
cast send --legacy --private-key $sender_private_key --value $amount --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
else
echo "cast send --legacy --private-key $sender_private_key --rpc-url $rpc_url $bridge_addr \"$bridge_sig\" $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes"
echo "cast send --legacy --private-key $sender_private_key --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes"
cast send --legacy --private-key $sender_private_key --rpc-url $rpc_url $bridge_addr $bridge_sig $destination_net $destination_addr $amount $token_addr $is_forced $meta_bytes
fi
fi
}

function claim() {
local destination_rpc_url="$1"
readonly claim_sig="claimAsset(bytes32[32],bytes32[32],uint256,bytes32,bytes32,uint32,address,uint32,address,uint256,bytes)"
local bridge_type="$2"
local claim_sig="claimAsset(bytes32[32],bytes32[32],uint256,bytes32,bytes32,uint32,address,uint32,address,uint256,bytes)"
if [[ $bridge_type == "bridgeMessage" ]]; then
claim_sig="claimMessage(bytes32[32],bytes32[32],uint256,bytes32,bytes32,uint32,address,uint32,address,uint256,bytes)"
fi

readonly bridge_deposit_file=$(mktemp)
readonly claimable_deposit_file=$(mktemp)
echo "Getting full list of deposits" >&3
Expand Down Expand Up @@ -94,6 +129,7 @@ function wait_for_claim() {
local timeout="$1" # timeout (in seconds)
local claim_frequency="$2" # claim frequency (in seconds)
local destination_rpc_url="$3" # destination rpc url
local bridge_type="$4" # bridgeAsset or bridgeMessage
local start_time=$(date +%s)
local end_time=$((start_time + timeout))

Expand All @@ -104,7 +140,7 @@ function wait_for_claim() {
exit 1
fi

run claim $destination_rpc_url
run claim $destination_rpc_url $bridge_type
if [ $status -eq 0 ]; then
break
fi
Expand Down

0 comments on commit d2cf7cc

Please sign in to comment.