From 30979577bad3ef4cf41ac23d4f1eeee626332318 Mon Sep 17 00:00:00 2001 From: James Snewin Date: Wed, 22 Nov 2023 14:34:29 +1000 Subject: [PATCH] refactor deposit --- src/child/ChildERC20Bridge.sol | 54 +++++++++++++++----------- test/unit/child/ChildERC20Bridge.t.sol | 1 + 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/child/ChildERC20Bridge.sol b/src/child/ChildERC20Bridge.sol index 22124e0a..79287c87 100644 --- a/src/child/ChildERC20Bridge.sol +++ b/src/child/ChildERC20Bridge.sol @@ -129,6 +129,9 @@ contract ChildERC20Bridge is BridgeRoles, IChildERC20BridgeErrors, IChildERC20Br // Initialize clonedETHToken.initialize(NATIVE_ETH, "Ethereum", "ETH", 18); childETHToken = address(clonedETHToken); + + // Map the supported tokens by default + rootTokenToChildToken[NATIVE_ETH] = childETHToken; } /** @@ -436,33 +439,38 @@ contract ChildERC20Bridge is BridgeRoles, IChildERC20BridgeErrors, IChildERC20Br revert ZeroAddress(); } - IChildERC20 childToken; - if (rootToken != rootIMXToken) { - if (rootToken == NATIVE_ETH) { - childToken = IChildERC20(childETHToken); - } else { - childToken = IChildERC20(rootTokenToChildToken[rootToken]); - if (address(childToken) == address(0)) { - revert NotMapped(); - } - } + _emitDepositEventAndHandleTransfer(rootToken, rootTokenToChildToken[rootToken], sender, receiver, amount); + } - if (address(childToken).code.length == 0) { - revert EmptyTokenContract(); - } + function _emitDepositEventAndHandleTransfer( + address rootToken, + address childToken, + address sender, + address receiver, + uint256 amount + ) private { + if (rootToken == rootIMXToken) { + Address.sendValue(payable(receiver), amount); + emit IMXDeposit(rootToken, sender, receiver, amount); + return; + } - if (!childToken.mint(receiver, amount)) { - revert MintFailed(); - } + if (childToken == address(0)) { + revert NotMapped(); + } - if (rootToken == NATIVE_ETH) { - emit NativeEthDeposit(rootToken, address(childToken), sender, receiver, amount); - } else { - emit ChildChainERC20Deposit(rootToken, address(childToken), sender, receiver, amount); - } + if (childToken.code.length == 0) { + revert EmptyTokenContract(); + } + + if (!IChildERC20(childToken).mint(receiver, amount)) { + revert MintFailed(); + } + + if (rootToken == NATIVE_ETH) { + emit NativeEthDeposit(rootToken, address(childToken), sender, receiver, amount); } else { - Address.sendValue(payable(receiver), amount); - emit IMXDeposit(rootToken, sender, receiver, amount); + emit ChildChainERC20Deposit(rootToken, address(childToken), sender, receiver, amount); } } diff --git a/test/unit/child/ChildERC20Bridge.t.sol b/test/unit/child/ChildERC20Bridge.t.sol index d7d24400..a5a954b2 100644 --- a/test/unit/child/ChildERC20Bridge.t.sol +++ b/test/unit/child/ChildERC20Bridge.t.sol @@ -155,6 +155,7 @@ contract ChildERC20BridgeUnitTest is Test, IChildERC20BridgeEvents, IChildERC20B assertTrue(childBridge.hasRole(childBridge.ADAPTOR_MANAGER_ROLE(), address(this)), "adaptorManager not set"); assertFalse(address(childBridge.childETHToken()) == address(0), "childETHToken not set"); assertFalse(address(childBridge.childETHToken()).code.length == 0, "childETHToken contract empty"); + assert(childBridge.rootTokenToChildToken(NATIVE_ETH) != address(0)); } function test_RevertIfInitializeTwice() public {