In this Optimism tutorial we will be looking at what Optimism layer 2 is, how to use it and how to develop smart contracts on the Ethereum layer 2.
- What is Optimism & Layer 2?
- How To Use Optimism On Ethereum [Video]
- How To Bridge Eth Via Optimism Gateway
- Uniswap v3 Alpha On Optimism
- Optimism Developer Experience
What is Optimism & Layer 2?
Optimism is a layer 2 scaling solution for Ethereum which offers lower transaction fees. For consumers who are used to switching between networks in metamask it provides another network to the ever expanding list of EVM (Ethereum virtual machine) compatible blockchains.
OΞ works by moving transactional and computation processes off the mainnet chain.
On Optimism solidity smart contract code is built using a custom compiler to generate OVM (Optimism Virtual Machine) bytecode. This is then sent to a node operating on the Optimistic network where it gets deployed to an address much like on L1 (Layer 1 or Ethereum Mainnet). Transactions to the smart contract are aggregated and stored in a transaction inbox on L1. The state changes of these transactions are computed by the Optimism virtual machine and the resulting state is pushed forward to the next block.
Optimism differs from Arbitrum in that single round fraud proofs are executed on L1 providing faster resolution.
From the testing that I’ve done on both Optimism and Arbitrum testnets Optimism seems to have better infrastructure setup and is notably faster to confirm transactions.
How To Use Optimism On Ethereum [Video]
This is a video which demonstrates how to bridge assets across to layer 2 and swap ETH for SNX via Uniswap v3 on Optimism.
How To Bridge Eth Via Optimism Gateway
This Optimism Tutorial uses Metamask which can be downloaded here: https://metamask.io
I’d always recommend starting with a testnet transaction to get used to moving funds around. In this demo I’ll be using the Kovan Test Network as layer 1 and the Optimism-Kovan Test Network for layer 2. Kovan testnet ETH and tokens are play money and have no real value.
Start by opening up Metamask and selecting “Kovan Test Network” from the drop down list of networks in metamask. This will be our layer 1 Ethereum testnet. If you have zero balance google “kovan testnet faucet” and get some free ETH to zap around.
Now let’s bridge some ETH across to Optimism:
It’s a very simple swap style platform with an excellent user experience. Connect metamask in the top right corner then enter the amount of ETH in the top input which will be deposited 1:1 on to the Optimism L2. Sign the transaction in Metamask and wait for it to go through (it often goes through in less than 5 mins).
Once the transaction is confirmed we can add the Optimism L2 to metamask. Use these links to add the Optimism Testnet (Kovan) and Optimism Mainnet
Switch the network to “Optimism Ethereum (Kovan)” from the dropdown list in Metamask and we should see our funds deposited and ready to use on layer two.
The process of sending real funds is the same except you use Ethereum mainnet for the layer 1. Bare in mind that the network is still new and that carries some risk and be aware of the delay period for withdrawals from L2 to L1 which can take up to 7 days.
Uniswap v3 Alpha On Optimism
Uniswap has deployed an alpha release of their popular automated market maker to Optimism. It’s live and ready to go all be it with a disclaimer and get out of jail card for the devs:
“Optimistic Ethereum is a complex Layer 2 scaling solution still in need of rigorous battletesting. At launch, the Optimism team will have upgrade rights over the bridge contract, allowing them to address any bugs that should arise.” *
We can use the funds we deposited to the layer 2 to swap tokens on Uniswap. Simply connect to the Optimism Network from the dropdown list in metamask and go to https://app.uniswap.org
You can then swap ETH for tokens in much the same way as you would normally.
The transaction speed and confirmation time on Optimism is shockingly fast. Transactions are confirmed almost instantaneously which wasn’t something I was expecting. If this scales it potentially opens up new applications for blockchain, dApps and Web3.
We can also add liquidity pools via Uniswap v3’s concentrated liquidity program.
Optimism Developer Experience
In theory compiling smart contracts on Optimism should be very similar to on layer 1.
There’s a custom compiler which can be installed with:
npm install @eth-optimism/solc
Setup a custom truffle config file, details here: https://github.com/ethereum-optimism/optimism-tutorial/tree/main/truffle
And it should be good to go. Except the RPC endpoint requires an Infura API key. Add an existing key and it wont work. You have to sign up for an Optimism add-on pack. This is free (to early adopters) but they still require credit card details, presumably to start billing at a later date which isn’t transparent.
If you have time then you may be able to set up your own node or provide card details if you trust Infura with that information. For me this was enough to put me off deploying a simple smart contract like we did in the Arbitrum Tutorial.
If you do develop on Optimism note that some of the system variables aren’t available. More details here: https://community.optimism.io/docs/protocol/evm-comparison.html#missing-opcodes
Update 18th July 2021 – From Michael Wuehler who is co-founder of Infura
I saw your feedback on Optimism. We don’t currently have any date for when the add-on would be charged. We certainly will be notifying everyone well in advance and not automatically charging for Optimism, or any other of the add-ons that are currently free, without proper notice.
We want to enable maximum experimentation before we start charging so we are working with early adopters to understand their needs and will give everyone ample time to make changes to their account prior to enabling billing.
Optimism is live and in production. It’s the first time we have been able to use the latest Uniswap application on a layer 2. It will be interesting to see how the ecosystem evolves and how much funds will move across to layer 2’s.
It’s possible that rollups suck liquidity from popular alternative EVM blockchains like Binance Smart Chain and Polygon. There are likely to be plenty of opportunities for yield farming, speculating and investing on these layer 2 platforms as the product matures.
The infura API key thing was the only negative I found with the network. Transaction speeds were surprisingly quick and this could potentially open up new applications that rely on near instant confirmations.
Optimism is a great brand that fits in not only with the underlying technology but the general optimistic narrative around Ethereum and it’s layer 2 scaling solutions.