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

Adding try catch to revertable functions #69

Merged
merged 2 commits into from
Dec 12, 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
17 changes: 13 additions & 4 deletions src/child/ChildERC20Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,12 @@ contract ChildERC20Bridge is
* @notice Private function to handle withdrawal of L1 native ETH.
*/
function _withdrawETH(uint256 amount) private returns (address) {
if (!IChildERC20(childETHToken).burn(msg.sender, amount)) {
try IChildERC20(childETHToken).burn(msg.sender, amount) returns (bool success) {
if (!success) revert BurnFailed();
} catch {
revert BurnFailed();
}

return NATIVE_ETH;
}

Expand All @@ -330,7 +333,9 @@ contract ChildERC20Bridge is
IWIMX wIMX = IWIMX(wIMXToken);

// Transfer to contract
if (!wIMX.transferFrom(msg.sender, address(this), amount)) {
try wIMX.transferFrom(msg.sender, address(this), amount) returns (bool success) {
if (!success) revert TransferWIMXFailed();
} catch {
revert TransferWIMXFailed();
}

Expand Down Expand Up @@ -372,7 +377,9 @@ contract ChildERC20Bridge is
}

// Burn tokens
if (!IChildERC20(childToken).burn(msg.sender, amount)) {
try IChildERC20(childToken).burn(msg.sender, amount) returns (bool success) {
if (!success) revert BurnFailed();
} catch {
revert BurnFailed();
}

Expand Down Expand Up @@ -486,7 +493,9 @@ contract ChildERC20Bridge is
revert EmptyTokenContract();
}

if (!IChildERC20(childToken).mint(receiver, amount)) {
try IChildERC20(childToken).mint(receiver, amount) returns (bool success) {
if (!success) revert MintFailed();
} catch {
revert MintFailed();
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/child/ChildERC20Bridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ contract ChildERC20BridgeUnitTest is Test, IChildERC20BridgeEvents, IChildERC20B
changePrank(attacker);

// Execute withdraw
vm.expectRevert("ReentrancyGuard: reentrant call");
vm.expectRevert(BurnFailed.selector);
childBridge.withdraw{value: 1 ether}(ChildERC20(address(attackToken)), 100);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ contract ChildERC20BridgeWithdrawETHUnitTest is Test, IChildERC20BridgeEvents, I
uint256 withdrawAmount = 101 ether;
uint256 withdrawFee = 300;

vm.expectRevert(bytes("ERC20: burn amount exceeds balance"));
vm.expectRevert(BurnFailed.selector);
childBridge.withdrawETH{value: withdrawFee}(withdrawAmount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ contract ChildERC20BridgeWithdrawETHToUnitTest is Test, IChildERC20BridgeEvents,
uint256 withdrawAmount = 101 ether;
uint256 withdrawFee = 300;

vm.expectRevert(bytes("ERC20: burn amount exceeds balance"));
vm.expectRevert(BurnFailed.selector);
childBridge.withdrawETHTo{value: withdrawFee}(address(this), withdrawAmount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ contract ChildERC20BridgeWithdrawWIMXUnitTest is Test, IChildERC20BridgeEvents,
uint256 withdrawFee = 300;

wIMXToken.approve(address(childBridge), withdrawAmount);
vm.expectRevert(bytes("Wrapped IMX: Insufficient balance"));
vm.expectRevert(TransferWIMXFailed.selector);
childBridge.withdrawWIMX{value: withdrawFee}(withdrawAmount);
}

Expand All @@ -83,7 +83,7 @@ contract ChildERC20BridgeWithdrawWIMXUnitTest is Test, IChildERC20BridgeEvents,
uint256 withdrawFee = 300;

wIMXToken.approve(address(childBridge), withdrawAmount - 1);
vm.expectRevert(bytes("Wrapped IMX: Insufficient allowance"));
vm.expectRevert(TransferWIMXFailed.selector);
childBridge.withdrawWIMX{value: withdrawFee}(withdrawAmount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ contract ChildERC20BridgeWithdrawWIMXToUnitTest is Test, IChildERC20BridgeEvents
uint256 withdrawFee = 300;

wIMXToken.approve(address(childBridge), withdrawAmount);
vm.expectRevert(bytes("Wrapped IMX: Insufficient balance"));
vm.expectRevert(TransferWIMXFailed.selector);
childBridge.withdrawWIMXTo{value: withdrawFee}(address(this), withdrawAmount);
}

Expand All @@ -92,7 +92,7 @@ contract ChildERC20BridgeWithdrawWIMXToUnitTest is Test, IChildERC20BridgeEvents
uint256 withdrawFee = 300;

wIMXToken.approve(address(childBridge), withdrawAmount - 1);
vm.expectRevert(bytes("Wrapped IMX: Insufficient allowance"));
vm.expectRevert(TransferWIMXFailed.selector);
childBridge.withdrawWIMXTo{value: withdrawFee}(address(this), withdrawAmount);
}

Expand Down
Loading