如何在Sepolia测试网上测试智能合约?
介绍
在主网部署之前,在测试网上测试智能合约对于确保其功能和安全性至关重要。Sepolia 测试网提供了一个理想的环境,可以在真实的网络条件下测试智能合约,而无需承担实际资产的风险。
真实世界模拟:在测试网上进行测试可以模拟实际的区块链环境,使开发人员能够看到他们的合约在真实交易和网络条件下的表现。
与其他合约的交互:测试网允许开发者检查他们的智能合约在与其他合约或 dApp 交互时的工作情况,从而帮助发现单元测试期间未发现的潜在问题。
我们已经使用 Hardhat 和 Chai 进行了彻底的单元测试,但我们将检查该智能合约在 Sepolia 测试网上的运行情况。
本文将引导您完成在以太坊测试网络 Sepolia 测试网上部署SPX ERC20 代币预售智能合约的过程,并在 sepolia etherscan 上对其进行彻底测试。
在 Sepolia 测试网上部署
配置安全帽设置
首先,我们需要配置 Hardhat 设置,以便在 Sepolia 测试网上部署我们的智能合约。
hardhat.config.ts在项目根目录下创建一个名为 的新文件。- 打开文件并添加以下代码:
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;
其次,我们需要.env在项目的根目录中创建一个文件。
INFURA_API_KEY=your_infura_api_key
PRIVATE_KEY=your_wallet_private_key
ETHERSCAN_KEY=your_etherscan_api_key
编写部署脚本
首先我们需要部署 SPX ERC20 代币智能合约,所以让我们deploy_SPX.ts在scripts项目目录中创建一个名为 的新文件。
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
})
然后,在 Sepolia 测试网上部署 SPX ERC20 代币智能合约。
npx hardhat run scripts/deploy_SPX.ts --network sepolia
在输出结果中,您将看到已部署的 SPX 合约地址。
SPX is deployed. 0xF4072Ee965121c2857EeBa0D3e3C6B9795403072
只需复制并粘贴到下方脚本spxAddress中的变量中即可deploy_Presale.ts。
同样地,在项目目录deploy_Presale.ts中创建一个名为 的新文件。scripts
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
})
然后,将预售智能合约部署到 Sepolia 测试网上。
npx hardhat run scripts/deploy_Presale.ts --network sepolia
输出结果中将显示已部署的预售合同地址。
输出结果应与以下内容类似:
Presale is deployed to 0x2Ae586f1EbE743eFDCD371E939757EEb42dC6CA7 and presale start time is 1731542400
在 Sepolia 测试网上测试预售智能合约
在Sepolia测试网上验证SPX代币合约和预售智能合约。
为了在 Sepolia 测试网上测试 SPX 代币合约和预售智能合约,我们需要在Sepolia Etherscan上验证它们。
- SPX代币合约验证
npx hardhat verify 0xF4072Ee965121c2857EeBa0D3e3C6B9795403072 --network sepolia
命令结构是npx hardhat verify <contract_address> <contract constructor arguments> --network <network_name>
输出结果应类似于以下内容:
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/
- 售前智能合约验证
npx hardhat verify 0x2Ae586f1EbE743eFDCD371E939757EEb42dC6CA7 300000000000 1020000000000 1731542400 2592000 0xF4072Ee965121c2857EeBa0D3e3C6B9795403072 10 --network sepolia
这里,我们需要逐个传递预售智能合约的构造函数参数space。
输出结果应类似于以下内容:
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/
在 Sepolia 测试网上测试预售智能合约
您可以使用 Sepolia Etherscan 在 Sepolia 测试网上测试预售智能合约。
- 前往Sepolia Etherscan网站,搜索 SPX ERC20 代币合约地址和预售智能合约地址。
- 点击“合同”选项卡。
- 点击“阅读合同”选项卡。
您可以查看 SPX ERC20 代币合约和预售智能合约的所有只读功能。
- 点击“撰写合同”选项卡。
您可以查看 SPX ERC20 代币合约和预售智能合约的所有写入功能。
- 将您的 MetaMask 钱包连接到 Sepolia 测试网。
请确保您的 MetaMask 钱包中有足够的 Sepolia ETH、USDT、USDC 和 DAI 水龙头代币。
- 我们可以在这里测试 SPX ERC20 代币合约和预售智能合约的所有功能。
预售前:
- 将代币转移到预售合约。
- 验证代币余额
buy预售开始前,请检查所有功能是否正常运行。
预售期间:
- 测试使用不同支付方式购买代币
- 核实投资金额
- 检查代币分配
预售后:
- 设置索赔时间
- 测试声明功能
- 验证代币分发
- 根据软上限达成情况测试提款或退款。
- 检查
buy预售结束后哪些功能无法正常工作
确保所有require语句都能正常运行,并且function输入输出正确,时间范围也正确。
结论
在 Sepolia 测试网上进行测试可以提供真实的验证环境:
- 合约功能
- 安全措施
- 气体优化
- 用户交互流程
- 与其他合同的整合
在进行主网部署之前,务必彻底验证所有功能。Sepolia 的系统化测试方法可确保主网上线平稳安全。
文章来源:https://dev.to/imcrazysteven/how-to-test-smart-contract-on-sepolia-testnet-i1d

