Page cover

Smart Contract Debugger: Contract Testing

1.1 Smart Contract Debugger Overview

Smart Contract Debugger in the CapsureLabs Developer Tools is built to aid developers in testing and debugging smart contracts, ensuring that code is reliable, optimized, and secure. For this purpose, tools like Hardhat and Ganache are widely used for deploying contracts on a local Ethereum network, simulating blockchain behavior, and stepping through transactions for in-depth debugging.


1.2 Using Hardhat for Contract Testing

Hardhat is an advanced Ethereum development environment that allows for local testing, debugging, and deployment. Hardhat offers:

  • A local blockchain that can be reset as needed.

  • Debugging with console logging.

  • Integration with Mocha and Chai for unit and integration tests.

Setting Up Hardhat

npm install --save-dev hardhat
npx hardhat

Hardhat Test Script

  1. Define the Smart Contract

// SimpleToken.sol
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SimpleToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("SimpleToken", "STK") {
        _mint(msg.sender, initialSupply);
    }
}
  1. Write a Hardhat Test Script

const { expect } = require("chai");

describe("SimpleToken contract", function () {
    let Token, simpleToken, owner, addr1;

    beforeEach(async function () {
        Token = await ethers.getContractFactory("SimpleToken");
        [owner, addr1] = await ethers.getSigners();
        simpleToken = await Token.deploy(1000);
        await simpleToken.deployed();
    });

    it("Should assign the initial supply to the owner", async function () {
        const ownerBalance = await simpleToken.balanceOf(owner.address);
        expect(await simpleToken.totalSupply()).to.equal(ownerBalance);
    });

    it("Should transfer tokens between accounts", async function () {
        await simpleToken.transfer(addr1.address, 50);
        expect(await simpleToken.balanceOf(addr1.address)).to.equal(50);
    });
});
  1. Run Tests

npx hardhat test

1.3 Using Ganache for Contract Testing

Ganache is a local blockchain simulator designed for testing smart contracts and transactions. It allows developers to track events and inspect state changes throughout the transaction process.

Setting Up Ganache

Install Ganache CLI:

npm install -g ganache-cli

Start Ganache CLI:

ganache-cli

This will start a local blockchain at http://127.0.0.1:8545.

Connecting Hardhat to Ganache

To connect Hardhat tests to Ganache, update hardhat.config.js:

module.exports = {
  networks: {
    ganache: {
      url: "http://127.0.0.1:8545",
    },
  },
  solidity: "0.8.0",
};

Run Hardhat tests on the Ganache network:

npx hardhat test --network ganache

1.4 Testing

Deploying and Debugging with Ganache

  1. Deploy the Contract

// scripts/deploy.js
async function main() {
    const [deployer] = await ethers.getSigners();
    console.log("Deploying contract with account:", deployer.address);

    const Token = await ethers.getContractFactory("SimpleToken");
    const simpleToken = await Token.deploy(1000);
    console.log("Token address:", simpleToken.address);
}

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

npx hardhat run scripts/deploy.js --network ganache
  1. Inspect the Deployment on Ganache

Open the Ganache GUI or CLI to see transaction logs, event details, and account balances.


Last updated