Skip to content

Commit

Permalink
fix: send EOA transaction test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Sep 18, 2024
1 parent b549826 commit 0a66a3b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
19 changes: 10 additions & 9 deletions test/basic-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ setup() {
}

@test "Send EOA transaction" {
local initial_nonce=$(cast nonce "$senderAddr" --rpc-url "$rpc_url") || return 1
local sender_addr=$(cast wallet address --private-key $private_key)
local initial_nonce=$(cast nonce "$sender_addr" --rpc-url "$rpc_url") || return 1
local value="10ether"

# case 1: Transaction successfull sernder has suffecient balance
# case 1: Transaction successfull sender has sufficient balance
run sendTx "$private_key" "$receiver" "$value"
assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"

# case 2: Transaction rejected sender attempts to transfer more than they have in their wallet
# Trx will fail pre validation check on the node and will be dropped subsequently from the pool
# without recording it on the chain and hence nonce will not change
local sender_balance=$(cast balance "$senderAddr" --ether --rpc-url "$rpc_url") || return 1
local excessive_value="$(echo "$sender_balance + 1" | bc)ether"
# Transaction will fail pre-validation check on the node and will be dropped subsequently from the pool
# without recording it on the chain and hence nonce will not change
local sender_balance=$(cast balance "$sender_addr" --ether --rpc-url "$rpc_url") || return 1
local excessive_value=$(echo "$sender_balance + 1" | bc)"ether"
run sendTx "$private_key" "$receiver" "$excessive_value"
assert_failure
assert_failure

# check wheather nonce of sender was updated correctly or not
local final_nonce=$(cast nonce "$senderAddr" --rpc-url "$rpc_url") || return 1
# check whether nonce of sender was updated correctly
local final_nonce=$(cast nonce "$sender_addr" --rpc-url "$rpc_url") || return 1
assert_equal "$final_nonce" "$(echo "$initial_nonce + 1" | bc)"
}

