Interaction Flow With Fees

Go trhough the complete deployment flow and send an incentivized message.

Now we have all the Smart Contract 📜 set we can start sending incentivized cross-chain messages. Now let complete the flow

  • Deploy sender contract on C-Chain
  • Deploy receiver contract on Dispatch
  • Approve the sender contract to spend ERC20 funds from the message sender address
  • Send the message

Deploy Sender Contract

To deploy the sender contract run:

forge create --rpc-url fuji-c --private-key $PK contracts/interchain-messaging/incentivize-relayer/senderWithFees.sol:SenderWithFeesOnCChain --broadcast
[] Compiling...
[] Compiling 2 files with Solc 0.8.18
[] Solc 0.8.18 finished in 92.98ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407
Transaction hash: 0xb0ff20527818fcc0836944a13ddb3b336b95047d434bb52f6c064ab407950166

Save the Sender Contract Address

export SENDER_ADDRESS=0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407

Deploy Receiver Contract

Now, deploy the receiver contract.

forge create --rpc-url fuji-dispatch --private-key $PK contracts/interchain-messaging/incentivize-relayer/receiverWithFees.sol:ReceiverOnDispatch --broadcast
[] Compiling...
[] Compiling 1 files with Solc 0.8.18
[] Solc 0.8.18 finished in 105.36ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0xe336d36FacA76840407e6836d26119E1EcE0A2b4
Transaction hash: 0x379f0a4b875effc9b80a33f039d9a49404514e7aabaef4490f57b56fb4818f65

Save the Receiver Contract Address

export RECEIVER_ADDRESS=0xe336d36FacA76840407e6836d26119E1EcE0A2b4

Approve ERC20 Token Expense

👩 The Message Sender (User) requires to approve the Cross-Avalanche L1 dApp as a spender on the ERC20 contract to be used as fee. We'll approve 0.0005 tokens (500000000000000 wei) from your total supply of 1,000,000 tokens which matches the fee amount set in the sender contract.

cast send --rpc-url fuji-c --private-key $PK $FEE_TOKEN_ADDRESS "approve(address,uint256)" $SENDER_ADDRESS 500000000000000

Send an incentivized message

cast send --rpc-url fuji-c --private-key $PK $SENDER_ADDRESS "sendMessage(address, string, address)" $RECEIVER_ADDRESS "Hello" $FEE_TOKEN_ADDRESS

Verify message was received on Avalanche L1

cast call --rpc-url fuji-dispatch $RECEIVER_ADDRESS "lastMessage()(string)"
"Hello"

Success! The message was received on the destination chain while incentivizing the Relayer.

Is this guide helpful?