Skip to content

Commit

Permalink
fix: address feedback from @vcastellm
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Sep 13, 2024
1 parent 047d60d commit 028378c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
13 changes: 5 additions & 8 deletions test/basic-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,28 @@ setup() {
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)}
readonly private_key=${SENDER_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
readonly receiver=${RECEIVER:-"0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"}
}

@test "Send EOA transaction" {
local private_key=${RAW_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
local receiver="0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"
local value="10ether"

run sendTx $private_key $receiver $value
run sendTx "$private_key" "$receiver" "$value"
assert_success
assert_output --regexp "Transaction successful \(transaction hash: 0x[a-fA-F0-9]{64}\)"
}

@test "Deploy ERC20Mock contract" {
local private_key=${RAW_PRIVATE_KEY:-"12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"}
local contract_artifact="./contracts/erc20mock/ERC20Mock.json"

# Deploy ERC20Mock
run deployContract $private_key $contract_artifact
run deployContract "$private_key" "$contract_artifact"
assert_success
contract_addr=$(echo "$output" | tail -n 1)
echo "Contract addr: $contract_addr"

# Mint ERC20 tokens
local mintFnSig="function mint(address to, uint256 amount)"
local receiver="0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"
local mintFnSig="function mint(address receiver, uint256 amount)"
local amount="5"

run sendTx "$private_key" "$contract_addr" "$mintFnSig" "$receiver" "$amount"
Expand Down
40 changes: 21 additions & 19 deletions test/helpers/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function deployContract() {
fi

if [[ ! -f "$contract_artifact" ]]; then
echo "Error: Contract artifact $contract_artifact does not exist."
echo "Error: Contract artifact '$contract_artifact' does not exist."
return 1
fi

Expand All @@ -21,8 +21,8 @@ function deployContract() {
echo "Error: Failed to retrieve sender address."
return 1
fi
echo "Attempting to deploy contract artifact $contract_artifact to $rpc_url (sender: $senderAddr)"

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

# Get bytecode from the contract artifact
local bytecode=$(jq -r .bytecode "$contract_artifact")
Expand All @@ -45,10 +45,12 @@ function deployContract() {
return 1
fi

echo "$cast_output"
echo "Deploy contract output:" >&3
echo "$cast_output" >&3

# Extract the contract address from the output
local deployed_contract_address=$(echo "$cast_output" | grep 'contractAddress' | sed 's/contractAddress\s\+//')
echo "Deployed contract address: $deployed_contract_address" >&3

if [[ -z "$deployed_contract_address" ]]; then
echo "Error: Failed to extract deployed contract address"
Expand All @@ -74,16 +76,16 @@ function sendTx() {
fi

local private_key="$1" # Sender private key
local receiver="$2" # Receiver address
local account_addr="$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'."
if [[ ! "$account_addr" =~ ^0x[a-fA-F0-9]{40}$ ]]; then
echo "Error: Invalid receiver address '$account_addr'."
return 1
fi

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

local senderAddr
senderAddr=$(cast wallet address "$private_key")
Expand All @@ -95,41 +97,40 @@ function sendTx() {
# 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)
echo "Sending EOA transaction (RPC URL: $rpc_url, sender: $senderAddr) to: $receiver with value: $value_or_function_sig"
echo "Sending EOA transaction (RPC URL: $rpc_url, sender: $senderAddr) to: $account_addr " \
"with value: $value_or_function_sig" >&3

cast_output=$(cast send --rpc-url "$rpc_url" \
--private-key "$private_key" \
"$receiver" --value "$value_or_function_sig" \
"$account_addr" --value "$value_or_function_sig" \
--legacy \
2>&1)
else
# Case: Smart contract transaction (contract interaction with function signature and parameters)
local params=("$@") # Collect all remaining arguments as function parameters

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

# Send the smart contract interaction using cast
cast_output=$(cast send --rpc-url "$rpc_url" \
--private-key "$private_key" \
"$receiver" "$value_or_function_sig" "${params[@]}" \
"$account_addr" "$value_or_function_sig" "${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 "Error: Failed to send transaction. The cast send output:"
echo "$cast_output"
return 1
fi
Expand All @@ -154,7 +155,7 @@ function queryContract() {
shift 2 # Shift past the first two arguments
local params=("$@") # Collect remaining arguments as parameters array

echo "Querying state of $addr account (RPC URL: $rpc_url) with function signature: '$funcSignature' and params: ${params[*]}"
echo "Querying state of $addr account (RPC URL: $rpc_url) with function signature: '$funcSignature' and params: ${params[*]}" >&3

# Check if rpc_url is available
if [[ -z "$rpc_url" ]]; then
Expand All @@ -181,5 +182,6 @@ function queryContract() {

# Return the result (contract query response)
echo "$result"

return 0
}

0 comments on commit 028378c

Please sign in to comment.