James Bachini

Arbitrum Tutorial | How To Use The Ethereum Layer 2 Solution

Arbitrum Tutorial

This Arbitrum Tutorial explores what Arbitrum is and how it works, the opportunities layer 2’s present for investors and developers, before testing the deployment of smart contracts on the Arbitrum network and calculating gas fee reductions.

  1. What Is Arbitrum?
  2. Opportunities On Arbitrum
  3. Arbitrum Tutorial
  4. How Does Arbitrum Work?
  5. When Will Arbitrum Launch?
  6. How To Use Arbitrum
  7. Arbitrum vs Optimism
  8. Arbitrum Smart Contracts
  9. Arbitrum Developer Tutorial
  10. How Much Are Fees On Arbitrum?
  11. Evolution of the EVM Landscape

What Is Arbitrum?

Arbitrum is a layer 2 scaling solution for the Ethereum network. The L2 has opened it’s mainnet for developers in late May and is gearing up to launch with the promise of lower fees and greater transaction capacity.

In a later section we will look at how Arbitrum works but for consumers it will act as a separate chain you can switch to in metamask or trust wallet and interact with various DeFi protocols. Uniswap, Etherscan, Chainlink, Sushiswap, Cream Finance etc. are all launching their products on the Arbitrum L2 chain.

A user will transfer funds from an Ethereum wallet to Arbitrum L2 using https://bridge.arbitrum.io/

They can then select the Arbitrum chain when using a DeFi platform like Uniswap for example and exchange tokens with gas fees estimated at around 50x lower than the main Ethereum chain.


Opportunities On Arbitrum

In the last twelve months we’ve seen three distinct opportunities for yield farming.

  1. First on the Ethereum mainnet during the summer of DeFi in 2020
  2. Then around the start of 2021 a lot of funds migrated to Binance Smart Chain
  3. In spring 2021 we saw the emergence of Polygon (Matic) as a key player in the EVM ecosystem.

Binance Smart Chain really proved the need for lower fees and greater transaction throughput. Users were willing to overlook the obvious, and to be fair transparent, centralisation flaws to engage with a cheap DeFi network.

Polygon in many ways was the next step forwards and offered a more technically appealing side-chain. This led to strong adoption and growth in terms of users and funds.

Arbitrum is the next leap forwards providing an optimistic rollup solution which interoperates more closely with the main Ethereum chain and provides a better answer to the blockchain trilema of decentralisation, scalability and security. For end users there wont be much difference in using Arbitrum to using BSC or Polygon but most developers will agree it’s better from a technical perspective and funds will potentially flow towards the best solution.

With Uniswap confirming their support for both Arbitrum and Optimism (another layer 2 optimistic rollup expected to be released this summer) we are likely to see significant amount of yield farming protocols migrated and launched on these layer 2 networks.

The potential is growing to have another DeFi boom cycle where investors and traders are using Arbitrum to swap funds for the latest and greatest tokens, supplying liquidity providers with fees and yield farming networks with the percentages to attract investment.

If Ethereum proved demand for decentralised finance, Binance Smart Chain proved demand for lower fees, Polygon proved demand for Ethereum sub-chains then the launch of Arbitrum could be a pivotal moment when all these things come together in a compelling package.

Arbitrum isn’t launching a native token, fees are paid for in Ethereum. So where is the opportunity? What’s good for layer 2’s is likely going to be good for Ethereum (ETH). Lower transaction fees could increase usage for existing DeFi protocols such as Uniswap (UNI) and Sushiswap (SUSHI). DeFi trading of synthetic assets could become more viable benefiting projects like Synthetix (SNX).

The highest risk/reward opportunity will likely be in new projects launching directly on the layer 2’s where governance tokens are supported by real value creation and platform usage. If we do get another DeFi boom then it will be a particularly lucrative time to be ahead of the money flow.


Arbitrum Tutorial

In this video I demonstrate how to move tokens on to Arbitrum layer 2, then I deploy a smart contract and finally exchange some tokens using Uniswap on the Arbitrum network.

