【问题标题】:Implement IBEP20 interface in smart contract在智能合约中实现 IBEP20 接口
【发布时间】:2022-01-09 20:30:55
【问题描述】:
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;
  }
}

我尝试在合约中充BUSD,调用Buy方法时报错:“insufficient quota”。

【问题讨论】:

    标签: solidity smartcontracts bep20


    【解决方案1】:

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

    【讨论】:

    • 对不起,我不确定,你能给我一些例子吗?我是新手。
    • 主要是前端的东西,它取决于您使用的工具,但只是执行对令牌合约调用批准函数的调用
    猜你喜欢
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    相关资源
    最近更新 更多