logoAcademy

Foundry Quickstart

Environment Setup

In this chapter, we will look into the Foundry that will be used to deploy and interact with smart contracts on the Avalanche network in our Avalanche Starter kit.

Foundry serves as a comprehensive toolchain for smart contract development. It handles dependency management, project compilation, testing, deployment, and facilitates interaction with the blockchain through both command-line interface and Solidity scripts.

In this course we will mainly use three commands:

  • forge create: Deploying smart contracts
  • cast send: Issuing transactions interacting with a smart contract
  • cast call: Read-only operations on the blockchain

Let's look at these three in a little more detail for this example contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract NumberStorage {
 
    uint256 num;
  
    function getNum() public view returns(uint) {
        return num; 
    }
  
    function setNum(uint _num) public {
        num = _num;
    }
  
}

forge create

This command can be used to deploy smart contracts from a solidity file. The syntax is:

forge create [options] contract

An example usage could look something like this:

forge create --rpc-url local-c --private-key $PK src/my-contracts/NumberStorage.sol:NumberStorage 

Let's look at the different parameters:

  • --rpc-url: The URL of the RPC server that the transaction will be sent to. Every blockchain can have multiple RPC servers. You will learn more about what RPCs are below.
  • --private-key: The private key that will be used for signing the transaction for the smart contract deployment
  • contract: The path to the contract is in the format <path>:<contract>, e.g. src/Contract.sol:Contract. A single solidity file can contain multiple contracts, therefore we have to also specify the contract name and not only the path to the file here.

cast send

When we want to sign and publish a transaction we will use cast send. We can either issue simple value transfers or call smart contracts The syntax is:

cast send [options] to [sig] [args…]

An example usage could look something like this:

cast send --rpc-url local-c --private-key $PK 0x17aB05351fC94a1a67Bf3f56DdbB941aE6c63E25 "setNum(uint)" 42

Let's look at the different parameters:

  • --rpc-url: See above
  • --private-key: See above
  • to: address of an account or a smart contract
  • sig: The function signature of a function of the smart contract, e.g. "setNum(uint)"
  • args: The arguments of that function (can be multiple or none)

cast call

Call a read-only function of a smart contract. We do not have to issue a transaction for reading state from the blockchain, since the state won't change. Therefore we don't have to pay for a transaction and don't need a private key. The syntax is:

cast call [options] to sig [args…]

An example usage could look something like this:

cast call --rpc-url local-c 0x17aB05351fC94a1a67Bf3f56DdbB941aE6c63E25 "getNum()(uint)"

Let's look at the different parameters:

  • --rpc-url: See above
  • --private-key: See above
  • to: address of an account or a smart contract
  • sig: The function signature of a function of the smart contract, e.g. "getNum()(uint)". In this example our function does not have any arguments and returns a single value of type uint.
  • args: The arguments of that function (can be multiple or none)

Private Key Management with Foundry

We need to pay for the fees of transactions that manipulate the state (deploying a contract, sending tokens or calling a non-view function of a smart contract). The ewoq address is an address where the private key is commonly known:

Address: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC Private Key: 56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027

Do not send any funds on any public blockchain to the ewoq address. The funds will most likely be lost immediately.

When you create an Avalanche L1 you will be offered an option to configure the initial token allocation. The second option will allocate 1m tokens to that address so you can easily deploy all contracts and pay for the transactions from that address:

? How would you like to distribute funds: 
    Airdrop 1 million tokens to a newly generate address (stored key)
 Airdrop 1 million tokens to the default ewoq address (do not use in production)
    Customize your airdrop
    Go back to previous step

The private key for the ewoq address is already stored in the environment variable $PK in the Avalanche Starter Kit. Therefore, we can easily access it by using the following flag for the transactions:

--private-key $PK

This address is obviously not suitable for any production use, since the private key is publicly known. We will look into how we can manage key for the deployment to the Fuji Testnet and the Mainnet later in this course.

RPC URLs

To issue our transactions, we need to send them to an RPC server. You can put the RPC URL directly after the --rpc-url flag. Foundry also allows to configure aliases for the URLs to make it more simple for you to send the transactions in shorter commands.

--rpc-url local-c

These aliases are defined in the foundry.toml file in the root of the Avalanche Starter Kit.

[rpc_endpoints]
local-c = "http://localhost:9650/ext/bc/C/rpc"
myblockchain = "http://localhost:9650/ext/bc/myblockchain/rpc"
fuji-c = "https://api.avax-test.network/ext/bc/C/rpc"
dispatch = "https://subnets.avax.network/dispatch/testnet/rpc"

The RPC URLs defined by default are the following:

  • local-c: The C-Chain of your local Avalanche network
  • myblockchain: An Avalanche L1 on your local Avalanche network called myblockchain
  • fuji-c: The C-Chain of the Fuji Testnet
  • dispatch: An Avalanche L1 on the Fuji Testnet called Dispatch. It was specifically created for testing Avalanche Interchain Messaging dApps.

The ability to use an RPC with the name of our L1 comes from the alias API in our nodes. You can read more about it here.

Feel free to add more RPC URLs to Avalanche L1s you create. You can also add more aliases for the RPC URLs to make it easier for you to send transactions to them.

Learn more about Foundry

Foundry has an extensive documentation collected in the Foundry Book.

On this page

No Headings