Hello Wormhole
This tutorial contains a solidity contract (HelloWormhole.sol
) that can be deployed onto many EVM chains to form a fully functioning cross-chain application.
Specifically, we will write and deploy a contract onto many chains that allows users to request, from one contract, that a GreetingReceived
event be emitted from a contracts on a different chain.
This also allows users to pay for their custom greeting to be emitted on a chain that they do not have any gas funds for!
Getting Started
Included in the repository is:
Example Solidity Code
Example Forge local testing setup
Testnet Deploy Scripts
Example Testnet testing setup
Environment Setup
Node 16.14.1 or later, npm 8.5.0 or later: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
forge 0.2.0 or later: https://book.getfoundry.sh/getting-started/installation
Testing Locally
Pull the code down from github and cd into the directory, then build and test it.
Expected output is
Deploying to Testnet
You will need a wallet with at least 0.05 Testnet AVAX and 0.01 Testnet CELO.
Testing on Testnet
You will need a wallet with at least 0.02 Testnet AVAX. Obtain testnet AVAX here
You must have also deployed contracts onto testnet (as described in the above section).
To test sending and receiving a message on testnet, execute the test as such:
Explanation of the HelloWormhole Cross-chain Contract
Let’s take a simple HelloWorld solidity application, and take it cross-chain!
Single-chain HelloWorld solidity contract
This single-chain HelloWorld smart contract allows users to send greetings. In other words, it allows them to cause an event GreetingReceived
to be emitted with their greeting!
Taking HelloWorld cross-chain using Wormhole Automatic Relayers
Suppose we want users to be able to request, through their Ethereum wallet, that a greeting be sent to Avalanche, and vice versa.
Let us begin writing a contract that we can deploy onto Ethereum, Avalanche, or any number of other chains, to enable greetings be sent freely between each contract, irrespective of chain.
We'll want to implement the following function:
The Wormhole Relayer contract lets us do exactly this! Let’s take a look at the Wormhole Relayer contract interface.
The Wormhole Relayer network is powered by Delivery Providers, who perform the service of watching for Wormhole Relayer delivery requests and performing the delivery to the intended target chain as instructed.
In exchange for calling your contract at targetAddress
on targetChain
and paying the gas fees that your contract consumes, they charge a source chain fee. The fee charged will depend on the conditions of the target network and the fee can be requested from the delivery provider:
So, following this interface, we can implement sendCrossChainGreeting
by simply calling sendPayloadToEvm with the payload being some information we'd like to send, such as the greeting and the sender of the greeting.
A key part of this system, though, is that the contract at the targetAddress
must implement the IWormholeReceiver
interface.
Since we want to allow sending and receiving messages by the HelloWormhole
contract, we must implement this interface.
After sendPayloadToEvm
is called on the source chain, the off-chain Delivery Provider will pick up the VAA corresponding to the message. It will then call the receiveWormholeMessages
method on the targetChain
and targetAddress
specified.
So, in receiveWormholeMessages, we want to:
Update the latest greeting
Emit a 'GreetingReceived' event with the 'greeting' and sender of the greeting
Note: It is crucial that only the Wormhole Relayer contract can call receiveWormholeMessages
To provide certainty about the validity of the payload, we must restrict the msg.sender of this function to only be the Wormhole Relayer contract. Otherwise, anyone could call this receiveWormholeMessages endpoint with fake greetings, source chains, and source senders.
And voila, we have a full contract that can be deployed to many EVM chains, and in totality would form a full cross-chain application powered by Wormhole!
Users with any wallet can request greetings to be emitted on any chain that is part of the system.
How does it work?
Check out Part 2 for an in-depth explanation of how Wormhole Relayer causes contracts on other blockchains to be called with the appropriate inputs!
Full Cross-chain HelloWormhole solidity contract
See the full implementation of the HelloWormhole.sol contract and the full Github repository with testing infrastructure
Wormhole integration complete?
Let us know so we can list your project in our ecosystem directory and introduce you to our global, multichain community!
Last updated