Skip to main content

Setup Foundry

Foundry is a blazing fast, portable, and modular toolkit for Ethereum application development written in Rust. This guide will help you set up Foundry for developing on SKALE.

Prerequisites

  • Rust installed on your machine
  • Basic command-line knowledge
If you are on a Windows machine, you will need to use Windows Subsystem for Linux (WSL), since Foundry doesn’t work natively on Windows.

Step 1: Install Foundry

Install Foundry by running:
foundryup
This will install the latest stable release of Foundry, including:
  • Forge: Build, test, and deploy smart contracts
  • Cast: Interact with EVM smart contracts, send transactions, and read chain data
  • Anvil: Local Ethereum node for testing
  • Chisel: Solidity REPL

Step 2: Verify Installation

Verify that Foundry is installed correctly:
forge --version
cast --version
anvil --version
You should see version numbers for each tool.

Step 3: Create a New Foundry Project

Create a new Foundry project:
forge init my-skale-project
cd my-skale-project
This creates a new directory with the following structure:
my-skale-project/
├── src/          # Smart contracts
├── script/       # Deployment scripts
├── test/         # Test files
├── lib/          # Dependencies
├── foundry.toml  # Configuration file
└── remappings.txt # Import remappings

Step 4: Configure Foundry for SKALE

Update the foundry.toml file to add SKALE Chain endpoints:
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# SKALE Chain Configuration
[rpc_endpoints]
skale_testnet = "https://testnet.skalenodes.com/v1/juicy-low-small-testnet"
skale_mainnet = "https://mainnet.skalenodes.com/v1/elated-tan-skat"
# Add your custom SKALE Chain endpoint here

[etherscan]
skale_testnet = { key = "", url = "https://juicy-low-small-testnet.explorer.testnet.skalenodes.com/api" }
Replace the RPC URLs with your specific SKALE Chain endpoint. You can find SKALE Chain endpoints in the SKALE Portal.

Step 5: Install Dependencies

Install OpenZeppelin Contracts (or other dependencies):
forge install OpenZeppelin/openzeppelin-contracts
This will add the dependency to your lib directory.

Step 6: Configure Remappings

Update your remappings.txt file to include:
@openzeppelin/=lib/openzeppelin-contracts/
For secure deployment, create a Foundry keystore:
cast wallet import skale-deployer --private-key $(cast wallet new | grep 'Private key:' | awk '{print $3}')
Provide a password to encrypt the keystore file. If you forget this password, you will not be able to recover it.
Get your wallet address:
cast wallet address --account skale-deployer
Copy this address and fund it with sFUEL from the sFUEL Station.

Step 8: Test Your Setup

Compile your contracts:
forge build
Run tests:
forge test

SKALE-Specific Considerations

When deploying to SKALE, remember to:
  1. Use the --legacy flag: SKALE Chains require legacy transaction format
    forge create src/MyContract.sol:MyContract \
        --account skale-deployer \
        --rpc-url skale_testnet \
        --broadcast \
        --legacy
    
  2. Use --slow flag for scripts: When running scripts with multiple transactions
    forge script script/Deploy.s.sol \
        --account skale-deployer \
        --rpc-url skale_testnet \
        --broadcast \
        --slow \
        --legacy
    
  3. Verify contracts: Use the block explorer API for verification
    forge verify-contract \
        --rpc-url skale_testnet \
        <CONTRACT_ADDRESS> \
        src/MyContract.sol:MyContract \
        --verifier blockscout \
        --verifier-url https://juicy-low-small-testnet.explorer.testnet.skalenodes.com/api
    

Next Steps

Now that Foundry is set up, you can: