Skip to content

Commit

Permalink
test: move variables and tests around, approve gas token on L1 and ru…
Browse files Browse the repository at this point in the history
…n deposit
  • Loading branch information
Stefan-Ethernal committed Sep 18, 2024
1 parent e6edbcb commit 7462182
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 74 deletions.
55 changes: 0 additions & 55 deletions test/basic-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ setup() {
load 'helpers/common'
_common_setup

readonly enclave=${KURTOSIS_ENCLAVE:-cdk-v1}
readonly node=${KURTOSIS_NODE:-cdk-erigon-node-001}
readonly contracts_container=${KURTOSIS_CONTRACTS:-contracts-001}
readonly sender_private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"}
readonly contracts_service_wrapper=${KURTOSIS_CONTRACTS_WRAPPER:-"kurtosis service exec $enclave $contracts_container"}

readonly l1_rpc_url=${L1_ETH_RPC_URL:-"$(kurtosis port print cdk-v1 el-1-geth-lighthouse rpc)"}
readonly l2_rpc_url=${L2_ETH_RPC_URL:-$(kurtosis port print "$enclave" "$node" http-rpc)}

readonly mint_fn_sig="function mint(address,uint256)"
readonly balance_of_fn_sig="function balanceOf(address) (uint256)"
}

@test "Send EOA transaction" {
Expand Down Expand Up @@ -52,51 +45,3 @@ setup() {
# Check if the balance is equal to the amount
assert_equal "$receiverBalance" "$amount"
}

@test "Custom native token transfer" {
# Use GAS_TOKEN_ADDR if provided, otherwise retrieve from file
if [[ -n "$GAS_TOKEN_ADDR" ]]; then
echo "Using provided GAS_TOKEN_ADDR: $GAS_TOKEN_ADDR" >&3
local gas_token_addr="$GAS_TOKEN_ADDR"
else
echo "GAS_TOKEN_ADDR not provided, retrieving from rollup parameters file." >&3
readonly rollup_params_file=/opt/zkevm/create_rollup_parameters.json
run bash -c "$contracts_service_wrapper 'cat $rollup_params_file' | tail -n +2 | jq -r '.gasTokenAddress'"
assert_success
assert_output --regexp "0x[a-fA-F0-9]{40}"
local gas_token_addr=$output
fi

echo "Gas token addr $gas_token_addr, L1 RPC: $l1_rpc_url" >&3

# Query for initial receiver balance
run queryContract "$l1_rpc_url" "$gas_token_addr" "$balance_of_fn_sig" "$receiver"
assert_success
local initial_receiver_balance=$(echo "$output" | tail -n 1)
echo "Initial receiver balance $initial_receiver_balance"

# Mint gas token on L1
local amount="1ether"
run sendTx "$l1_rpc_url" "$sender_private_key" "$gas_token_addr" "$mint_fn_sig" "$receiver" "$amount"
local wei_amount=$(cast --to-unit $amount wei)

assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

# Assert that balance of gas token (on the L1) is correct
run queryContract "$l1_rpc_url" "$gas_token_addr" "$balance_of_fn_sig" "$receiver"
assert_success
local receiver_balance=$(echo "$output" |
tail -n 1 |
awk '{print $1}')
local expected_balance=$(echo "$initial_receiver_balance + $wei_amount" |
bc |
awk '{print $1}')

echo "Receiver balance: $receiver_balance" >&3
assert_equal "$receiver_balance" "$expected_balance"

# TODO:
# Deposit
# Check whether native token balance on L2 has increased and send a dummy transaction
}
100 changes: 83 additions & 17 deletions test/bridge-e2e.bats
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
setup() {
load 'helpers/common-setup'
_common_setup
load 'helpers/common'
load 'helpers/lxly-bridge-test'

readonly data_availability_mode=${DATA_AVAILABILITY_MODE:-"cdk-validium"}
$PROJECT_ROOT/test/scripts/kurtosis_prepare_params_yml.sh ../kurtosis-cdk $data_availability_mode
$PROJECT_ROOT/test/scripts/kurtosis_prepare_params_yml.sh ../../kurtosis-cdk $data_availability_mode
[ $? -ne 0 ] && echo "Error preparing params.yml" && exit 1

# Check if the genesis file is already downloaded
if [ ! -f "./tmp/cdk/genesis/genesis.json" ]; then
mkdir -p ./tmp/cdk
kurtosis files download cdk-v1 genesis ./tmp/cdk/genesis
[ $? -ne 0 ] && echo "Error downloading genesis file" && exit 1
if [ -z "$BRIDGE_ADDRESS" ]; then
local combined_json_path="/opt/zkevm/combined.json"
echo "BRIDGE_ADDRESS env variable is not provided, resolving the bridge address from the Kurtosis CDK '$combined_json_path'" >&3

# Fetching the combined JSON output and filtering to get polygonZkEVMBridgeAddress
combined_json_output=$($contracts_service_wrapper "cat $combined_json_path" | tail -n +2)
bridge_default_address=$(echo "$combined_json_output" | jq -r .polygonZkEVMBridgeAddress)
BRIDGE_ADDRESS=$bridge_default_address
fi
# Download the genesis file
readonly bridge_default_address=$(jq -r ".genesis[] | select(.contractName == \"PolygonZkEVMBridge proxy\") | .address" ./tmp/cdk/genesis/genesis.json)

echo "Bridge address=$BRIDGE_ADDRESS" >&3

readonly sender_private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly destination_net=${DESTINATION_NET:-"1"}
readonly destination_addr=${DESTINATION_ADDRESS:-"0x0bb7AA0b4FdC2D2862c088424260e99ed6299148"}
readonly ether_value=${ETHER_VALUE:-"0.0200000054"}
readonly token_addr=${TOKEN_ADDRESS:-"0x0000000000000000000000000000000000000000"}
destination_net=${DESTINATION_NET:-"1"}
destination_addr=${DESTINATION_ADDRESS:-"0x0bb7AA0b4FdC2D2862c088424260e99ed6299148"}
ether_value=${ETHER_VALUE:-"0.0200000054"}
token_addr=${TOKEN_ADDRESS:-"0x0000000000000000000000000000000000000000"}
readonly is_forced=${IS_FORCED:-"true"}
readonly bridge_addr=${BRIDGE_ADDRESS:-$bridge_default_address}
readonly bridge_addr=$BRIDGE_ADDRESS
readonly meta_bytes=${META_BYTES:-"0x"}

readonly l1_rpc_url=${L1_ETH_RPC_URL:-"$(kurtosis port print cdk-v1 el-1-geth-lighthouse rpc)"}
Expand All @@ -37,15 +42,13 @@ setup() {
}

@test "Run deposit" {
load 'helpers/lxly-bridge-test'
echo "Running LxLy deposit" >&3
run deposit
assert_success
assert_output --partial 'transactionHash'
}

@test "Run claim" {
load 'helpers/lxly-bridge-test'
echo "Running LxLy claim"

# The script timeout (in seconds).
Expand All @@ -59,13 +62,76 @@ setup() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ❌ Exiting... Timeout reached!"
exit 1
fi

run claim
if [ $status -eq 0 ]; then
break
fi
sleep 10
done

assert_success
}

@test "Custom native token transfer" {
# Use GAS_TOKEN_ADDR if provided, otherwise retrieve from file
if [[ -n "$GAS_TOKEN_ADDR" ]]; then
echo "Using provided GAS_TOKEN_ADDR: $GAS_TOKEN_ADDR" >&3
local gas_token_addr="$GAS_TOKEN_ADDR"
else
echo "GAS_TOKEN_ADDR not provided, retrieving from rollup parameters file." >&3
readonly rollup_params_file=/opt/zkevm/create_rollup_parameters.json
run bash -c "$contracts_service_wrapper 'cat $rollup_params_file' | tail -n +2 | jq -r '.gasTokenAddress'"
assert_success
assert_output --regexp "0x[a-fA-F0-9]{40}"
local gas_token_addr=$output
fi

echo "Gas token addr $gas_token_addr, L1 RPC: $l1_rpc_url" >&3

receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"}

# Query for initial receiver balance
run queryContract "$l1_rpc_url" "$gas_token_addr" "$balance_of_fn_sig" "$receiver"
assert_success
local initial_receiver_balance=$(echo "$output" | tail -n 1)
echo "Initial receiver balance $initial_receiver_balance"

# Mint gas token on L1
local tokens_amount="1ether"
run sendTx "$l1_rpc_url" "$sender_private_key" "$gas_token_addr" "$mint_fn_sig" "$receiver" "$tokens_amount"
local wei_amount=$(cast --to-unit $tokens_amount wei)

assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

# Assert that balance of gas token (on the L1) is correct
run queryContract "$l1_rpc_url" "$gas_token_addr" "$balance_of_fn_sig" "$receiver"
assert_success
local receiver_balance=$(echo "$output" |
tail -n 1 |
awk '{print $1}')
local expected_balance=$(echo "$initial_receiver_balance + $wei_amount" |
bc |
awk '{print $1}')

echo "Receiver balance: $receiver_balance" >&3
assert_equal "$receiver_balance" "$expected_balance"

# TODO:
# Send approve transaction
deposit_ether_value="0.1ether"
run sendTx "$l1_rpc_url" "$sender_private_key" "$gas_token_addr" "$approve_fn_sig" "$bridge_addr" "$deposit_ether_value"
assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

# Deposit
token_addr=$gas_token_addr
destination_addr=$receiver
destination_net=$l2_rpc_network_id
run deposit
assert_success

# Run claim?
# Check whether native token balance on L2 has increased and send a dummy transaction
}
14 changes: 12 additions & 2 deletions test/helpers/common-setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
_common_setup() {
bats_load_library 'bats-support'
bats_load_library 'bats-assert'

# get the containing directory of this file
# use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0,
# as those will point to the bats executable's location or the preprocessed file respectively
PROJECT_ROOT="$( cd "$( dirname "$BATS_TEST_FILENAME" )/.." >/dev/null 2>&1 && pwd )"
PROJECT_ROOT="$(cd "$(dirname "$BATS_TEST_FILENAME")/.." >/dev/null 2>&1 && pwd)"
# make executables in src/ visible to PATH
PATH="$PROJECT_ROOT/src:$PATH"

# ERC20 contracts function signatures
readonly mint_fn_sig="function mint(address,uint256)"
readonly balance_of_fn_sig="function balanceOf(address) (uint256)"
readonly approve_fn_sig="function approve(address,uint256)"

# Kurtosis enclave and service identifiers
readonly enclave=${KURTOSIS_ENCLAVE:-cdk-v1}
readonly contracts_container=${KURTOSIS_CONTRACTS:-contracts-001}
readonly contracts_service_wrapper=${KURTOSIS_CONTRACTS_WRAPPER:-"kurtosis service exec $enclave $contracts_container"}
}

0 comments on commit 7462182

Please sign in to comment.