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

Refactor SafeTransferLib and interfaces #343

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/interfaces/IERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ pragma solidity >=0.5.0;
/// @title IERC20
/// @author Morpho Labs
/// @custom:contact [email protected]
interface IERC20 {
function transfer(address to, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
/// @dev Empty because we only call library functions. It prevents calling transfer (transferFrom) instead of safeTransfer (safeTransferFrom).
interface IERC20 {}
21 changes: 11 additions & 10 deletions src/libraries/SafeTransferLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import {ErrorsLib} from "../libraries/ErrorsLib.sol";

import {IERC20} from "../interfaces/IERC20.sol";

interface IERC20Internal {
function transfer(address to, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}

/// @title SafeTransferLib
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @notice Library to manage tokens not fully ERC20 compliant:
/// not returning a boolean for `transfer` and `transferFrom` functions.
/// @dev It is the responsibility of the market creator to make sure that the address of the token has non-zero code.
library SafeTransferLib {
function safeTransfer(IERC20 token, address to, uint256 value) internal {
(bool success, bytes memory returndata) = address(token).call(abi.encodeCall(token.transfer, (to, value)));
require(
success && address(token).code.length > 0 && (returndata.length == 0 || abi.decode(returndata, (bool))),
ErrorsLib.TRANSFER_FAILED
);
(bool success, bytes memory returndata) =
address(token).call(abi.encodeCall(IERC20Internal.transfer, (to, value)));
require(success && (returndata.length == 0 || abi.decode(returndata, (bool))), ErrorsLib.TRANSFER_FAILED);
}

function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
(bool success, bytes memory returndata) =
address(token).call(abi.encodeCall(token.transferFrom, (from, to, value)));
require(
success && address(token).code.length > 0 && (returndata.length == 0 || abi.decode(returndata, (bool))),
ErrorsLib.TRANSFER_FROM_FAILED
);
address(token).call(abi.encodeCall(IERC20Internal.transferFrom, (from, to, value)));
require(success && (returndata.length == 0 || abi.decode(returndata, (bool))), ErrorsLib.TRANSFER_FROM_FAILED);
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 0 additions & 7 deletions test/forge/SafeTransferLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ contract SafeTransferLibTest is Test {
tokenWithBooleanAlwaysFalse = new ERC20WithBooleanAlwaysFalse();
}

function testSafeTransferShouldRevertOnTokenWithEmptyCode(address noCode) public {
vm.assume(noCode.code.length == 0);

vm.expectRevert(bytes(ErrorsLib.TRANSFER_FAILED));
this.safeTransfer(noCode, address(0), 0);
}

function testSafeTransfer(address to, uint256 amount) public {
tokenWithoutBoolean.setBalance(address(this), amount);

Expand Down