https://eips.ethereum.org/EIPS/eip-20 says this about the decimals() method for an ERC20 token:

Returns the number of decimals the token uses - e.g. 8, means to divide the token amount by 100000000 to get its user representation.

OPTIONAL - This method can be used to improve usability, but interfaces and other contracts MUST NOT expect these values to be present.

function decimals() public view returns (uint8)

Is there anything, anywhere, that talks about whether or not different values can be returned when decimals() is called? (Not at all saying that it would be a good thing, or at all useful, but going on the idea that if an idea is terrible, some token has implemented it.)


The standard doesn't say anything about how the contract needs to be implemented. It only says which function signatures need to be implemented. So, in theory, you can write a fully standard compatible contract which only reverts with each function, for example.

So you can add any sort of custom logic in the decimals() function, as long as the signature stays the same (well, the function is not even needed in the standard, but if it's used it should be with the given signature).

It's not uncommon for some more complicated token contracts to have custom logic in, especially, the balanceOf function. It can return the balance based on various calculations. Here's an example: https://github.com/superfluid-finance/protocol-monorepo/blob/dev/packages/ethereum-contracts/contracts/superfluid/SuperToken.sol#L365

  • Yeah, that's what I was afraid of. ERC20 appears to be a standard that doesn't really have very many hard requirements. Looks like you could return a pseudorandom number for decimals and still be compliant. (Not that the token would be useful - that's a completely different question.) – James Moore Apr 10 at 21:46

Is there anything, anywhere, that talks about whether or not different values can be returned when decimals() is called?

No, but in practice, your token would not be listed on any exchange if you do something like this, because it would break a lot of assumptions that are not in the standard.

  • You're right, but what you're saying is unrelated to the question. If you want to write software that works with "ERC20 tokens" you need to be prepared for whatever an ERC20 can do. Whether or not it's a good idea for them to do that is a completely different kind of question. – James Moore Apr 10 at 21:44
  • Nobody is prepared for what you suggest, so you are mixing theoretical idealism with what is practical today. – Mikko Ohtamaa Apr 12 at 8:32
  • There are definitely a set of people who need to worry about this kind of problem, today, in the real world. Anyone working on anything like a decentralized exchange has to care. You can't just say "don't list these sorts of tokens" because that assumes you have a mechanism to decide what tokens you're talking about in the first place, or even a meaningful way to say what listing a token means. What happens when users can trade any token and they use one that return random numbers for decimals()? It's easy to say don't do that, but it's much harder to figure out how to enforce that. – James Moore Apr 14 at 18:40
  • "There are definitely a set of people who need to worry about this kind of problem" I was there when ERC-20 standard was being discussed. It is more guidelining, created by few people, not a paid committee. Your worries are right, but your perspective how the ecosystem operates is incorrect. There is no party where you can go and file complaint if the there is no perfect standard in perfect world. What comes to listing and trading crap, the problem is avoided by not promoting tokens that do not have value. Why would user trade this in the first place? – Mikko Ohtamaa Apr 15 at 7:32
  • "Why would user trade this in the first place?" because the world has vast numbers of people who are crazy or stupid or malicious or combinations of all of those. You often need to write software for the worst case. It's not a question of why they would do it, it's a question of what happens when they do it. – James Moore Apr 17 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.