Deploy Your First Contract on Etherlink With Remix IDE and Hardhat
What is Etherlink?
Etherlink is an EVM-compatible layer-2 blockchain with a decentralized sequencer, offering very low fees and MEV protection, powered by Tezos Smart Rollup technology.
What makes Etherlink Special?
Decentralized 🌐: The decentralized sequencer reduces the risk of centralized control and manipulation.
Secure: Built-in MEV protection protects users against exploitation.
Low fees: Think $0.01 per transaction, not $20.
Etherlink uses Smart Rollups on the decentralized Tezos protocol for data availability and will expand to use the Tezos Data Availability Layer
What’s an Optimistic Rollup?
Optimistic rollups are a type of scaling solution for Layer 1 chains — they are referred to as Layer 2 solutions.
They inherit the consensus of Layer 1 chains, for example, Etherlink inherits the governance/consensus of Tezos Layer 1.
Optimistic rollups move computation and state storage off chain.
How to Deploy your first Smart Contract on Etherlink
Connecting Metamask to Etherlink
- Install the metamask chrome extension
- Once installed, we’ll need to let Metamask know how to connect to the Etherlink Testnet.
Click on the network button to change the network;
Click Add network
On the next screen, click Add a network manually and use the following information;
Network name: Etherlink
New RPC URL: https://node.ghostnet.etherlink.com
Chain ID 128123
Currency Symbol XTZ
Block Explorer URL https://testnet-explorer.etherlink.com/
Save the network configurations.
Getting test XTZ on Etherlink
Next, we need to get test XTZ tokens(the native token of Etherlink).
- Head to the Etherlink Faucet
- Connect your wallet on Etherlink (remember to change the network) to metamask.
- Follow the prompts to connect your wallet
- Click the Send 0.1 XTZ button to get test XTZ in your Etherlink wallet
Writing your First Contract
In this tutorial, we’ll write a very simple counter contract that increases the number in the storage by 1 each time the contract is called. We’ll be using the Remix IDE — a simple UI interface that allows us to write and deploy smart contracts compatible with the EVM (Ethereum virtual machine).
- Head over to Remix
- Remix will launch with a sample contract that you can modify. Navigate into Contracts > Storage.sol. Modify the contract in here to use the following code;
// SPDX-License-Identifier: MIT
pragma solidity <0.9.0;
contract Counter{
uint number;
function store (uint num) public{
number = num;
}
function increment () public{
number++;
}
function decrement () public {
number--;
}
function getter () public view returns (uint256) {
return number;
}
}
3. Compile your contract using the Compile (play) button within the Remix IDE. This will generate the bytecode (the code that actually runs on the EVM). You should also notice that 2 new files were created Counter.json and Counter_metadata.json
4. In the left navbar within the Remix IDE, click the Deploy and run transactions button. Change the selected environment from Remix VM to injected provider. This will trigger your metamask wallet to pop up. Select the wallet you’ll like to connect to deploy your transaction — make sure it’s the wallet containing the test 0.1XTZ on the Etherlink network.
Follow the prompt on metamask to connect your wallet to Remix.
5. Lastly, click the Deploy button.
Once deployed, look in the Remix IDE terminal for the contract address of the deployed contract on Etherlink.
Using the Etherlink Explorer
With the contract deployed, we can use the Etherlink Explorer to see more details about the deployed contract. Paste in the contract address into the explorer. Here’s an example of the details of our deployed smart contract on the Etherlink testnet.
Interacting with the Smart Contract using the Exporer UI
We can interact with the smart contract by clicking the contract tab, then write contract. Make sure your wallet is already connected by clicking the connect wallet button in the top right. Then, enter the number you’d like to update the storage to.
Click the write button and confirm the transaction with your wallet.
You can see the number 10 that we wrote to the storage under the read contract tab. The retrieve entrypoint in our smart contract returns the number in the storage.
And that’s how you deploy your first smart contract on Etherlink using the Remix IDE.
Interacting with the Smart Contract using the Remix UI
Under the Deploy and Run tab, you can also interact with the smart contract you’ve deployed on Etherlink.
Click the transact button just under store to write a call the store method.
You can then navigate to the contract page on the Explorer to see the effects of calling that method.
Deploying Your Smart Contract on Etherlink using Hardhat
Hardhat is a JavaScript library that makes it easy to write, compile and deploy EVM contracts. It’s also easily extendable and you can deploy to a custom chain by making changes to the hardhat config
file.
Getting started
- Create a new directory for this project and navigate into it;
mkdir etherlink-demo && cd etherlink-demo
- Install hardhat using
npx install hardhat
and use the default options in the prompts. - Edit
hardhat.config.js
in the root of your project to add the Etherlink testnet like below;
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.24",
networks: {
etherlinkTest: {
url: "https://node.ghostnet.etherlink.com",
accounts: ["YOUR_PRIVATE_KEY_HERE"],
},
},
};
Replace YOUR_PRIVATE_KEY_HERE
with your private key from Metamask. To get your private key from Metamask;
- Click the dropdown with your Etherlink account on Metamask. Then click Account 1 — The default name for your first Etherlink account.
2. Click Account details
3. Click Show private key
4. Hold to reveal your private key. Copy and paste the private key into your project; DO NOT COMMIT THIS KEY INTO A PUBLIC REPO.
Writing your Smart Contract in Hardhat
Head into the contracts folder within the project. Create a new file called Counter.sol
which will hold the smart contract code:
// SPDX-License-Identifier: MIT
pragma solidity <0.9.0;
contract Counter{
uint number;
function store (uint num) public{
number = num;
}
function increment () public{
number++;
}
function decrement () public {
number--;
}
function getter () public view returns (uint256) {
return number;
}
}
Install the solidity extension to get syntax highlighting for Solidity in VSCode
Deploying your Smart Contract in Hardhat
Next, replace the contents of the deploy.js
file in the scripts folder with the code below;
const { ethers } = require("hardhat");
async function deployMyContract() {
const deployedContract = await ethers.deployContract("Counter");
console.log("Contract Address: ", deployedContract.target);
await deployedContract.waitForDeployment();
}
deployMyContract().catch((error) => {
console.error(error);
process.exitCode = 1;
});
The console.log
line will provide the contract address of the deployed contract. You can then use this address to look up more details about the contract using the Etherlink Explorer.
Next, run the command
npx hardhat run --network etherlinkTest scripts/deploy.js
This will deploy the compiled Counter
contract to the Etherlink testnet. You may use the contract address generated to see the contract on the Etherlink explorer.
Interacting with your Smart Contract in Hardhat
In your scripts folder, create a myScript.js
file and add the following lines;
const { ethers } = require("hardhat");
async function callMyContract() {
const MyContract = await ethers.getContractFactory("Counter");
const contract = MyContract.attach(
// The deployed contract address
"0xD4D502bF38Fb667d06B184434295c9f66d486838"
);
const store = await contract.store(201);
console.log("Original Value:", 201);
await contract.increment();
let finalResult = await contract.getter();
console.log("Increment Result:", finalResult);
await contract.increment();
finalResult = await contract.getter();
console.log("Increment Result:", finalResult);
await contract.decrement();
finalResult = await contract.getter();
console.log("Decrement Result:", finalResult);
}
callMyContract().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Next, run the script usingnpx hardhat run - network etherlinkTest scripts/myScript.js
This will run the methods in the smart contract and log out the results like below;
You can view all of these contract interactions on the Etherlink explorer.