Deploying a smart contract using Hardhat

Tutorial: Deploying Smart Contracts on Biturbo Using Hardhat

This tutorial will guide you through deploying a smart contract on the Biturbo test network using Hardhat, a comprehensive development environment for Ethereum software.

Prerequisites

  1. Node.js: Ensure Node.js is installed on your machine.

  2. Hardhat: Installed via npm.

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

Step-by-Step Guide

Step 1: Install Hardhat

  1. Open your terminal and create a new directory for your project:

    mkdir my-hardhat-project
    cd my-hardhat-project
  2. Initialize a new Hardhat project:

    npm init -y
    npm install --save-dev hardhat
  3. Run Hardhat to create a basic sample project:

    npx hardhat
  4. Select "Create a basic sample project" and follow the prompts.

Step 2: Write the Smart Contract

  1. Navigate to the contracts directory and open Greeter.sol. Replace its content with the following:

    // 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 3: Configure Hardhat

  1. Open the hardhat.config.js file and configure it to connect to the Biturbo Testnet:

    require("@nomiclabs/hardhat-waffle");
    require('dotenv').config();
    
    module.exports = {
      solidity: "0.8.0",
      networks: {
        biturboTestnet: {
          url: "https://test-rpc.biturbo.io",
          chainId: 725019,
          accounts: [`0x${process.env.PRIVATE_KEY}`]
        }
      }
    };
  2. Ensure you set your private key as an environment variable. Create a .env file in the root directory:

    PRIVATE_KEY=your_private_key_here

Step 4: Deploy the Smart Contract

  1. Create a new script in the scripts directory named deploy.js and add the following code:

    async function main() {
      const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
      const simpleStorage = await SimpleStorage.deploy();
      await simpleStorage.deployed();
      console.log("SimpleStorage deployed to:", simpleStorage.address);
    }
    
    main()
      .then(() => process.exit(0))
      .catch(error => {
        console.error(error);
        process.exit(1);
      });
  2. Deploy your contract to the Biturbo Testnet:

    npx hardhat run scripts/deploy.js --network biturboTestnet

Step 5: Interact with the Smart Contract

  1. After deployment, you can interact with your contract using Hardhat scripts or the console. For example, to call the set and get functions, you can write additional scripts or use the Hardhat console.

    const simpleStorageAddress = "your_contract_address_here";
    const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
    const simpleStorage = SimpleStorage.attach(simpleStorageAddress);
    
    // Setting a value
    await simpleStorage.set(42);
    
    // Getting the stored value
    const value = await simpleStorage.get();
    console.log("Stored value:", value.toString());

Summary

You have successfully deployed and interacted with a smart contract on the Biturbo Testnet using Hardhat. This process leverages Hardhat's robust development environment and the EVM compatibility of Biturbo.

Last updated