Contract Deployment Examples
Deployment examples for OVM 2.0
Please refer to the Github Examples Repo. There are currently three examples and we are adding more every few days:
  1. 1.
    hardhat-simple-storage - shows how to deploy a storage contract to Rinkeby Boba.
  2. 2.
    init-fund-l2 - shows how to deposit funds from L1 to L2, on Rinkeby Boba
  3. 3.
    truffle-erc20 Simple ERC20 Token Truffle Tutorial
We'll work though one of those examples in more detail.

Simple ERC20 Token Truffle Tutorial

Hi there! Welcome to our ERC20 Truffle example. If you're interested in writing your first L2 smart contract using Truffle as your smart contract testing framework, then you've come to the right place. This repo serves as an example for how go through and compile/test/deploy your contracts on Ethereum and the Boba L2.
Let's begin.

Step 1: Compile your contracts

Compiling a contract for Boba is identical to compiling a contract for Ethereum mainchain. Notably, all standard solidity compiler versions can be used. For this ERC20, we will use 0.6.12. Create a truffle-config.js and add the following to it:
1
const HDWalletProvider = require('@truffle/hdwallet-provider')
2
3
require('dotenv').config()
4
const env = process.env
5
6
const pk_1 = env.pk_1
7
const pk_2 = env.pk_2
8
9
module.exports = {
10
contracts_build_directory: './build',
11
networks: {
12
boba_rinkeby: {
13
provider: function () {
14
return new HDWalletProvider({
15
privateKeys: [pk_1, pk_2],
16
providerOrUrl: 'https://rinkeby.boba.network',
17
})
18
},
19
network_id: 28,
20
host: 'https://rinkeby.boba.network',
21
}
22
},
23
compilers: {
24
solc: {
25
version: '0.6.12',
26
},
27
},
28
}
Copied!
Now add a .env file that follows the format of env.example with two private keys. NOTE: these accounts must be funded, i.e. contain enough Rinkeby ETH to cover the cost of the deployment. Then,
1
yarn compile
Copied!
Yep, it's that easy. You can verify that everything went well by looking for the build directory that contains your new JSON files. Now let's move on to testing!

Step 2: Testing your contract

Woot! It's time to test our contract. Since the JSON RPC provider URL (for Boba Rinkeby) has already been specified in your Truffle config file, all we need to do next is run the test command. Run:
1
yarn test:integration
Copied!
You should see a set of passing tests for your ERC20 contract.
1
$ truffle test ./test/erc20.spec.js --network boba_rinkeby --config truffle-config.js
2
Using network 'boba_rinkeby'.
3
4
Compiling your contracts...
5
===========================
6
> Everything is up to date, there is nothing to compile.
7
8
Contract: ERC20
9
✓ creation: should create an initial balance of 10000 for the creator (562ms)
10
✓ creation: test correct setting of vanity information (1697ms)
11
✓ creation: should succeed in creating over 2^256 - 1 (max) tokens (2350ms)
12
✓ transfers: ether transfer should be reversed. (1699ms)
13
✓ transfers: should transfer 10000 to accounts[1] with accounts[0] having 10000 (1986ms)
14
✓ transfers: should fail when trying to transfer 10001 to accounts[1] with accounts[0] having 10000 (664ms)
15
✓ transfers: should handle zero-transfers normally (570ms)
16
✓ approvals: msg.sender should approve 100 to accounts[1] (1981ms)
17
✓ approvals: approve max (2^256 - 1) (2124ms)
18
✓ events: should fire Transfer event properly (1413ms)
19
✓ events: should fire Transfer event normally on a zero transfer (1424ms)
20
✓ events: should fire Approval event properly (1603ms)
21
22
23
12 passing (43s)
24
25
✨ Done in 53.27s.
Copied!
If so, congrats! You're ready to deploy an application to Boba. It really is that easy.

Step 3: Deploying your Contract

Now we're going to deploy a contract using truffle. For Truffle based deployments, we're going to use Truffle's migrate command to run a migrations file for us that will deploy the contract we specify.
First, let's create that migrations file. Create a new directory called migrations in the topmost path of your project and create a file within it called 1_deploy_ERC20_contract.js.
Next, within 1_deploy_ERC20_contract.js, we're going to add the following logic:
1
const ERC20 = artifacts.require('ERC20')
2
3
module.exports = function (deployer, accounts) {
4
const tokenName = 'My Optimistic Coin'
5
const tokenSymbol = 'OPT'
6
const tokenDecimals = 1
7
8
// deployment steps
9
deployer.deploy(
10
ERC20,
11
10000,
12
tokenName,
13
tokenDecimals,
14
tokenSymbol
15
)
16
}
Copied!
Now we're ready to run our migrations file! Let's go ahead and deploy this contract:
1
yarn deploy
Copied!
After a few seconds your contract should be deployed. Now you'll see this in your terminal:
1
$ yarn deploy
2
yarn run v1.22.15
3
$ truffle migrate --network boba_rinkeby --config truffle-config
4
5
Compiling your contracts...
6
===========================
7
> Everything is up to date, there is nothing to compile.
8
9
10
11
Starting migrations...
12
======================
13
> Network name: 'boba_rinkeby'
14
> Network id: 28
15
> Block gas limit: 11000000 (0xa7d8c0)
16
17
18
1_deploy_ERC20_contract.js
19
==========================
20
21
Replacing 'ERC20'
22
-----------------
23
> transaction hash: 0xe7cc5d048ffd426587b7d9c89aed4b0d7b2bd29c5532300bce8a9a57a4c4d689
24
> Blocks: 0 Seconds: 0
25
> contract address: 0xE769105D8bDC4Fb070dD3057c7e48BB98771dE15
26
> block number: 6270
27
> block timestamp: 1635787822
28
> account: 0x21724227d169eAcBf216dE61EE7dc28F80CF8A92
29
> balance: 0.901997296123301024
30
> gas used: 855211 (0xd0cab)
31
> gas price: 0.02 gwei
32
> value sent: 0 ETH
33
> total cost: 0.00001710422 ETH
34
35
> Saving artifacts
36
-------------------------------------
37
> Total cost: 0.00001710422 ETH
38
39
40
Summary
41
=======
42
> Total deployments: 1
43
> Final cost: 0.00001710422 ETH
44
45
46
✨ Done in 10.11s.
Copied!
That's pretty much it. Contracts deployed! Tutorial complete. Hopefully now you know the basics of working with Optimistic Ethereum v2 and Boba! 🅾️

Troubleshooting

Example project not working? [Create a Github Issue]https://github.com/omgnetwork/optimism-v2/issues).
Last modified 2mo ago