Skip to content

Commit f536bd6

Browse files
committed
feat(bridge): add xdai bridges
1 parent c6953af commit f536bd6

File tree

9 files changed

+148
-8
lines changed

9 files changed

+148
-8
lines changed

contracts/src/bridge/IL1Bridge.sol

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
interface IL1Bridge {
6+
/**
7+
* Sends an arbitrary message from one domain to another.
8+
*
9+
* @dev The caller needs to pay some ETH to cover the gas costs
10+
* of the call on L2. Excess ETH that is not used by gas costs will
11+
* be refunded to the `msg.sender` address on L2.
12+
*
13+
* @notice if a user does not desire immediate redemption, they should
14+
* provide a DepositValue of at least CallValue + MaxSubmissionCost.
15+
* If they do desire immediate execution, they should provide a DepositValue
16+
* of at least CallValue + MaxSubmissionCost + (GasPrice x MaxGas).
17+
*
18+
* @param _calldata The L2 encoded message data.
19+
* @param _maxGas Gas limit for immediate L2 execution attempt.
20+
* @param _gasPriceBid L2 Gas price bid for immediate L2 execution attempt.
21+
* @return Unique id to track the message request/transaction.
22+
*/
23+
function sendCrossDomainMessage(
24+
bytes memory _calldata,
25+
uint256 _maxGas,
26+
uint256 _gasPriceBid
27+
) external payable returns (uint256);
28+
29+
function getSubmissionPrice(uint256 _calldatasize) external view returns (uint256);
30+
31+
function onlyAuthorized(address _sender) external;
32+
}

contracts/src/bridge/IL2Bridge.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
interface IL2Bridge {
6+
/**
7+
* Sends an arbitrary message from one domain to another.
8+
*
9+
* @param _calldata The L1 encoded message data.
10+
* @return Unique id to track the message request/transaction.
11+
*/
12+
function sendCrossDomainMessage(bytes memory _calldata) external returns (uint256);
13+
14+
function onlyAuthorized(address _sender) external;
15+
}

contracts/src/bridge/arbitrum/L1Bridge.sol

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import "./interfaces/IInbox.sol";
66
import "./interfaces/IOutbox.sol";
77
import "./interfaces/IArbRetryableTx.sol";
88

