Skip to content

Commit

Permalink
Fix tests with child contracts initializable
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjimmutable committed Oct 31, 2023
1 parent ea24181 commit 59a4140
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
4 changes: 1 addition & 3 deletions script/DeployChildContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ contract DeployChildContracts is Script {
""
);

// TODO put behind proxy
ChildAxelarBridgeAdaptor childBridgeAdaptorImplementation = new ChildAxelarBridgeAdaptor(
childGateway, // child gateway
address(childBridgeProxy) // child bridge
childGateway
);

// TODO confirm that we want the same proxyAdmin for both
Expand Down
2 changes: 1 addition & 1 deletion script/InitializeChildContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract InitializeChildContracts is Script {
address(childAxelarBridgeAdaptor), rootBridgeAdaptorString, childTokenTemplate, rootChainName, rootIMXToken
);

childAxelarBridgeAdaptor.setRootBridgeAdaptor();
childAxelarBridgeAdaptor.initialize(address(childERC20Bridge));

vm.stopBroadcast();

Expand Down
29 changes: 14 additions & 15 deletions src/child/ChildAxelarBridgeAdaptor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
pragma solidity ^0.8.21;

import {AxelarExecutable} from "@axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol";
import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import {IChildERC20Bridge} from "../interfaces/child/IChildERC20Bridge.sol";
import {IChildAxelarBridgeAdaptorErrors} from "../interfaces/child/IChildAxelarBridgeAdaptor.sol";

contract ChildAxelarBridgeAdaptor is AxelarExecutable, IChildAxelarBridgeAdaptorErrors {
contract ChildAxelarBridgeAdaptor is AxelarExecutable, Initializable, IChildAxelarBridgeAdaptorErrors {
/// @notice Address of bridge to relay messages to.
IChildERC20Bridge public immutable CHILD_BRIDGE;
IChildERC20Bridge public childBridge;
string public rootBridgeAdaptor;

constructor(address _gateway, address _childBridge) AxelarExecutable(_gateway) {
constructor(address _gateway) AxelarExecutable(_gateway) {}

/**
* @notice Initializes the contract.
* @param _childBridge Address of the child bridge contract.
* @dev Always sets the rootBridgeAdaptor to whatever the rootERC20BridgeAdaptor of the bridge contract is.
*/
function initialize(address _childBridge) external initializer {
if (_childBridge == address(0)) {
revert ZeroAddress();
}

CHILD_BRIDGE = IChildERC20Bridge(_childBridge);
}

// TODO tests for this
// TODO does this need to be permissioned?
/**
* @notice Sets the root bridge adaptor address.
* @dev Always sets it to whatever the rootERC20BridgeAdaptor of the bridge contract is.
*/
function setRootBridgeAdaptor() external {
rootBridgeAdaptor = CHILD_BRIDGE.rootERC20BridgeAdaptor();
childBridge = IChildERC20Bridge(_childBridge);
rootBridgeAdaptor = childBridge.rootERC20BridgeAdaptor();
}

/**
Expand All @@ -36,6 +35,6 @@ contract ChildAxelarBridgeAdaptor is AxelarExecutable, IChildAxelarBridgeAdaptor
internal
override
{
CHILD_BRIDGE.onMessageReceive(sourceChain_, sourceAddress_, payload_);
childBridge.onMessageReceive(sourceChain_, sourceAddress_, payload_);
}
}
4 changes: 4 additions & 0 deletions src/test/child/MockChildERC20Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ pragma solidity ^0.8.21;

contract MockChildERC20Bridge {
function onMessageReceive(string calldata, string calldata, bytes calldata) external {}

function rootERC20BridgeAdaptor() external pure returns (string memory) {
return "rootERC20BridgeAdaptor";
}
}
4 changes: 3 additions & 1 deletion test/integration/child/ChildAxelarBridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract ChildERC20BridgeIntegrationTest is Test, IChildERC20BridgeEvents, IChil
childERC20Bridge = new ChildERC20Bridge();
mockChildAxelarGateway = new MockChildAxelarGateway();
childAxelarBridgeAdaptor =
new ChildAxelarBridgeAdaptor(address(mockChildAxelarGateway), address(childERC20Bridge));
new ChildAxelarBridgeAdaptor(address(mockChildAxelarGateway));

childERC20Bridge.initialize(
address(childAxelarBridgeAdaptor),
Expand All @@ -42,6 +42,8 @@ contract ChildERC20BridgeIntegrationTest is Test, IChildERC20BridgeEvents, IChil
ROOT_CHAIN_NAME,
IMX_TOKEN_ADDRESS
);

childAxelarBridgeAdaptor.initialize(address(childERC20Bridge));
}

function test_ChildTokenMap() public {
Expand Down
9 changes: 5 additions & 4 deletions test/unit/child/ChildAxelarBridgeAdaptor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ contract ChildAxelarBridgeAdaptorUnitTest is Test, IChildAxelarBridgeAdaptorErro
mockChildERC20Bridge = new MockChildERC20Bridge();
mockChildAxelarGateway = new MockChildAxelarGateway();
childAxelarBridgeAdaptor =
new ChildAxelarBridgeAdaptor(address(mockChildAxelarGateway), address(mockChildERC20Bridge));
new ChildAxelarBridgeAdaptor(address(mockChildAxelarGateway));
childAxelarBridgeAdaptor.initialize(address(mockChildERC20Bridge));
}

function test_Constructor_SetsValues() public {
assertEq(address(childAxelarBridgeAdaptor.CHILD_BRIDGE()), address(mockChildERC20Bridge), "childBridge not set");
assertEq(address(childAxelarBridgeAdaptor.childBridge()), address(mockChildERC20Bridge), "childBridge not set");
assertEq(address(childAxelarBridgeAdaptor.gateway()), address(mockChildAxelarGateway), "gateway not set");
}

function test_RevertIf_ConstructorGivenZeroAddress() public {
ChildAxelarBridgeAdaptor newAdaptor = new ChildAxelarBridgeAdaptor(GATEWAY_ADDRESS);
vm.expectRevert(ZeroAddress.selector);
// Gateway address being zero is checked in Axelar's AxelarExecutable smart contract.
new ChildAxelarBridgeAdaptor(GATEWAY_ADDRESS, address(0));
newAdaptor.initialize(address(0));
}

function test_Execute() public {
Expand Down

0 comments on commit 59a4140

Please sign in to comment.