Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a way to clear approve first if token require it #1

Open
nachomazzara opened this issue Dec 12, 2018 · 1 comment
Open

Support a way to clear approve first if token require it #1

nachomazzara opened this issue Dec 12, 2018 · 1 comment
Assignees
Labels
good first issue Good for newcomers

Comments

@nachomazzara
Copy link
Owner

Some ERC20 tokens as MANA require to clear the approval before setting a new value

/**
   * @dev Aprove the passed address to spend the specified amount of tokens on behalf of msg.sender.
   * @param _spender The address which will spend the funds.
   * @param _value The amount of tokens to be spent.
   */
  function approve(address _spender, uint256 _value) returns (bool) {

    // To change the approve amount you first have to reduce the addresses`
    //  allowance to zero by calling `approve(_spender, 0)` if it is not
    //  already 0 to mitigate the race condition described here:
    //  https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
    require((_value == 0) || (allowed[msg.sender][_spender] == 0));

    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
    return true;
  }

The idea is to support this behaviour cleaning the approval first and the set it the new one

@nachomazzara nachomazzara added the good first issue Good for newcomers label Dec 12, 2018
@Agusx1211 Agusx1211 self-assigned this Dec 12, 2018
@nachomazzara
Copy link
Owner Author

Alternative:

function safeApprove(IERC20 _token, address _spender, uint256 _value) internal returns (bool) {
        uint256 prevAllowance = _token.allowance(address(this), _spender);

        if (prevAllowance > 0 && _value > 0) {
            // clear approve
            address(_token).call(
                abi.encodeWithSignature("approve(address,uint256)",_spender, 0)
            );
        }        
        
        address(_token).call(
            abi.encodeWithSignature("approve(address,uint256)",_spender, _value)
        );

        if (_token.allowance(address(this), _spender) != _value) {
            // Approve failed
            return false;
        }

        return true;
    }

We should update doc string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants