Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contracts): lido gateway #988

Merged
merged 16 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions contracts/.solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
skipFiles: [
'mocks',
'test',
'L2/predeploys/L1BlockContainer.sol',
'libraries/verifier/ZkTrieVerifier.sol',
'libraries/verifier/PatriciaMerkleTrieVerifier.sol'
],
istanbulReporter: ["lcov", "json"]
};
1 change: 0 additions & 1 deletion contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ sender = '0x00a329c0648769a73afac7f9381e08fb43dbea72' # the address of `
tx_origin = '0x00a329c0648769a73afac7f9381e08fb43dbea72' # the address of `tx.origin` in tests
initial_balance = '0xffffffffffffffffffffffff' # the initial balance of the test contract
block_number = 0 # the block number we are at in tests
chain_id = 99 # the chain id we are on in tests
gas_limit = 9223372036854775807 # the gas limit in tests
gas_price = 0 # the gas price (in wei) in tests
block_base_fee_per_gas = 0 # the base fee (in wei) in tests
Expand Down
121 changes: 121 additions & 0 deletions contracts/src/lido/L1LidoGateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// SPDX-License-Identifier: MIT

pragma solidity =0.8.16;

import {IL1ERC20Gateway} from "../L1/gateways/IL1ERC20Gateway.sol";
import {L1ERC20Gateway} from "../L1/gateways/L1ERC20Gateway.sol";
import {IL1ScrollMessenger} from "../L1/IL1ScrollMessenger.sol";
import {IL2ERC20Gateway} from "../L2/gateways/IL2ERC20Gateway.sol";
import {ScrollGatewayBase} from "../libraries/gateway/ScrollGatewayBase.sol";

import {LidoBridgeableTokens} from "./LidoBridgeableTokens.sol";
import {LidoGatewayManager} from "./LidoGatewayManager.sol";

contract L1LidoGateway is L1ERC20Gateway, LidoBridgeableTokens, LidoGatewayManager {
/**********
* Errors *
**********/

/// @dev Thrown when deposit zero amount token.
error ErrorDepositZeroAmount();

/*************
* Variables *
*************/

/// @dev The initial version of `L1LidoGateway` use `L1CustomERC20Gateway`. We keep the storage
zimpha marked this conversation as resolved.
Show resolved Hide resolved
/// slot for `tokenMapping` for compatibility. It should no longer be used.
mapping(address => address) private __tokenMapping;

/***************
* Constructor *
***************/

/// @param _l1Token The address of the bridged token in the L1 chain
/// @param _l2Token The address of the token minted on the L2 chain when token bridged
constructor(address _l1Token, address _l2Token) LidoBridgeableTokens(_l1Token, _l2Token) {
_disableInitializers();
}

/// @notice Initialize the storage of L1LidoGateway v1.
function initialize(
address _counterpart,
address _router,
address _messenger
) external initializer {
require(_router != address(0), "zero router address");

ScrollGatewayBase._initialize(_counterpart, _router, _messenger);
}

/// @notice Initialize the storage of L1LidoGateway v2.
function initializeV2() external reinitializer(2) {
__LidoGatewayManager_init();
}

/*************************
* Public View Functions *
*************************/

/// @inheritdoc IL1ERC20Gateway
function getL2ERC20Address(address _l1Token)
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
external
view
override
onlySupportedL1Token(_l1Token)
returns (address)
{
return l2Token;
}

/**********************
* Internal Functions *
**********************/

/// @inheritdoc L1ERC20Gateway
function _beforeFinalizeWithdrawERC20(
address _l1Token,
address _l2Token,
address,
address,
uint256,
bytes calldata
) internal virtual override onlySupportedL1Token(_l1Token) onlySupportedL2Token(_l2Token) whenWithdrawalsEnabled {
if (msg.value != 0) revert ErrorNonZeroMsgValue();
icemelon marked this conversation as resolved.
Show resolved Hide resolved
}

/// @inheritdoc L1ERC20Gateway
function _beforeDropMessage(
address _token,
address,
uint256
) internal virtual override onlySupportedL1Token(_token) {
if (msg.value != 0) revert ErrorNonZeroMsgValue();
icemelon marked this conversation as resolved.
Show resolved Hide resolved
}

/// @inheritdoc L1ERC20Gateway
function _deposit(
address _token,
address _to,
uint256 _amount,
bytes memory _data,
uint256 _gasLimit
) internal virtual override nonReentrant onlySupportedL1Token(_token) onlyNonZeroAccount(_to) whenDepositsEnabled {
if (_amount == 0) revert ErrorDepositZeroAmount();

// 1. Transfer token into this contract.
address _from;
(_from, _amount, _data) = _transferERC20In(_token, _amount, _data);

// 2. Generate message passed to L2LidoGateway.
bytes memory _message = abi.encodeCall(
IL2ERC20Gateway.finalizeDepositERC20,
(_token, l2Token, _from, _to, _amount, _data)
);

// 3. Send message to L1ScrollMessenger.
IL1ScrollMessenger(messenger).sendMessage{value: msg.value}(counterpart, 0, _message, _gasLimit, _from);
icemelon marked this conversation as resolved.
Show resolved Hide resolved

emit DepositERC20(_token, l2Token, _from, _to, _amount, _data);
}
}
156 changes: 156 additions & 0 deletions contracts/src/lido/L2LidoGateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// SPDX-License-Identifier: MIT

pragma solidity =0.8.16;

import {IL1ERC20Gateway} from "../L1/gateways/IL1ERC20Gateway.sol";
import {IL2ERC20Gateway} from "../L2/gateways/IL2ERC20Gateway.sol";
import {L2ERC20Gateway} from "../L2/gateways/L2ERC20Gateway.sol";
import {IL2ScrollMessenger} from "../L2/IL2ScrollMessenger.sol";
import {IScrollERC20Upgradeable} from "../libraries/token/IScrollERC20Upgradeable.sol";
import {ScrollGatewayBase} from "../libraries/gateway/ScrollGatewayBase.sol";

import {LidoBridgeableTokens} from "./LidoBridgeableTokens.sol";
import {LidoGatewayManager} from "./LidoGatewayManager.sol";

contract L2LidoGateway is L2ERC20Gateway, LidoBridgeableTokens, LidoGatewayManager {
/**********
* Errors *
**********/

/// @dev Thrown when withdraw zero amount token.
error ErrorWithdrawZeroAmount();

/*************
* Variables *
*************/

/// @dev The initial version of `L2LidoGateway` use `L2CustomERC20Gateway`. We keep the storage
/// slot for `tokenMapping` for compatibility. It should no longer be used.
mapping(address => address) private __tokenMapping;

/***************
* Constructor *
***************/

/// @param _l1Token The address of the bridged token in the L1 chain
/// @param _l2Token The address of the token minted on the L2 chain when token bridged
constructor(address _l1Token, address _l2Token) LidoBridgeableTokens(_l1Token, _l2Token) {
_disableInitializers();
}

/// @notice Initialize the storage of L2LidoGateway v1.
function initialize(
address _counterpart,
address _router,
address _messenger
) external initializer {
require(_router != address(0), "zero router address");

ScrollGatewayBase._initialize(_counterpart, _router, _messenger);
}

/// @notice Initialize the storage of L2LidoGateway v2.
function initializeV2() external reinitializer(2) {
__LidoGatewayManager_init();
}

/*************************
* Public View Functions *
*************************/

/// @inheritdoc IL2ERC20Gateway
function getL1ERC20Address(address _l2Token)
external
view
override
onlySupportedL2Token(_l2Token)
returns (address)
{
return l1Token;
}

/// @inheritdoc IL2ERC20Gateway
function getL2ERC20Address(address _l1Token)
external
view
override
onlySupportedL1Token(_l1Token)
returns (address)
{
return l2Token;
}

/*****************************
* Public Mutating Functions *
*****************************/

/// @inheritdoc IL2ERC20Gateway
function finalizeDepositERC20(
address _l1Token,
address _l2Token,
address _from,
address _to,
uint256 _amount,
bytes calldata _data
)
external
payable
override
onlyCallByCounterpart
nonReentrant
onlySupportedL1Token(_l1Token)
onlySupportedL2Token(_l2Token)
whenDepositsEnabled
{
if (msg.value != 0) revert ErrorNonZeroMsgValue();

IScrollERC20Upgradeable(_l2Token).mint(_to, _amount);

_doCallback(_to, _data);
zimpha marked this conversation as resolved.
Show resolved Hide resolved

emit FinalizeDepositERC20(_l1Token, _l2Token, _from, _to, _amount, _data);
}

/**********************
* Internal Functions *
**********************/

/// @inheritdoc L2ERC20Gateway
function _withdraw(
address _l2Token,
address _to,
uint256 _amount,
bytes memory _data,
uint256 _gasLimit
)
internal
virtual
override
nonReentrant
onlySupportedL2Token(_l2Token)
onlyNonZeroAccount(_to)
whenWithdrawalsEnabled
{
if (_amount == 0) revert ErrorWithdrawZeroAmount();

// 1. Extract real sender if this call is from L2GatewayRouter.
zimpha marked this conversation as resolved.
Show resolved Hide resolved
address _from = _msgSender();
if (router == _from) {
(_from, _data) = abi.decode(_data, (address, bytes));
}

// 2. Burn token.
IScrollERC20Upgradeable(_l2Token).burn(_from, _amount);

// 3. Generate message passed to L1LidoGateway.
bytes memory _message = abi.encodeCall(
IL1ERC20Gateway.finalizeWithdrawERC20,
(l1Token, _l2Token, _from, _to, _amount, _data)
);

// 4. send message to L2ScrollMessenger
IL2ScrollMessenger(messenger).sendMessage{value: msg.value}(counterpart, 0, _message, _gasLimit);
zimpha marked this conversation as resolved.
Show resolved Hide resolved

emit WithdrawERC20(l1Token, _l2Token, _from, _to, _amount, _data);
}
}
70 changes: 70 additions & 0 deletions contracts/src/lido/LidoBridgeableTokens.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: MIT

pragma solidity =0.8.16;

abstract contract LidoBridgeableTokens {
/*************
* Constants *
*************/

/// @notice The address of bridged token in L1 chain.
address public immutable l1Token;

/// @notice The address of the token minted on the L2 chain when token bridged.
address public immutable l2Token;

/**********
* Errors *
**********/

/// @dev Thrown the given `l1Token` is not supported.
error ErrorUnsupportedL1Token();

/// @dev Thrown the given `l2Token` is not supported.
error ErrorUnsupportedL2Token();

/// @dev Thrown the given account is zero address.
error ErrorAccountIsZeroAddress();

/// @dev Thrown the `msg.value` is not zero.
error ErrorNonZeroMsgValue();

/**********************
* Function Modifiers *
**********************/

/// @dev Validates that passed `_l1Token` is supported by the bridge
modifier onlySupportedL1Token(address _l1Token) {
if (_l1Token != l1Token) {
revert ErrorUnsupportedL1Token();
}
_;
}

/// @dev Validates that passed `_l2Token` is supported by the bridge
modifier onlySupportedL2Token(address _l2Token) {
if (_l2Token != l2Token) {
revert ErrorUnsupportedL2Token();
}
_;
}

/// @dev validates that `_account` is not zero address
modifier onlyNonZeroAccount(address _account) {
if (_account == address(0)) {
revert ErrorAccountIsZeroAddress();
}
_;
}

/***************
* Constructor *
***************/

/// @param _l1Token The address of the bridged token in the L1 chain
/// @param _l2Token The address of the token minted on the L2 chain when token bridged
constructor(address _l1Token, address _l2Token) {
l1Token = _l1Token;
l2Token = _l2Token;
}
}
Loading
Loading