首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数选择器未识别错误

函数选择器未识别错误
EN

Ethereum用户
提问于 2022-05-06 11:38:00
回答 1查看 2.1K关注 0票数 0

我正在扩展OpenZeppelins ERC20,以创建一个令牌,用于在另一个智能契约中进行测试。以下是它的外观:

代码语言:javascript
复制
//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));
    }
}

这是我如何使用它的另一个合同:

代码语言:javascript
复制
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在构造函数中实例化:

代码语言:javascript
复制
IERC20 private _token;
constructor(address token, uint256 duration) {
        owner = msg.sender;
        deployedAt = block.timestamp;
        _token = IERC20(token);
    }

因此,在部署要使用令牌的契约时,我传递令牌契约的地址。

但是,当我使用hardhat运行这个js测试代码时:

代码语言:javascript
复制
    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);

我得到以下错误:

代码语言:javascript
复制
 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继承。不知道为什么会失败

EN

回答 1

Ethereum用户

发布于 2022-05-06 12:40:19

因此,在两个deployContract函数中,我将硬编码地址附加到契约实例,例如:

代码语言:javascript
复制
const Contract = await ethers.getContractFactory("TestERC20Token");
const contract = await Contract.deploy(initialMintAmount);
contract.attach("0x5FbDB2315678afecb367f032d93F642f64180aa3");
await contract.deployed();

在删除它并在部署后获得地址之后,上面的问题就消失了。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/127676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档