Skip to content

Commit

Permalink
example script interacts with c-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-schultz committed Dec 21, 2023
1 parent 838e9b7 commit d38c26d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ set -a # export all variables so child processes can acce
source vars.sh
```

- An example of how to interact with Teleporter is provided in `scripts/local/examples/basic_send_receive.sh`, and can be run directly in the container:
- An example of how to interact with Teleporter is provided in `scripts/local/examples/basic_send_receive.sh`. This script sends a dummy payload via Teleporter from the C-Chain to a subnet, and back again. This script can be run directly in the container:

```
# Open a shell in the container
Expand Down
64 changes: 32 additions & 32 deletions scripts/local/examples/basic_send_receive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@ set -e # Stop on first error
# Variables provided by run_setup.sh:
# user_private_key
# user_address
# c_chain_blockchain_id
# subnet_a_blockchain_id
# subnet_b_blockchain_id
# c_chain_subnet_id
# subnet_a_subnet_id
# subnet_b_subnet_id
# subnet_a_rpc_url
# subnet_b_rpc_url
# c_chain_rpc_url
# subnet_a_subnet_id
# c_chain_blockchain_id_hex
# subnet_a_blockchain_id_hex
# subnet_b_blockchain_id_hex
# teleporter_contract_address
# warp_messenger_precompile_addr

# Test covers:
# - Sending bidirectional cross chain messages between two chains, by calling Teleporter contract sendCrossChainMessage function directly.
# - Sending bidirectional cross chain messages between the C-Chain and a subnet chain, by calling Teleporter contract sendCrossChainMessage function directly.
# - Checking message delivery for message that was sent on destination chain.
# - Calling to the warp precompile address directly for blockchain ID.

# Deploy a test ERC20 to be used in the E2E test.
cd contracts
erc20_deploy_result=$(forge create --private-key $user_private_key src/Mocks/ExampleERC20.sol:ExampleERC20 --rpc-url $subnet_a_rpc_url)
erc20_deploy_result=$(forge create --private-key $user_private_key src/Mocks/ExampleERC20.sol:ExampleERC20 --rpc-url $c_chain_rpc_url)
erc20_contract_address_a=$(parseContractAddress "$erc20_deploy_result")
echo "Test ERC20 contract deployed to $erc20_contract_address_a on subnet A"
erc20_deploy_result=$(forge create --private-key $user_private_key src/Mocks/ExampleERC20.sol:ExampleERC20 --rpc-url $subnet_b_rpc_url)
erc20_deploy_result=$(forge create --private-key $user_private_key src/Mocks/ExampleERC20.sol:ExampleERC20 --rpc-url $subnet_a_subnet_id)
erc20_contract_address_b=$(parseContractAddress "$erc20_deploy_result")
echo "Test ERC20 contract deployed to $erc20_contract_address_b on subnet B"

###
# Send from subnet A -> subnet B
###
echo "Sending from subnet A to subnet B"
blockchainID=$(cast call $warp_messenger_precompile_addr "getBlockchainID()(bytes32)" --rpc-url $subnet_a_rpc_url)
blockchainID=$(cast call $warp_messenger_precompile_addr "getBlockchainID()(bytes32)" --rpc-url $c_chain_rpc_url)
echo "Got blockchain ID $blockchainID"

echo "Sending call to teleporter contract address $teleporter_contract_address $subnet_b_blockchain_id_hex $subnet_a_rpc_url"
result=$(cast call $teleporter_contract_address "getNextMessageID(bytes32)(uint256)" $subnet_b_blockchain_id_hex --rpc-url $subnet_a_rpc_url)
echo "Next message ID for subnet $subnet_b_blockchain_id_hex is $result"
echo "Sending call to teleporter contract address $teleporter_contract_address $subnet_a_blockchain_id_hex $c_chain_rpc_url"
result=$(cast call $teleporter_contract_address "getNextMessageID(bytes32)(uint256)" $subnet_a_blockchain_id_hex --rpc-url $c_chain_rpc_url)
echo "Next message ID for subnet $subnet_a_blockchain_id_hex is $result"

# Directly send a transaction to the teleporter contract sendCrossChainMessage function.
send_cross_subnet_message_destination_chain_id=$subnet_b_blockchain_id_hex
send_cross_subnet_message_destination_chain_id=$subnet_a_blockchain_id_hex
send_cross_subnet_message_destination_address=abcedf1234abcedf1234abcedf1234abcedf1234
send_bytes32=000000000000000000000000$send_cross_subnet_message_destination_address
send_cross_subnet_message_fee_amount=255
Expand All @@ -55,8 +55,8 @@ send_cross_subnet_message_message_data=cafebabecafebabecafebabecafebabecafebabec
approve_amount=100000000000000000000000000000
cast send $erc20_contract_address_a "approve(address,uint256)(bool)" $teleporter_contract_address \
$approve_amount \
--private-key $user_private_key --rpc-url $subnet_a_rpc_url
result=$(cast call $erc20_contract_address_a "allowance(address,address)(uint256)" $user_address $teleporter_contract_address --rpc-url $subnet_a_rpc_url)
--private-key $user_private_key --rpc-url $c_chain_rpc_url
result=$(cast call $erc20_contract_address_a "allowance(address,address)(uint256)" $user_address $teleporter_contract_address --rpc-url $c_chain_rpc_url)
if [[ $result -ne $approve_amount ]]; then
echo $result
echo $approve_amount
Expand All @@ -66,13 +66,13 @@ fi

