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 11 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
43 changes: 43 additions & 0 deletions contracts/scripts/foundry/DeployLidoGateway.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

import {Script} from "forge-std/Script.sol";
import {console} from "forge-std/console.sol";

import {L1LidoGateway} from "../../src/lido/L1LidoGateway.sol";
import {L2LidoGateway} from "../../src/lido/L2LidoGateway.sol";

// solhint-disable state-visibility
// solhint-disable var-name-mixedcase

contract DeployLidoGateway is Script {
string NETWORK = vm.envString("NETWORK");

uint256 L1_DEPLOYER_PRIVATE_KEY = vm.envUint("L1_DEPLOYER_PRIVATE_KEY");

uint256 L2_DEPLOYER_PRIVATE_KEY = vm.envUint("L2_DEPLOYER_PRIVATE_KEY");

address L1_WSTETH_ADDR = vm.envAddress("L1_WSTETH_ADDR");

address L2_WSTETH_ADDR = vm.envAddress("L2_WSTETH_ADDR");

function run() external {
vm.startBroadcast(L2_DEPLOYER_PRIVATE_KEY);

if (keccak256(abi.encodePacked(NETWORK)) == keccak256(abi.encodePacked("L1"))) {
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
// depoly l1 lido gateway
zimpha marked this conversation as resolved.
Show resolved Hide resolved
L1LidoGateway gateway = new L1LidoGateway(L1_WSTETH_ADDR, L2_WSTETH_ADDR);
logAddress("L1_LIDO_GATEWAY_IMPLEMENTATION_ADDR", address(gateway));
} else if (keccak256(abi.encodePacked(NETWORK)) == keccak256(abi.encodePacked("L2"))) {
// depoly l2 lido gateway
L2LidoGateway gateway = new L2LidoGateway(L1_WSTETH_ADDR, L2_WSTETH_ADDR);
logAddress("L2_LIDO_GATEWAY_IMPLEMENTATION_ADDR", address(gateway));
}

vm.stopBroadcast();
}

function logAddress(string memory name, address addr) internal view {
console.log(string(abi.encodePacked(name, "=", vm.toString(address(addr)))));
}
}
135 changes: 135 additions & 0 deletions contracts/src/lido/L1LidoGateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// 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();

/// @dev Thrown when deposit erc20 with calldata.
error DepositAndCallIsNotAllowed();

/*************
* 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.
/// @param _depositsEnabler The address of user who can enable deposits
/// @param _depositsEnabler The address of user who can disable deposits
/// @param _withdrawalsEnabler The address of user who can enable withdrawals
/// @param _withdrawalsDisabler The address of user who can disable withdrawals
function initializeV2(
address _depositsEnabler,
address _depositsDisabler,
address _withdrawalsEnabler,
address _withdrawalsDisabler
) external reinitializer(2) {
__LidoGatewayManager_init(_depositsEnabler, _depositsDisabler, _withdrawalsEnabler, _withdrawalsDisabler);
}

/*************************
* 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
/// @dev The length of `_data` always be zero, which guarantee by `L2LidoGateway`.
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);
if (_data.length != 0) revert DepositAndCallIsNotAllowed();
Thegaram marked this conversation as resolved.
Show resolved Hide resolved

// 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);
}
}
168 changes: 168 additions & 0 deletions contracts/src/lido/L2LidoGateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// 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();

/// @dev Thrown when withdraw erc20 with calldata.
error WithdrawAndCallIsNotAllowed();

/*************
* 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.
/// @param _depositsEnabler The address of user who can enable deposits
/// @param _depositsEnabler The address of user who can disable deposits
/// @param _withdrawalsEnabler The address of user who can enable withdrawals
/// @param _withdrawalsDisabler The address of user who can disable withdrawals
function initializeV2(
address _depositsEnabler,
address _depositsDisabler,
address _withdrawalsEnabler,
address _withdrawalsDisabler
) external reinitializer(2) {
__LidoGatewayManager_init(_depositsEnabler, _depositsDisabler, _withdrawalsEnabler, _withdrawalsDisabler);
}

/*************************
* 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
/// @dev The length of `_data` always be zero, which guarantee by `L1LidoGateway`.
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);

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));
}
if (_data.length != 0) revert WithdrawAndCallIsNotAllowed();

// 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);
}
}
Loading
Loading