DEV Community

Cover image for How to test smart contract on Sepolia testnet?
Steven
Steven

Posted on

How to test smart contract on Sepolia testnet?

Introduction

Testing smart contracts on a testnet before mainnet deployment is crucial for ensuring functionality and security. The Sepolia testnet provides an ideal environment to test the smart contract with real network conditions without risking real assets.

Real-World Simulation: Testing on a testnet mimics the actual blockchain environment, allowing developers to see how their contracts perform with real transactions and network conditions.

Interaction with Other Contracts: Testnets let developers check how their smart contracts work when interacting with other contracts or dApps, helping to catch potential issues that weren't apparent during unit testing.

We have already done the thorough unit testing using Hardhat and Chai, but we are going to check if this smart contract is working well on Sepolia testnet.

This article will walk you through deploying the SPX ERC20 token presale smart contract on the Sepolia testnet, an Ethereum test network and thorough testing it on sepolia etherscan.

Deployment on Sepolia testnet

Configure Hardhat settings

First, we need to configure the Hardhat settings to deploy our smart contract on the Sepolia testnet.

  1. Create a new file named hardhat.config.ts in the root directory of your project.
  2. Open the file and add the following code:
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import "dotenv/config";

const infuraKey: string = process.env.INFURA_API_KEY as string;
const privateKey: string = process.env.PRIVATE_KEY ? process.env.PRIVATE_KEY as string: "";
const etherscanKey: string = process.env.ETHERSCAN_KEY ? process.env.ETHERSCAN_KEY as string: "";

const config: HardhatUserConfig = {
  solidity: {
    version: "0.8.27",
    settings: {
      optimizer: {
        enabled: true,
        runs: 100,
      },
      viaIR: true,
    },
  },
  networks: {
    sepolia: {
      url: `https://sepolia.infura.io/v3/${infuraKey}`,
      accounts: [`0x${privateKey}`],
    },
    mainnet: {
      url: `https://mainnet.infura.io/v3/${infuraKey}`,
      accounts: [`0x${privateKey}`],
    },
    hardhat: {
      chainId: 31337,
    },
  },
  etherscan: {
    apiKey: {
      eth_mainnet: etherscanKey,
      eth_sepolia: etherscanKey
    },
  },
  gasReporter: {
    enabled: true,
  },
  sourcify: {
    enabled: true,
  },
};

export default config;
Enter fullscreen mode Exit fullscreen mode

Second, we need to create a .env file in the root directory of your project.

INFURA_API_KEY=your_infura_api_key
PRIVATE_KEY=your_wallet_private_key
ETHERSCAN_KEY=your_etherscan_api_key
Enter fullscreen mode Exit fullscreen mode

Writing the deployment script

First we need to deploy SPX ERC20 token smart contract, so let's create a new file named deploy_SPX.ts in the scripts directory of your project.

import { ethers } from 'hardhat'

async function main() {
  const [deployer] = await ethers.getSigners();
  const instanceSPX = await ethers.deployContract("SPX");
  await instanceSPX.waitForDeployment()
  const SPX_Address = await instanceSPX.getAddress();
  console.log(`SPX is deployed. ${SPX_Address}`);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error)
    process.exitCode = 1
  })
Enter fullscreen mode Exit fullscreen mode

Then, deploy the SPX ERC20 token smart contract on the Sepolia testnet.

npx hardhat run scripts/deploy_SPX.ts --network sepolia
Enter fullscreen mode Exit fullscreen mode

In the output, you will see the deployed SPX contract address.

SPX is deployed. 0xF4072Ee965121c2857EeBa0D3e3C6B9795403072
Enter fullscreen mode Exit fullscreen mode

Just copy it and paste it into the spxAddress variable in the deploy_Presale.ts script below.

Similarly, create a new file named deploy_Presale.ts in the scripts directory of your project.

import { ethers } from 'hardhat'

async function main() {
  const softcap = ethers.parseUnits("300000", 6);
  const hardcap = ethers.parseUnits("1020000", 6);
  const presaleStartTimeInMilliSeconds = new Date("2024-11-15T00:00:00Z"); //2024-11-15T00:00:00Z 
  const presaleStartTime = Math.floor(presaleStartTimeInMilliSeconds.getTime() / 1000);

  const presaleDuration = 24 * 3600 * 30;  //30 days
  const presaleTokenPercent = 10;
  const spxAddress = "0xF4072Ee965121c2857EeBa0D3e3C6B9795403072";   //Deployed SPX token contract on Sepolia

  const [deployer] = await ethers.getSigners();

  const instancePresale = await ethers.deployContract("Presale", [softcap, hardcap, presaleStartTime, presaleDuration, ficcoAddress, presaleTokenPercent]);
  await instancePresale.waitForDeployment();
  const Presale_Address = await instancePresale.getAddress();
  console.log(`Presale is deployed to ${Presale_Address} and presale start time is ${presaleStartTime}`);       
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error)
    process.exitCode = 1
  })
Enter fullscreen mode Exit fullscreen mode

Then, deploy the presale smart contract on the Sepolia testnet.

npx hardhat run scripts/deploy_Presale.ts --network sepolia
Enter fullscreen mode Exit fullscreen mode

In the output, you will see the deployed Presale contract address.
The output should be similar to the following:

