Skip to content

Commit

Permalink
refactor deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
tsnewnami committed Nov 22, 2023
1 parent 3fb6bf9 commit 3097957
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/child/ChildERC20Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions test/unit/child/ChildERC20Bridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3097957

Please sign in to comment.