James On YouTube

How Does Arbitrum Work?

On the Ethereum mainnet smart contract code runs within the Ethereum virtual machine (EVM) which is executed by nodes across the Ethereum network. On Arbitrum the same smart contract code is compiled into individual programs using a custom compiler. Transactions interact with the smart contracts on layer 2 via a transaction inbox which is stored on the Ethereum mainnet providing a decentralized network of Arbitrum validator nodes the opportunity to execute transactions modifying the persistent state of the network (who owns what).

Computation of transactions are bulked together and happen off the main Ethereum chain. Only the inbox and the state are stored on Ethereum’s mainnet. This enables far more transactions and reduces the gas fees needed to execute each transaction but still provides a decentralised trustless layer 2 network.

How Optimistic Roll Ups Work

A server can be setup using ArbOS to run a validator node which will carry out execution, track state in an account table and stake funds by bonding them to their version of the current state. Staking actions cannot be undone but can be moved forwards as the chain progresses. If two validators disagree on the current state then they can launch a dispute. A dispute resolution procedure then takes place and the validator in the wrong risks losing their staked funds.

This provides a trustless network where anyone can run a node which is a direct contrast to CeDeFi protocols like Binance Smart Chain where nodes are run by trusted internal parties only.

Arbitrum and Optimism are both optimistic rollups, let’s look at what this term actually means.

What is an Optimistic Roll Up?

A roll up works by collecting layer 2 transactions into an inbox and recording that as call data on layer 1. The incoming transactions are public and deterministic meaning that the outcome is always the same. If Alice sends Bob 5 tokens then that transaction will have a provable effect on the current state (balances and data stored on the network).

Optimistic means that any node can post a roll up block that it determines to be true. If other validators disagree then they can launch a dispute after the block has been posted. When validators post a block they provide a stake in the form of ETH which will be transferred to the disputing nodes account if the block is actually invalid.


When Will Arbitrum Launch?

Arbitrum is developed by an organisation called Offchain Labs who have been working on the current code base since at least 2019. The mainnet was launched for developer access on May 28th 2021 and it’s expected to launch fully at some point in July.

That’s all the information I have currently. It’s expected imminently as long as there are no major issues with the developer rollout.

I signed up for the developer roll out early and have also tried contacting some of Offchain Labs employees but haven’t been given any additional information.

…soon


How To Use Arbitrum

To use Arbitrum you’ll need to setup your wallet and move some funds across from mainnet using the bridge. For this demo I’ll be using metamask.

Let’s start by adding the Arbitrum Goerli Testnet to Metamask using chainlist:

https://chainlist.org/chain/421613

If you need some testnet ETH tokens then use a Goerli faucet such as this one from Arbitrum (requires an account):

https://goerlifaucet.com/

We can now go to the ethBridge and transfer some play money ETH across to see how that works.

https://bridge.arbitrum.io/

Connect Metamask and ensure you are connected to the Goerli Testnet (Not Arbitrum Goerli we just set up) because we want to transfer tokens from layer 1 (Goerli ) to layer 2 (Arbitrum Goerli). We can then go ahead and deposit ETH funds to our layer 2 account which will use the same address.

Arbitrum Tutorial Bridge

Once the deposit-l2 transaction goes through we can switch the network back to Arbitrum Goerli and it will show our funds in our Metamask wallet. We have successfully transferred Ethereum from layer 1 to layer 2.

To do this on mainnet we will either need an approved address (currently restrictions due to developer only access) or to wait for the official launch. However the process is exactly the same except we user the main Ethereum network for layer 1 and Arbitrum mainnet details for layer 2.

Once we have the funds in a metamask wallet on layer 2 we can move them around and interact with DeFi applications that support Arbitrum as we do normally.


Arbitrum vs Optimism

Both are soon to be released layer 2 sub-chains which use optimistic rollup technology. From a technical perspective the main difference is the fraud proof mechanism which comes into effect when there is a dispute.

