prb-contracts icon indicating copy to clipboard operation
prb-contracts copied to clipboard

Rewrite ERC-20 implementation to use EIP-6093

Open PaulRBerg opened this issue 3 years ago • 2 comments

See EIP-6093.

interface ERC20Errors {
    error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed);
    error ERC20InvalidSender(address sender);
    error ERC20InvalidReceiver(address receiver);
    error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed);
    error ERC20InvalidApprover(address approver);
    error ERC20InvalidSpender(address spender);
}

Though I am not sure it would be worth it to work on this given that OpenZeppelin v5 will come with custom errors.

PaulRBerg avatar Feb 06 '23 10:02 PaulRBerg

@PaulRBerg

There is no need to do a complete rewrite of the ERC-20 implementation to use EIP-6093, as the change is only in the way error messages are triggered and not in the underlying mechanisms of the token.

The ERC-20 implementation can now use EIP-6093 simply by including the ERC20Errors interface in the token class. Then, instead of throwing exceptions, the error messages are returned using the revert() function passing the corresponding error message as an argument.

For example, instead of throwing an exception with the following code:

require(balance >= needed, "ERC20InsufficientBalance");

to

require(balance >= needed, new bytes32("ERC20InsufficientBalance(address,uint256,uint256)").packed(address(this), balance, needed));

developerfred avatar Feb 06 '23 21:02 developerfred

Thanks @developerfred, but I was referring to the ERC-20 implementation in this repo:

https://github.com/PaulRBerg/prb-contracts/blob/5af7b4c6b3c0bce09fee677b251e99344ac5d426/src/token/erc20/IERC20.sol

PaulRBerg avatar Feb 06 '23 22:02 PaulRBerg