echo "Approved the Teleporter contract to spend the test ERC20 token from the user account."

startID=$(cast call $teleporter_contract_address "sendCrossChainMessage((bytes32,address,(address,uint256),uint256,address[],bytes))(uint256)" "($send_cross_subnet_message_destination_chain_id,$send_cross_subnet_message_destination_address,($erc20_contract_address_a,$send_cross_subnet_message_fee_amount),$send_cross_subnet_message_required_gas_limit,[],$send_cross_subnet_message_message_data)" --from $user_address --rpc-url $subnet_a_rpc_url)
startID=$(cast call $teleporter_contract_address "sendCrossChainMessage((bytes32,address,(address,uint256),uint256,address[],bytes))(uint256)" "($send_cross_subnet_message_destination_chain_id,$send_cross_subnet_message_destination_address,($erc20_contract_address_a,$send_cross_subnet_message_fee_amount),$send_cross_subnet_message_required_gas_limit,[],$send_cross_subnet_message_message_data)" --from $user_address --rpc-url $c_chain_rpc_url)
echo "Got starting ID $startID to teleport address $teleporter_contract_address"
echo "Got Ids $subnet_a_blockchain_id_hex $subnet_b_blockchain_id_hex $subnet_a_subnet_id $subnet_b_subnet_id"
cast send $teleporter_contract_address "sendCrossChainMessage((bytes32,address,(address,uint256),uint256,address[],bytes))(uint256)" "($send_cross_subnet_message_destination_chain_id,$send_cross_subnet_message_destination_address,($erc20_contract_address_a,$send_cross_subnet_message_fee_amount),$send_cross_subnet_message_required_gas_limit,[],$send_cross_subnet_message_message_data)" --private-key $user_private_key --rpc-url $subnet_a_rpc_url
echo "Got Ids $c_chain_blockchain_id_hex $subnet_a_blockchain_id_hex $c_chain_subnet_id $subnet_a_subnet_id"
cast send $teleporter_contract_address "sendCrossChainMessage((bytes32,address,(address,uint256),uint256,address[],bytes))(uint256)" "($send_cross_subnet_message_destination_chain_id,$send_cross_subnet_message_destination_address,($erc20_contract_address_a,$send_cross_subnet_message_fee_amount),$send_cross_subnet_message_required_gas_limit,[],$send_cross_subnet_message_message_data)" --private-key $user_private_key --rpc-url $c_chain_rpc_url

retry_count=0
received=$(cast call $teleporter_contract_address "messageReceived(bytes32,uint256)(bool)" $subnet_a_blockchain_id_hex $startID --rpc-url $subnet_b_rpc_url)
received=$(cast call $teleporter_contract_address "messageReceived(bytes32,uint256)(bool)" $c_chain_blockchain_id_hex $startID --rpc-url $subnet_a_subnet_id)
until [[ $received == "true" ]]
do
if [[ retry_count -ge 10 ]]; then
Expand All @@ -82,7 +82,7 @@ do
echo "Waiting for destination chain on subnet B to receive message ID $startID. Retry count: $retry_count"
sleep 3

received=$(cast call $teleporter_contract_address "messageReceived(bytes32,uint256)(bool)" $subnet_a_blockchain_id_hex $startID --rpc-url $subnet_b_rpc_url)
received=$(cast call $teleporter_contract_address "messageReceived(bytes32,uint256)(bool)" $c_chain_blockchain_id_hex $startID --rpc-url $subnet_a_subnet_id)
retry_count=$((retry_count+1))
done

Expand All @@ -93,22 +93,22 @@ echo "Received on subnet B is $received"
# Send from subnet B -> subnet A
###
echo "Sending from subnet B to subnet A"
blockchainID=$(cast call $warp_messenger_precompile_addr "getBlockchainID()(bytes32)" --rpc-url $subnet_b_rpc_url)
blockchainID=$(cast call $warp_messenger_precompile_addr "getBlockchainID()(bytes32)" --rpc-url $subnet_a_subnet_id)
echo "Got blockchain ID $blockchainID"

echo "Sending call to teleporter contract address $teleporter_contract_address $subnet_a_blockchain_id_hex $subnet_b_rpc_url"
result=$(cast call $teleporter_contract_address "getNextMessageID(bytes32)(uint256)" $subnet_a_blockchain_id_hex --rpc-url $subnet_b_rpc_url)
echo "Next message ID for subnet $subnet_a_blockchain_id_hex is $result"
echo "Sending call to teleporter contract address $teleporter_contract_address $c_chain_blockchain_id_hex $subnet_a_subnet_id"
result=$(cast call $teleporter_contract_address "getNextMessageID(bytes32)(uint256)" $c_chain_blockchain_id_hex --rpc-url $subnet_a_subnet_id)
echo "Next message ID for subnet $c_chain_blockchain_id_hex is $result"

