无法在坚固的功能中需要ERC-20令牌

发布于 2025-02-07 15:14:51 字数 1633 浏览 1 评论 0原文

我有一个基本的ERC-20 令牌定义为

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {
    constructor() ERC20("Token", "TOKEN") {}

    function mint(address account, uint256 amount) public {
        _mint(account, amount);
    }
}

我有另一个合同item,该方法的方法我想花费1代币,基于答案。

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Item {
    IERC20 private token;

    constructor(address tokenAddress) {
        token = IERC20(tokenAddress);
    }

    function myMethod() public {
        bool success = token.transferFrom(msg.sender, address(this), 1 ether); // also tried just 1
        require(success, "Insufficient Funds: Requires 1 Token");
        // do things
    }
}

https://ethereum.stackexchange.com/a/a/78911/30804

  const TokenFactory = await ethers.getContractFactory("Token");
  Token = await Token.deploy();
  await Token.mint(owner.address, ethers.utils.parseUnits("1", 18));
  await Token.approve(owner.address, ethers.utils.parseUnits("1", 18));

  const ItemFactory = await ethers.getContractFactory("Item");
  Item = await ItemFactory.deploy(Token.address);
  await Item.myMethod(); // this is the line that errors

一切都在运行,我可以从坚固的合同中看到调试代码,但是我一直遇到一个错误

用理性字符串'erc20恢复:不足津贴'

,我追溯到Openzeppelin内部功能_spendallowance

在进行一次调试之后,我以为罪魁祸首是因为我需要批准,所以我添加了一个批准的电话,但是我仍然遇到与同样的错误

,我假设我的问题是基本的问题,谁能看到它可能是什么?提前致谢。

I have a basic ERC-20 Token defined as

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {
    constructor() ERC20("Token", "TOKEN") {}

    function mint(address account, uint256 amount) public {
        _mint(account, amount);
    }
}

I have another contract Item that has a method I want to cost 1 Token, basing answer off https://ethereum.stackexchange.com/a/78911/30804

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Item {
    IERC20 private token;

    constructor(address tokenAddress) {
        token = IERC20(tokenAddress);
    }

    function myMethod() public {
        bool success = token.transferFrom(msg.sender, address(this), 1 ether); // also tried just 1
        require(success, "Insufficient Funds: Requires 1 Token");
        // do things
    }
}

I'm running this inside a Hardhat test

  const TokenFactory = await ethers.getContractFactory("Token");
  Token = await Token.deploy();
  await Token.mint(owner.address, ethers.utils.parseUnits("1", 18));
  await Token.approve(owner.address, ethers.utils.parseUnits("1", 18));

  const ItemFactory = await ethers.getContractFactory("Item");
  Item = await ItemFactory.deploy(Token.address);
  await Item.myMethod(); // this is the line that errors

Everything runs and I can see debug code from the solidity contracts, but I keep getting an error that

reverted with reason string 'ERC20: insufficient allowance'

that I traced back to the openzeppelin internal function _spendAllowance

After some debugging I thought that the culprit was because I needed to approve, so I added a call to approve but I'm still getting the same error

I'm assuming my issue is something basic can anyone see what it might be? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-02-14 15:14:51
await Token.approve(owner.address, ethers.utils.parseUnits("1", 18));

您正在批准所有者地址,但是实际的msg.sender transferfrom()函数是item地址(作为函数的呼叫者是item合同)。

解决方案:批准item地址作为spender。

// after the `Item` contract has been deployed
await Token.approve(Item.address, ethers.utils.parseUnits("1", 18));
await Item.myMethod();
await Token.approve(owner.address, ethers.utils.parseUnits("1", 18));

You're giving the approval to the owner address, but the actual msg.sender inside the transferFrom() function is the Item address (as the caller of the function is the Item contract).

Solution: Approve the Item address as the spender.

// after the `Item` contract has been deployed
await Token.approve(Item.address, ethers.utils.parseUnits("1", 18));
await Item.myMethod();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文