Using Foundry

Foundry is a smart contract development toolchain that manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the CLI and via Solidity scripts.

See the finished code at https://github.com/thundercore/foundry-thundercore

One Time Setup

Installing Foundry

See the Installation chapter in the Foundry Book.

Creating A Wallet

You can use "cast wallet new" to create a ThunderCore compatible wallet.

Here we name our newly created wallet deployer by moving the encrypted keystore file generated by "cast":

$ k=$(cast wallet new --json ~/.foundry/keystores | jq -r .[0].path)
Enter secret:
$ mv $k ~/.foundry/keystores/deployer

Encrypted Keystores

Foundry generates standard encrypted Ethereum keystore files under $HOME/.foundry/keystores

Private keys in the keystores can be exported with:

 ethkey inspect ~/.foundry/keystores/deployer

by using the `ethkey` utility from go-ethereum.

Obtaining Funds

Create A New Project

To start a new project with Foundry, use forge init:

$ forge init <project_name>

The src folder may already contain Counter.sol, a minimal Solidity contract.

Setup Foundry For ThunderCore

$ cd <project_name>
$ EDIT foundry.toml

Edit foundry.toml to contain:

[profile.default]
evm_version = "london"

[rpc_endpoints]
thunder-mainnet = "https://mainnet-rpc.thundercore.com"
thunder-testnet = "https://testnet-rpc.thundercore.com"

# See more config options
# https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

Compiling

Run the command forge build to build the project.

$ forge build

And test the build with "forge test".

$ forge test

Deploy to ThunderCore Network

Edit script/Counter.s.sol and add "new Counter()" at the end of run():

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {Script, console} from "forge-std/Script.sol";
import "../src/Counter.sol";

contract CounterScript is Script {
	function setUp() public {}

	function run() public {
    		vm.broadcast();
    		new Counter();
	}
}

Use "forge script" to deploy your contract to ThunderCore:

$ forge script --rpc-url thunder-testnet --broadcast --account deployer ./script/Counter.s.sol
[⠰] Compiling...
No files changed, compilation skipped
Enter keystore password:
Script ran successfully.

## Setting up 1 EVM.

==========================

Chain 18

Estimated gas price: 2360 gwei

Estimated total gas used for script: 138734

Estimated amount required: 0.32741224 ETH

==========================
##
Sending transactions [0 - 0].
⠁ [00:00:00] [#] 1/1 txes (0.0s)##
Waiting for receipts.
⠉ [00:00:12] [#] 1/1 receipts (0.0s)
##### 18
✅  [Success]Hash: 0xe459d759617b8f6054646cd0aa37e2c14db66a051747b0c32609b2e3c87c3d78
Contract Address: 0x8133D6f94ae2cB864540BF9a3c736dd6f6F2E426
Block: 1754750
Paid: 0.2454537 ETH (106719 gas * 2300 gwei)



==========================

ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
Total Paid: 0.2454537 ETH (106719 gas * avg 2300 gwei)

Transactions saved to: (...)/broadcast/Counter.s.sol/18/run-latest.json

Check the deployed contract in Explorer

Copy the address of your newly deployed contract from the output. Then go to the ThunderCore Blockchain Explorer, and paste the address in the Search by address field.

Mainnet: https://explorer-mainnet.thundercore.com

Testnet: https://explorer-testnet.thundercore.com

Interact With The Counter Contract

We can read the Counter value via cast call:

$ cast call --rpc-url thunder-testnet <YOUR_CONTRACT_ADDR> 'number()'
0x0000000000000000000000000000000000000000000000000000000000000000

We can increment the Counter value via "cast send":

$ cast send --rpc-url thunder-testnet --account deployer <YOUR_CONTRACT_ADDR> 'increment()'
Enter keystore password:

blockHash           	0x03cdd8cb4bafb1028d3893ac34abd68b2f92a0b13c193dcf4b202812a4d2225d
(...)

After a successful transaction that calls increment , reading the counter value again should now return 1 instead of 0.

Conclusion

In this guide, we've accomplished:

  1. Created a new wallet in the Foundry keystore named deployer ($HOME/.foundry/deployer)

  2. Obtained funds for gas to the deployer account

  3. Created a new Foundry project

  4. Configured that project for ThunderCore via foundry.toml

  5. Deployed our contract to the ThunderCore blockchain

  6. Interacted with our contract to read and write information

And that's the basics of how to develop on ThunderCore via Foundry! Remember the finished code is available at https://github.com/thundercore/foundry-thundercore

Last updated