Set up a Foundry project

In this workshop we will learn how to create an evm project with hardhat

This guide will walk you through setting up an EVM project on the XRPL sidechain using Foundry, a powerful toolkit for Ethereum application development.

Part 1: Installing Foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust. It consists of four main tools:

  • Forge: Ethereum testing framework

  • Cast: Swiss army knife for interacting with EVM smart contracts

  • Anvil: Local Ethereum node for development

  • Chisel: Fast, utilitarian, and verbose solidity REPL

Prerequisites

Before installing Foundry, ensure you have:

  • A terminal application (Git BASH or WSL for Windows users)

  • Internet connection for downloading the installer

Installation Methods

Foundryup is the official installer for the Foundry toolchain and the easiest way to get started.

  1. Install Foundryup

    Open your terminal and run:

    curl -L https://foundry.paradigm.xyz | bash
  2. Follow the on-screen instructions to complete the installation

  3. Install Foundry tools

    Run the following command to install the latest stable version:

    foundryup

    For the latest nightly build (with newest features), use:

    foundryup --version nightly

Option 2: Building from Source

If you prefer to build from source or need a custom configuration:

  1. Install Rust

    First, install Rust using rustup:

  2. Update Rust (if already installed)

  3. Install via Cargo

Option 3: Using Docker

For containerized development:

Verification

After installation, verify that Foundry is properly installed by checking the version:

You should see version information for Forge, confirming the installation was successful.

Platform-Specific Notes

  • Windows: Use Git BASH or WSL as your terminal. PowerShell and Command Prompt are not currently supported by foundryup

  • macOS: Standard terminal works perfectly

  • Linux: Any terminal emulator will work

Troubleshooting

If you encounter issues during installation:

  1. Check your internet connection

  2. Ensure you have the latest version of your terminal

  3. For Windows users: Make sure you're using Git BASH or WSL

  4. Refer to the Foundry FAQ for additional help

Security Note

Foundry binaries are verified using GitHub artifact attestations to ensure integrity and authenticity. The installer automatically verifies these attestations during installation.


✅ Checkpoint: You now have Foundry installed and ready to use for XRPL sidechain development!

Part 2: Initializing a Foundry Project

Now that Foundry is installed, let's create a new project and understand the project structure.

Creating a New Project

Initialize a new Foundry project using the forge init command:

This creates a new directory with a complete Foundry project structure.

Understanding the Project Structure

After initialization, your project will have the following structure:

Key Directories Explained

  • /src: Contains your smart contracts written in Solidity

  • /test: Contains test files for your contracts (typically with .t.sol extension)

  • /script: Contains deployment scripts and other automation scripts

  • /lib: Contains external dependencies managed by Soldeer package manager

Setting Up Soldeer Package Manager

Soldeer is Foundry's native package manager for handling smart contract dependencies. Let's configure it for your project:

  1. Initialize Soldeer

  2. Verify Soldeer Configuration

    Check that foundry.toml now includes Soldeer configuration:

    You should see something like:

Building Your Project

Compile the smart contracts to ensure everything is set up correctly:

You should see output indicating successful compilation of the example Counter contract.

Running Tests

Run the included tests to verify the setup:

This will execute all tests in the /test directory and show the results.

Understanding the Foundry Tools

Your project now has access to all Foundry tools:

Tool
Purpose

forge

Build, test, debug, deploy and verify smart contracts

anvil

Run a local Ethereum development node with forking capabilities

cast

Interact with contracts, send transactions, and retrieve chain data

chisel

Fast Solidity REPL for rapid prototyping and debugging

Adding Dependencies with Soldeer

To add external smart contract libraries (like OpenZeppelin), use Soldeer:

Dependencies will be automatically added to your foundry.toml and downloaded to the /lib directory.

Customizing Your Project

You can customize various aspects of your project by editing foundry.toml:


Checkpoint: You now have a fully initialized Foundry project with proper structure and dependency management!

Part 3: Setting Up Your Environment

Before deploying contracts to the XRPL sidechain, you need to set up a wallet and obtain test tokens for deployment.

Installing MetaMask