Expand Down
42 changes: 19 additions & 23 deletions test/helpers/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function deployContract() {
fi

# Get the sender address
local senderAddr=$(cast wallet address "$private_key")
local sender_addr=$(cast wallet address "$private_key")
if [[ $? -ne 0 ]]; then
echo "Error: Failed to retrieve sender address."
return 1
fi

echo "Attempting to deploy contract artifact '$contract_artifact' to $rpc_url (sender: $senderAddr)" >&3
echo "Attempting to deploy contract artifact '$contract_artifact' to $rpc_url (sender: $sender_addr)" >&3

# Get bytecode from the contract artifact
local bytecode=$(jq -r .bytecode "$contract_artifact")
Expand Down Expand Up @@ -87,22 +87,21 @@ function sendTx() {

shift 3 # Shift the first 3 arguments (private_key, account_addr, value_or_function_sig)

local senderAddr
senderAddr=$(cast wallet address "$private_key")
local sender_addr
sender_addr=$(cast wallet address "$private_key")
if [[ $? -ne 0 ]]; then
echo "Error: Failed to extract the sender address for $private_key"
return 1
fi

# Check initial ether balance of sender and receiver
local sender_initial_balance=$(cast balance "$senderAddr" --ether --rpc-url "$rpc_url") || return 1
# Check initial ether balance of sender and receiver
local sender_initial_balance=$(cast balance "$sender_addr" --ether --rpc-url "$rpc_url") || return 1
local receiver_initial_balance=$(cast balance "$account_addr" --ether --rpc-url "$rpc_url") || return 1


# Check if the first remaining argument is a numeric value (Ether to be transferred)
if [[ "$value_or_function_sig" =~ ^[0-9]+(ether)?$ ]]; then
if [[ "$value_or_function_sig" =~ ^[0-9]+(\.[0-9]+)?(ether)?$ ]]; then
# Case: EOA transaction (Ether transfer)
echo "Sending EOA transaction (RPC URL: $rpc_url, sender: $senderAddr) to: $account_addr " \
echo "Sending EOA transaction (RPC URL: $rpc_url, sender: $sender_addr) to: $account_addr " \
"with value: $value_or_function_sig" >&3

cast_output=$(cast send --rpc-url "$rpc_url" \
Expand All @@ -114,15 +113,13 @@ function sendTx() {
# Case: Smart contract transaction (contract interaction with function signature and parameters)
local params=("$@") # Collect all remaining arguments as function parameters

echo "Function signature: '$value_or_function_sig'" >&3

# 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 $account_addr" \
echo "Sending smart contract transaction (RPC URL: $rpc_url, sender: $sender_addr) to $account_addr" \
"with function signature: '$value_or_function_sig' and params: ${params[*]}" >&3

# Send the smart contract interaction using cast
Expand Down Expand Up @@ -150,15 +147,14 @@ function sendTx() {
fi

if [[ "$value_or_function_sig" =~ ^[0-9]+(ether)?$ ]]; then
sleep 7
checkTransactionSuccess "$senderAddr" "$receiver" "$value_or_function_sig" "$tx_hash" "$sender_initial_balance" "$receiver_initial_balance"
checkTransactionSuccess "$sender_addr" "$receiver" "$value_or_function_sig" "$tx_hash" "$sender_initial_balance" "$receiver_initial_balance"
if [[ $? -ne 0 ]]; then
echo "Error: Balance not updated correctly."
return 1
fi
fi

echo "Transaction successful (transaction hash: '$tx_hash')"
echo "Transaction successful (transaction hash: $tx_hash)"

return 0
}
Expand Down Expand Up @@ -201,9 +197,9 @@ function queryContract() {
}

function rpcQuery() {
local method="$1" # The JSON-RPC method name
shift
local params=("$@") # Remaining arguments are the parameters for the RPC method
local method="$1" # The JSON-RPC method name
shift
local params=("$@") # Remaining arguments are the parameters for the RPC method

# Check if rpc_url is available
if [[ -z "$rpc_url" ]]; then
Expand All @@ -227,14 +223,14 @@ function rpcQuery() {
}

function checkTransactionSuccess() {
local senderAddr="$1"
local sender_addr="$1"
local receiver="$2"
local value_or_function_sig="$3"
local tx_hash="$4"
local sender_initial_balance="$5"
local receiver_initial_balance="$6"

local sender_final_balance=$(cast balance "$senderAddr" --ether --rpc-url "$rpc_url") || return 1
local sender_final_balance=$(cast balance "$sender_addr" --ether --rpc-url "$rpc_url") || return 1
local gas_used=$(cast tx "$tx_hash" --rpc-url "$rpc_url" | grep '^gas ' | awk '{print $2}')
local gas_price=$(cast tx "$tx_hash" --rpc-url "$rpc_url" | grep '^gasPrice' | awk '{print $2}')
local gas_fee=$(echo "$gas_used * $gas_price" | bc)
Expand All @@ -246,19 +242,19 @@ function checkTransactionSuccess() {

local receiver_final_balance=$(cast balance "$receiver" --ether --rpc-url "$rpc_url") || return 1
local receiver_balance_change=$(echo "$receiver_final_balance - $receiver_initial_balance" | bc)
echo "Receiver balance changed by: `$receiver_balance_change` wei"
echo "Receiver balance changed by: '$receiver_balance_change' wei"

# Trim 'ether' suffix from value_or_function_sig to get the numeric part
local value_in_ether=$(echo "$value_or_function_sig" | sed 's/ether$//')

if ! echo "$receiver_balance_change == $value_in_ether" | bc -l; then
echo "Error: receiver balance updated incorrectly. Expected: $value_in_ether, Actual: $receiver_balance_change"
return 1
fi

# Calculate expected sender balance change
local expected_sender_change=$(echo "$value_in_ether + $gas_fee_in_ether" | bc)
if ! echo "$sender_balance_change == $expected_sender_change" | bc -l; then
if ! echo "$sender_balance_change == $expected_sender_change" | bc -l; then
echo "Error: sender balance updated incorrectly. Expected: $expected_sender_change, Actual: $sender_balance_change"
return 1
fi
Expand Down

0 comments on commit 0a66a3b

Please sign in to comment.