0
contract Main  {
  string public name_ = "Test";

  mapping (address=>bool) addressIsApproved; 

  IBEP20 public immutable busd;
  constructor (IBEP20 _busdContract){
    busd = _busdContract;
  }


  function approve (uint256 _amount) public {
     bool isApproved =  IBEP20(busd).approve(msg.sender,_amount);
     addressIsApproved[msg.sender] = isApproved;
  }

  function buy(uint256 _amount) public returns (uint) {
      //
      bool isApproved = addressIsApproved[msg.sender];
      if (!isApproved) return 0;

      bool isPay =  IBEP20(busd).transferFrom(msg.sender,address(this), _amount);  
      if (!isPay) return 0;

      //do something...;
      
      return 1;
  }
}

I tried to charge BUSD in the contract, and when calling the Buy method, there is an error message: "insufficient allowance".

4

1 回答 1

1

在批准功能中,当您调用IBEP20(busd).approve(msg.sender,amount)您的合同时,就是将交易发送到 busd 合同,所以在这里您没有批准您的合同来移动用户令牌,您正在做相反的事情,您正在批准用户移动令牌合约拥有,如果要用户批准合约,用户应该先直接调用busd合约的approve函数,然后调用buy函数

于 2021-12-03T13:45:04.530 回答