我正在扩展OpenZeppelins ERC20,以创建一个令牌,用于在另一个智能契约中进行测试。以下是它的外观:
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract TestERC20Token is ERC20 {
constructor(uint256 initialSupply) ERC20("Test Coin", "TEST") {
_mint(msg.sender, initialSupply * (10 ** 18));
}
}这是我如何使用它的另一个合同:
function deposit(uint256 amount)
external
checkTokenBalance(msg.sender, amount)
{
if (!_isClosedForDeposit()) {
_token.transferFrom(msg.sender, address(this), amount);
deposits[msg.sender] += amount;
totalDeposits += amount;
_addToDepositors(msg.sender);
} else {
revert DepositPeriodExpired("Depost period has expired");
}
}_token在构造函数中实例化:
IERC20 private _token;
constructor(address token, uint256 duration) {
owner = msg.sender;
deployedAt = block.timestamp;
_token = IERC20(token);
}因此,在部署要使用令牌的契约时,我传递令牌契约的地址。
但是,当我使用hardhat运行这个js测试代码时:
await deployTokenContract();
await deployOtherContract(5);
const amount = utils.parseUnits("5", 18);
const token = await initTokenContract(accounts.one.privateKey);
const approveTx = await token.approve(contractAddress, amount);
const res = await approveTx.wait(1);
const allowance = await token.allowance(accounts.one.address, contractAddress);
const otherContract = await initOtherContract(accounts.one.privateKey);
const depositTx = await otherContract.deposit(amount, {
gasLimit: 25000
});
await depositTx.wait(1);我得到以下错误:
Error: Transaction reverted: function selector was not recognized and there's no fallback function
at TestERC20Token.<unrecognized-selector> (contracts/TestERC20Token.sol:6)在我看来,当在我的另一个契约中调用transferFrom时,这个函数不在TestERC20Token契约中,而是应该从ERC20继承。不知道为什么会失败
发布于 2022-05-06 12:40:19
因此,在两个deployContract函数中,我将硬编码地址附加到契约实例,例如:
const Contract = await ethers.getContractFactory("TestERC20Token");
const contract = await Contract.deploy(initialMintAmount);
contract.attach("0x5FbDB2315678afecb367f032d93F642f64180aa3");
await contract.deployed();在删除它并在部署后获得地址之后,上面的问题就消失了。
https://ethereum.stackexchange.com/questions/127676
复制相似问题