Foundry
Deploy and interact with smart contracts using foundry on a local Avalanche Network and the Fuji C-Chain.
Foundry toolchain is a smart contract development toolchain written in Rust. It manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command-line.
Recommended Knowledge
- Basic understanding of Solidity and Avalanche.
- You are familiar with Avalanche Smart Contract Quickstart.
- Basic understanding of the Avalanche's architecture
- performed a cross-chain swap via this this tutorial to get funds to your C-Chain address.
Requirements
- You have installed Foundry and run
foundryup
. This installation includes theforge
andcast
binaries used in this walk-through. - NodeJS version
16.x
AvalancheGo and Avalanche Network Runner
AvalancheGo is an Avalanche node implementation written in Go.
Avalanche Network Runner is a tool to quickly deploy local test networks. Together, you can deploy local test networks and run tests on them.
Start a local five node Avalanche network:
A five node Avalanche network is running on your machine. Network will run until you press Ctrl + C
to exit.
Getting Started
This section will walk you through creating an ERC721.
Clone Avalanche Smart Contract Quick Start
Clone the quickstart repository and install the necessary packages via yarn
.
The repository cloning method used is HTTPS, but SSH can be used too:
git clone [[email protected]](https://docs.avax.network/cdn-cgi/l/email-protection):ava-labs/avalanche-smart-contract-quickstart.git
You can find more about SSH and how to use it here.
In order to deploy contracts, you need to have some AVAX. You can get testnet AVAX from the Avalanche Faucet, which is an easy way to get to play around with Avalanche. If you already have an AVAX balance greater than zero on Mainnet, paste your C-Chain address there, and request test tokens. Otherwise, please request a faucet coupon on Guild. Admins and mods on the official Discord can provide testnet AVAX if developers are unable to obtain it from the other two options. After getting comfortable with your code, you can run it on Mainnet after making the necessary changes to your workflow.
Write Contracts
We will use our example ERC721 smart contract, NFT.sol
found in ./contracts
of our project.
Let's examine this implementation of an NFT as a Game Item. We start by importing to contracts from our node modules. We import OpenZeppelin's open source implementation of the ERC721 standard which our NFT contract will inherit from. Our constructor takes the _name
and _symbol
arguments for our NFT and passes them on to the constructor of the parent ERC721 implementation. Lastly we implement the awardItem
function which allows anyone to mint an NFT to a player's wallet address. This function increments the currentTokenId
and makes use of the _mint
function of our parent contract.
Compile, Deploy, and Verify with Forge
Forge is a command-line tool that ships with Foundry. Forge tests, builds, and deploys your smart contracts.
It requires some initial project configuration in the form of a foundry.toml which can be generated by running:
The foundry.toml
by default points to the folders it added. We will want to change this to make sure the src
points to the contracts
directory. Change your foundry.toml
to look like the following:
By default, the contract artifacts will be in the out
directory, as specified in the foundry.toml
. To deploy our compiled contract with Forge, we have to set environment variables for the RPC endpoint and the private key we want to use to deploy.
Set your environment variables by running:
Since we are deploying to Fuji testnet, our RPC_URL
export should be:
If you would like to verify you contracts during the deployment process (fastest and easiest way), get a Snowtrace API Key. Add this as an environment variable:
Once set, you can deploy your NFT with Forge by running the command below while adding the values for _name
and _symbol
, the relevant constructor arguments of the NFT contract. You can verify the contracts with Snowtrace by adding --verify
before the --constructor-args
:
Upon successful deployment, you will see the deploying wallet's address, the contract's address as well as the transaction hash printed to your terminal.
Here's an example output from an NFT deployment and verification.
Upon successful verification, after your deployment you will see the contract verification status as successfully verified
:
Note: Please store your Deployed to
address for use in the next sections.
Verifying After Deployment
If you did not verify within the deployment process, you can still verify a deployed contract with foundry, using forge verify-contract
. The foundry.toml
and environment variables will have to be set like they were in the previous section.
For example, if we were to verify the NFT contract we just deployed in the previous section it would look this:
Upon successful verification, you will see the contract verification status as successfully verified
:
Using Cast to Interact with the Smart Contract
We can call functions on our NFT contract with Cast, Foundry's command-line tool for interacting with smart contracts, sending transactions, and getting chain data. In this scenario, we will mint a Game Item to a player's wallet using the awardItem
function in our smart contract.
Mint an NFT from your contract by replacing <NFT-CONTRACT-ADDRESS>
with your Deployed to
address and <NFT-RECIPIENT-ADDRESS>
with an address of your choice.
Note: This section assumes that you have already set your RPC and private key env variables during deployment
Upon success, the command line will display the transaction data.
Well done! You just minted your first NFT from your contract. You can check the owner of tokenId
1 by running the cast call
command below:
The address you provided above should be returned as the owner.
Mainnet Workflow
The Fuji workflow above can be adapted to Mainnet with the following modifications to the environment variables:
Local Workflow
The Fuji workflow above can be adapted to a Local Network by doing following:
In a new terminal navigate to your Avalanche Network Runner directory.
Next, deploy a new Avalanche Network with five nodes (a Cluster) locally.
Next, modify the environment variables in your Foundry project:
The example PRIVATE_KEY variable above provides a pre-funded account on Avalanche Network Runner and should be used for LOCAL DEVELOPMENT ONLY.
Summary
Now you have the tools you need to launch a local Avalanche network, create a Foundry project, as well as create, compile, deploy and interact with Solidity contracts.
Join our Discord Server to learn more and ask any questions you may have.
Last updated on