From 950bdc8cbf12dc54f2ee15b7d36f505ab17010c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Wed, 11 Sep 2024 16:00:44 +0200 Subject: [PATCH] feat: tweaks --- test/basic-e2e.bats | 10 ++++----- test/helpers/common.bash | 46 +++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/test/basic-e2e.bats b/test/basic-e2e.bats index d85621e6..a064a411 100644 --- a/test/basic-e2e.bats +++ b/test/basic-e2e.bats @@ -20,20 +20,20 @@ setup() { @test "Deploy ERC20Mock contract" { local private_key="12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625" - local contract_artifact="./test/contracts/erc20mock/ERC20Mock.json" + local contract_artifact="./contracts/erc20mock/ERC20Mock.json" # Deploy ERC20Mock run deployContract $private_key $contract_artifact assert_success - contract_addr="$output" + contract_addr=$(echo "$output" | tail -n 1) echo "Contract addr: $contract_addr" # Mint ERC20 tokens - local receiver="0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6" local mintFnSig="function mint(address to, uint256 amount)" + local receiver="0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6" local amount="5" - run sendTx $private_key $contract_addr $mintFnSig $receiver $amount + run sendTx "$private_key" "$contract_addr" "$mintFnSig" "$receiver" "$amount" assert_success assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)" @@ -41,7 +41,7 @@ setup() { local balanceOfFnSig="function balanceOf(address) (uint256)" run queryContract $contract_addr $balanceOfFnSig $receiver assert_success - receiverBalance="$output" + receiverBalance=$(echo "$output" | tail -n 1) # Convert balance and amount to a standard format for comparison (e.g., remove any leading/trailing whitespace) receiverBalance=$(echo "$receiverBalance" | xargs) diff --git a/test/helpers/common.bash b/test/helpers/common.bash index 609989c4..9b5de01b 100644 --- a/test/helpers/common.bash +++ b/test/helpers/common.bash @@ -68,45 +68,56 @@ function deployContract() { function sendTx() { # Check if at least 3 arguments are provided if [[ $# -lt 3 ]]; then - echo "Usage: sendTx ] [ ...]" + echo "Usage: sendTx [ ...]" return 1 fi - # Assign variables from function arguments - local private_key="$1" # Sender private key - local receiver="$2" # Receiver address + local private_key="$1" # Sender private key + local receiver="$2" # Receiver address + local value_or_function_sig="$3" # Value or function signature + # Error handling: Ensure the receiver is a valid Ethereum address if [[ ! "$receiver" =~ ^0x[a-fA-F0-9]{40}$ ]]; then echo "Error: Invalid receiver address '$receiver'." return 1 fi - shift 2 # Shift the first 2 arguments (private_key, receiver) - - local first_remaining_arg="$1" - shift # Shift the first remaining argument (value or function signature) + shift 3 # Shift the first 3 arguments (private_key, receiver, value_or_function_sig) - local senderAddr=$(cast wallet address "$private_key") + local senderAddr + senderAddr=$(cast wallet address "$private_key") + if [[ $? -ne 0 ]]; then + echo "Error: Failed to extract the sender address for $private_key" + return 1 + fi # Check if the first remaining argument is a numeric value (Ether to be transferred) - if [[ "$first_remaining_arg" =~ ^[0-9]+(ether)?$ ]]; then + if [[ "$value_or_function_sig" =~ ^[0-9]+(ether)?$ ]]; then # Case: EOA transaction (Ether transfer) - echo "Sending EOA transaction (RPC URL: $rpc_url, sender: $senderAddr) to: $receiver with value: $first_remaining_arg" + echo "Sending EOA transaction (RPC URL: $rpc_url, sender: $senderAddr) to: $receiver with value: $value_or_function_sig" cast_output=$(cast send --rpc-url "$rpc_url" \ --private-key "$private_key" \ - "$receiver" --value "$first_remaining_arg" \ + "$receiver" --value "$value_or_function_sig" \ --legacy \ 2>&1) else # Case: Smart contract transaction (contract interaction with function signature and parameters) - local functionSignature="$first_remaining_arg" local params=("$@") # Collect all remaining arguments as function parameters - echo "Sending smart contract transaction (RPC URL: $rpc_url, sender: $senderAddr) to $receiver with function signature: $functionSignature and params: ${params[*]}" - # Prepare the function signature with parameters for cast send + echo "$value_or_function_sig" + + # Verify if the function signature starts with "function" + if [[ ! "$value_or_function_sig" =~ ^function\ .+\(.+\)$ ]]; then + echo "Error: Invalid function signature format '$value_or_function_sig'." + return 1 + fi + + echo "Sending smart contract transaction (RPC URL: $rpc_url, sender: $senderAddr) to $receiver with function signature: $value_or_function_sig and params: ${params[*]}" + + # Send the smart contract interaction using cast cast_output=$(cast send --rpc-url "$rpc_url" \ --private-key "$private_key" \ - "$receiver" "$functionSignature" "${params[@]}" \ + "$receiver" "$value_or_function_sig" "${params[@]}" \ --legacy \ 2>&1) fi @@ -123,7 +134,8 @@ function sendTx() { fi # Extract the transaction hash from the output - local tx_hash=$(echo "$cast_output" | grep 'transactionHash' | sed 's/transactionHash\s\+//') + local tx_hash + tx_hash=$(echo "$cast_output" | grep 'transactionHash' | sed 's/transactionHash\s\+//') echo "Tx hash: $tx_hash" if [[ -z "$tx_hash" ]]; then