Documentation
Learn everything about the Theta Network.

To demonstrate how to develop DApps on Theta using the Hardhat suite, we prepared a demo project . This guide will walk you through the steps to deploy and test the demo project on Theta using Hardhat and ethers.js.

Deployment and Testing against Theta Local Privatenet

Setup the Hardhat demo project

The first things you need to do is to clone the demo project repository and install its dependencies:

git clone https://github.com/thetatoken/theta-hardhat-demo
cd theta-hardhat-demo
npm install

Setup Theta local privatenet

Once installed, let's setup the Theta local privatenet with the Theta/Ethereum RPC Adaptor following this guide. The ETH RPC adaptor running at http://localhost:18888/rpc interacts with the javascript code by translating the Theta RPC interface into the ETH RPC interface.

Fund the test accounts with some TFuel

Execute the two commands below to fun the test accounts with some TFuel:

export SEQ=`thetacli query account --address=0x2E833968E5bB786Ae419c4d13189fB081Cc43bab | grep sequence | grep -o '[[:digit:]]\+'`

thetacli tx send --chain="privatenet" --from=0x2E833968E5bB786Ae419c4d13189fB081Cc43bab --to=0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A --tfuel=1000 --password=qwertyuiop --seq=$(($SEQ+1))

thetacli tx send --chain="privatenet" --from=0x2E833968E5bB786Ae419c4d13189fB081Cc43bab --to=0x1563915e194D8CfBA1943570603F7606A3115508 --tfuel=1000 --password=qwertyuiop --seq=$(($SEQ+2))

Deploy the contract to the local privatenet

Then, on a new terminal, go to the repository's root folder and run this to deploy your contract:

npx hardhat run scripts/deploy.js --network theta_privatenet

Run the unit tests against the local privatenet

Run the unit tests against the local privatenet with the command below. The tests are based on the ethers.js and Waffle framework. This is the recommended choice for testing per Hardhat documentation. There is a caveat though for using ethers.js with Theta, please click here for more details.

npx hardhat test --network theta_privatenet

Run the frontend

Finally, we can run the frontend with:

cd frontend
npm install
npm start

Deploy to the Theta Mainnet

First, edit the hardhat.config.js file, replace "11...1" with the actual private key of the deployer wallet (should delete the key after use, do NOT commit the private key to GitHub):

...
    theta_mainnet: {
      url: `https://eth-rpc-api.thetatoken.org/rpc`,
      accounts: ["1111111111111111111111111111111111111111111111111111111111111111"],
      chainId: 361,
      gasPrice: 4000000000000
    },
...

Next, go to the repository's root folder and run this to deploy your contract:

npx hardhat run scripts/deploy.js --network theta_mainnet

Special Notes on Testing with the Ethers.js+Waffle framework

Note 1: Currently the RPC Adaptor does NOT support non-standard methods such as evm_snapshot, evm_revert, and evm_mine. Thus, test cases using Fixtures (e.g. waffle.loadFixture()) are expected to fail when running against the Theta blockchain.

Note 2: Currently the RPC Adaptor returns a generic "evm: execution reverted" messages in most case when the Theta EVM execution fails. Some test cases may check the EVM errors with code like this:

await expect(this.bar.leave("200")).to.be.revertedWith("ERC20: burn amount exceeds balance")

To make these test pass, for now we need to replace the expected error message with "evm: execution reverted" as shown below:

await expect(this.bar.leave("200")).to.be.revertedWith("evm: execution reverted")

Updated about a month ago


What's Next

Remix

Hardhat


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.