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

Refactor safe approve method #32

Open
gabririgo opened this issue Nov 26, 2023 · 0 comments
Open

Refactor safe approve method #32

gabririgo opened this issue Nov 26, 2023 · 0 comments

Comments

@gabririgo
Copy link
Contributor

Summary

Refactor the safe approve internal method _safeApprove(...args) to avoid the use of low-level calls.

Motivation

A refactoring of the safe approve method has the scope of:

  1. avoiding the use of low-level calls
  2. using a set of more explicit try/catch statements to improve readability
  3. allowing to exclude edge scenarios without having to make implicit/non-linear assumptions
  4. excluding the target token not being a smart contract in an alternative way

Specification

function _safeApprove(address token, address spender, uint256 amount) internal {
  try IToken(token).approve(spender, amount) returns (bool success) {
    assert(success);
  } catch {
    try IToken(token).approve(spender, amount) {

    } catch { revert(); }
  }
}

Rationale

Using a try/catch statement we will be able to have the transaction revert in case the approve call fails silently. While a silent failure does not cause negative side effects to the protocol (the swap transaction would revert with error), we will be able to exclude that the approval target token is a not smart contract or does not implement the method as expected. As a result, we can remove the assertion of the target token being a smart contract, which will result in net gas efficiency (will cost just slightly more in general).

Notes

  • An initial implementation has been proposed here.
  • As Uniswap is launching v4 sometime in Q1 2024, it would be ideal to have this proposal combined with the future proposal to support the new release.
@gabririgo gabririgo added enhancement New feature or request and removed enhancement New feature or request labels Nov 26, 2023
@gabririgo gabririgo changed the title feat: refactor safe approve method Refactor safe approve method Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant