const { expect } = require("chai");
const { ethers } = require("hardhat"); // Import ethers from Hardhat
describe("Query Account Balance on Forked Mainnet", function () {
it("should fetch the balance of the specified account", async function () {
// Replace this with the address you want to query
const targetAddress = "...";
// Use ethers to get the balance
const balance = await ethers.provider.getBalance(targetAddress);
// Log the balance for debugging
console.log(`Balance of ${targetAddress}: ${balance} ETH`);
// Verify that the balance is returned as a BigNumber
expect(balance).to.be.a("BigInt");
});
});
或是以下獲取主網特定地址的 USDC 餘額
const { ethers } = require("hardhat");
const { expect } = require("chai");
describe("USDC Balance Check", function () {
it("Should return the USDC balance of the address", async function () {
const usdcAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; // USDC Contract Address
const addressToCheck = "..."; // Address whose balance you want to check
// USDC Contract ABI (Simplified; use the full ABI from Etherscan)
const usdcAbi = ["function balanceOf(address owner) view returns (uint256)"];
// Connect to the USDC contract
const usdcContract = new ethers.Contract(usdcAddress, usdcAbi, ethers.provider);
// Fetch the balance
const balance = await usdcContract.balanceOf(addressToCheck);
// Log the balance (optional)
console.log("USDC Balance:", balance.toString());
// 也可手動改變地址 ETH 餘額
const mainnetAccount = addressToCheck; // Replace with a mainnet account address
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [mainnetAccount],
});
await hre.network.provider.send("hardhat_setBalance", [
mainnetAccount,
"0x8AC7230489E80000000", // 40960 ETH
]);
const ethBalance = await ethers.provider.getBalance(addressToCheck);
// Log the balance (optional)
console.log("ETH Balance:", ethers.formatEther(ethBalance), "ETH");
// Assert (for example, checking if the balance is greater than a certain amount)
expect(balance).to.be.gt(ethers.parseUnits("100", 6)); // Check if balance is greater than 100 USDC
});
});
測試檔案內設置區塊時間
const { ethers } = require("hardhat");
// Fast forward time by 7 days for testing
await ethers.provider.send("evm_increaseTime", [7 * 24 * 60 * 60]);
await ethers.provider.send("evm_mine");
import { ethers } from "hardhat";
async function main() {
const EthStaker = await ethers.deployContract("EthStaker");
await EthStaker.waitForDeployment();
console.log(
`EthStaker deployed to ${EthStaker.target}`
);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
之後輸入
npx hardhat run scripts/deploy.ts --network someTestnet