Make 0-valued transfers valid semantics (fire the event and return true)
See original GitHub issueEDIT: Replace “revert” with “returned false” in this issue; I realized StandardToken does not throw on error but then realized the issue is practically the same and still just as harmful
Reverting on 0-valued transfer and approval is an anti-pattern that pops up repeatedly in code reviews. I realized it’s because many people are copying your StandardToken.
These two lines should have && _value > 0
removed:
if (balances[msg.sender] >= _value && _value > 0) {
if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) {
0-valued transfers and approvals should be valid and behave almost like no-ops, without reverting state. This is so that consumer contracts do not have to take care to work around this edge case.
Issue Analytics
- State:
- Created 6 years ago
- Comments:18 (7 by maintainers)
Top Results From Across the Web
Listener (Infinispan Distribution 7.0.3.Final API)
Class-level annotation used to annotate an object as being a valid cache listener. Used with the Listenable.addListener(Object) and related APIs.
Read more >FSMDirector
Return true if the model errors are handled. boolean, implementsStrictActorSemantics(). Return true if all state refinements have directors that implement the ...
Read more >#GTMtips: Create A Generic Event Tag | Simo Ahava's blog
How to create a generic event tag using Google Tag Manager. ... and making sure that the Tag fires whenever the GAEvent value...
Read more >Design for ECSL-DP - Institute for Software Integrated Systems
Title: Compositional Specification of Behavioral Semantics ... while the execution of a guard only returns a Boolean value for the true or false...
Read more >Database Engine events and errors - SQL Server
ls' used with WAITFOR is not a valid value. ... Additional messages in the SQL Server error log and system event log may...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Great points and well put @mbrock. Agreed.
Thanks for the comments/discussions.
Will change/fix soon.
I also agree that it’s more parsimonious to have 0-valued transfers be harmless no-ops rather than failures.
It’s kind of like how
append(x, [])
is a harmless no-op rather than something which causes anEmptyAppendException
.From an algebraic perspective, zero being the identity for additive operations is an ancient and fundamental meme; returning an error on “addition by zero” is surprising and unusual.
We don’t yet know which contract will be the first to be surprised by this behavior. We’ve already seen some possible examples by @nmushegian.
For example a system that interacts with any number of external tokens and imposes some kind of configurable fees denominated in tokens. That fee is changed to 0 for some reason, which causes the contract to attempt to transfer
0 * x
, which then fails unexpectedly, and now some user’s funds are locked.That kind of problem seems much more serious than the risk of accidentally wasting a user’s gas because of a glitchy frontend. Such gas costs are a very limited risk, whereas the 0 fee example is an unbounded risk.
Someone could ask “why are you writing code that appends an empty string?”, and maybe the examples I came up with would sound contrived – but I still don’t want the string library to fail in such cases.