ERC20标准中的批准和允许方法到底是做什么的?

22

问题是什么是allowanceapprove真正做的事情?

_spender是什么,它在做什么?

有人能解释一下吗?

contract Token {
    uint256 public totalSupply;
    function balanceOf(address _owner) constant returns (uint256 balance);
    function transfer(address _to, uint256 value) returns (bool success);
    function transferFrom(address _from, address _to, uint256 value) returns (bool success);
    function approve(address _spender, uint256 _value) returns (bool success);
    function allowance(address _owner, address _spender) constant returns (uint256 remaining);
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
2个回答

57

“allowance”和“approve”究竟是做什么用的?

我们假设有用户A和用户BA拥有1000个代币并想授权B使用其中的100个。

  • A将调用:approve(address(B), 100, {"from": address(A)})
  • B将通过调用allowance(address(A), address(B))检查A给予他使用的代币数量。
  • B将通过调用transferFrom(address(A), address(B), 100, {"from": address(B)})将这些代币发送到自己的账户中。

3
在 transferFrom 中,B 可以将这笔钱转给其他人,比如 C 吗? - Eres
1
是的,在那种情况下,B可以向C发送令牌。@EresDev - Rabiul Islam

10
  • Allowance means that we can grant approval to another contract or address to be able to transfer our ERC20 tokens. And this requirement is common in distributed applications, such as escrows, games, auctions, etc. Hence, we need a way to approve other addresses to spend our tokens. Let's say you have tether contract and you want a DEX(Decentralized Exchange) or any other entity transfer coins from the tether contract. So you keep track of which entity how much can transfer from tether contract in a mapping.

     // my address is allowing your address for this much token
     mapping(address=>mapping(address=>uint)) public allowance;
    
  • In the ERC20 standard, we have a global variable allowed in which we keep the mapping from an "owner's address" to an "approved spender’s" address and then to the amount of tokens. Calling approve() function can add an approval to its desired _spender and _value. The amount of token is not checked here and it will be checked in transfer().

  • Once the approval is granted, the "approved spender" can use transferFrom() to transfer tokens. _from is the owner address and _to is the receiver’s address and _value is the required number of tokens to be sent. First, we check if the owner actually possesses the required number of tokens.

假设您想向一个DEFI平台存入一些以太币。与DEFI平台交互实际上是与该平台的智能合约进行交互。在存款之前,您首先需要批准交易。这意味着您告诉该合约地址可以从我的账户中取出一些钱。然后,您调用DEFI智能合约的deposit函数并存入资金。以下是转移发生的顺序:
1- 在Defi内部,defi智能合约有deposit来获取tether代币。
function depositTokens(uint _amount) public{
  require(_amount>0,'amount cannot be zero');
  // transfer tether to this contract address for staking
  tether.transferFrom(msg.sender,address(this), _amount);
 // update the state inside Defi, like staked tokens, amount etc
}

2- 在tether内部,我们有transferFrom

mapping(address=>mapping(address=>uint)) public allowance;

function transferFrom(address _from, address _to, uint256 _value) public returns (bool success){
        // check the allowance
        require(_value <=allowance[_from][msg.sender]);
        balanceOf[_to]+=_value;
        balanceOf[_from]-=_value;
        allowance[_from][msg.sender]-=_value;
        emit Transfer(_from,_to,_value);
        return true;
    }

首先需要检查“津贴(allowance)”:mapping(address=>mapping(address=>uint)) public allowance。所以在调用transferFrom之前,tether合约必须更新其allowance映射,以使此过程顺利运行。

3- 使用approve更新津贴:

function approve(address _spender, uint _value)public returns (bool success){
        allowance[msg.sender][_spender]=_value;
        // This event must trigger when a successful call is made to the approve function.
        emit Approval(msg.sender,_spender,_value);
        return true;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接