9-
contract L1Bridge {
9+
import "../IL1Bridge.sol";
10+
11+
contract L1Bridge is IL1Bridge {
1012
address public l2Target;
1113
IInbox public inbox;
1214
IArbRetryableTx constant arbRetryableTx = IArbRetryableTx(address(110));
@@ -41,6 +43,7 @@ contract L1Bridge {
4143
uint256 _gasPriceBid
4244
) external payable returns (uint256) {
4345
uint256 baseSubmissionCost = getSubmissionPrice(_calldata.length);
46+
require(msg.value >= baseSubmissionCost);
4447

4548
uint256 ticketID = inbox.createRetryableTicket{value: msg.value}(
4649
l2Target,

contracts/src/bridge/arbitrum/L2Bridge.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ pragma solidity ^0.8.0;
55
import "./interfaces/IArbSys.sol";
66
import "./AddressAliasHelper.sol";
77

8-
contract L2Bridge {
8+
import "../IL2Bridge.sol";
9+
10+
contract L2Bridge is IL2Bridge {
911
address public l1Target;
1012
IArbSys constant arbsys = IArbSys(address(100));
1113

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
import "./interfaces/IAMB.sol";
6+
7+
import "../IL1Bridge.sol";
8+
9+
contract L1Bridge is IL1Bridge {
10+
address public l2Target;
11+
IAMB amb;
12+
13+
constructor(address _l2Target, IAMB _amb) {
14+
l2Target = _l2Target;
15+
amb = _amb;
16+
}
17+
18+
function sendCrossDomainMessage(
19+
bytes memory _calldata,
20+
uint256 _maxGas,
21+
uint256 _gasPriceBid
22+
) external payable returns (uint256) {
23+
bytes32 id = amb.requireToPassMessage(l2Target, _calldata, amb.maxGasPerTx());
24+
return uint256(id);
25+
}
26+
27+
/**
28+
* @dev The xDai bridge gas cost doesn't depend on the calldata size
29+
*
30+
*/
31+
function getSubmissionPrice(
32+
uint256 /* _calldatasize */
33+
) public view returns (uint256) {
34+
return 0;
35+
}
36+
37+
function onlyAuthorized(address _sender) external {
38+
require(_sender == address(amb), "Only AMB allowed");
39+
//require(amb.messageSourceChainId() == homeChainId, "Only home chain allowed");
40+
require(amb.messageSender() == l2Target, "Only home proxy allowed");
41+
}
42+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
import "./interfaces/IAMB.sol";
6+
7+
import "../IL2Bridge.sol";
8+
9+
contract L2Bridge is IL2Bridge {
10+
address public l1Target;
11+
IAMB amb;
12+
13+
constructor(address _l1Target, IAMB _amb) {
14+
l1Target = _l1Target;
15+
amb = _amb;
16+
}
17+
18+
function sendCrossDomainMessage(bytes memory _calldata) external returns (uint256) {
19+
bytes32 id = amb.requireToPassMessage(l1Target, _calldata, amb.maxGasPerTx());
20+
return uint256(id);
21+
}
22+
23+
function onlyAuthorized(address _sender) external {
24+
require(_sender == address(amb), "Only AMB allowed");
25+
//require(amb.messageSourceChainId() == homeChainId, "Only home chain allowed");
26+
require(amb.messageSender() == l1Target, "Only home proxy allowed");
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
interface IAMB {
5+
function requireToPassMessage(
6+
address _contract,
7+
bytes memory _data,
8+
uint256 _gas
9+
) external returns (bytes32);
10+
11+
function maxGasPerTx() external view returns (uint256);
12+
13+
function messageSender() external view returns (address);
14+
15+
function messageSourceChainId() external view returns (bytes32);
16+
17+
function messageId() external view returns (bytes32);
18+
}

contracts/src/gateway/arbitrum/ArbitrumGateway.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pragma solidity ^0.8.0;
44

55
import "../../arbitration/IArbitrator.sol";
6-
import "../../bridge/arbitrum/L2Bridge.sol";
6+
import "../../bridge/IL2Bridge.sol";
77

88
import "../IHomeGateway.sol";
99
import "../IForeignGateway.sol";
@@ -12,7 +12,7 @@ import "../IHomeEvidence.sol";
1212

1313
contract ArbitrumGateway is IHomeGateway, IHomeEvidence {
1414
// L2 bridge with the ForeignGateway as the l1target
15-
L2Bridge internal l2bridge;
15+
IL2Bridge internal l2bridge;
1616

1717
mapping(uint256 => bytes32) public disputeIDtoHash;
1818
mapping(bytes32 => uint256) public disputeHashtoID;
@@ -28,7 +28,7 @@ contract ArbitrumGateway is IHomeGateway, IHomeEvidence {
2828

2929
constructor(
3030
IArbitrator _arbitrator,
31-
L2Bridge _l2bridge,
31+
IL2Bridge _l2bridge,
3232
IForeignGateway _foreignGateway
3333
) {
3434
arbitrator = _arbitrator;

contracts/src/gateway/arbitrum/EthereumGateway.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pragma solidity ^0.8.0;
44

55
import "../../arbitration/IArbitrable.sol";
6-
import "../../bridge/arbitrum/L1Bridge.sol";
6+
import "../../bridge/IL1Bridge.sol";
77

88
import "../IHomeGateway.sol";
99
import "../IForeignGateway.sol";
@@ -12,7 +12,7 @@ import "../IForeignEvidence.sol";
1212

1313
contract EthereumGateway is IForeignGateway, IForeignEvidence {
1414
// L1 bridge with the HomeGateway as the l2target
15-
L1Bridge internal l1bridge;
15+
IL1Bridge internal l1bridge;
1616
uint256 internal localDisputeID;
1717

1818
// For now this is just a constant, but we'd probably need to
@@ -37,7 +37,7 @@ contract EthereumGateway is IForeignGateway, IForeignEvidence {
3737

3838
constructor(
3939
uint256 _arbitrationCost,
40-
L1Bridge _l1bridge,
40+
IL1Bridge _l1bridge,
4141
IHomeGateway _homeGateway
4242
) {
4343
internalArbitrationCost = _arbitrationCost;

0 commit comments

Comments
 (0)