This tutorial aims to help you get started with developing decentralized applications on Optimistic Ethereum using Hardhat. Applications running on top of Optimistic Ethereum are about as secure as those running on the underlying Ethereum mainnet itself, but are significantly cheaper.
⚠️ We are currently in the process of upgrading to OVM 2.0, but that is still work in progress (expected to end 11 NOV 2021). This tutorial is already upgraded for OVM 2.0, but parts of it might change during the upgrade process.
The fastest way to test and debug apps on Optimistic Ethereum is to run a local Optimistic Ethereum node, so we'll build one. The directions in this section are for an Ubuntu 20.04 VM running on GCP with a 20 GB disk (the default, 10 GB, is not enough) and 16 GB RAM but they should be similar for other Linux versions and other platforms.
-
Install Docker. If you prefer not to use the convenience script shown below, there are other installation methods.
curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh
-
Configure Docker settings.
sudo usermod -a -G docker `whoami`
-
Install Docker Compose.
sudo apt install -y docker-compose
-
Install Node.js. The version in the OS repository is out of date, so we'll get the package from a different source.
curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh sudo bash nodesource_setup.sh sudo apt install -y nodejs
-
Install the Node.js packages we need. See here if you'd rather install these packages without root permissions.
sudo npm install -g yarn hardhat
-
Log out and log back in to complete the Docker installation (required).
This process downloads and starts an Optimistic Ethereum network of one node.
-
Clone the Optimism monorepo. Note that until we upgrade the production network to OVM 2.0 you need to clone the
regenesis/0.5.0
branch.git clone https://github.com/ethereum-optimism/optimism.git -b regenesis/0.5.0
-
Start the Optimistic Ethereum node. This process downloads the images from the Docker hub, and depending on the hardware it can take up to ten minutes.
cd optimism/ops docker-compose -f docker-compose-nobuild.yml up -t 3600
You might get a timeout at first. If that is the case, just run the
docker-compose
command again.
ℹ️ It takes a few minutes for all the processes to start and communicate with each other. If at first you see
curl
failure to connect errors wait a few minutes.
Now that we have Optimistic Ethereum running, it is time to run a decentralized application (dapp) on it.
ℹ️ If you don't need the explanations and just want to see running code, click here. The
hardhat/dapp
directory only requires these steps to run:
yarn
npx hardhat test --network optimistic
The easiest way is to start with a sample application.
- Open a second command line terminal
- Run
hardhat
, the development environment we use in this tutorialmkdir dapp cd dapp npx hardhat
- Select Create a basic sample project and accept all the defaults.
- Verify the sample application.
npx hardhat test
If you want to be more hands on, you can interact with the contract manually.
-
Start the console
npx hardhat console
-
Deploy the greeter contract.
const Greeter = await ethers.getContractFactory("Greeter") const greeter = await Greeter.deploy("Hello, world!") await greeter.deployed()
-
Get the current greeting.
await greeter.greet()
-
Modify the greeting.
const tx = await greeter.setGreeting("Hola, mundo") await tx.wait()
-
Verify the greeting got modified.
await greeter.greet()
-
Leave the console.
.exit
Now that we have a running Optimistic Ethereum node and a dapp to run on it, we can deploy to Optimistic Ethereum.
-
Edit
hardhat.config.js
to addoptimistic
to the list of networks:// hardhat.config.js ... module.exports = { solidity: "0.8.4", networks: { optimistic: { url: 'http://127.0.0.1:8545', accounts: { mnemonic: 'test test test test test test test test test test test junk' } } } };
-
Test the contract on Optimistic Ethereum.
npx hardhat --network optimistic test
-
If you want to interact with the app manually, use the console. You can use the same JavaScript commands to control it you used above.
npx hardhat --network optimistic console
⚠️ Until we deploy to the Kovan test network (planned for 14 OCT 2021), this section is not relevant
To deploy to a real network (Optimistic Ethereum or Optimistic Kovan),
edit hardhat.config.js
's modules.export.networks
to add a definition
similar to this one:
"optimistic-kovan": {
url: 'https://kovan.optimism.io',
accounts: { mnemonic: <your account mnemonic goes here> }
}
As you may have noticed, in this tutorial we ran all the tests first on the HardHat EVM and only then on Optimistic Ethereum. This is important, because it lets you isolate contract problems from problems that are the result of using Optimistic Ethereum rather than vanilla Ethereum.
This tutorial has only touched the most basic points of Optimistic Ethereum development. For more information, you can check out the full integration guide on the Optimism community hub. Go read it, and then write a dapp that will amaze us.