Deploying a smart contract using Foundry

Tutorial: Deploying Smart Contracts on Biturbo Using Foundry

This tutorial will guide you through deploying a smart contract on the Biturbo test network using Foundry, a powerful development framework for Ethereum.

Prerequisites

  1. Foundry: Ensure Foundry is installed and configured on your machine.

  2. MetaMask: Installed and configured for the Biturbo network.

  3. Git: Installed on your machine.

Step-by-Step Guide

Step 1: Install Foundry

  1. Open your terminal and run the following command to install Foundry:

    curl -L https://foundry.paradigm.xyz | bash
  2. Initialize Foundry:

    foundryup

Step 2: Set Up a New Project

  1. Create a new Foundry project:

    forge init my-foundry-project
  2. Navigate into the project directory:

    cd my-foundry-project

Step 3: Write a Smart Contract

  1. In the src directory, create a new file named SimpleStorage.sol and add the following code:

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    contract SimpleStorage {
        uint256 private storedData;
    
        function set(uint256 x) public {
            storedData = x;
        }
    
        function get() public view returns (uint256) {
            return storedData;
        }
    }

Step 4: Compile the Smart Contract

  1. Compile your contract using Foundry:

    forge build

Step 5: Configure Foundry for Deployment

  1. Create a deployment script in the script directory, name it DeploySimpleStorage.s.sol, and add the following code:

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    import "forge-std/Script.sol";
    import "../src/SimpleStorage.sol";
    
    contract DeploySimpleStorage is Script {
        function run() external {
            vm.startBroadcast();
            new SimpleStorage();
            vm.stopBroadcast();
        }
    }
  2. Create a .env file in the root directory to store your private key:

    PRIVATE_KEY=your_private_key_here
  3. Load the environment variables by installing dotenv:

    forge install foundry-rs/forge-std --no-commit

Step 6: Deploy the Smart Contract

  1. Deploy your contract to the Biturbo Testnet:

    forge script script/DeploySimpleStorage.s.sol --rpc-url https://test-rpc.biturbo.io --private-key $PRIVATE_KEY --broadcast

Step 7: Interact with the Smart Contract

  1. Interact with your deployed contract using cast or via a front-end application. For example, to call the set and get functions, you can use cast:

    cast send <CONTRACT_ADDRESS> "set(uint256)" <VALUE> --rpc-url https://test-rpc.biturbo.io --private-key $PRIVATE_KEY
    cast call <CONTRACT_ADDRESS> "get()" --rpc-url https://test-rpc.biturbo.io

Summary

You have successfully deployed and interacted with a smart contract on the Biturbo Testnet using Foundry. This process leverages the powerful toolchain provided by Foundry and the EVM compatibility of Biturbo.

Last updated