Coder Social home page Coder Social logo

Comments (2)

vladbochok avatar vladbochok commented on July 30, 2024

Thanks @PatrickAlphaC. This is correct, but forceApprove may be better our case, wdyt?

In the only usage of safeApprove, the intention is to approve to 0, before actual approve to support USDT like tokens. This can be achieved by using forceApprove instead.

from era-contracts.

PatrickAlphaC avatar PatrickAlphaC commented on July 30, 2024

You introduce a race condition if you always use forceApprove. Here is the scenario:

  1. Bob forceApprove Sally to spend 10 tokens.
  2. Bob decides he actually wants her to be able to spend 15 instead of 10, so sends another forceApprove transaction for 15 tokens.
  3. Before Bob's transaction lands, Sally notices this, and front-runs the transaction, spends the 10 tokens. Now Bob's forceApprove transaction hits for an additional 15.
  4. Sally is able to spend 25 tokens instead of the 15 she was supposed to spend.

If instead, Bob used safeIncreaseAllowance, he would have increased the allowance by 5, so Sally only would have ever been able to spend the 15 tokens Bob originally intended.

Additionally, the new openzeppelin contracts use forceApprove inside of the safeIncreaseAllowance function anyways.

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/53b5d84212b73388a2a7096ff503693dae692726/contracts/token/ERC20/utils/SafeERC20.sol#L52

from era-contracts.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.