Skip to content

Commit

Permalink
fix(MToken): handle mint reverts in Portal
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Aug 19, 2024
1 parent 524d98a commit 8067bd4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 41 deletions.
23 changes: 2 additions & 21 deletions src/MToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ contract MToken is IMToken, ContinuousIndexing, ERC20Extended {
* @param amount_ The present amount to burn.
*/
function _burn(address account_, uint256 amount_) internal {
_revertIfInsufficientAmount(amount_);
if (amount_ == 0) revert InsufficientAmount(amount_);

emit Transfer(account_, address(0), amount_);

Expand All @@ -199,9 +199,6 @@ contract MToken is IMToken, ContinuousIndexing, ERC20Extended {
* @param amount_ The present amount to mint.
*/
function _mint(address recipient_, uint256 amount_) internal {
_revertIfInsufficientAmount(amount_);
_revertIfInvalidRecipient(recipient_);

emit Transfer(address(0), recipient_, amount_);

uint240 safeAmount_ = UIntMath.safe240(amount_);
Expand Down Expand Up @@ -327,7 +324,7 @@ contract MToken is IMToken, ContinuousIndexing, ERC20Extended {
* @param amount_ The present amount to transfer.
*/
function _transfer(address sender_, address recipient_, uint256 amount_) internal override {
_revertIfInvalidRecipient(recipient_);
if (recipient_ == address(0)) revert InvalidRecipient(recipient_);

emit Transfer(sender_, recipient_, amount_);

Expand Down Expand Up @@ -411,22 +408,6 @@ contract MToken is IMToken, ContinuousIndexing, ERC20Extended {
return RegistrarReader.isEarnersListIgnored(registrar) || RegistrarReader.isApprovedEarner(registrar, account_);
}

/**
* @dev Reverts if the amount of a `mint` or `burn` is equal to 0.
* @param amount_ Amount to check.
*/
function _revertIfInsufficientAmount(uint256 amount_) internal pure {
if (amount_ == 0) revert InsufficientAmount(amount_);
}

/**
* @dev Reverts if the recipient of a `mint` or `transfer` is address(0).
* @param recipient_ Address of the recipient to check.
*/
function _revertIfInvalidRecipient(address recipient_) internal pure {
if (recipient_ == address(0)) revert InvalidRecipient(recipient_);
}

/// @dev Reverts if the caller is not the portal.
function _revertIfNotPortal() internal view {
if (msg.sender != portal) revert NotPortal();
Expand Down
26 changes: 6 additions & 20 deletions test/MToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ contract MTokenTests is TestUtils {
_mToken.mint(_alice, 0, 0);
}

function test_mint_insufficientAmount() external {
vm.expectRevert(abi.encodeWithSelector(IERC20Extended.InsufficientAmount.selector, 0));

vm.prank(_portal);
_mToken.mint(_alice, 0, 0);
}

function test_mint_invalidRecipient() external {
vm.expectRevert(abi.encodeWithSelector(IERC20Extended.InvalidRecipient.selector, address(0)));

vm.prank(_portal);
_mToken.mint(address(0), 1_000, 0);
}

function test_mint_toNonEarner() external {
vm.prank(_portal);
_mToken.mint(_alice, 1_000, 0);
Expand Down Expand Up @@ -207,12 +193,6 @@ contract MTokenTests is TestUtils {
_mToken.burn(_alice, 0);
}

function test_burn_insufficientAmount() external {
vm.expectRevert(abi.encodeWithSelector(IERC20Extended.InsufficientAmount.selector, 0));
vm.prank(_portal);
_mToken.burn(_alice, 0);
}

function test_burn_insufficientBalance_fromNonEarner() external {
_mToken.setInternalBalanceOf(_alice, 999);

Expand All @@ -221,6 +201,12 @@ contract MTokenTests is TestUtils {
_mToken.burn(_alice, 1_000);
}

function test_burn_insufficientAmount() external {
vm.expectRevert(abi.encodeWithSelector(IERC20Extended.InsufficientAmount.selector, 0));
vm.prank(_portal);
_mToken.burn(_alice, 0);
}

function test_burn_insufficientBalance_fromEarner() external {
_mToken.setIsEarning(_alice, true);
_mToken.setInternalBalanceOf(_alice, 908);
Expand Down

0 comments on commit 8067bd4

Please sign in to comment.