无法在坚固的功能中需要ERC-20令牌
我有一个基本的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在批准
所有者
地址,但是实际的msg.sender
transferfrom()
函数是item
地址(作为函数的呼叫者是item
合同)。解决方案:批准
item
地址作为spender。You're giving the approval to the
owner
address, but the actualmsg.sender
inside thetransferFrom()
function is theItem
address (as the caller of the function is theItem
contract).Solution: Approve the
Item
address as the spender.