简体   繁体   English

批准 function 不增加 ERC20 的津贴

[英]Aprove function does not increase allowance of ERC20

I am trying to implement a simple token transfer to a Vault but I'm having trouble approving the transaction and when I run tests using foundry, I receive this error:我正在尝试将简单的令牌传输到 Vault,但我在批准交易时遇到了麻烦,当我使用代工厂运行测试时,我收到此错误:

[FAIL. [失败。 Reason: ERC20: transfer amount exceeds allowance] testDeposit() (gas: 86770)原因:ERC20:转账金额超出限额] testDeposit() (gas: 86770)

My code is for the deposit function is here:我的代码用于存款 function 在这里:

function deposit(uint256 amount) external {
    console.log("RANDOM inside deposit = ");
    console.log(IERC20(underlyingToken).balanceOf(msg.sender));

    console.log("msg sender =");
    console.log(msg.sender);

    console.log("approve = ");
    console.log(IERC20(underlyingToken).approve(address(this), amount));

    // IERC20(underlyingToken).approve(msg.sender, amount);

    console.log("RANDOM inside deposit after approve = ");
    console.log(IERC20(underlyingToken).allowance(msg.sender, address(this)));


    IERC20(underlyingToken).transferFrom(msg.sender, address(this), amount);
    // // totalDeposited += amount;
    IPool(aavePool).supply(underlyingToken, amount, address(this), 0);

    totalUnderlyingDeposited += amount;
}

Thank you for the help in advance提前感谢您的帮助

You can't have the vault give itself an allowance for the sender.你不能让保险库给自己一个发件人的津贴。 That would defeat the whole point of the approval mechanism.这将破坏批准机制的全部意义。

What your code IERC20(underlyingToken).approve(address(this), amount) actually does is give the vault permission to transfer any of its own tokens using transferFrom .您的代码IERC20(underlyingToken).approve(address(this), amount)实际所做的是授予保险库使用transferFrom转移任何自己的令牌的权限。 Obviously this is a bit silly since the vault can just use transfer to do that.显然这有点傻,因为保险库可以使用transfer来做到这一点。

Your commented-out code // IERC20(underlyingToken).approve(msg.sender, amount);您注释掉的代码// IERC20(underlyingToken).approve(msg.sender, amount); , as you probably figured out, lets the sender transfer the vault's tokens. ,您可能已经猜到了,让发件人转移保险库的令牌。

The only way to let the vault do transferFrom(msg.sender, ..., ...) is if the sender interacts directly with the ERC20 by calling approve him/herself.让保管库执行transferFrom(msg.sender, ..., ...)的唯一方法是发送者通过调用approve他/她自己直接与 ERC20 交互。

This means the user will need to do two transactions to do the first deposit into the vault: 1) approve the vault for an allowance sufficient to cover the deposit 2) do the deposit.这意味着用户需要进行两次交易才能将第一笔存款存入保险库:1)批准保险库获得足以支付存款的津贴 2)进行存款。

If the approval is for an "infinite" amount (max uint256), then subsequent deposits only require a single transaction each;如果批准的是“无限”金额(最大 uint256),则后续存款每次只需要一次交易; however, this is not considered wise from a security standpoint.但是,从安全的角度来看,这被认为是不明智的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM