Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjimmutable committed Oct 23, 2023
1 parent bda3547 commit b566caa
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 42 deletions.
1 change: 0 additions & 1 deletion script/DeployChildContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ contract DeployChildContracts is Script {
vm.createSelectFork(childRpcUrl);
vm.startBroadcast(deployerPrivateKey);

// new ChildERC20();
ChildERC20 childTokenTemplate = new ChildERC20();
childTokenTemplate.initialize(address(123), "TEMPLATE", "TPT", 18);

Expand Down
2 changes: 1 addition & 1 deletion script/InitializeChildContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract InitializeChildContracts is Script {
vm.startBroadcast(deployerPrivateKey);

childERC20Bridge.initialize(
address(childAxelarBridgeAdaptor), rootBridgeAdaptorString , childTokenTemplate, rootChainName, rootIMXToken
address(childAxelarBridgeAdaptor), rootBridgeAdaptorString, childTokenTemplate, rootChainName, rootIMXToken
);

childAxelarBridgeAdaptor.setRootBridgeAdaptor();
Expand Down
2 changes: 1 addition & 1 deletion script/InitializeRootContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract InitializeRootContracts is Script {
string[] memory checksumInputs = Utils.getChecksumInputs(childBridgeAdaptor);
bytes memory checksumOutput = vm.ffi(checksumInputs);
string memory childBridgeAdaptorChecksum = string(Utils.removeZeroByteValues(checksumOutput));
address childETHToken = vm.envAddress("CHILD_ETH_ADDRESS");
address childETHToken = vm.envAddress("CHILD_ETH_ADDRESS");
/**
* INITIALIZE ROOT CHAIN CONTRACTS
*/
Expand Down
30 changes: 14 additions & 16 deletions src/root/RootERC20Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,14 @@ contract RootERC20Bridge is
address newRootBridgeAdaptor,
address newChildERC20Bridge,
string memory newChildBridgeAdaptor,
address newChildTokenTemplate,
address newChildTokenTemplate,
address newRootIMXToken,
address newChildETHToken)
public
initializer
{
if (newRootBridgeAdaptor == address(0)
|| newChildERC20Bridge == address(0)
|| newChildTokenTemplate == address(0)
|| newRootIMXToken == address(0)
|| newChildETHToken == address(0))
{
address newChildETHToken
) public initializer {
if (
newRootBridgeAdaptor == address(0) || newChildERC20Bridge == address(0)
|| newChildTokenTemplate == address(0) || newRootIMXToken == address(0) || newChildETHToken == address(0)
) {
revert ZeroAddress();
}
if (bytes(newChildBridgeAdaptor).length == 0) {
Expand All @@ -97,11 +93,13 @@ contract RootERC20Bridge is
return _mapToken(rootToken);
}

function depositETH(uint256 amount) external payable { //override removed?
function depositETH(uint256 amount) external payable {
//override removed?
_depositETH(msg.sender, amount);
}

function depositToETH(address receiver, uint256 amount) external payable { //override removed?
function depositToETH(address receiver, uint256 amount) external payable {
//override removed?
_depositETH(receiver, amount);
}

Expand All @@ -111,7 +109,7 @@ contract RootERC20Bridge is
}

uint256 expectedBalance = address(this).balance - (msg.value - amount);

_deposit(IERC20Metadata(NATIVE_TOKEN), receiver, amount);

// invariant check to ensure that the root native balance has increased by the amount deposited
Expand Down Expand Up @@ -188,7 +186,7 @@ contract RootERC20Bridge is
// TODO We can call _mapToken here, but ordering in the GMP is not guaranteed.
// Therefore, we need to decide how to handle this and it may be a UI decision to wait until map token message is executed on child chain.
// Discuss this, and add this decision to the design doc.
if (address(rootToken) != NATIVE_TOKEN) {
if (address(rootToken) != NATIVE_TOKEN) {
if (address(rootToken) != rootIMXToken) {
childToken = rootTokenToChildToken[address(rootToken)];
if (childToken == address(0)) {
Expand All @@ -201,7 +199,7 @@ contract RootERC20Bridge is
} else {
feeAmount = msg.value - amount;
}

// Deposit sig, root token address, depositor, receiver, amount
bytes memory payload = abi.encode(DEPOSIT_SIG, rootToken, msg.sender, receiver, amount);
// TODO investigate using delegatecall to keep the axelar message sender as the bridge contract, since adaptor can change.
Expand Down
3 changes: 2 additions & 1 deletion test/integration/root/RootERC20Bridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ contract RootERC20BridgeIntegrationTest is Test, IRootERC20BridgeEvents, IRootAx
string memory childBridgeAdaptorString = Strings.toHexString(CHILD_BRIDGE_ADAPTOR);
// (address childToken, bytes memory predictedPayload) =
// setupDeposit(token, rootBridge, gasPrice, tokenAmount, true);
(address childToken, bytes memory predictedPayload) = setupDeposit(token, rootBridge, mapTokenFee, depositFee, tokenAmount, true);
(address childToken, bytes memory predictedPayload) =
setupDeposit(token, rootBridge, mapTokenFee, depositFee, tokenAmount, true);

vm.expectEmit(address(axelarAdaptor));
emit MapTokenAxelarMessage(CHILD_CHAIN_NAME, childBridgeAdaptorString, predictedPayload);
Expand Down
55 changes: 38 additions & 17 deletions test/unit/root/RootERC20Bridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ contract RootERC20BridgeUnitTest is Test, IRootERC20BridgeEvents, IRootERC20Brid
mockAxelarAdaptor = new MockAdaptor();

// The specific ERC20 token template does not matter for these unit tests
rootBridge.initialize(address(mockAxelarAdaptor), CHILD_BRIDGE, CHILD_BRIDGE_ADAPTOR_STRING, address(token), IMX_TOKEN, CHILD_ETH_TOKEN);
rootBridge.initialize(
address(mockAxelarAdaptor),
CHILD_BRIDGE,
CHILD_BRIDGE_ADAPTOR_STRING,
address(token),
IMX_TOKEN,
CHILD_ETH_TOKEN
);
}

/**
Expand All @@ -59,7 +66,14 @@ contract RootERC20BridgeUnitTest is Test, IRootERC20BridgeEvents, IRootERC20Brid

function test_RevertIfInitializeTwice() public {
vm.expectRevert("Initializable: contract is already initialized");
rootBridge.initialize(address(mockAxelarAdaptor), CHILD_BRIDGE, CHILD_BRIDGE_ADAPTOR_STRING, address(token), IMX_TOKEN, CHILD_ETH_TOKEN);
rootBridge.initialize(
address(mockAxelarAdaptor),
CHILD_BRIDGE,
CHILD_BRIDGE_ADAPTOR_STRING,
address(token),
IMX_TOKEN,
CHILD_ETH_TOKEN
);
}

function test_RevertIf_InitializeWithAZeroAddressRootAdapter() public {
Expand Down Expand Up @@ -119,7 +133,6 @@ contract RootERC20BridgeUnitTest is Test, IRootERC20BridgeEvents, IRootERC20Brid
}

function test_mapToken_CallsAdaptor() public {

bytes memory payload =
abi.encode(rootBridge.MAP_TOKEN_SIG(), token, token.name(), token.symbol(), token.decimals());

Expand Down Expand Up @@ -202,15 +215,16 @@ contract RootERC20BridgeUnitTest is Test, IRootERC20BridgeEvents, IRootERC20Brid

function test_depositETHCallsSendMessage() public {
uint256 amount = 1000;
(, bytes memory predictedPayload) = setupDeposit(ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, false);
(, bytes memory predictedPayload) =
setupDeposit(ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, false);

vm.expectCall(
address(mockAxelarAdaptor),
depositFee,
abi.encodeWithSelector(mockAxelarAdaptor.sendMessage.selector, predictedPayload, address(this))
);

rootBridge.depositETH{value: amount+depositFee}(amount);
rootBridge.depositETH{value: amount + depositFee}(amount);
}

function test_depositETHEmitsNativeDepositEvent() public {
Expand All @@ -219,51 +233,57 @@ contract RootERC20BridgeUnitTest is Test, IRootERC20BridgeEvents, IRootERC20Brid

vm.expectEmit();
emit NativeDeposit(NATIVE_TOKEN, CHILD_ETH_TOKEN, address(this), address(this), amount);
rootBridge.depositETH{value: amount+depositFee}(amount);
rootBridge.depositETH{value: amount + depositFee}(amount);
}

function test_RevertIf_depositETHInsufficientValue() public {
uint256 amount = 1000;
setupDeposit(ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, false);

vm.expectRevert(InsufficientValue.selector);
rootBridge.depositETH{value: (amount/2)+depositFee}(amount);
rootBridge.depositETH{value: (amount / 2) + depositFee}(amount);
}

/**
/**
* DEPOSIT TO ETH
*/

function test_depositToETHCallsSendMessage() public {
uint256 amount = 1000;
address receiver = address(12345);
(, bytes memory predictedPayload) = setupDepositTo(ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, receiver, false);
(, bytes memory predictedPayload) = setupDepositTo(
ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, receiver, false
);
vm.expectCall(
address(mockAxelarAdaptor),
depositFee,
abi.encodeWithSelector(mockAxelarAdaptor.sendMessage.selector, predictedPayload, address(this))
);

rootBridge.depositToETH{value: amount+depositFee}(receiver, amount);
rootBridge.depositToETH{value: amount + depositFee}(receiver, amount);
}

function test_depositToETHEmitsNativeDepositEvent() public {
uint256 amount = 1000;
address receiver = address(12345);
setupDepositTo(ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, receiver, false);
setupDepositTo(
ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, receiver, false
);

vm.expectEmit();
emit NativeDeposit(NATIVE_TOKEN, CHILD_ETH_TOKEN, address(this), receiver, amount);
rootBridge.depositToETH{value: amount+depositFee}(receiver, amount);
rootBridge.depositToETH{value: amount + depositFee}(receiver, amount);
}

function test_RevertIf_depositToETHInsufficientValue() public {
uint256 amount = 1000;
address receiver = address(12345);
setupDepositTo(ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, receiver, false);
setupDepositTo(
ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, receiver, false
);

vm.expectRevert(InsufficientValue.selector);
rootBridge.depositToETH{value: (amount/2)+depositFee}(receiver, amount);
rootBridge.depositToETH{value: (amount / 2) + depositFee}(receiver, amount);
}

/**
Expand All @@ -275,7 +295,7 @@ contract RootERC20BridgeUnitTest is Test, IRootERC20BridgeEvents, IRootERC20Brid
setupDeposit(ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, false);

vm.expectRevert(ZeroAmount.selector);
rootBridge.depositETH{value: amount+depositFee}(amount);
rootBridge.depositETH{value: amount + depositFee}(amount);
}

function test_RevertIf_depositToETHAmountIsZero() public {
Expand All @@ -285,7 +305,7 @@ contract RootERC20BridgeUnitTest is Test, IRootERC20BridgeEvents, IRootERC20Brid
setupDeposit(ERC20PresetMinterPauser(NATIVE_TOKEN), rootBridge, mapTokenFee, depositFee, amount, false);

vm.expectRevert(ZeroAmount.selector);
rootBridge.depositToETH{value: amount+depositFee}(receiver, amount);
rootBridge.depositToETH{value: amount + depositFee}(receiver, amount);
}

function test_RevertIf_depositAmountIsZero() public {
Expand Down Expand Up @@ -407,7 +427,8 @@ contract RootERC20BridgeUnitTest is Test, IRootERC20BridgeEvents, IRootERC20Brid
uint256 amount = 100;
address receiver = address(12345);

(, bytes memory predictedPayload) = setupDepositTo(token, rootBridge, mapTokenFee, depositFee, amount, receiver, true);
(, bytes memory predictedPayload) =
setupDepositTo(token, rootBridge, mapTokenFee, depositFee, amount, receiver, true);

vm.expectCall(
address(mockAxelarAdaptor),
Expand Down
17 changes: 12 additions & 5 deletions test/utils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {IChildERC20, ChildERC20} from "../src/child/ChildERC20.sol";
import {RootAxelarBridgeAdaptor} from "../src/root/RootAxelarBridgeAdaptor.sol";

contract Utils is Test {

function integrationSetup(
address childBridge,
address childBridgeAdaptor,
string memory childBridgeName,
address imxTokenAddress,
address ethTokenAddress)
address ethTokenAddress
)
public
returns (
ERC20PresetMinterPauser token,
Expand All @@ -43,7 +43,14 @@ contract Utils is Test {
address(axelarGasService)
);

rootBridge.initialize(address(axelarAdaptor), childBridge, Strings.toHexString(childBridgeAdaptor), address(token), imxTokenAddress, ethTokenAddress);
rootBridge.initialize(
address(axelarAdaptor),
childBridge,
Strings.toHexString(childBridgeAdaptor),
address(token),
imxTokenAddress,
ethTokenAddress
);
axelarAdaptor.setChildBridgeAdaptor();
}

Expand Down Expand Up @@ -84,12 +91,12 @@ contract Utils is Test {
childToken = rootBridge.mapToken{value: mapTokenFee}(token);
}
if (address(token) == address(0xeee)) {
vm.deal(to, tokenAmount+depositFee);
vm.deal(to, tokenAmount + depositFee);
} else {
token.mint(address(this), tokenAmount);
token.approve(address(rootBridge), tokenAmount);
}

return (childToken, predictedPayload);
}

Expand Down

0 comments on commit b566caa

Please sign in to comment.