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

test: Added check for EOA transfer E2E Test #75

Merged
merged 44 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
bcd62fd
tests: PoC bridge testing with bats
vcastellm Sep 3, 2024
3be3e46
Run bats in e2e
vcastellm Sep 3, 2024
2ed2c8a
Remove unused make lines
vcastellm Sep 6, 2024
8b57644
tests: wip
vcastellm Sep 9, 2024
702165f
test: better
vcastellm Sep 10, 2024
67d3075
Merge remote-tracking branch 'origin/develop' into poc_bats
vcastellm Sep 10, 2024
5e19625
Let's see
vcastellm Sep 10, 2024
f4a7bfa
test: fix test
vcastellm Sep 10, 2024
97d357e
test: use cdk image
vcastellm Sep 10, 2024
d91c20f
test: bats path
vcastellm Sep 10, 2024
98a5261
test: fix
vcastellm Sep 10, 2024
09eeb68
test: deposit on 1
vcastellm Sep 12, 2024
7ad30b2
test: wait for claim
vcastellm Sep 12, 2024
7a2a313
test: timeout
vcastellm Sep 12, 2024
728befb
test: timeout
vcastellm Sep 12, 2024
5e0c439
test: increase timeout
vcastellm Sep 12, 2024
1600349
test: apply feedback
vcastellm Sep 13, 2024
4bf549b
Merge remote-tracking branch 'origin/develop' into poc_bats
vcastellm Sep 13, 2024
3d03ba4
ci: lint action
vcastellm Sep 13, 2024
244eb2d
test: do not prepare if already present
vcastellm Sep 13, 2024
7a1213b
test: Send EOA and deploy contract E2E tests using Bats (#69)
Stefan-Ethernal Sep 16, 2024
2edafe6
test: apply feedback
vcastellm Sep 16, 2024
13df74c
Merge remote-tracking branch 'refs/remotes/origin/poc_bats' into poc_…
vcastellm Sep 16, 2024
0e0172c
Merge remote-tracking branch 'origin/develop' into poc_bats
vcastellm Sep 16, 2024
5d717fa
test: lint
vcastellm Sep 16, 2024
0d71a87
ci: increase lint timeout
vcastellm Sep 16, 2024
6ee20ea
test: balance check for ether transafers
rachit77 Sep 16, 2024
af407d2
test: wip
rachit77 Sep 16, 2024
1f68e5c
fix: apply feedback
rachit77 Sep 16, 2024
0415ce1
fix: cast commands
rachit77 Sep 16, 2024
9b39a67
test: add edge case
rachit77 Sep 16, 2024
4fa3c8f
fix: tests
rachit77 Sep 17, 2024
283f5ed
fix: resolve conflicts
rachit77 Sep 17, 2024
75a4d19
fix: resolve conflicts
rachit77 Sep 17, 2024
bc4a1a4
fix: tests
rachit77 Sep 17, 2024
99e2c4c
fix: tests
rachit77 Sep 17, 2024
fc85712
fix: rpc
rachit77 Sep 17, 2024
f064c2a
fix: typo
rachit77 Sep 17, 2024
b549826
fix: checkTransactionSuccess
rachit77 Sep 17, 2024
913354b
fix: send eoa transaction test
Stefan-Ethernal Sep 18, 2024
0aacf5a
refactor: feedback
rachit77 Sep 18, 2024
cb051ea
refactor: feedback
rachit77 Sep 18, 2024
8b8a864
fix: simplifications
Stefan-Ethernal Sep 18, 2024
2a2a55d
fix: even more simplifications
Stefan-Ethernal Sep 18, 2024
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
14 changes: 14 additions & 0 deletions test/basic-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@ setup() {
}

@test "Send EOA transaction" {
local initial_nonce=$(rpcQuery "nonce" "$senderAddr") || return 1
local value="10ether"

# case 1: Transaction successfull sernder has suffecient 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
rachit77 marked this conversation as resolved.
Show resolved Hide resolved
# 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=$(rpcQuery "balance" "--ether" "$senderAddr") || return 1
local excessive_value="$(echo "$sender_balance + 1" | bc)ether"
run sendTx "$private_key" "$receiver" "$excessive_value"
assert_failure

# check wheather nonce of sender was updated correctly or not
local final_nonce=$(rpcQuery "nonce" "$senderAddr") || return 1
assert_equal "$final_nonce" "$(echo "$initial_nonce + 1" | bc)"
}

@test "Deploy ERC20Mock contract" {
Expand Down
71 changes: 70 additions & 1 deletion test/helpers/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ function sendTx() {
return 1
fi

# Check initial ether balance of sender and receiver
local sender_initial_balance=$(rpcQuery "balance" "$senderAddr" "--block latest") || return 1
local receiver_initial_balance=$(rpcQuery "balance" "$account_addr" "--block latest") || 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
# Case: EOA transaction (Ether transfer)
Expand Down Expand Up @@ -144,7 +149,15 @@ function sendTx() {
return 1
fi

echo "Transaction successful (transaction hash: $tx_hash)"
sleep 7

checkTransactionSuccess "$senderAddr" "$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

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

return 0
}
Expand Down Expand Up @@ -185,3 +198,59 @@ function queryContract() {

return 0
}

function rpcQuery() {
rachit77 marked this conversation as resolved.
Show resolved Hide resolved
rachit77 marked this conversation as resolved.
Show resolved Hide resolved
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
echo "Error: rpc_url environment variable is not set."
return 1
fi

# Use cast to perform a generic RPC call
local response
response=$(cast --rpc-url "$rpc_url" "$method" "${params[@]}" 2>&1)
Stefan-Ethernal marked this conversation as resolved.
Show resolved Hide resolved

# Check if the cast rpc command was successful
if [[ $? -ne 0 ]]; then
echo "Error: Failed to perform RPC query."
echo "$response"
return 1
fi

echo "$response"
return 0
}

function checkTransactionSuccess() {
rachit77 marked this conversation as resolved.
Show resolved Hide resolved
rachit77 marked this conversation as resolved.
Show resolved Hide resolved
local senderAddr="$1"
local receiver="$2"
local value_or_function_sig="$3"
rachit77 marked this conversation as resolved.
Show resolved Hide resolved
local tx_hash="$4"
local sender_initial_balance="$5"
local receiver_initial_balance="$6"

local sender_final_balance=$(rpcQuery "balance" "$senderAddr" "--block latest") || 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)
local sender_balance_change=$(echo "$sender_initial_balance - $sender_final_balance" | bc)

echo "Sender balance changed by: '$sender_balance_change' wei"
echo "Gas fee paid: '$gas_fee' wei"

if [[ "$value_or_function_sig" =~ ^[0-9]+(ether)?$ ]]; then
local receiver_final_balance=$(rpcQuery "balance" "$receiver" "--block latest") || return 1
local receiver_balance_change=$(echo "$receiver_final_balance - $receiver_initial_balance" | bc)
echo "Receiver balance changed by: `$receiver_balance_change` wei"

value_in_wei=$(cast --to-unit wei "$value_or_function_sig")
assert_equal "$receiver_balance_chang" "$value_in_wei" "Error receiver balance updated incorrectly"
fi

# Asserts sender's balance change is equal to the value transferred plus the gas fee
assert_equal "$sender_balance_change" "$(echo "$value_in_wei + $gas_fee" | bc)" "Error sender balance updated incorrectly"
}
Loading