Skip to content

Commit

Permalink
fix: script fixes (#726)
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
Co-authored-by: Neo <[email protected]>
Co-authored-by: tommysr <[email protected]>
Co-authored-by: Rahul Saxena <[email protected]>
Co-authored-by: Artem Makhortov <[email protected]>
Co-authored-by: Bence Haromi <[email protected]>
Co-authored-by: Zach Kolodny <[email protected]>
Co-authored-by: Stanislav Bezkorovainyi <[email protected]>
Co-authored-by: Vlad Bochok <[email protected]>
Co-authored-by: perekopskiy <[email protected]>
Co-authored-by: perekopskiy <[email protected]>
Co-authored-by: Danil <[email protected]>
Co-authored-by: Ivan Schasny <[email protected]>
Co-authored-by: Raid5594 <[email protected]>
Co-authored-by: Raid Ateir <[email protected]>
  • Loading branch information
15 people authored Aug 21, 2024
1 parent a91bc7a commit 391fa4d
Show file tree
Hide file tree
Showing 70 changed files with 2,096 additions and 444 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/l1-contracts-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name: L1 contracts CI
on:
pull_request:

# We need this permissions for this CI to work with external contributions
permissions:
contents: read
pull-requests: write

jobs:
build:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion gas-bound-caller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"ts-node": "^10.1.0",
"typechain": "^4.0.0",
"typescript": "^4.6.4",
"zksync-ethers": "5.8.0-beta.5"
"zksync-ethers": "^5.9.0"
},
"mocha": {
"timeout": 240000,
Expand Down
10 changes: 7 additions & 3 deletions l1-contracts/contracts/bridge/L1NativeTokenVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol

import {IL1NativeTokenVault} from "./interfaces/IL1NativeTokenVault.sol";
import {IL1AssetHandler} from "./interfaces/IL1AssetHandler.sol";

import {IL1AssetRouter} from "./interfaces/IL1AssetRouter.sol";
import {ETH_TOKEN_ADDRESS} from "../common/Config.sol";
import {DataEncoding} from "../common/libraries/DataEncoding.sol";
Expand All @@ -23,7 +22,7 @@ import {BridgeHelper} from "./BridgeHelper.sol";
/// @custom:security-contact [email protected]
/// @dev Vault holding L1 native ETH and ERC20 tokens bridged into the ZK chains.
/// @dev Designed for use with a proxy for upgradability.
contract L1NativeTokenVault is IL1NativeTokenVault, IL1AssetHandler, Ownable2StepUpgradeable, PausableUpgradeable {
contract L1NativeTokenVault is IL1NativeTokenVault, Ownable2StepUpgradeable, PausableUpgradeable {
using SafeERC20 for IERC20;

/// @dev The address of the WETH token on L1.
Expand Down Expand Up @@ -220,10 +219,15 @@ contract L1NativeTokenVault is IL1NativeTokenVault, IL1AssetHandler, Ownable2Ste
}

/// @dev Receives and parses (name, symbol, decimals) from the token contract
function getERC20Getters(address _token) public view returns (bytes memory) {
function getERC20Getters(address _token) public view override returns (bytes memory) {
return BridgeHelper.getERC20Getters(_token, ETH_TOKEN_ADDRESS);
}

/// @dev Shows the assetId for a given chain and token address
function getAssetId(uint256 _chainId, address _l1Token) external pure override returns (bytes32) {
return DataEncoding.encodeNTVAssetId(_chainId, _l1Token);
}

/// @dev Transfers tokens from the depositor address to the smart contract address.
/// @return The difference between the contract balance before and after the transferring of funds.
function _depositFunds(address _from, IERC20 _token, uint256 _amount) internal returns (uint256) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

/// @title L1 Base Token Asset Handler contract interface
/// @author Matter Labs
/// @custom:security-contact [email protected]
/// @notice Used for any asset handler and called by the L1AssetRouter
interface IL1BaseTokenAssetHandler {
/// @notice Used to get the token address of an assetId
function tokenAddress(bytes32 _assetId) external view returns (address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
pragma solidity 0.8.24;

import {IL1AssetRouter} from "./IL1AssetRouter.sol";
import {IL1AssetHandler} from "./IL1AssetHandler.sol";
import {IL1BaseTokenAssetHandler} from "./IL1BaseTokenAssetHandler.sol";

/// @title L1 Native token vault contract interface
/// @author Matter Labs
/// @custom:security-contact [email protected]
/// @notice The NTV is an Asset Handler for the L1AssetRouter to handle native tokens
interface IL1NativeTokenVault {
interface IL1NativeTokenVault is IL1AssetHandler, IL1BaseTokenAssetHandler {
/// @notice The L1AssetRouter contract
function L1_SHARED_BRIDGE() external view returns (IL1AssetRouter);

Expand All @@ -24,6 +26,6 @@ interface IL1NativeTokenVault {
/// @notice Used the get token balance for specific ZK chain in shared bridge
function chainBalance(uint256 _chainId, address _l1Token) external view returns (uint256);

/// @notice Used to get the token address of an assetId
function tokenAddress(bytes32 _assetId) external view returns (address);
/// @dev Shows the assetId for a given chain and token address
function getAssetId(uint256 _chainId, address _l1Token) external pure returns (bytes32);
}
51 changes: 32 additions & 19 deletions l1-contracts/contracts/bridgehub/Bridgehub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/security/

import {IBridgehub, L2TransactionRequestDirect, L2TransactionRequestTwoBridgesOuter, L2TransactionRequestTwoBridgesInner} from "./IBridgehub.sol";
import {IL1AssetRouter} from "../bridge/interfaces/IL1AssetRouter.sol";
import {IL1BaseTokenAssetHandler} from "../bridge/interfaces/IL1BaseTokenAssetHandler.sol";
import {IStateTransitionManager} from "../state-transition/IStateTransitionManager.sol";
import {ReentrancyGuard} from "../common/ReentrancyGuard.sol";
import {DataEncoding} from "../common/libraries/DataEncoding.sol";
Expand Down Expand Up @@ -48,15 +49,17 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
IL1AssetRouter public sharedBridge;

/// @notice StateTransitionManagers that are registered, and ZKchains that use these STMs can use this bridgehub as settlement layer.
mapping(address _stateTransitionManager => bool) public stateTransitionManagerIsRegistered;
mapping(address stateTransitionManager => bool) public stateTransitionManagerIsRegistered;

/// @notice we store registered tokens (for arbitrary base token)
mapping(address _baseToken => bool) public tokenIsRegistered;
mapping(address baseToken => bool) public __DEPRECATED_tokenIsRegistered;

/// @notice chainID => StateTransitionManager contract address, STM that is managing rules for a given ZKchain.
mapping(uint256 _chainId => address) public stateTransitionManager;
mapping(uint256 chainId => address) public stateTransitionManager;

/// @notice chainID => baseToken contract address, token that is used as 'base token' by a given child chain.
mapping(uint256 _chainId => address) public baseToken;
// slither-disable-next-line uninitialized-state
mapping(uint256 chainId => address) public __DEPRECATED_baseToken;

/// @dev used to manage non critical updates
address public admin;
Expand All @@ -73,7 +76,7 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
IMessageRoot public override messageRoot;

/// @notice Mapping from chain id to encoding of the base token used for deposits / withdrawals
mapping(uint256 _chainId => bytes32) public baseTokenAssetId;
mapping(uint256 chainId => bytes32) public baseTokenAssetId;

/// @notice The deployment tracker for the state transition managers.
ISTMDeploymentTracker public stmDeployer;
Expand All @@ -89,6 +92,9 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
/// @dev Sync layer chain is expected to have .. as the base token.
mapping(uint256 chainId => bool isWhitelistedSettlementLayer) public whitelistedSettlementLayers;

/// @notice we store registered assetIds (for arbitrary base token)
mapping(bytes32 baseTokenAssetId => bool) public assetIdIsRegistered;

modifier onlyOwnerOrAdmin() {
require(msg.sender == admin || msg.sender == owner(), "BH: not owner or admin");
_;
Expand Down Expand Up @@ -181,7 +187,7 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
if (baseTokenAssetId[_chainId] == bytes32(0)) {
return;
}
address token = baseToken[_chainId];
address token = __DEPRECATED_baseToken[_chainId];
require(token != address(0), "BH: token not set");
baseTokenAssetId[_chainId] = DataEncoding.encodeNTVAssetId(block.chainid, token);
}
Expand Down Expand Up @@ -222,13 +228,13 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
emit StateTransitionManagerRemoved(_stateTransitionManager);
}

/// @notice token can be any contract with the appropriate interface/functionality
/// @param _token address of base token to be registered
function addToken(address _token) external onlyOwner {
require(!tokenIsRegistered[_token], "BH: token already registered");
tokenIsRegistered[_token] = true;
/// @notice asset id can represent any token contract with the appropriate interface/functionality
/// @param _baseTokenAssetId asset id of base token to be registered
function addTokenAssetId(bytes32 _baseTokenAssetId) external onlyOwner {
require(!assetIdIsRegistered[_baseTokenAssetId], "BH: asset id already registered");
assetIdIsRegistered[_baseTokenAssetId] = true;

emit TokenRegistered(_token);
emit BaseTokenAssetIdRegistered(_baseTokenAssetId);
}

/// @notice Used to register a chain as a settlement layer.
Expand Down Expand Up @@ -274,15 +280,15 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
/// @notice for Eth the baseToken address is 1
/// @param _chainId the chainId of the chain
/// @param _stateTransitionManager the state transition manager address
/// @param _baseToken the base token of the chain
/// @param _baseTokenAssetId the base token asset id of the chain
/// @param _salt the salt for the chainId, currently not used
/// @param _admin the admin of the chain
/// @param _initData the fixed initialization data for the chain
/// @param _factoryDeps the factory dependencies for the chain's deployment
function createNewChain(
uint256 _chainId,
address _stateTransitionManager,
address _baseToken,
bytes32 _baseTokenAssetId,
// solhint-disable-next-line no-unused-vars
uint256 _salt,
address _admin,
Expand All @@ -294,21 +300,19 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
require(_chainId != block.chainid, "BH: chain id must not match current chainid");

require(stateTransitionManagerIsRegistered[_stateTransitionManager], "BH: state transition not registered");
require(tokenIsRegistered[_baseToken], "BH: token not registered");
require(assetIdIsRegistered[_baseTokenAssetId], "BH: asset id not registered");
require(address(sharedBridge) != address(0), "BH: shared bridge not set");

require(stateTransitionManager[_chainId] == address(0), "BH: chainId already registered");

stateTransitionManager[_chainId] = _stateTransitionManager;
baseToken[_chainId] = _baseToken;

/// For now all base tokens have to use the NTV.
baseTokenAssetId[_chainId] = DataEncoding.encodeNTVAssetId(block.chainid, _baseToken);
baseTokenAssetId[_chainId] = _baseTokenAssetId;
settlementLayer[_chainId] = block.chainid;

address chainAddress = IStateTransitionManager(_stateTransitionManager).createNewChain({
_chainId: _chainId,
_baseToken: _baseToken,
_baseTokenAssetId: _baseTokenAssetId,
_sharedBridge: address(sharedBridge),
_admin: _admin,
_initData: _initData,
Expand All @@ -332,6 +336,15 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
Getters
//////////////////////////////////////////////////////////////*/

/// @notice baseToken function, which takes assetId as input, reads assetHandler from AR, and tokenAddress from AH
function baseToken(uint256 _chainId) public view returns (address) {
bytes32 baseTokenAssetId = baseTokenAssetId[_chainId];
IL1BaseTokenAssetHandler assetHandlerAddress = IL1BaseTokenAssetHandler(
sharedBridge.assetHandlerAddress(baseTokenAssetId)
);
return assetHandlerAddress.tokenAddress(baseTokenAssetId);
}

/// @notice Returns all the registered hyperchain addresses
function getAllHyperchains() public view override returns (address[] memory chainAddresses) {
uint256[] memory keys = hyperchainMap.keys();
Expand Down
10 changes: 4 additions & 6 deletions l1-contracts/contracts/bridgehub/IBridgehub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ interface IBridgehub is IL1AssetHandler {

function stateTransitionManager(uint256 _chainId) external view returns (address);

function tokenIsRegistered(address _baseToken) external view returns (bool);
function assetIdIsRegistered(bytes32 _baseTokenAssetId) external view returns (bool);

function baseToken(uint256 _chainId) external view returns (address);

Expand Down Expand Up @@ -137,7 +137,7 @@ interface IBridgehub is IL1AssetHandler {
function createNewChain(
uint256 _chainId,
address _stateTransitionManager,
address _baseToken,
bytes32 _baseTokenAssetId,
uint256 _salt,
address _admin,
bytes calldata _initData,
Expand All @@ -148,7 +148,7 @@ interface IBridgehub is IL1AssetHandler {

function removeStateTransitionManager(address _stateTransitionManager) external;

function addToken(address _token) external;
function addTokenAssetId(bytes32 _baseTokenAssetId) external;

function setAddresses(
address _sharedBridge,
Expand All @@ -162,9 +162,7 @@ interface IBridgehub is IL1AssetHandler {

event StateTransitionManagerRemoved(address indexed stateTransitionManager);

event TokenRegistered(address indexed token);

event SharedBridgeUpdated(address indexed sharedBridge);
event BaseTokenAssetIdRegistered(bytes32 indexed assetId);

function whitelistedSettlementLayers(uint256 _chainId) external view returns (bool);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ contract DummyAdminFacetNoOverlap is ZkSyncHyperchainBase {

function executeUpgradeNoOverlap(Diamond.DiamondCutData calldata _diamondCut) external {
Diamond.diamondCut(_diamondCut);
s.baseTokenAssetId = DataEncoding.encodeNTVAssetId(block.chainid, s.baseToken);
}

function receiveEther() external payable {}
Expand Down
2 changes: 2 additions & 0 deletions l1-contracts/contracts/dev-contracts/test/DummyBridgehub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {IMessageRoot} from "../../bridgehub/IMessageRoot.sol";

import {IGetters} from "../../state-transition/chain-interfaces/IGetters.sol";

/// @title DummyBridgehub
/// @notice A test smart contract that allows to set State Transition Manager for a given chain
contract DummyBridgehub {
IMessageRoot public messageRoot;

Expand Down
4 changes: 4 additions & 0 deletions l1-contracts/contracts/dev-contracts/test/DummyHyperchain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ contract DummyHyperchain is MailboxFacet {
s.bridgehub = bridgeHubAddress;
}

function getEraChainId() public view returns (uint256) {
return ERA_CHAIN_ID;
}

function setBridgeHubAddress(address bridgeHubAddress) public {
s.bridgehub = bridgeHubAddress;
}
Expand Down
Loading

0 comments on commit 391fa4d

Please sign in to comment.