# Directly send a few transaction to the teleporter contract sendCrossChainMessage function.
send_cross_subnet_message_destination_chain_id=$subnet_a_blockchain_id_hex
send_cross_subnet_message_destination_chain_id=$c_chain_blockchain_id_hex
send_cross_subnet_message_destination_address=abcedf1234abcedf1234abcedf1234abcedf1234
send_bytes32=000000000000000000000000$send_cross_subnet_message_destination_address
send_cross_subnet_message_message_data=cafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabecafe

# Approve the teleporter contract to some ERC20 tokens from the user account we're using to send transactions
cast send $erc20_contract_address_b "approve(address,uint256)(bool)" $teleporter_contract_address $approve_amount --private-key $user_private_key --rpc-url $subnet_b_rpc_url
result=$(cast call $erc20_contract_address_b "allowance(address,address)(uint256)" $user_address $teleporter_contract_address --rpc-url $subnet_b_rpc_url)
cast send $erc20_contract_address_b "approve(address,uint256)(bool)" $teleporter_contract_address $approve_amount --private-key $user_private_key --rpc-url $subnet_a_subnet_id
result=$(cast call $erc20_contract_address_b "allowance(address,address)(uint256)" $user_address $teleporter_contract_address --rpc-url $subnet_a_subnet_id)
if [[ $result -ne $approve_amount ]]; then
echo $result
echo "Error approving Teleporter contract to spend ERC20 from user account."
Expand All @@ -117,13 +117,13 @@ fi

echo "Approved the Teleporter contract to spend the test ERC20 token from the user account."

startID=$(cast call $teleporter_contract_address "sendCrossChainMessage((bytes32,address,(address,uint256),uint256,address[],bytes))(uint256)" "($send_cross_subnet_message_destination_chain_id,$send_cross_subnet_message_destination_address,($erc20_contract_address_b,$send_cross_subnet_message_fee_amount),$send_cross_subnet_message_required_gas_limit,[],$send_cross_subnet_message_message_data)" --from $user_address --rpc-url $subnet_b_rpc_url)
startID=$(cast call $teleporter_contract_address "sendCrossChainMessage((bytes32,address,(address,uint256),uint256,address[],bytes))(uint256)" "($send_cross_subnet_message_destination_chain_id,$send_cross_subnet_message_destination_address,($erc20_contract_address_b,$send_cross_subnet_message_fee_amount),$send_cross_subnet_message_required_gas_limit,[],$send_cross_subnet_message_message_data)" --from $user_address --rpc-url $subnet_a_subnet_id)
echo "Got starting ID $startID to teleport address $teleporter_contract_address"
echo "Got Ids $subnet_b_blockchain_id_hex $subnet_a_blockchain_id_hex $subnet_b_subnet_id $subnet_a_subnet_id"
cast send $teleporter_contract_address "sendCrossChainMessage((bytes32,address,(address,uint256),uint256,address[],bytes))(uint256)" "($send_cross_subnet_message_destination_chain_id,$send_cross_subnet_message_destination_address,($erc20_contract_address_b,$send_cross_subnet_message_fee_amount),$send_cross_subnet_message_required_gas_limit,[],$send_cross_subnet_message_message_data)" --private-key $user_private_key --rpc-url $subnet_b_rpc_url
echo "Got Ids $subnet_a_blockchain_id_hex $c_chain_blockchain_id_hex $subnet_a_subnet_id $c_chain_subnet_id"
cast send $teleporter_contract_address "sendCrossChainMessage((bytes32,address,(address,uint256),uint256,address[],bytes))(uint256)" "($send_cross_subnet_message_destination_chain_id,$send_cross_subnet_message_destination_address,($erc20_contract_address_b,$send_cross_subnet_message_fee_amount),$send_cross_subnet_message_required_gas_limit,[],$send_cross_subnet_message_message_data)" --private-key $user_private_key --rpc-url $subnet_a_subnet_id

retry_count=0
received=$(cast call $teleporter_contract_address "messageReceived(bytes32,uint256)(bool)" $subnet_b_blockchain_id_hex $startID --rpc-url $subnet_a_rpc_url)
received=$(cast call $teleporter_contract_address "messageReceived(bytes32,uint256)(bool)" $subnet_a_blockchain_id_hex $startID --rpc-url $c_chain_rpc_url)
until [[ $received == "true" ]]
do
if [[ retry_count -ge 10 ]]; then
Expand All @@ -133,7 +133,7 @@ do
echo "Waiting for destination on subnet A chain to receive message ID $startID. Retry count: $retry_count"
sleep 3

received=$(cast call $teleporter_contract_address "messageReceived(bytes32,uint256)(bool)" $subnet_b_blockchain_id_hex $startID --rpc-url $subnet_a_rpc_url)
received=$(cast call $teleporter_contract_address "messageReceived(bytes32,uint256)(bool)" $subnet_a_blockchain_id_hex $startID --rpc-url $c_chain_rpc_url)
retry_count=$((retry_count+1))
done

Expand Down

0 comments on commit d38c26d

Please sign in to comment.