One of the most popular blockchains in use today is the Ethereum blockchain. At the time of writing, there are 3,000 decentralized applications (dApps) on the blockchain, and there are many more in development.
Furthermore, Ethereum's ability to power the Decentralized Finance (Defi) space also points to the potential for a massive increase in the number of applications and users on the chain in the future. The growing number of applications and users on the Ethereum blockchain is responsible for increasing network load and the capacity of the chain is limited. Therefore, the cost of using the blockchain increases significantly as more and more users compete to add transactions to the Ethereum blockchain.
Due to the increasing number of users and applications on the network, various solutions are being developed and tested to achieve scalability. Improving the network's transactions per second (TPS) of just 13–17 TPS is one of Ethereum's main scalability objectives.
Although the Ethereum network has undergone a transition from a Proof-of-Work (PoW) consensus mechanism to a Proof-of-Stake (PoS) consensus mechanism, this upgrade is termed "The Merge". Co-founder and lead developer of the Ethereum Blockchain, Vitalik Buterin claimed that after "The Merge," the Ethereum blockchain will process up to 100,000 TPS.
However, this transition from a Proof-of-Work (PoW) consensus mechanism to a Proof-of-Stake (PoS) consensus mechanism whilst it increases TPS, it does not solve the problem of excessive gas prices on the Ethereum Mainnet. In this article, I will help you understand why Ethereum's Layer 2 solutions are needed, and the different types of Rollup technology used to scale the Ethereum Blockchain. I will also help you understand Ethereum's prime Layer 2 solution; Arbitrum, and how to deploy a Smart-contract on the Arbitrum network.
Prerequisites
To follow along with this tutorial, you’ll need the following:
- A text editor
- Node.js installed locally on your machine
- Experience with any C-based programming Language (you can still gain from the article if you do not have one)
There’s no need to have any prior knowledge of Arbitrum. This article will introduce what Arbitrum is all about and build on those in the demonstration section. Let’s start with a little background on Ethereum Layer 2 and Arbitrum.
What are Ethereum Layer 2 solutions?
Layer 2 is a term used for solutions designed to scale an application by allowing transactions to be processed outside of the Ethereum Mainnet while maintaining the same security measures and decentralization as the Ethereum Mainnet. Layer 2 solutions increase TPS and reduce gas charges.
Rollups are one such Layer 2 scaling solution, which is simply a method of making a slow blockchain faster and less expensive. Sidechains and state channels are two other scaling solutions. Rollups are classified into two types: Optimistic and Zero-knowledge (ZK).
The advantage of either is that they significantly reduce transaction costs. Instead of waiting and paying for each transaction on Ethereum Mainnet to process transactions independently, dozens of transactions are recorded on the layer 2 chain, then "rolled up" into a single transaction that is then fed back to the more expensive, slower blockchain. By doing so, the cost of a single transaction is distributed among many users.
Optimistic Rollup:
The first type of rollup is known as Optimistic rollup. This is due to the optimistic assumption that all transactions contained within a rollup are valid. Optimistic rollups allow everyone on the network, usually a week, to contest fraudulent transactions. The Optimistic rollup has the advantage of being quick; by assuming transactions are valid and correct, the network does not have to waste time confirming each transaction. Examples of Layer 2 solutions that employ the Optimistic Rollup technology are Arbitrum and Optimism.
Zero-Knowledge Rollup:
A Zero-Knowledge rollup, also known as a ZK-rollup, is the second type of rollup. This roll-up employs a complex piece of cryptography known as a Zero-Knowledge proof to determine the validity of a transaction with minimal information about that transaction. ZKsync and Loopring are examples of Layer 2 solutions that make use of the Zero-Knowledge Rollup technology.
Arbitrum
Arbitrum is a fast, private, secure Layer 2 platform for cryptocurrencies and scalable smart contracts. Arbitrum's scaling solution uses Optimistic Rollups to improve the speed and efficiency of Ethereum-based dApps.
Arbitrum was designed by Off-chain Labs with features for close integration with Ethereum, making it simpler for Solidity programmers to compile their smart contracts. The three primary parts of Arbitrum are validators, compilers, and EthBridge.
- The compiler aids in assembling a variety of Solidity Smart Contracts into an executable compatible with running on Arbitrum Virtual Machine (AVM) or any virtual machine.
- The majority of the off-chain operations, along with confirming the AVM's state, is essentially the responsibility of the validators.
- EthBridge is a decentralized application running on the Ethereum Mainnet, It acts as a bridge between Arbitrum and the Ethereum Mainnet.
Why should you use Arbitrum?
Validators do the majority of Arbitrum's work. Each Arbitrum dApp has its own set of validators. Validators are chosen by the developers. Validators record Arbitrum Virtual Machine (AVM) activity and adhere to code within dApps to ensure the AVM runs properly. Using the Arbitrum platform benefits developers and teams creating dApps in a variety of ways. Some of the advantages are as follows:
- Scalability Scalability has long been a problem for Ethereum-based applications. When attempting to scale the user base of your dApps, network congestion and high fees on the Ethereum network can be exorbitant. Cost and response time can also lead to additional security concerns. Furthermore, Arbitrum-based dApps only interact with the Ethereum Mainnet when digital assets are initiated, sent, or received outside of Arbitrum. All other interactions occur off-chain.
- Privacy: Developers can select validators for each dApp and even control what the dApp's users can see. Although this relies on each dApp's specifications, users' machines also can validate information if they receive it.
Building & Deploying a Smart contract on Arbitrum
Setting up the project
We'll be using Hardhat for development in this article. To use hardhat, we have to install it globally to do this, run the following command
npm i -g hardhat
To set up a project, run the command.
npm init -y
Then install the following dependencies.
npm install hardhat ethers @nomicfoundation/hardhat-toolbox @nomiclabs/hardhat-ethers@^2.0.0 @openzeppelin/contracts @nomicfoundation/hardhat-network-helpers@^1.0.0 @nomicfoundation/hardhat-chai-matchers@^1.0.0 @nomiclabs/hardhat-etherscan@^3.0.0 @types/mocha@^9.1.0 @typechain/ethers-v5@^10.1.0 @typechain/hardhat@^6.1.2 chai@^4.2.0 hardhat-gas-reporter@^1.0.8 solidity-coverage@^0.7.21 ts-node@>=8.0.0 typechain@^8.1.0 typescript@>=4.5.0
Next, we initialize the project by running the following command.
npx hardhat
You should now have an interactive prompt in the command line.
Here, we'll choose to "Create a Javascript project"
Press the return key for the next few prompts. Next, we will install a package called dotenv, to install this package, run the command.
npm i dotenv
dotenv
is a library that allows us to load environment variables by defining those variables inside a .env
file.
Next up, create a .env
file, we'll declare 2 variables inside this file.
PRIVKEY=0x[private key from metamask wallet]
L2_RPC=https://rinkeby.arbitrum.io/rpc
PRIVKEY
is the private key of the Metamask account that will be used to deploy the contract to Arbitrum Layer 2, the private key is prefixed with 0x
in the .env
file.
L2RPC
is the URL of the Layer 2 node where the transaction will be submitted.
Next, we'll set up the hardhat.config.js
file.
Edit the hardhat.config.js
file with the following code.
require("@nomicfoundation/hardhat-toolbox");
const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, "./.env") });
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.9",
networks: {
arbitrum: {
url: process.env.L2_RPC,
accounts: [process.env.PRIVKEY],
},
},
};
Let's go over what these lines of code do.
Line 3 will read and load the environment variables declared in the dotenv file so it can be used in the config file.
Next, we set up the networks, url
is set to the L2_RPC
which is the Layer 2 node to which the transaction will be submitted.
accounts
is set to the PRIVKEY
which is the private key of the wallet carrying out the transaction.
Creating a Contract
Next up we'll create a greeter contract In the contracts folder, create a Greeter. sol file and paste in the following lines of code;
//SPDX-License-Identifier; Unlicensed
pragma solidity ^0.8.0;
contract Greeter {
string private greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}
The next step is to compile the contract we have just written. Open up the terminal and run the following command;
npx hardhat compile
If the contract compiles, you should see a prompt similar to this in the terminal.
Deploying the Contract
To deploy the Greeter contract. In the scripts folder, create a deploy.js file. Next, paste in the following lines of code.
const hre = require("hardhat");
async function main() {
const Greeter = await hre.ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();
console.log("Greeter deployed to:", greeter.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
These lines of code simply help us to deploy the Greeter contract.
We are now set up to deploy the contract, since we are deploying this contract to Arbitrum Layer 2, we need to set up the Arbitrum Rinkenby test-net and also bridge some Rinkenby test eth to the test-net, follow this step-by-step guide to set up the Arbitrum Rinkenby testnet
After setting up the test-net, open up the terminal and run the following command;
npx hardhat run scripts/deploy.js ---network arbitrum
We have just deployed our contract on the Arbitrum network, next step is to verify and publish the contract.
Verify and Publish the contract
Copy the transaction ID from the terminal, which looks like this (0x84b1E0A7a8c26092fF3FD8A8100f4b24E97EFABe).
Next head over to testnet.arbiscan.io paste in the contract address and search.
Search for the contract on Arbiscan.
After searching for the contract, click on the contract and select verify and publish.
Copy and paste the solidity contract code in the text box.
Fill in the options to verify and publish the contract.
Complete the captcha and click verify and publish.
Our contract is now published next step is to interact with the contract.
Interacting with the contract
Now we are set up to interact with our contract.
Click on the Contract Address, next navigate to the contract tab then select Write contract
.
Next click on Connect to Web3
to connect your Metamask wallet.
Next, input the following string in the text box and click Write
"Hello Arbitrum"
Next, confirm the transactions in your Metamask wallet.
After the transaction is confirmed, click on Read contract
to view your transaction, you should see the string Hello Arbitrum
.
Conclusion
If you followed this article to the end, you should now have a good grasp of what Ethereum Layer 2s are, the different types of Ethereum Layer 2 technologies and what Arbitrum is all about, and the problem the Arbitrum Layer 2 chain is looking to solve.
You should also have a working knowledge of how to build smart contracts with a web3 tool like Hardhat and also be able to deploy, verify, publish, and interact with your smart contract through the Arbitrum network. You can build on the knowledge gained from this article to create dapps that interact with the Arbitrum network.