EVM with Foundry

Bitroot EVM Smart Contract Development with Foundry

Since Bitroot is an EVM compatible chain, existing EVM tooling like foundry forge  or other could be re-used.

In this example we will be using foundry tooling .

Install the foundry tooling  by following this Installation guide .

Create a new project following the Creating New Project Guide .

Also make sure you have a wallet on Bitroot network.

Once project is created, tweak the contract code to the following, by adding a getCounter function:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.22;
 
contract Counter {
    uint256 public number;
 
    function setNumber(uint256 newNumber) public {
        number = newNumber;
    }
 
    function increment() public {
        number++;
    }
 
    function getCount() public view returns (uint256) {
        return number;
    }
}
 

And the test code to the following:

Run the tests with the following command:

If tests pass, deploy the contract to the Bitroot chain with the following command:

Where $BITROOT_NODE_URI is the URI of the Bitroot node and $MNEMONIC is the mnemonic of the account that will deploy the contract. If you run local Bitroot node, the address will be http://localhost:8545 , otherwise you could grab a evm_rpc url from the registry . If deployment is successful, you will get the EVM contract address in the output.

Let’s use the cast command to query the contract:

The command should return 0 as the initial value of the counter.

Now we can use the cast command to call the increment function:

If command is successful, you will get the transaction hash and other info back.

Now let’s call the getCount function again and this case it should return 1.

Foundry generates the ABI for the contract in the out folder. You can use this ABI to interact with the contract from other tools like ethers.js or web3.js.

Calling contract using ethers.js

To call or query the contract from a frontend or a NodeJS script, you could use ethers.js like:

Last updated