On Optimism single round fraud proofs are executed at the layer 1 level providing instant resolution but limiting Layer 2 gas to the standard block gas limit. Arbitrum uses multi round fraud proofs where only the disputed block is executed on-chain providing the fraud proof.

This effectively means that Arbitrum is cheaper to use but slower to reach finality due to the need to wait for dispute challenges. From a user perspective this will mostly affect withdrawals from L2 back to L1 which could take up to a week on both networks.

Code is compiled differently on each network. On Arbitrum validator nodes do automatic EVM to AVM (Arbitrum virutal machine) compilation. On Optimism a custom Solidity compiler is required to generate OVM (Optimism virtual machine) bytecode.

Also noteworthy zk rollups (zero knowledge) these differ in that they do not assume validity and submit a proof for each block. ZKSync is a project building technology using zk rollups which is expected later in 2021.


Arbitrum Smart Contracts

Arbitrum supports standard contract deployment using existing tools such as Truffle and Hardhat.

Smart contracts on Arbitrum are written using Solidity in the same way as on the Ethereum mainnet.

There is a tutorial on Solidity development here: Solidity Tutorial | For Developers Coming From Javascript, C++, Python

What this means is that existing DeFi infrastructure can be quickly ported from Ethereum to Arbitrum.

It is possible to communicate between layer 1 and layer 2 smart contracts using retryable tickets.


Arbitrum Developer Tutorial

This section assumes previous experience working with Solidity and Ethereum. Check out this tutorial first if that is not the case: Solidity Tutorial | For Developers Coming From Javascript, C++, Python

For demonstration purposes we will be using a standard hello world solidity contract and deploying it with Truffle to Arbitrum.

The following need to be installed if they aren’t already:-

Once nodeJS is installed we can install truffle and the other modules using npm with the following command

npm install -g truffle
npm install arb-ethers-web3-bridge
npm install @truffle/hdwallet-provider

We also need an account on Arbitrum with some funds in it and a mnemonic key for that account. These funds in ETH will be used to Arb Gas fees. To generate a new testnet account I used Ganache which is available on the Truffle Suite website and used the mnemonic and first public key address provided. I then sent funds from the ArbRinkeby testnet account we created earlier. Once we have transferred some funds across we can check they are in our new account on the ArbRinkeby Explorer at: https://rinkeby-explorer.arbitrum.io/

Now let’s go ahead and create a new directory and initialise a truffle application

mkdir arbitrum-test
cd arbitrum-test
truffle init

Change the filename on contracts/Migrations.sol to HelloArbitrum.sol and paste in the following Solidity code overwriting the existing:-

pragma solidity >=0.7.0 <0.9.0;

contract HelloArbitrumContract {
  string public myStateVariable = 'Hello Arbitrum';
}

Now open up the file migrations/1_initial_migration.js and paste in the following overwriting the existing code:-

const HelloArbitrumContract = artifacts.require("HelloArbitrumContract");

module.exports = function (deployer) {
  deployer.deploy(HelloArbitrumContract);
};

Now let’s edit the truffle-config.js file.

Around line 20 let’s add the following modules and mnemonic.

const wrapProvider = require('arb-ethers-web3-bridge').wrapProvider
const HDWalletProvider = require('@truffle/hdwallet-provider')
const mnemonic = 'your mnemonic keyword phrase here';

Then further down around line 40 underneath and inside the networks object, let’s add an arbitrum instance.

arbitrum: {
  provider: function () {
    return wrapProvider(
      new HDWalletProvider(mnemonic, 'https://rinkeby.arbitrum.io/rpc')
    )
  },
  network_id: 421611,
  gas: 287853530,
},

Finally around line 100 where it mentions compilers let’s uncomment and set our solidity compiler to the version we are using.

