Skip to content

Commit

Permalink
Merge pull request #329 from morpho-labs/fix/zero-transfer
Browse files Browse the repository at this point in the history
fix(transfer): allow zero transfers
  • Loading branch information
Rubilmax authored Oct 31, 2023
2 parents 97bab2b + 7048fe8 commit 08e893c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/TransferBundler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ abstract contract TransferBundler is BaseBundler {
/// @dev Warning: `recipient` can re-enter the bundler flow.
/// @dev Pass `amount = type(uint256).max` to transfer all.
/// @param recipient The address that will receive the native tokens.
/// @param amount The amount of native tokens to transfer.
/// @param amount The amount of native tokens to transfer. Passing zero will skip the transfer.
function nativeTransfer(address recipient, uint256 amount) external payable {
require(recipient != address(0), ErrorsLib.ZERO_ADDRESS);
require(recipient != address(this), ErrorsLib.BUNDLER_ADDRESS);

amount = Math.min(amount, address(this).balance);

require(amount != 0, ErrorsLib.ZERO_AMOUNT);
if (amount == 0) return;

SafeTransferLib.safeTransferETH(recipient, amount);
}
Expand All @@ -40,14 +40,14 @@ abstract contract TransferBundler is BaseBundler {
/// @dev Pass `amount = type(uint256).max` to transfer all.
/// @param asset The address of the ERC20 token to transfer.
/// @param recipient The address that will receive the tokens.
/// @param amount The amount of `asset` to transfer.
/// @param amount The amount of `asset` to transfer. Passing zero will skip the transfer.
function erc20Transfer(address asset, address recipient, uint256 amount) external payable {
require(recipient != address(0), ErrorsLib.ZERO_ADDRESS);
require(recipient != address(this), ErrorsLib.BUNDLER_ADDRESS);

amount = Math.min(amount, ERC20(asset).balanceOf(address(this)));

require(amount != 0, ErrorsLib.ZERO_AMOUNT);
if (amount == 0) return;

ERC20(asset).safeTransfer(recipient, amount);
}
Expand Down
21 changes: 10 additions & 11 deletions test/forge/TransferBundlerLocalTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract TransferBundlerLocalTest is LocalTest {
}

function testTransfer(uint256 amount) public {
amount = bound(amount, MIN_AMOUNT, MAX_AMOUNT);
amount = bound(amount, 0, MAX_AMOUNT);

bundle.push(_erc20Transfer(address(loanToken), RECEIVER, amount));

Expand Down Expand Up @@ -45,11 +45,17 @@ contract TransferBundlerLocalTest is LocalTest {
bundler.multicall(bundle);
}

function testTranferZeroAmount() public {
bundle.push(_erc20Transfer(address(loanToken), RECEIVER, 0));
function testNativeTransfer(uint256 amount) public {
amount = bound(amount, 0, MAX_AMOUNT);

bundle.push(_nativeTransfer(RECEIVER, amount));

deal(address(bundler), amount);

vm.expectRevert(bytes(ErrorsLib.ZERO_AMOUNT));
bundler.multicall(bundle);

assertEq(address(bundler).balance, 0, "bundler.balance");
assertEq(RECEIVER.balance, amount, "RECEIVER.balance");
}

function testNativeTransferZeroAddress(uint256 amount) public {
Expand All @@ -70,13 +76,6 @@ contract TransferBundlerLocalTest is LocalTest {
bundler.multicall(bundle);
}

function testNativeTransferZeroAmount() public {
bundle.push(_nativeTransfer(RECEIVER, 0));

vm.expectRevert(bytes(ErrorsLib.ZERO_AMOUNT));
bundler.multicall(bundle);
}

function testTransferFrom(uint256 amount) public {
amount = bound(amount, MIN_AMOUNT, MAX_AMOUNT);

Expand Down

0 comments on commit 08e893c

Please sign in to comment.