快速示例:读取代币信息
复制
询问AI
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider("https://evmrpc.kasplex.org");
const FACTORY = "0x5F65Df9DCf7d764CA9d319Baf89611d289546A81";
const FACTORY_ABI = [
"function getTokenInfo(address token) view returns (tuple(uint256 tokenId, address creator, address pool, uint256 tokenReserves, uint256 kasReserves, uint256 createdAt, bool fulfilled, bool graduated, string metadataUri, uint256 paramVersion))",
"function getCurrentPrice(address token) view returns (uint256)",
"function getBondingCurveProgress(address token) view returns (uint256)",
];
const factory = new ethers.Contract(FACTORY, FACTORY_ABI, provider);
const TOKEN = "0x..."; // 代币地址
const info = await factory.getTokenInfo(TOKEN);
const price = await factory.getCurrentPrice(TOKEN);
const progress = await factory.getBondingCurveProgress(TOKEN);
console.log("Token ID:", info.tokenId.toString());
console.log("储备:", ethers.formatEther(info.kasReserves), "KAS");
console.log("价格:", ethers.formatEther(price), "KAS");
console.log("进度:", Number(progress) / 100, "%");
console.log("已毕业:", info.graduated);
快速示例:买入代币
复制
询问AI
import { ethers } from "ethers";
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
const FACTORY = "0x5F65Df9DCf7d764CA9d319Baf89611d289546A81";
const FACTORY_ABI = [
"function buy(address token, uint256 minTokensOut) payable",
];
const factory = new ethers.Contract(FACTORY, FACTORY_ABI, signer);
const TOKEN = "0x...";
const kasAmount = ethers.parseEther("100"); // 100 KAS
const minTokensOut = 0n; // 生产环境请设置合理的滑点保护!
const tx = await factory.buy(TOKEN, minTokensOut, { value: kasAmount });
await tx.wait();
console.log("买入交易:", tx.hash);
请务必设置合理的
minTokensOut 进行滑点保护。设置为 0 意味着没有滑点保护,仅适用于测试。