Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bridge l2 erc20 token #229

Draft
wants to merge 4 commits into
base: feat/new-bridge-service
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion test/bats/helpers/lxly-bridge.bash
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,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')
Expand Down
120 changes: 104 additions & 16 deletions test/bats/pp/bridge-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -121,15 +121,15 @@ 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
meta_bytes="0x"
run bridge_asset "$gas_token_addr" "$l1_rpc_url"
assert_success

# Claim deposits (settle them on the L2)
# Claim deposit (settle it on the L2)
timeout="180"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url" "bridgeAsset"
Expand All @@ -140,18 +140,19 @@ 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

# 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
Expand All @@ -168,7 +169,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
Expand Down Expand Up @@ -204,7 +204,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
Expand All @@ -213,24 +213,112 @@ setup() {
run bridge_asset "$l1_erc20_addr" "$l1_rpc_url"
assert_success

# Claim deposits (settle them on the L2)
# Claim deposit (settle it on the L2)
timeout="180"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url" "bridgeAsset"
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

# Claim deposit (settle it on the L1)
timeout="180"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency" "$l1_rpc_url" "bridgeAsset"
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

# Claim deposit (settle it on the L2)
timeout="180"
claim_frequency="10"
run wait_for_claim "$timeout" "$claim_frequency" "$l2_rpc_url" "bridgeAsset"
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
}
Loading