ERC20津贴再次不足

问题描述 投票:0回答:1

我知道这个问题被问得太频繁了。我很抱歉,但我不太明白很多答案。我一直在尝试创建一个质押合约,但这个错误困扰了我。这不是权益功能代码本身,只是转移部分。 这是代码:

   function stake(uint256 _amount) public {
        stakingToken.transferFrom(msg.sender, address(this), _amount);
        stakingToken.approve(msg.sender, _amount);
    }

这是错误:

The transaction has been reverted to the initial state.
Reason provided by the contract: "ERC20: insufficient allowance".
Debug the transaction to get more information.

我尝试将approve 功能放在第一位,而TransferFrom 最后。我尝试过使用 Transfer 函数,但都出现了完全相同的错误。

我还将我的 Gas 限制增加到 12000000,我也尝试添加以太币,但没有任何效果。

如果这个问题困扰您,我深表歉意,我只是在寻找能够解决我的问题的信息丰富的答案。

愿你到达星星。

ethereum solidity transfer erc20
1个回答
0
投票

问题是将

approve
函数放入
stake
函数中:

stakingToken.approve(msg.sender, _amount);

下面是控制流程来说明:

     stake()                  approve()
user   -->   staking contract    -->   staking token

从质押代币的角度来看,

approve
调用是质押合约,允许用户花费质押合约的代币

但我们想要的是相反的:用户允许质押合约花费用户的代币。

解决方案:要进行所需的转账,您必须有两个单独的交易:

  1. 用户直接在质押代币上调用
    approve
    ,以允许质押合约花费用户的代币。
     approve()
user    -->   staking token
  1. 用户调用
    stake
    ,将代币从用户转移到质押合约。
     stake()
user   -->   staking contract
© www.soinside.com 2019 - 2024. All rights reserved.