Skip to content

Commit

Permalink
refactor(protocol): added cached version of the bridge and vaults (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik committed Jul 16, 2024
1 parent 4076324 commit b70cc57
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 68 deletions.
26 changes: 13 additions & 13 deletions packages/protocol/contracts/libs/LibNetwork.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ pragma solidity 0.8.24;

/// @title LibNetwork
library LibNetwork {
uint256 internal constant MAINNET = 1;
uint256 internal constant ROPSTEN = 2;
uint256 internal constant RINKEBY = 4;
uint256 internal constant GOERLI = 5;
uint256 internal constant KOVAN = 42;
uint256 internal constant HOLESKY = 17_000;
uint256 internal constant SEPOLIA = 11_155_111;
uint256 internal constant ETHEREUM_MAINNET = 1;
uint256 internal constant ETHEREUM_ROPSTEN = 2;
uint256 internal constant ETHEREUM_RINKEBY = 4;
uint256 internal constant ETHEREUM_GOERLI = 5;
uint256 internal constant ETHEREUM_KOVAN = 42;
uint256 internal constant ETHEREUM_HOLESKY = 17_000;
uint256 internal constant ETHEREUM_SEPOLIA = 11_155_111;

uint64 internal constant TAIKO_MAINNET = 167_000;
uint64 internal constant TAIKO_HEKLA = 167_009;
Expand All @@ -18,17 +18,17 @@ library LibNetwork {
/// @param _chainId The chain ID.
/// @return true if the chain ID represents an Ethereum testnet, false otherwise.
function isEthereumTestnet(uint256 _chainId) internal pure returns (bool) {
return _chainId == LibNetwork.ROPSTEN || _chainId == LibNetwork.RINKEBY
|| _chainId == LibNetwork.GOERLI || _chainId == LibNetwork.KOVAN
|| _chainId == LibNetwork.HOLESKY || _chainId == LibNetwork.SEPOLIA;
return _chainId == LibNetwork.ETHEREUM_ROPSTEN || _chainId == LibNetwork.ETHEREUM_RINKEBY
|| _chainId == LibNetwork.ETHEREUM_GOERLI || _chainId == LibNetwork.ETHEREUM_KOVAN
|| _chainId == LibNetwork.ETHEREUM_HOLESKY || _chainId == LibNetwork.ETHEREUM_SEPOLIA;
}

/// @dev Checks if the chain ID represents an Ethereum testnet or the Etheruem mainnet.
/// @param _chainId The chain ID.
/// @return true if the chain ID represents an Ethereum testnet or the Etheruem mainnet, false
/// otherwise.
function isEthereumMainnetOrTestnet(uint256 _chainId) internal pure returns (bool) {
return _chainId == LibNetwork.MAINNET || isEthereumTestnet(_chainId);
return _chainId == LibNetwork.ETHEREUM_MAINNET || isEthereumTestnet(_chainId);
}

/// @dev Checks if the chain ID represents the Taiko L2 mainnet.
Expand All @@ -51,7 +51,7 @@ library LibNetwork {
/// @param _chainId The chain ID.
/// @return true if the chain supports Dencun hardfork, false otherwise.
function isDencunSupported(uint256 _chainId) internal pure returns (bool) {
return _chainId == LibNetwork.MAINNET || _chainId == LibNetwork.HOLESKY
|| _chainId == LibNetwork.SEPOLIA || isTaikoDevnet(_chainId);
return _chainId == LibNetwork.ETHEREUM_MAINNET || _chainId == LibNetwork.ETHEREUM_HOLESKY
|| _chainId == LibNetwork.ETHEREUM_SEPOLIA || isTaikoDevnet(_chainId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
pragma solidity 0.8.24;

import "../common/LibStrings.sol";
import "../libs/LibNetwork.sol";

/// @title LibAddressCache
/// @title LibRollupAddressCache
/// @custom:security-contact security@taiko.xyz
library LibAddressCache {
library LibRollupAddressCache {
function getAddress(
uint64 _chainId,
bytes32 _name
Expand All @@ -14,7 +15,7 @@ library LibAddressCache {
pure
returns (bool found, address addr)
{
if (_chainId == 1) {
if (_chainId == LibNetwork.ETHEREUM_MAINNET) {
if (_name == LibStrings.B_TAIKO_TOKEN) {
return (true, 0x10dea67478c5F8C5E2D90e5E9B26dBe60c54d800);
}
Expand Down
69 changes: 69 additions & 0 deletions packages/protocol/contracts/mainnet/LibSharedAddressCache.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../common/LibStrings.sol";
import "../libs/LibNetwork.sol";

/// @title LibSharedAddressCache
/// @custom:security-contact security@taiko.xyz
library LibSharedAddressCache {
function getAddress(
uint64 _chainId,
bytes32 _name
)
internal
pure
returns (bool found, address addr)
{
if (_chainId == LibNetwork.ETHEREUM_MAINNET) {
if (_name == LibStrings.B_TAIKO_TOKEN) {
return (true, 0x10dea67478c5F8C5E2D90e5E9B26dBe60c54d800);
}
if (_name == LibStrings.B_QUOTA_MANAGER) {
return (true, 0x91f67118DD47d502B1f0C354D0611997B022f29E);
}
if (_name == LibStrings.B_BRIDGE) {
return (true, 0xd60247c6848B7Ca29eDdF63AA924E53dB6Ddd8EC);
}
if (_name == LibStrings.B_BRIDGED_ERC20) {
return (true, 0x79BC0Aada00fcF6E7AB514Bfeb093b5Fae3653e3);
}
if (_name == LibStrings.B_BRIDGED_ERC721) {
return (true, 0xC3310905E2BC9Cfb198695B75EF3e5B69C6A1Bf7);
}
if (_name == LibStrings.B_BRIDGED_ERC1155) {
return (true, 0x3c90963cFBa436400B0F9C46Aa9224cB379c2c40);
}
if (_name == LibStrings.B_ERC20_VAULT) {
return (true, 0x996282cA11E5DEb6B5D122CC3B9A1FcAAD4415Ab);
}
if (_name == LibStrings.B_ERC721_VAULT) {
return (true, 0x0b470dd3A0e1C41228856Fb319649E7c08f419Aa);
}
if (_name == LibStrings.B_ERC1155_VAULT) {
return (true, 0xaf145913EA4a56BE22E120ED9C24589659881702);
}
if (_name == LibStrings.B_SIGNAL_SERVICE) {
return (true, 0x9e0a24964e5397B566c1ed39258e21aB5E35C77C);
}
} else if (_chainId == LibNetwork.TAIKO_MAINNET) {
if (_name == LibStrings.B_BRIDGE) {
return (true, 0x1670000000000000000000000000000000000001);
}
if (_name == LibStrings.B_ERC20_VAULT) {
return (true, 0x1670000000000000000000000000000000000002);
}
if (_name == LibStrings.B_ERC721_VAULT) {
return (true, 0x1670000000000000000000000000000000000003);
}
if (_name == LibStrings.B_ERC1155_VAULT) {
return (true, 0x1670000000000000000000000000000000000004);
}
if (_name == LibStrings.B_SIGNAL_SERVICE) {
return (true, 0x1670000000000000000000000000000000000005);
}
}

return (false, address(0));
}
}
18 changes: 18 additions & 0 deletions packages/protocol/contracts/mainnet/MainnetBridge.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../bridge/Bridge.sol";
import "./LibSharedAddressCache.sol";

/// @title MainnetBridge
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost. In theory, the contract can also be deplyed on Taiko L2 but this is
/// not well testee nor necessary.
/// @notice See the documentation in {Bridge}.
/// @custom:security-contact security@taiko.xyz
contract MainnetBridge is Bridge {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibSharedAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
18 changes: 18 additions & 0 deletions packages/protocol/contracts/mainnet/MainnetERC1155Vault.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../tokenvault/ERC1155Vault.sol";
import "./LibSharedAddressCache.sol";

/// @title MainnetERC1155Vault
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost. In theory, the contract can also be deplyed on Taiko L2 but this is
/// not well testee nor necessary.
/// @notice See the documentation in {ER1155Vault}.
/// @custom:security-contact security@taiko.xyz
contract MainnetERC1155Vault is ERC1155Vault {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibSharedAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
18 changes: 18 additions & 0 deletions packages/protocol/contracts/mainnet/MainnetERC20Vault.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../tokenvault/ERC20Vault.sol";
import "./LibSharedAddressCache.sol";

/// @title MainnetERC20Vault
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost. In theory, the contract can also be deplyed on Taiko L2 but this is
/// not well testee nor necessary.
/// @notice See the documentation in {ER20Vault}.
/// @custom:security-contact security@taiko.xyz
contract MainnetERC20Vault is ERC20Vault {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibSharedAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
18 changes: 18 additions & 0 deletions packages/protocol/contracts/mainnet/MainnetERC721Vault.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../tokenvault/ERC721Vault.sol";
import "./LibSharedAddressCache.sol";

/// @title MainnetERC721Vault
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost. In theory, the contract can also be deplyed on Taiko L2 but this is
/// not well testee nor necessary.
/// @notice See the documentation in {ER721Vault}.
/// @custom:security-contact security@taiko.xyz
contract MainnetERC721Vault is ERC721Vault {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibSharedAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
7 changes: 5 additions & 2 deletions packages/protocol/contracts/mainnet/MainnetProverSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
pragma solidity 0.8.24;

import "../team/proving/ProverSet.sol";
import "./LibAddressCache.sol";
import "./LibRollupAddressCache.sol";

/// @title MainnetProverSet
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost. In theory, the contract can also be deplyed on Taiko L2 but this is
/// not well testee nor necessary.
/// @notice See the documentation in {ProverSet}.
/// @custom:security-contact security@taiko.xyz
contract MainnetProverSet is ProverSet {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibAddressCache.getAddress(_chainId, _name);
(bool found, address addr) = LibRollupAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ pragma solidity 0.8.24;

import "../common/AddressManager.sol";
import "../common/LibStrings.sol";
import "./LibAddressCache.sol";
import "./LibRollupAddressCache.sol";

/// @title MainnetRollupAddressManager
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost.
/// @notice See the documentation in {IAddressManager}.
/// @custom:security-contact security@taiko.xyz
contract MainnetRollupAddressManager is AddressManager {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibAddressCache.getAddress(_chainId, _name);
(bool found, address addr) = LibRollupAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,16 @@ pragma solidity 0.8.24;

import "../common/AddressManager.sol";
import "../common/LibStrings.sol";
import "./LibSharedAddressCache.sol";

/// @title MainnetSharedAddressManager
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost.
/// @notice See the documentation in {IAddressManager}.
/// @custom:security-contact security@taiko.xyz
contract MainnetSharedAddressManager is AddressManager {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
if (_chainId == 1) {
if (_name == LibStrings.B_TAIKO_TOKEN) {
return 0x10dea67478c5F8C5E2D90e5E9B26dBe60c54d800;
}
if (_name == LibStrings.B_QUOTA_MANAGER) {
return 0x91f67118DD47d502B1f0C354D0611997B022f29E;
}
if (_name == LibStrings.B_BRIDGE) {
return 0xd60247c6848B7Ca29eDdF63AA924E53dB6Ddd8EC;
}
if (_name == LibStrings.B_ERC20_VAULT) {
return 0x996282cA11E5DEb6B5D122CC3B9A1FcAAD4415Ab;
}
if (_name == LibStrings.B_ERC721_VAULT) {
return 0x0b470dd3A0e1C41228856Fb319649E7c08f419Aa;
}
if (_name == LibStrings.B_ERC1155_VAULT) {
return 0xaf145913EA4a56BE22E120ED9C24589659881702;
}
if (_name == LibStrings.B_SIGNAL_SERVICE) {
return 0x9e0a24964e5397B566c1ed39258e21aB5E35C77C;
}
} else if (_chainId == 167_000) {
if (_name == LibStrings.B_BRIDGE) {
return 0x1670000000000000000000000000000000000001;
}
if (_name == LibStrings.B_ERC20_VAULT) {
return 0x1670000000000000000000000000000000000002;
}
if (_name == LibStrings.B_ERC721_VAULT) {
return 0x1670000000000000000000000000000000000003;
}
if (_name == LibStrings.B_ERC1155_VAULT) {
return 0x1670000000000000000000000000000000000004;
}
if (_name == LibStrings.B_SIGNAL_SERVICE) {
return 0x1670000000000000000000000000000000000005;
}
}

return super._getAddress(_chainId, _name);
(bool found, address addr) = LibSharedAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
18 changes: 18 additions & 0 deletions packages/protocol/contracts/mainnet/MainnetSignalService.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "../signal/SignalService.sol";
import "./LibSharedAddressCache.sol";

/// @title MainnetSignalService
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost. In theory, the contract can also be deplyed on Taiko L2 but this is
/// not well testee nor necessary.
/// @notice See the documentation in {SignalService}.
/// @custom:security-contact security@taiko.xyz
contract MainnetSignalService is SignalService {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibSharedAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
6 changes: 4 additions & 2 deletions packages/protocol/contracts/mainnet/MainnetTaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
pragma solidity 0.8.24;

import "../L1/TaikoL1.sol";
import "./LibAddressCache.sol";
import "./LibRollupAddressCache.sol";

/// @title MainnetTaikoL1
/// @dev This contract shall be deployed to replace its parent contract on Ethereum for Taiko
/// mainnet to reduce gas cost.
/// @notice See the documentation in {TaikoL1}.
/// @custom:security-contact security@taiko.xyz
contract MainnetTaikoL1 is TaikoL1 {
function _getAddress(uint64 _chainId, bytes32 _name) internal view override returns (address) {
(bool found, address addr) = LibAddressCache.getAddress(_chainId, _name);
(bool found, address addr) = LibRollupAddressCache.getAddress(_chainId, _name);
return found ? addr : super._getAddress(_chainId, _name);
}
}
5 changes: 5 additions & 0 deletions packages/protocol/contracts/mainnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Optimized Smart Contracts for Taiko Mainnet

## Overview

This directory contains optimized versions of smart contracts for deployment on Ethereum for Taiko mainnet. While some of these contracts may be used on Taiko L2, deployment on Layer 2 is not recommended due to lack of testing.
Loading

0 comments on commit b70cc57

Please sign in to comment.