Skip to content

Commit

Permalink
Implement fileable escrow (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunbreak1211 authored Sep 17, 2024
1 parent 5037eb4 commit a78371b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion deploy/TokenBridgeDeploy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ library TokenBridgeDeploy {
) internal returns (L1TokenBridgeInstance memory l1BridgeInstance) {
l1BridgeInstance.govRelay = address(new L1GovernanceRelay(l2GovRelay, l1Messenger));
l1BridgeInstance.escrow = address(new Escrow());
l1BridgeInstance.bridge = address(new L1TokenBridge(l2Bridge, l1BridgeInstance.escrow, l1Messenger));
l1BridgeInstance.bridge = address(new L1TokenBridge(l2Bridge, l1Messenger));
ScriptTools.switchOwner(l1BridgeInstance.govRelay, deployer, owner);
ScriptTools.switchOwner(l1BridgeInstance.escrow, deployer, owner);
ScriptTools.switchOwner(l1BridgeInstance.bridge, deployer, owner);
Expand Down
6 changes: 4 additions & 2 deletions deploy/TokenBridgeInit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ interface L1TokenBridgeLike {
function otherBridge() external view returns (address);
function escrow() external view returns (address);
function messenger() external view returns (address);
function registerToken(address l1Token, address l2Token) external;
function file(bytes32, address) external;
function registerToken(address, address) external;
}

interface L1RelayLike {
Expand Down Expand Up @@ -69,13 +70,14 @@ library TokenBridgeInit {
// sanity checks
require(l1Bridge.isOpen() == 1, "TokenBridgeInit/not-open");
require(l1Bridge.otherBridge() == l2BridgeInstance.bridge, "TokenBridgeInit/other-bridge-mismatch");
require(l1Bridge.escrow() == address(escrow), "TokenBridgeInit/escrow-mismatch");
require(l1Bridge.messenger() == cfg.l1Messenger, "TokenBridgeInit/l1-bridge-messenger-mismatch");
require(l1GovRelay.l2GovernanceRelay() == l2BridgeInstance.govRelay, "TokenBridgeInit/l2-gov-relay-mismatch");
require(l1GovRelay.messenger() == cfg.l1Messenger, "TokenBridgeInit/l1-gov-relay-messenger-mismatch");
require(cfg.l1Tokens.length == cfg.l2Tokens.length, "TokenBridgeInit/token-arrays-mismatch");
require(cfg.minGasLimit <= 1_000_000_000, "TokenBridgeInit/min-gas-limit-out-of-bounds");

l1Bridge.file("escrow", address(escrow));

for (uint256 i; i < cfg.l1Tokens.length; ++i) {
(address l1Token, address l2Token) = (cfg.l1Tokens[i], cfg.l2Tokens[i]);
require(l1Token != address(0), "TokenBridgeInit/invalid-l1-token");
Expand Down
12 changes: 9 additions & 3 deletions src/L1TokenBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ contract L1TokenBridge {
mapping(address => uint256) public wards;
mapping(address => address) public l1ToL2Token;
uint256 public isOpen = 1;
address public escrow;

// --- immutables ---

address public immutable otherBridge;
address public immutable escrow;
CrossDomainMessengerLike public immutable messenger;

// --- events ---

event Rely(address indexed usr);
event Deny(address indexed usr);
event File(bytes32 indexed what, address data);
event Closed();
event TokenSet(address indexed l1Token, address indexed l2Token);
event ERC20BridgeInitiated(
Expand Down Expand Up @@ -81,11 +82,9 @@ contract L1TokenBridge {

constructor(
address _otherBridge,
address _escrow,
address _messenger
) {
otherBridge = _otherBridge;
escrow = _escrow;
messenger = CrossDomainMessengerLike(_messenger);

wards[msg.sender] = 1;
Expand All @@ -104,6 +103,13 @@ contract L1TokenBridge {
emit Deny(usr);
}

function file(bytes32 what, address data) external auth {
if (what == "escrow") {
escrow = data;
} else revert("L1TokenBridge/file-unrecognized-param");
emit File(what, data);
}

function close() external auth {
isOpen = 0;
emit Closed();
Expand Down
12 changes: 8 additions & 4 deletions test/L1TokenBridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ contract L1TokenBridgeTest is DssTest {
function setUp() public {
messenger = new MessengerMock();
messenger.setXDomainMessageSender(otherBridge);
bridge = new L1TokenBridge(otherBridge, escrow, address(messenger));
bridge = new L1TokenBridge(otherBridge, address(messenger));
bridge.file("escrow", escrow);
l1Token = new GemMock(1_000_000 ether);
l1Token.transfer(address(0xe0a), 500_000 ether);
vm.prank(escrow); l1Token.approve(address(bridge), type(uint256).max);
Expand All @@ -71,12 +72,11 @@ contract L1TokenBridgeTest is DssTest {
function testConstructor() public {
vm.expectEmit(true, true, true, true);
emit Rely(address(this));
L1TokenBridge b = new L1TokenBridge(address(111), address(222), address(333));
L1TokenBridge b = new L1TokenBridge(address(111), address(222));

assertEq(b.isOpen(), 1);
assertEq(b.otherBridge(), address(111));
assertEq(b.escrow(), address(222));
assertEq(address(b.messenger()), address(333));
assertEq(address(b.messenger()), address(222));
assertEq(b.wards(address(this)), 1);
}

Expand All @@ -93,6 +93,10 @@ contract L1TokenBridgeTest is DssTest {
]);
}

function testFileAddress() public {
checkFileAddress(address(bridge), "L1TokenBridge", ["escrow"]);
}

function testTokenRegistration() public {
assertEq(bridge.l1ToL2Token(address(11)), address(0));

Expand Down

0 comments on commit a78371b

Please sign in to comment.