Skip to content

Commit

Permalink
Working call
Browse files Browse the repository at this point in the history
  • Loading branch information
wcgcyx committed Feb 19, 2024
1 parent 87c8927 commit c8acfcc
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
101 changes: 101 additions & 0 deletions test/invariant/InvariantBridge.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// SPDX-License-Identifier: Apache 2.0
pragma solidity 0.8.19;

import {Test} from "forge-std/Test.sol";
import {ChildERC20} from "../../src/child/ChildERC20.sol";
import {WIMX} from "../../src/child/WIMX.sol";
import {IChildERC20Bridge, ChildERC20Bridge} from "../../src/child/ChildERC20Bridge.sol";
import {IRootERC20Bridge, IERC20Metadata} from "../../src/root/RootERC20Bridge.sol";
import {RootERC20BridgeFlowRate} from "../../src/root/flowrate/RootERC20BridgeFlowRate.sol";
import {MockAdaptor} from "./MockAdaptor.sol";
import "forge-std/console.sol";

contract InvariantBridge is Test {
string public constant ROOT_CHAIN_URL = "http://127.0.0.1:8500";
string public constant CHILD_CHAIN_URL = "http://127.0.0.1:8501";
uint256 public constant IMX_DEPOSIT_LIMIT = 10000 ether;
address public constant ADMIN = address(0x111);

uint256 childId;
uint256 rootId;
ChildERC20Bridge childBridge;
RootERC20BridgeFlowRate rootBridge;
MockAdaptor childAdaptor;
MockAdaptor rootAdaptor;

function setUp() public {
childId = vm.createFork(CHILD_CHAIN_URL);
rootId = vm.createFork(ROOT_CHAIN_URL);

// Deploy contracts on child chain.
vm.selectFork(childId);
vm.startPrank(ADMIN);
ChildERC20 childTokenTemplate = new ChildERC20();
childTokenTemplate.initialize(address(123), "Test", "TST", 18);
childAdaptor = new MockAdaptor();
vm.stopPrank();
childBridge = new ChildERC20Bridge(address(this));
WIMX wIMX = new WIMX();

// Deploy contracts on root chain.
vm.selectFork(rootId);
vm.startPrank(ADMIN);
ChildERC20 rootTokenTemplate = new ChildERC20();
rootTokenTemplate.initialize(address(123), "Test", "TST", 18);
rootAdaptor = new MockAdaptor();
vm.stopPrank();
rootBridge = new RootERC20BridgeFlowRate(address(this));
ChildERC20 rootIMXToken = new ChildERC20();
rootIMXToken.initialize(address(123), "Immutable X", "IMX", 18);
WIMX wETH = new WIMX();

// Configure contracts on child chain.
vm.selectFork(childId);
childAdaptor.initialize(rootId, address(childBridge), address(rootAdaptor));
IChildERC20Bridge.InitializationRoles memory childRoles = IChildERC20Bridge.InitializationRoles({
defaultAdmin: address(this),
pauser: address(this),
unpauser: address(this),
adaptorManager: address(this),
initialDepositor: address(this),
treasuryManager: address(this)
});
childBridge.initialize(
childRoles, address(childAdaptor), address(childTokenTemplate), address(rootIMXToken), address(wIMX)
);

// Configure contracts on root chain.
vm.selectFork(rootId);
rootAdaptor.initialize(childId, address(rootBridge), address(childAdaptor));
IRootERC20Bridge.InitializationRoles memory rootRoles = IRootERC20Bridge.InitializationRoles({
defaultAdmin: address(this),
pauser: address(this),
unpauser: address(this),
variableManager: address(this),
adaptorManager: address(this)
});
rootBridge.initialize(
rootRoles,
address(rootAdaptor),
address(childBridge),
address(rootTokenTemplate),
address(rootIMXToken),
address(wETH),
IMX_DEPOSIT_LIMIT,
ADMIN
);

// Try to map one token
ChildERC20 testRootToken = new ChildERC20();
testRootToken.initialize(address(123), "Test", "TST", 18);

rootBridge.mapToken{value: 1}(IERC20Metadata(address(testRootToken)));

// Verify
console.log(rootBridge.rootTokenToChildToken(address(testRootToken)));
vm.selectFork(childId);
console.log(childBridge.rootTokenToChildToken(address(testRootToken)));
}

function test1() public {}
}
9 changes: 5 additions & 4 deletions test/invariant/MockAdaptor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ interface MessageReceiver {
}

contract MockAdaptor is Test, IChildBridgeAdaptor, IRootBridgeAdaptor {
uint256 chainId;
uint256 otherChainId;
MessageReceiver messageReceiver;
MockAdaptor otherAdaptor;

constructor() {}

function initialize(uint256 _chainId, uint256 _otherChainId, address _messageReceiver, address _otherAdaptor) public {
chainId = _chainId;
function initialize(uint256 _otherChainId, address _messageReceiver, address _otherAdaptor) public {
otherChainId = _otherChainId;
messageReceiver = MessageReceiver(_messageReceiver);
otherAdaptor = MockAdaptor(_otherAdaptor);
Expand All @@ -29,9 +27,12 @@ contract MockAdaptor is Test, IChildBridgeAdaptor, IRootBridgeAdaptor {
payable
override(IChildBridgeAdaptor, IRootBridgeAdaptor)
{
// Switch to the other chain.
vm.selectFork(otherChainId);
otherAdaptor.onMessageReceive(payload);
vm.selectFork(chainId);
// Switch back to the original chain.
// Due to symmetry, the original chain is now stored as otherChainId.
vm.selectFork(otherChainId);
}

function onMessageReceive(bytes calldata data) external {
Expand Down

0 comments on commit c8acfcc

Please sign in to comment.