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 (24)

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
 
james_takahashi_77908481e profile image
James Takahashi

This article is of great help for me.
Thank you.

Collapse
 
james_takahashi_286ac1676 profile image
James Takahashi

Thanks for your effort.

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
 
eugene_garrett_d1a47f08f6 profile image
eugene garrett

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

Collapse
 
steven0822 profile image
Steven

👍👍Thank you

Collapse
 
anders_nielsen_604d456759 profile image
anders nielsen

This article gave me clear understanding of smart contract testing on sepolia testnet.
Thanks

Collapse
 
johnny_webster_27313b58e3 profile image
Johnny Webster

Great

Collapse
 
minato_shaikh_22c617ca779 profile image
Minato Shaikh

This article is really impressive.
Thanks for your effort
🎈🎉

Collapse
 
jason_smith_6d79eeb6aaba0 profile image
Jason

Thanks for your article.
Deeply impressive.