Presale is deployed to 0x2Ae586f1EbE743eFDCD371E939757EEb42dC6CA7 and presale start time is 1731542400
Enter fullscreen mode Exit fullscreen mode

Testing the Presale smart contract on Sepolia testnet

Verification of the SPX token contract and Presale smart contract on Sepolia testnet

In order to test the SPX token contract and Presale smart contract on Sepolia testnet, we need to verify them on Sepolia Etherscan.

  • SPX token contract verification
npx hardhat verify 0xF4072Ee965121c2857EeBa0D3e3C6B9795403072 --network sepolia
Enter fullscreen mode Exit fullscreen mode

The command structure is npx hardhat verify <contract_address> <contract constructor arguments> --network <network_name>

The output should be similar to the following:

The contract 0xF4072Ee965121c2857EeBa0D3e3C6B9795403072 has been successfully verified on the block explorer. 
https://sepolia.etherscan.io/address/0xF4072Ee965121c2857EeBa0D3e3C6B9795403072#code

The contract 0xF4072Ee965121c2857EeBa0D3e3C6B9795403072 has already been verified on Sourcify.
https://repo.sourcify.dev/contracts/full_match/11155111/0xF4072Ee965121c2857EeBa0D3e3C6B9795403072/
Enter fullscreen mode Exit fullscreen mode
  • Presale smart contract verification
npx hardhat verify 0x2Ae586f1EbE743eFDCD371E939757EEb42dC6CA7 300000000000 1020000000000 1731542400 2592000 0xF4072Ee965121c2857EeBa0D3e3C6B9795403072 10 --network sepolia
Enter fullscreen mode Exit fullscreen mode

Here, we need to pass the constructor arguments of the Presale smart contract one by one with space .

The output should be similar to the following:

The contract 0x2Ae586f1EbE743eFDCD371E939757EEb42dC6CA7 has been successfully verified on the block explorer.
https://sepolia.etherscan.io/address/0x2Ae586f1EbE743eFDCD371E939757EEb42dC6CA7#code

Successfully verified contract Presale on Sourcify.
https://repo.sourcify.dev/contracts/full_match/11155111/0x2Ae586f1EbE743eFDCD371E939757EEb42dC6CA7/
Enter fullscreen mode Exit fullscreen mode

Testing the Presale smart contract on Sepolia testnet

You can test the Presale smart contract on Sepolia testnet with Sepolia Etherscan.

  • Go to the Sepolia Etherscan website and search for the SPX ERC20 token contract address and Presale smart contract address.
  • Click on the "Contract" tab.
  • Click on the "Read Contract" tab.

You can see all the read-only functions of the SPX ERC20 token contract and Presale smart contract.

Read functions of Presale smart contract

  • Click on the "Write Contract" tab.

You can see all the write functions of the SPX ERC20 token contract and Presale smart contract.

Write functions of Presale smart contract

  • Connect your MetaMask wallet to Sepolia testnet.

Make sure you have enough Sepolia ETH and USDT, USDC and DAI faucet in your MetaMask wallet.

  • We can test all the functionalities of the SPX ERC20 token contract and Presale smart contract here.

Before Presale:

  • Transfer tokens to presale contract.
  • Verify token balance
  • Check the buy functions not working before presale starts

During Presale:

  • Test buying tokens with different payment methods
  • Verify investment amounts
  • Check token allocation

After Presale:

  • Set claim time
  • Test claim function
  • Verify token distribution
  • Test withdrawal or refund based on softcap achievement
  • Check the buy functions not working after presale ends

Make sure all the require statements are working properly and functions are working correctly with the correct input and output and within correct timespan.

Conclusion

Testing on Sepolia Testnet provides a realistic environment to validate:

  • Contract functionality
  • Security measures
  • Gas optimization
  • User interaction flows
  • Integration with other contracts

Always verify all functions thoroughly before proceeding to mainnet deployment. The systematic testing approach on Sepolia ensures a smooth and secure launch on the mainnet.

Top comments (10)

Collapse
 
sebastian_robinson_884ddc profile image
Sebastian Robinson

Thanks.
Good article.

Collapse
 
arlo_oscar_d8a2de736e7c73 profile image
Arlo Oscar

Looks amazing

Collapse
 
btc415 profile image
LovelyBTC

😍😍😍
Thanks for your article.
Testing smart contract with unit testing and Sepolia testnet tesing is the best option to check the smart contract.
🌹🌹🌹

Collapse
 
ichikawa0822 profile image
Ichikawa Hiroshi

Thanks for your post.
Highly recommend it to anyone looking to enhance their knowledge and skills in Smart Contract testing

Collapse
 
jacksonmoridev0507 profile image
Jackson Mori

Thanks for your article.
Looks nice like always.
👍👍👍

Collapse
 
robert_angelo_484 profile image
Robert Angelo

Really impressive.
Thank you for sharing.

Collapse
 
eugene_garrett_d1a47f08f6 profile image
eugene garrett

This article gave me comprehensive testing strategy of smart contract on Sepolia testnet.
Thanks Steven

Collapse
 
jin_william_f348810a6d5e6 profile image
Jin William

Thank you Steven.
I love to read your article.
Its a great help

Collapse
 
jason_smith_6d79eeb6aaba0 profile image
Jason

Thanks for your article.
Deeply impressive.

Collapse
 
dodger213 profile image
Mitsuru Kudo

Thank you so much for the helpful information!
Highly recommended.
Thanks again