Skip to content

Commit

Permalink
feat: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Sep 11, 2024
1 parent fda7d18 commit 950bdc8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
10 changes: 5 additions & 5 deletions test/basic-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ 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}\)"

# Assert that balance is correct
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)
Expand Down
46 changes: 29 additions & 17 deletions test/helpers/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,56 @@ function deployContract() {
function sendTx() {
# Check if at least 3 arguments are provided
if [[ $# -lt 3 ]]; then
echo "Usage: sendTx <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 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
Expand All @@ -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
Expand Down

0 comments on commit 950bdc8

Please sign in to comment.