solc: {
  version: "0.8.4",

Now let’s fire up the console and migrate the contract

truffle console --network arbitrum
migrate

If all goes well we can expect a confirmation something like this

Deploy smart contract on Arbitrum

Differences Developing On Arbitrum

System variables can vary compared to L1

  • block.number return an estimate of the L1 block number provided by sequencer
  • tx.gasprice will return the arbGas price

ArbSys is a precompiled contract that provides system functionality

  • Address: 0x0000000000000000000000000000000000000064
  • Example: uint256 txCount = ArbSys(address(100)).getTransactionCount();

Retryable Tickets provide cross chain communications

  • Layer 2 message encoded and delivered by Layer 1, stored in retry buffer to be executed later
  • Useful for depositing and withdrawing assets to Arbitrum

How Much Are Fees On Arbitrum?

Offchain Labs has suggested fees for most transactions on Arbitrum will be around 50x reduced from the mainnet fees.

The transaction above used a gas price of 20 gwei which is roughly the same as the current Ethereum gas price however some transactions on the explorer were going through at much lower rates. Also this is testnet so experiences and gas prices might fluctuate widely when this goes live.

I managed to deploy an ERC20 token to Arbitrum at contract address: 0x128FE859c6EE9912bE1bd7c78815f1f3cf1A3fEf

It cost 0.0026 Eth or just over $5 to approve and create the initial liquidity pool on Uniswap V3 which is usable with the ArbRinkeby testnet just by going to https://app.uniswap.org and selecting the network on metamask.

Once the liquidity pool was set up I could test swaps to get an idea of how much it’ll cost per transaction to use Uniswap.

Uniswap on Arbitrum

Here’s the pricing from the tx hash on the block explorer

Arbitrum gas prices and fees

A standard ETH to ERC20 token swap cost 0.0001415 ETH or $0.29 USD

Interestingly the same swap on the Rinkeby layer 1 testnet was going through with a gas fee of 0.000183 ETH. This went through using a default 1 gwei gas price which may not be realistic on mainnet.

There is also a gas fee calculator for Optimism which may provide some insights https://optimism.io/gas-comparison

Conclusion: I believe the gas fees will work out between $0.2 and $2 per swap on an AMM like Uniswap once Arbitrum goes live.

Gas fees will fluctuate on Arbitrum in line with Ethereum gas prices as ArbGas is used to pay for the layer 1 Arbitrum contract transactions.


Conclusion: Evolution of the EVM Landscape

Arbitrum will be the first real layer 2 solution to launch on Ethereum. It promises lower transaction fees which could increase adoption and reignite the DeFi movement. Later this year we will see other layer 2 solutions launching such as Optimism and ZKSync.

Multichain is quickly becoming a thing and recent changes to Metamask and other wallets have made it easier than ever to add new networks and switch between them seamlessly.

It’s possible even that Ethereum becomes a master chain of sub-chains and demand grows to a point where transactional cost means only layer 2 transactions are viable. For everyday transactions general users will interact solely with layer 2’s which will compete for business and usage.

There’s is a potential threat to any smart contract chains that aren’t EVM compatible such as EOS, Neo and Solana. As a developer would you be compelled to learn a new language and system to deploy on a single chain or code in Solidity and have access to an entire ecosystem of networks, layer 2’s and side-chains?

Already we have Ethereum, Binance Smart Chain and Polygon which are all EVM compatible and attracting large amounts of investment and user activity. This EVM multi-chain ecosystem looks set to at least double over the next 12 months with Arbitrum, Optimism and ZKSync getting ready to launch.

There will likely be more too in the future as demand grows and technology improves. Arbitrum called their mainnet Arbitrum One suggesting they are already thinking about Arbitrum Two or perhaps opening up the possibility of horizontal scaling.

This is new technology and there are always risks, especially from new attack vectors which layer 2’s may open up. At this stage we don’t know how robust the dispute resolution procedure is for optimistic rollups and what challenges they may face.

Arbitrum will likely provide well needed relief to ease congestion on Ethereum and make many of the DeFi protocols and platforms cost effectively viable for the masses.

Hopefully this will breath new life and optimism in to the DeFi and blockchain sector as whole.


Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.