Skip to content

Commit

Permalink
Send EOA transaction test basic
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Sep 9, 2024
1 parent b8d40eb commit 94f594b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
28 changes: 28 additions & 0 deletions test/basic-e2e.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
setup() {
load '/usr/local/lib/bats/bats-support/load'
load '/usr/local/lib/bats/bats-assert/load'

# 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
DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)"
# make executables in src/ visible to PATH
PATH="$DIR/../src:$PATH"

readonly enclave=${ENCLAVE:-cdk-v1}
readonly node=${KURTOSIS_NODE:-cdk-erigon-node-001}
readonly rpc_url=${RPC_URL:-$(kurtosis port print "$enclave" "$node" http-rpc)}
}

@test "Send EOA transaction" {
load 'helpers/common'

local receiver="0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"
local value="10ether"
local private_key="0x12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"

run sendTx $private_key $receiver $value

assert_success
assert_output --partial 'Transaction successful!'
}
31 changes: 18 additions & 13 deletions test/helpers/common.bash
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env bash
function deployContract() {
local rpc_url="$1"
local private_key="$2"
local contract_artifact="$3"
local private_key="$1"
local contract_artifact="$2"

if [[ ! -f "$contract_artifact" ]]; then
echo "Error: Contract artifact $contract_artifact does not exist."
Expand Down Expand Up @@ -43,15 +42,14 @@ function deployContract() {
function sendTx() {
# Check if at least 3 arguments are provided
if [[ $# -lt 3 ]]; then
echo "Usage: sendTx <rpc_url> <private_key> <receiver> [<value_or_function_signature> <param1> <param2> ...]"
echo "Usage: sendTx <private_key> <receiver> <value_or_function_signature>] [<param1> <param2> ...]"
return 1
fi

# Assign variables from function arguments
local rpc_url="$1"
local private_key="$2"
local receiver="$3"
shift 3 # Shift the first 3 arguments (rpc_url, private_key, receiver)
local private_key="$1"
local receiver="$2"
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)
Expand All @@ -63,35 +61,42 @@ function sendTx() {
fi

# Check if the first remaining argument is a numeric value (Ether to be transferred)
if [[ "$first_remaining_arg" =~ ^[0-9]+$ ]]; then
if [[ "$first_remaining_arg" =~ ^[0-9]+(ether)?$ ]]; then
# Case: EOA transaction (Ether transfer)
echo "Sending Ether transaction to EOA: $receiver with value: $first_remaining_arg Wei"
echo "Sending EOA transaction (RPC URL: $rpc_url) to: $receiver with value: $first_remaining_arg"
cast_output=$(cast send --rpc-url "$rpc_url" \
--private-key "$private_key" \
"$receiver" --value "$first_remaining_arg" \
--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 to $receiver with function signature: $functionSignature and params: ${params[*]}"
echo "Sending smart contract transaction (RPC URL: $rpc_url) to $receiver with function signature: $functionSignature and params: ${params[*]}"

# Prepare the function signature with parameters for cast send
cast_output=$(cast send --rpc-url "$rpc_url" \
--private-key "$private_key" \
"$receiver" "$functionSignature" "${params[@]}" \
--legacy \
2>&1)
fi

# Print the cast output
echo "cast send output:"
echo "$cast_output"

# Check if the transaction was successful
if [[ $? -ne 0 ]]; then
echo "Error: Failed to send transaction. Output:"
echo "$cast_output"
return 1
fi

# Transaction was successful, extract and display the transaction hash
tx_hash=$(echo "$cast_output" | grep -oP '(?<=Transaction hash: )0x[a-fA-F0-9]+')
# Extract the transaction hash from the output
local tx_hash=$(echo "$cast_output" | grep 'transactionHash' | sed 's/transactionHash\s\+//')
echo "Tx hash: $tx_hash"

if [[ -z "$tx_hash" ]]; then
echo "Error: Failed to extract transaction hash."
Expand Down

0 comments on commit 94f594b

Please sign in to comment.