Deploy a DApp on the C-Chain with Foundry

This guide shows how to deploy and interact with smart contracts using foundry on a local Avalanche Network and the Fuji C-Chain.

Back

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

Run Avalanche Starter Kit

The Avalanche Starter Kit contains everything you need to get started quickly with Avalanche. Among other tools it contains Avalanche CLI. With that you can set up a local network, create a Avalanche L1, customize the Avalanche L1/VM configuration, and so on.

In this course we will run the Avalanche Starter Kit in a hosted environment on Github. This is the most quick way to get started.

Create a Codespace

  • Click the green Code button
  • Switch to the Codespaces tab
  • Click Create Codespace on main or if you already have a Codespace click the plus (+) button

The Codespace will open in a new tab. Wait a few minutes until it's fully built.

Verify everything is working correctly

Open the terminal with Ctrl + ` or by opening it through the menu:

Now enter avalanche -h to verify everything is working:

avalanche -h

As a result you should see the help of Avalanche CLI:

Avalanche-CLI is a command-line tool that gives developers access to
everything Avalanche. This release specializes in helping developers
build and test Avalanche L1s.

To get started, look at the documentation for the subcommands or jump right
in with avalanche blockchain create myNewAvalanche L1.

Usage:
  avalanche [command]

Available Commands:
  config                Modify configuration for Avalanche-CLI
  help                  Help about any command
  key                   Create and manage testnet signing keys

Optional: Open Codespace locally in Visual Studio Code

You can switch any time from the browser IDE to Visual Studio Code:

The first time you switch, you will be asked to install the Codespaces extension and connect VS Code to you GitHub account, if it is not already connceted.

Create a Smart Contract

Create a new Solidity file in the src directory of your project. For example, src/MyContract.sol.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract MyContract {
    uint256 public myNumber;
 
    function setNumber(uint256 _num) public {
        myNumber = _num;
    }
}

Create Deployment Address

You can create a new wallet with the case command:

cast wallet new

Fund Your Account

Go the the faucet and fund the address with Fuji AVAX. You can use the code avalanche-academy:

Avalanche Testnet Faucet

Deploy the Smart Contract

With foundry's forge command, you can deploy your smart contract to the Fuji C-Chain.

forge create --rpc-url fuji-c --private-key <private-key> src/MyContract.sol:MyContract

Written by

On

Sun May 26 2024

Topics

solidityfoundryAvalanche Starter Kit
Edit on Github