Please refer to the Github Examples Repo. There are currently three examples and we are adding more every few days:
hardhat-simple-storage - shows how to deploy a storage contract to Habtor testnet.
init-fund-l2 - shows how to deposit funds from L1 to L2, on Habtor testnet
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 BSC mainnet and the Habtor L2.
Let's begin.
Step 1: Compile your contracts
Compiling a contract for Habtor is identical to compiling a contract for BSC mainnet. 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:
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 testnet BNB to cover the cost of the deployment. Then,
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 Habtor testnet) has already been specified in your Truffle config file, all we need to do next is run the test command. Run:
You should see a set of passing tests for your ERC20 contract.
If so, congrats! You're ready to deploy an application to Habtor. 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:
Now we're ready to run our migrations file! Let's go ahead and deploy this contract:
After a few seconds your contract should be deployed. Now you'll see this in your terminal:
That's pretty much it. Contracts deployed! Tutorial complete. Hopefully now you know the basics of working with Habtor network! 🅾️
Troubleshooting
Example project not working? [Create a Github Issue]https://github.com/habtorcom/optimism-v2/issues).
$ truffle test ./test/erc20.spec.js --network habtor_testnet --config truffle-config.js
Using network 'habtor_testnet'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Contract: ERC20
✓ creation: should create an initial balance of 10000 for the creator (562ms)
✓ creation: test correct setting of vanity information (1697ms)
✓ creation: should succeed in creating over 2^256 - 1 (max) tokens (2350ms)
✓ transfers: ether transfer should be reversed. (1699ms)
✓ transfers: should transfer 10000 to accounts[1] with accounts[0] having 10000 (1986ms)
✓ transfers: should fail when trying to transfer 10001 to accounts[1] with accounts[0] having 10000 (664ms)
✓ transfers: should handle zero-transfers normally (570ms)
✓ approvals: msg.sender should approve 100 to accounts[1] (1981ms)
✓ approvals: approve max (2^256 - 1) (2124ms)
✓ events: should fire Transfer event properly (1413ms)
✓ events: should fire Transfer event normally on a zero transfer (1424ms)
✓ events: should fire Approval event properly (1603ms)
12 passing (43s)
✨ Done in 53.27s.