Using Optimism

Using Optimism

The following highlight how to use Optimism with the SDK, CLI, and/or smart contracts.

Before diving into the details, be sure to check out the section about Optimism in Choosing a Chain, which details the basics about optimistic rollups and various links / resources that may be helpful during development.

SDK

Selecting a chain is rather straightforward when using the SDK. Upon connecting to Tableland, pass the chain as "optimism-goerli":

const tableland = await connect({ chain: "optimism-goerli" })

From there, chain interactions will now leverage Optimism Goerli. For Optimism mainnet, simply do the following:

const tableland = await connect({ chain: "optimism" })

CLI

When using the CLI, you can check the different chains supported, which includes both the Optimism Goerli testnet and mainnet (since the CLI is built on top of the SDK). To use Optimism, specify the chain in a command — for example, with a read query:

> tableland chains # check the chains, optionally
> tableland read "SELECT * FROM healthbot_420_1" --chain "optimism-goerli" 
> tableland read "SELECT * FROM healthbot_10_1" --chain "optimism"

Smart Contracts

Selecting a chain is part of the smart contract deployment process. Since Optimism is EVM compatible, it’s easy to port over contracts that may have been developed on Ethereum or with Solidity since Optimism has full EVM support.

One of the most popular frameworks for deploying smart contracts is hardhat. Although the SDK and CLI examples above didn’t mention this, it is a best practice to use a provider for interacting with a chain. This ensures developers are not throttled and have full control of their deployment process.

When using hardhat, first set up a provider API key as well as a wallet private key. Make sure you store these in a .env that is never shared publicly. Sharing keys publicly could result in the loss of funds or unwanted usage of the API key itself.

In order to run hardhat with Optimism, first set up a project with hardhat using npx hardhat and follow the prompts (e.g., “Create a basic sample project”). Once a project is set up, one could run the following to deploy a contract on Optimism:

npx hardhat run scripts/deploy.js --network "optimism-goerli"

But first, the hardhat.config.js should be configured to resemble the following. Use dotenv as a best practice for accessing local environment variables like PRIVATE_KEY, OPTIMISM_API_KEY, and OPTIMISM_GOERLI_API_KEY. Note that the provider used here is Alchemy, but other providers can also be used with Tableland, too (including Etherscan and Infura):

module.exports = {
	solidity: "0.8.4",
	networks: {
		optimism: {
			url: `https://arb-mainnet.g.alchemy.com/v2/${process.env.OPTIMISM_API_KEY ?? ""}`,
			accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
		},
		"optimism-goerli": {
			url: `https://arb-goerli.g.alchemy.com/v2/${process.env.OPTIMISM_GOERLI_API_KEY ?? ""}`,
			accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
		},
	},
}

← Previous