Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-return-data ERC20 support to SafeERC20. #1655

Merged
merged 6 commits into from
Feb 28, 2019
Merged

Conversation

nventuro
Copy link
Contributor

Reimplementation of #1344, based on discussion from there (thanks @k06a for inspiration for the assembly code!).

This adds support to SafeERC20 for ERC20 contracts that return no value (i.e. those that do nothing on success, and revert/throw when an error occurs). Solidity now performs automatic return data size checks, so prior to this patch those contracts would've caused a revert. We now resort to low level calls to workaround this.

I've refactored the mocks and tests in the process, since they were getting a bit out of hand. I tried running the old contract against the updated test suite, and as expected, all tests pass except the ones in the 'with token that returns no boolean values' describe block.

@nventuro nventuro added feature New contracts, functions, or helpers. contracts Smart contract code. labels Feb 27, 2019
@nventuro nventuro added this to the v2.2 milestone Feb 27, 2019
@nventuro nventuro self-assigned this Feb 27, 2019
contracts/token/ERC20/SafeERC20.sol Outdated Show resolved Hide resolved
* @dev Parses the return value of the last contract call and attempts to return a boolean value out of it.
*/
function getBooleanReturnValue() private pure returns (bool result) {
// We need to use inline assembly here, since it is the only way we can utilize the returndatasize opcode.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true for the opcode, but we could be using the data byte array returned by call and reading data.length. What do you think?

I don't feel so comfortable having this function use the returndata opcodes because they're like reading from global variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data.length sure, but I'm not sure if there's a convenient way to check for a boolean value stored inside the bytes array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's a good time to introduce that bytes conversion library. 👀

test/token/ERC20/SafeERC20.test.js Show resolved Hide resolved
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@nventuro nventuro merged commit 41aa39a into master Feb 28, 2019
@nventuro nventuro deleted the safeerc20-no-return branch February 28, 2019 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contracts Smart contract code. feature New contracts, functions, or helpers.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants