Deploying A Test Contract

Introduction

In this getting started tutorial, you will deploy a sample contract within Carbon's EVM module.

What You'll Need

  • Solidity Language Knowledge: For this guide, you write the smart contract code in Solidity programming language. Solidity is the primary programming language employed in Ethereum - designed specifically for writing smart contracts. This code will define the rules and logic of the smart contract, specifying how it will function and interact with the Carbon network.

  • A Carbon account: You will need a Carbon account to deploy the smart contract. The account will hold the necessary funds to pay for the gas fees required for the deployment.

  • Sufficient funds: Deploying a smart contract on the Ethereum network requires paying transaction fees, also known as gas fees. The fees are payable in Ether (ETH), the native cryptocurrency of the Ethereum network. You must have sufficient funds in your Ethereum account to cover the transaction fees.

  • A deployment script or plugin: You will need deployment tools like HardHat or Truffle, to deploy the smart contract on Carbon EVM. These tools will enable you to interact with the Carbon network and execute the smart contract deployment transaction.

  • Access to a Carbon node, either by running your own, connecting to a public node, or via an API key using a node service.

Guide

To deploy a Solidity Smart Contract via HardHat, you can follow these steps:

Step 1: Set up the development environment. Make sure to install the following prerequisites before setting up the project:

Step 2: Create a new project via npm init --y.

Step 3: Install hardhat dependencies required for the deployment via npm install --save-dev hardhat and npm install @nomicfoundation/hardhat-toolbox.

Step 4: Create a new hardhat project by running npx hardhat in your project directory. Ensure that the Create an empty hardhat.config.js option is selected.

Step 5: Create or insert your smart contract in the contracts directory. In this example, we will be using a sample ERC20 contract, named ERC20Token.sol.

Sample contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol
interface IERC20 {
    function totalSupply() external view returns (uint);

    function balanceOf(address account) external view returns (uint);

    function transfer(address recipient, uint amount) external returns (bool);

    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);
}

contract ERC20 is IERC20 {
    uint public totalSupply;
    mapping(address => uint) public balanceOf;
    mapping(address => mapping(address => uint)) public allowance;
    string public name = "Solidity by Example";
    string public symbol = "SOLBYEX";
    uint8 public decimals = 18;

    function transfer(address recipient, uint amount) external returns (bool) {
        balanceOf[msg.sender] -= amount;
        balanceOf[recipient] += amount;
        emit Transfer(msg.sender, recipient, amount);
        return true;
    }

    function approve(address spender, uint amount) external returns (bool) {
        allowance[msg.sender][spender] = amount;
        emit Approval(msg.sender, spender, amount);
        return true;
    }

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool) {
        allowance[sender][msg.sender] -= amount;
        balanceOf[sender] -= amount;
        balanceOf[recipient] += amount;
        emit Transfer(sender, recipient, amount);
        return true;
    }

    function mint(uint amount) external {
        balanceOf[msg.sender] += amount;
        totalSupply += amount;
        emit Transfer(address(0), msg.sender, amount);
    }

    function burn(uint amount) external {
        balanceOf[msg.sender] -= amount;
        totalSupply -= amount;
        emit Transfer(msg.sender, address(0), amount);
    }
}

Step 6: Create a deploy.js script to deploy your contract under the scripts directory.

const { ethers } = require("hardhat");

async function main() {

  const [deployer] = await ethers.getSigners();
       
  console.log("Deployer ", deployer.address);

  const balance = await deployer.getBalance();

  console.log("Balance ", balance);

  const Token = await ethers.getContractFactory("ERC20");
  const token = await Token.deploy();

  console.log("Token address: ", token.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Step 7: Configure HardHat to use the Carbon EVM via hardhat.config.js.

/** @type import('hardhat/config').HardhatUserConfig */
require('@nomicfoundation/hardhat-toolbox')
module.exports = {
  solidity: '0.8.18',
  networks: {
    testnet: {
      url: 'https://test-evm-api.carbon.network/',
      accounts: [YOUR_PRIVATE_KEY],
    },
  },
}

Ensure that your private key is inserted into accounts before running the next step.

Step 8: Deploy your contract by running npx hardhat run --network testnet scripts/deploy.js.

In this example, we are deploying to the testnet environment. The name of the environment should be the same as the one defined under networks in hardhat.config.js.

Step 9: If everything is working correctly, you should observe the corresponding logs being displayed on the console without any further errors.

Sample logs:

Deployer  0x5161e15Fee8b918d4621703dB75641BbC25301c8
Balance  BigNumber { value: "12333000000000000000000" }
Token address:  0x112601CcF79eF9D634B45ABC972e2863e595569e

Congratulations, you’ve successfully deployed your first smart contract on Carbon EVM!

Last updated