I’m brand new to Solidity and am wondering how, in openzeppelin-solidity 2.3.0, transferFrom(), in ERC20.sol, enforces that the caller must have allowance for sender's tokens of at least amount?
I see that in earlier versions of openzeppelin-solidity, transferFrom() does a require check for this but I don’t see this in 2.3.0. Can someone kindly explain this to me? Wondering if I need to implement this myself? Thank you.
ERC20 and ERC721 are now more gas efficient due to removed redundant SSTORE s and require s. (#1409 and #1549)
The allowance check is handled by the subtraction from _allowances using SafeMath.
If the allowance is not sufficient then there is a revert with reason "SafeMath: subtraction overflow"
it('reverts when more than the full allowance is removed', async function () {
await expectRevert(
this.token.decreaseAllowance(spender, approvedAmount.addn(1), { from: initialHolder }),
'SafeMath: subtraction overflow'
);
});
Thanks very much @abcoathup! Super helpful. A bit of a newb question but…
in SafeMath.sol, the sub function takes two parameters (a and b), however, when sub() is called in ERC20.sol, it only passes in one single parameter (amount). I see that it is:
_allowances[sender][msg.sender].sub(amount));
Does using the dot notation (chaining) somehow pass _allowances in as the first parameter and “amount” as the second parameter to the sub method?
All questions are good . (I had to look up the answer as I didn't know either).
_allowances is passed as the first parameter to sub due to the usingfor in the ERC20 contract.
contract ERC20 is IERC20 {
using SafeMath for uint256;
The Solidity documentation explains how this works:
https://solidity.readthedocs.io/en/latest/contracts.html#using-for
The directive using A for B; can be used to attach library functions (from the library A ) to any type ( B ). These functions will receive the object they are called on as their first parameter (like the self variable in Python).
Housekeeping: I marked my previous reply as the solution to your question and moved to #support:openzeppelin category.
I would suggest asking a question per topic (unless the questions are related), so that other people can easily find the answers.