⚔️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:
Install Foundry if you haven't already. If on Linux or MacOS, you can run these commands:
If on Windows, you'll have to install Rust & then build Foundry from source:
Create the project, which will create a folder with three folders within it:
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:
Open the file and add the following contract to it:
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:
Compiling Solidity
Once all dependencies have been installed, you can compile the contract:
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:
forge create --rpc-url https://rpc-testnet.gitshock.com/buitenzorg \ --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:
You should get this data in hexidecimal format:
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":
You can also mutate data with cast as well. Try burning tokens by sending them to the zero address.
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!
Last updated