feat: 添加 MiniSwap CLI 工具及相关文档
- 新增 CLI 工具,支持通过命令行与 MiniSwapAMM 合约交互 - 创建 CLI 使用指南文档,详细说明命令和操作流程 - 更新合约地址,确保与新部署的合约匹配 - 添加快速网络连接测试脚本,验证合约连接和网络状态 - 实现完整测试场景脚本,模拟用户操作和流动性管理
This commit is contained in:
97
scripts/quick-test.js
Normal file
97
scripts/quick-test.js
Normal file
@ -0,0 +1,97 @@
|
||||
const { ethers } = require("hardhat");
|
||||
const fs = require("fs");
|
||||
|
||||
// 颜色输出
|
||||
const colors = {
|
||||
reset: '\x1b[0m',
|
||||
red: '\x1b[31m',
|
||||
green: '\x1b[32m',
|
||||
yellow: '\x1b[33m',
|
||||
blue: '\x1b[34m',
|
||||
cyan: '\x1b[36m',
|
||||
white: '\x1b[37m'
|
||||
};
|
||||
|
||||
function log(color, message) {
|
||||
console.log(`${colors[color]}${message}${colors.reset}`);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
log('cyan', '🔍 快速网络和合约连接测试');
|
||||
|
||||
try {
|
||||
// 测试网络连接
|
||||
log('blue', '\n1. 测试网络连接...');
|
||||
const signers = await ethers.getSigners();
|
||||
const signer = signers[0];
|
||||
const network = await signer.provider.getNetwork();
|
||||
|
||||
log('green', `✅ 网络连接成功: ${network.name || 'localhost'} (Chain ID: ${network.chainId})`);
|
||||
log('white', `账户地址: ${signer.address}`);
|
||||
|
||||
const balance = await signer.provider.getBalance(signer.address);
|
||||
log('white', `ETH 余额: ${ethers.formatEther(balance)}`);
|
||||
|
||||
// 测试合约地址文件
|
||||
log('blue', '\n2. 检查合约地址文件...');
|
||||
if (!fs.existsSync("contract-addresses.json")) {
|
||||
log('red', '❌ 合约地址文件不存在,请运行 npm run deploy');
|
||||
return;
|
||||
}
|
||||
|
||||
const addresses = JSON.parse(fs.readFileSync("contract-addresses.json", "utf8"));
|
||||
log('green', '✅ 合约地址文件存在');
|
||||
log('white', `TokenA: ${addresses.TokenA}`);
|
||||
log('white', `TokenB: ${addresses.TokenB}`);
|
||||
log('white', `MiniSwapAMM: ${addresses.MiniSwapAMM}`);
|
||||
|
||||
// 测试合约连接
|
||||
log('blue', '\n3. 测试合约连接...');
|
||||
|
||||
const TokenA = await ethers.getContractFactory("TokenA");
|
||||
const TokenB = await ethers.getContractFactory("TokenB");
|
||||
const MiniSwapAMM = await ethers.getContractFactory("MiniSwapAMM");
|
||||
|
||||
const tokenA = TokenA.attach(addresses.TokenA);
|
||||
const tokenB = TokenB.attach(addresses.TokenB);
|
||||
const amm = MiniSwapAMM.attach(addresses.MiniSwapAMM);
|
||||
|
||||
// 测试基本调用
|
||||
const nameA = await tokenA.name();
|
||||
const nameB = await tokenB.name();
|
||||
const [reserveA, reserveB] = await amm.getReserves();
|
||||
|
||||
log('green', '✅ 合约连接成功');
|
||||
log('white', `TokenA 名称: ${nameA}`);
|
||||
log('white', `TokenB 名称: ${nameB}`);
|
||||
log('white', `AMM 储备: ${ethers.formatEther(reserveA)} TKA, ${ethers.formatEther(reserveB)} TKB`);
|
||||
|
||||
// 测试余额查询
|
||||
log('blue', '\n4. 测试代币余额查询...');
|
||||
const balanceA = await tokenA.balanceOf(signer.address);
|
||||
const balanceB = await tokenB.balanceOf(signer.address);
|
||||
const lpBalance = await amm.balanceOf(signer.address);
|
||||
|
||||
log('green', '✅ 余额查询成功');
|
||||
log('white', `TokenA 余额: ${ethers.formatEther(balanceA)} TKA`);
|
||||
log('white', `TokenB 余额: ${ethers.formatEther(balanceB)} TKB`);
|
||||
log('white', `LP Token 余额: ${ethers.formatEther(lpBalance)} MSLP`);
|
||||
|
||||
log('cyan', '\n🎉 所有测试通过!CLI 工具应该可以正常工作了');
|
||||
log('yellow', '现在可以运行: npm run cli');
|
||||
|
||||
} catch (error) {
|
||||
log('red', `❌ 测试失败: ${error.message}`);
|
||||
log('yellow', '\n请检查:');
|
||||
log('white', '1. 本地网络是否正在运行: npm run node');
|
||||
log('white', '2. 合约是否已部署: npm run deploy');
|
||||
log('white', '3. 网络配置是否正确');
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => process.exit(0))
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user