⚔

Foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.

What is Foundry?

​Foundry is an Ethereum development environment written in Rust that helps developers manage dependencies, compile projects, run tests, deploy contracts, and interact with blockchains from the command line. Foundry can directly interact with Gitshock's Ethereum API so it can be used to deploy smart contracts into Gitshock. There are three tools that make up Foundry:
  • ​Forge - compiles, tests, and deploys contracts
  • ​Cast - a command line interface for interacting with contracts
  • ​Anvil - a local TestNet node for development purposes that can fork preexisting networks

Creating a Foundry Project

You will need to create a Foundry project if you don't already have one. You can create one by completing the following steps:
  1. 1.
    Install Foundry if you haven't already. If on Linux or MacOS, you can run these commands:
    curl -L https://foundry.paradigm.xyz | bash
    foundryup
    If on Windows, you'll have to install Rust & then build Foundry from source:
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs/ | sh
    cargo install --git https://github.com/foundry-rs/foundry foundry-cli anvil --bins --locked
  2. 2.
    Create the project, which will create a folder with three folders within it:
    forge init foundry

The Source Folder

The src folder may already contain Contract.sol, a minimal Solidity contract. Feel free to delete it. Instead, you will be deploying an ERC-20 contract. In the contracts directory, you can create the MyToken.sol file:
cd src
touch MyToken.sol
Open the file and add the following contract to it:
pragma solidity ^0.8.0;
​
// Import OpenZeppelin Contract
import "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
​
// This ERC-20 contract mints the specified amount of tokens to the contract creator
contract MyToken is ERC20 {
constructor(uint256 initialSupply) ERC20("MyToken", "MYTOK") {
_mint(msg.sender, initialSupply);
}
}
Before you attempt to compile, install OpenZeppelin contracts as a dependency. You may have to commit previous changes to git beforehand. By default, Foundry uses git submodules instead of npm packages, so the traditional npm import path and command are not used. Instead, use the name of OpenZeppelin's Github repository:
forge install OpenZeppelin/openzeppelin-contracts --no-commit

Compiling Solidity

Once all dependencies have been installed, you can compile the contract:
forge build
After compilation, two folders will be created: out and cache. The ABI and bytecode for your contracts will be contained within the out folder. These two folders are already ignored by the .gitignore included in the default Foundry project initialization.

Deploying the Contract

Deploying the contract with Forge takes a single command, but you will need to include an RPC endpoint, a funded private key, and constructor arguments. MyToken.sol asks for an initial supply of tokens in its constructor, so each of the following commands include 100 as a constructor argument. You can deploy the MyToken.sol contract using the command for the correct network:
Gitshock Buitenzorg
Gitshock Cartenz
forge create --rpc-url https://rpc-testnet.gitshock.com/buitenzorg \ --constructor-args 100 \ --private-key YOUR_PRIVATE_KEY \ src/MyToken.sol:MyToken
forge create --rpc-url https://rpc-cartenz.gitshock.com \ --constructor-args 100 \ --private-key YOUR_PRIVATE_KEY \ src/MyToken.sol:MyToken
Congratulations, your contract is live! Save the address, as you will use it to interact with this contract instance in the next step.

Interacting with the Contract

Foundry includes cast, a CLI for performing Ethereum RPC calls.
Try to retreive your token's name using cast, where YOUR_CONTRACT_ADDRESS is the address of the contract that you deployed in the previous section:
cast call YOUR_CONTRACT_ADDRESS "name()" --rpc-url RPC-API-ENDPOINT-HERE
You should get this data in hexidecimal format:
0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000074d79546f6b656e00000000000000000000000000000000000000000000000000
This is far from readable, but you can use cast to convert it into your desired format. In this case, the data is text, so you can convert it into ascii characters to see "My Token":
cast --to-ascii 0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000074d79546f6b656e00000000000000000000000000000000000000000000000000
You can also mutate data with cast as well. Try burning tokens by sending them to the zero address.
cast send --private-key YOUR_PRIVATE_KEY \
--rpc-url RPC-API-ENDPOINT-HERE \
--chain 1284 \
YOUR_CONTRACT_ADDRESS \
"transfer(address,uint256)" 0x00000000000000000000000000000000000000011
The transaction will be signed by your Gitshock account and be broadcasted to the network.

Congratulations, you have successfully deployed and interacted with a contract using Foundry!