πŸ“”Deploying Contracts

Deploying Contracts using Hardhat

You can deploy your contracts in 3Base using hardhat.

What is Hardhat?

Hardhat is a development environment for Ethereum. It helps developer manage and automate building smart contracts. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.

Creating a Hardhat Project

  1. Create a directory for you project

//make adirectory and go inside it 
mkdir 3Base_Project && cd 3Base_Project
  1. Initialize the project with hardhat

npx hardhat init 
  1. Choose the Configuration of your hardhat project, a sample typescript project(recommended)

Creating Your Smart Contract

  1. Add your contracts to the contracts folder or

  2. Create a contracts directory, if there is none

mkdir contracts && cd contracts
  1. Create your contract in the contracts directory

touch your_contract.sol

Creating Your Configuration File

  1. Create a secrets.json file to store your private key

touch secrets.json
  1. Add your private key to secrets.json

{
"privateKey": "YOUR_PRIVATE_KEY_HERE"
}
  1. Add secrets.json to .gitignore, to avoid pushing your private key to github

  2. Modify the hardhat.config.ts file as below

// hardhat.config.ts

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
const { privateKey } = require("./secrets.json");

const config: HardhatUserConfig = {
  solidity: {
    version: "0.8.24",
  },
  networks: {
    three_base: {
      url: `RPC URL`,//INSERT YOUR RPC URL HERE
      accounts: [privateKey],
      timeout: 60000,
    },
  },
};

export default config;

Deploying Your Smart Contract

  1. Compile your contract

npx hardhat compile
  1. Create a scripts directory and create your deployment scripts

mkdir scripts && cd scripts
touch deploy.ts
  1. Create a deployment script, like the one below

async function main() {
  // 1. Get the contract to deploy
  const Your_Contract = await ethers.getContractFactory("your_contract");
  console.log("Deploying Your_Contract...");

  // 2. Instantiating a new smart contract
  const your_contract = await Your_Contract.deploy();

  // 3. Waiting for the deployment to resolve
  await your_contract.waitForDeployment();

  // 4. Use the contract instance to get the contract address
  const contract_address = await your_contract.getAddress();
  console.log("Your_Contract deployed to:", contract_address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
  1. Deploy your_contract.sol using the command below

npx hardhat run scripts/deploy.ts

Last updated

Copyright Β© 2024 3Base . All rights reserved.