我在玩hardhat
。我的目标是balanceOf
从分叉的主网络打印特定地址。
hardhat.config.js
require("@nomiclabs/hardhat-waffle");
module.exports = {
solidity: "0.8.0",
networks: {
hardhat: {
forking: {
url: "https://eth-mainnet.alchemyapi.io/v2/my-api-key",
},
},
},
};
合同:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "hardhat/console.sol";
interface IERC20 {
function totalSupply() external view returns (uint);
function balanceOf(address account) external view returns (uint);
function transfer(address recipient, uint amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
contract Balance {
function getBalance() public returns (uint) {
address ethAddr = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
address addrWithBalance = 0x00000000219ab540356cBB839Cbe05303d7705Fa; // <-- address from etherium scan from main net with huge balance
uint balance = IERC20(ethAddr).balanceOf(addrWithBalance);
console.log(balance); // <- here print 0, expecting 7,729,794.000069000000000069 ether
return balance;
}
}
测试:
it("Should print balance", async function () {
const BalanceContract = await ethers.getContractFactory("Balance");
const balanceToken = await BalanceContract.deploy();
const balance = await balanceToken.getBalance()
console.log(balance)
});