MetaMask is a popular Ethereum wallet that works with XRPL EVM sidechain. Follow these steps to install and set it up:

  1. Install MetaMask

    • Click "Download" and choose your browser

    • Install the browser extension

    • Create a new wallet or import an existing one

  2. Create a New Wallet (if you don't have one)

    • Click "Create a wallet"

    • Set a strong password

    • IMPORTANT: Write down your seed phrase securely and never share it

    • Confirm your seed phrase

Adding XRPL EVM Sidechain to MetaMask

Configure MetaMask to connect to the XRPL EVM sidechain:

  1. Open MetaMask and click the network dropdown (usually shows "Ethereum Mainnet")

  2. Add Custom Network with these details:

  3. Save the network configuration

  4. Switch to the XRPL EVM Sidechain network

Getting Your Private Key

⚠️ Security Warning: Never share your private key with anyone. Only use it for development and testing purposes.

  1. Open MetaMask

  2. Click the three dots next to your account name

  3. Select "Account Details"

  4. Click "Show private key"

  5. Enter your password

  6. Copy the private key (you'll need this for deployment)

Getting Test Tokens from the Faucet

You need test XRP tokens to deploy contracts on the XRPL sidechain:

  1. Visit the XRPL EVM Faucet: https://faucet.xrplevm.org/

  2. Connect Your Wallet

    • Click "Connect Wallet"

    • Select MetaMask

    • Approve the connection

  3. Request Test Tokens

    • Make sure you're on the XRPL EVM Sidechain network

    • Your wallet address should be displayed

    • Click "Send me XRP" or the equivalent button

    • Wait for the transaction to complete

  4. Verify Token Receipt

    • Check your MetaMask balance

    • You should see test XRP tokens in your wallet

Setting Up Environment Variables

Create a .env file in your project root to store your private key securely:

  1. Create the file:

  2. Add your private key:

  3. Add .env to .gitignore (if not already present):

Configuring Foundry for XRPL

Update your foundry.toml to include XRPL-specific settings:

Testing Your Setup

Verify your environment is ready:

  1. Check your balance:

  2. Load environment variables (if using .env):

  3. Test connection:

Security Best Practices

  • Never commit your private key to version control

  • Use environment variables for sensitive data

  • Use separate wallets for development and production

  • Regularly backup your seed phrase securely

  • Consider using hardware wallets for production deployments


Checkpoint: Your environment is now configured with MetaMask, test tokens, and proper security setup!

Part 4: Deploying Your Smart Contract

Now that your environment is set up, let's deploy the example Counter contract to the XRPL EVM sidechain using the forge create command.

Understanding the Counter Contract

First, let's look at the Counter contract in src/Counter.sol:

This simple contract stores a number and provides functions to set and increment it.

Deploying with forge create

The forge create command allows you to deploy a single contract directly without deployment scripts.

Basic Deployment Command

Using Environment Variables

For better security, load your environment variables first:

Using Foundry Configuration

If you configured the RPC endpoint in foundry.toml, you can use:

Understanding Deployment Output

After successful deployment, you'll see output like:

Key Information:

  • Deployer: Your wallet address

  • Deployed to: Your contract's address on the blockchain

  • Transaction hash: Reference for the deployment transaction

Deploying Contracts with Constructor Arguments

If your contract has constructor parameters, use the --constructor-args flag:

Deploy with arguments:

Dry Run Deployment

Test your deployment without broadcasting to the network:

This simulates the deployment and shows gas estimates without spending real tokens.

Verifying Your Deployment

  1. Check deployment on block explorer:

  2. Interact with your contract using Cast:

Contract Verification (Optional)

If the XRPL EVM sidechain supports contract verification, you can verify your contract:

Deployment Best Practices

  1. Always test locally first:

  2. Check gas costs before deployment:

  3. Keep track of deployed contracts:

    • Save contract addresses in a deployment log

    • Document which version was deployed when

    • Keep constructor arguments for reference

Troubleshooting Common Issues

  1. Insufficient Funds:

    • Check your balance: cast balance YOUR_ADDRESS --rpc-url $RPC_URL

    • Get more test tokens from the faucet if needed

  2. Private Key Format:

    • Ensure your private key starts with 0x

    • Check for any extra spaces or characters

  3. Network Issues:

    • Verify the RPC URL is correct

    • Check if the XRPL EVM sidechain is operational

  4. Compilation Errors:

    • Run forge build first to check for compilation issues

    • Ensure all dependencies are properly installed

Using Legacy Transactions

If you encounter EIP-1559 issues, use the --legacy flag:


🎉 Congratulations! You have successfully deployed your first smart contract to the XRPL EVM sidechain using Foundry! Your contract is now live and ready for interaction.

Last updated