Develop with Foundry

Use Foundry to develop and deploy smart contracts on GIWA.

Foundry is a toolkit useful for developing Ethereum applications. It's fast and modular. Once installed, Foundry provides four core developer tools listed below.

  • forge: Smart contract development, testing, deployment, and verification

  • anvil: Run a Local Ethereum Node with Network Forking for Development

  • cast: Contract interaction, sending transactions, and querying chain data

  • chisel: Solidity REPL for rapid prototyping and debugging


Set up development environment

Install Foundry.

curl -L https://foundry.paradigm.xyz | bash
foundryup

After installing the Foundry toolkit, you can use the forge command to create a new Solidity project.

forge init giwa_project
cd giwa_project

The generated giwa_project has the following structure.

giwa_project/
├── foundry.toml    # a file with project configurations
├── lib             # external libraries used in the project
├── script          # scripts for deployment / simulation
├── src             # project source code directory
└── test            # contract test directory

Writing a contract

Let’s start by writing a simple contract.

The code above defines a contract named Giwa, which includes a function called helloGiwa and an event called HelloGiwa.

As explained earlier, high-level Solidity code must be compiled into low-level bytecode before it can run on the EVM. Use the following command to compile the contract:

Writing tests

You can write tests to confirm the contract behaves as intended. With forge, you can write tests in Solidity.

To see the results, run the following:

If you see the output below, it means the test you wrote has passed successfully.


Deploying the contract

With forge, you can write scripts in Solidity to interact with the chain or deploy contracts.

You can deploy a contract directly with the forge create command, without writing a separate deployment script.

However, if you need to deploy multiple contracts at once or manage complex deployment logic, it’s best to create and manage a deployment script as shown above.

To deploy the contract on-chain, set environment variables as follows.

You can import the wallet to use for deployment cast using the following command.

Run the script you wrote to perform the deployment. Execute the script like this:

Low on gas? Claim Test ETH here.

If successful, you should see output like the following.

From the output above, you can see the contract address and verify on the explorer that the contract was deployed successfully. Once deployed and verified, you can interact with it directly through the explorer UI.


Learn more

Read more guides in the Foundry Book.

Last updated