Contract Source Code:
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IStateOracle} from "./interfaces/IStateOracle.sol";
import {ITokenPairs} from "./interfaces/ITokenPairs.sol";
import {IProofVerifier} from "./interfaces/IProofVerifier.sol";
import {IProvingContract} from "./interfaces/IProvingContract.sol";
import {IMintedBurnableERC20} from "./interfaces/IMintedBurnableERC20.sol";
/// The L2 part of the bridge. Allows to claim tokens deposited on the L1 side.
/// Allows to initiate withdrawal from L2 back to L1.
/// @custom:security-contact [email protected]
contract Bridge is IProvingContract, Ownable {
mapping (uint256 withdrawalId => bytes32 senderTokenAmount) public withdrawals; // slot index 1
mapping (uint256 depositId => bool isClaimed) public claims; // slot index 2
address public proofVerifier; // for verification of proofs of state on L1 chain
address public deposit; // Deposit contract on the L1 chain
address public immutable tokenPairs; // TokenPairs contract
address public immutable stateOracle; // StateOracle contract
uint256 private constant TIME_UNTIL_OFFLINE = 24 hours;
event Withdrawal(uint256 indexed id, address indexed owner, address token, uint256 amount);
event Claim(uint256 id, address indexed owner, address token, uint256 amount);
event ProofVerifierSet(address proofVerifier);
constructor(address _proofVerifier, address _tokenPairs, address _stateOracle, address _ownedBy) Ownable(_ownedBy) {
require(_proofVerifier != address(0), "ProofVerifier not set");
require(_tokenPairs != address(0), "TokenPairs not set");
require(_stateOracle != address(0), "StateOracle not set");
proofVerifier = _proofVerifier;
tokenPairs = _tokenPairs;
stateOracle = _stateOracle;
// deposit addr not known while deploying Bridge
}
/// Initialize deposit contract address after the deployment.
function setTokenDepositAddress(address _deposit) external onlyOwner {
require(_deposit != address(0), "TokenDeposit address not set");
require(deposit == address(0), "Address already set"); // callable only once, for init
deposit = _deposit;
}
/// Claim minted tokens representing tokens deposited on L1.
function claim(uint256 id, address token, uint256 amount, bytes calldata proof) external {
require(claims[id] == false, "Already claimed");
address mintedToken = ITokenPairs(tokenPairs).originalToMinted(token);
require(mintedToken != address(0), "No minted token for given token");
bytes32 depositHash = hash(msg.sender, token, amount);
IProofVerifier(proofVerifier).verifyProof(
deposit,
getDepositSlotIndex(id),
depositHash,
IStateOracle(stateOracle).lastState(),
proof
);
// the deposit exists on the L1
claims[id] = true; // write before other contract call (reentrancy!)
require(IMintedBurnableERC20(mintedToken).mint(msg.sender, amount), "Minting failed");
emit Claim(id, msg.sender, token, amount);
}
/// Burn minted tokens to withdraw tokens deposited on L1.
function withdraw(uint96 uid, address token, uint256 amount) external {
uint256 id = userOperationId(msg.sender, uid);
require(withdrawals[id] == 0, "Withdrawal id is already used");
require(amount > 0, "No tokens to transfer");
address mintedToken = ITokenPairs(tokenPairs).originalToMintedTerminable(token);
require(mintedToken != address(0), "No minted token for given token");
require(isOnline(), "Bridge is offline");
withdrawals[id] = hash(msg.sender, token, amount); // write before other contract call (reentrancy!)
IMintedBurnableERC20(mintedToken).burnFrom(msg.sender, amount);
emit Withdrawal(id, msg.sender, token, amount);
}
function isOnline() private view returns (bool) {
return IStateOracle(stateOracle).lastUpdateTime() >= block.timestamp - TIME_UNTIL_OFFLINE;
}
/// Calculate slotId for deposit in the Deposit L1 contract.
function getDepositSlotIndex(uint256 id) pure public returns (bytes32) {
return keccak256(abi.encode(id, uint8(7))); // deposits mapping is at slot index 7
}
/// Get deposit/withdrawal hash.
function hash(address sender, address token, uint256 amount) pure public returns (bytes32) {
return keccak256(abi.encode(sender, token, amount));
}
/// Calculate mapping key for user operation.
/// Combines calling user identity and user-chosen value into a single key.
function userOperationId(address sender, uint96 uid) pure public returns (uint256) {
return (uint256(uint160(sender)) << 96) + uint256(uid);
}
/// Set new proof verifier
function setProofVerifier(address _proofVerifier) external onlyOwner {
require(_proofVerifier != address(0), "ProofVerifier set to zero");
proofVerifier = _proofVerifier;
emit ProofVerifierSet(_proofVerifier);
}
function setExitAdministrator(address) external view onlyOwner {
revert("ExitAdministrator not relevant for Bridge contract");
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;
/// Minted ERC-20 tokens represents an Ethereum ERC-20 tokens on L2.
interface IMintedBurnableERC20 {
function mint(address account, uint256 amount) external returns (bool);
function burn(uint256 value) external;
function burnFrom(address account, uint256 value) external;
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;
/// Proof verifier allows to validate witness proof about a storage slot value on a different chain.
interface IProofVerifier {
/// Verify witness proof - proof about storage slot value on a different chain.
/// Reverts if the slot value does not match the expected value or if the proof is invalid.
function verifyProof(address contractAddress, bytes32 slotIndex, bytes32 expectedValue, bytes32 stateRoot, bytes calldata proof) external view;
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;
/// Proving contract represents a contract which use the proof verifier.
/// Used for updating the proof verifier address.
interface IProvingContract {
function proofVerifier() external view returns(address);
function setProofVerifier(address proofVerifier) external;
function setExitAdministrator(address exitAdministrator) external;
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;
/// State oracle provides the hash of a different chain state.
interface IStateOracle {
function lastState() external view returns (bytes32);
function lastBlockNum() external view returns (uint256);
function lastUpdateTime() external view returns (uint256);
function chainId() external view returns (uint256);
function update(uint256 blockNum, bytes32 stateRoot) external;
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;
// The token pairs registry maps Ethereum ERC-20 tokens to L2 tokens minted by the bridge.
interface ITokenPairs {
/// Map Ethereum token to L2 token - pairs can be only added into this mapping.
function originalToMinted(address) external view returns (address);
/// Map Ethereum token to L2 token - pairs can be removed from here to block new transfers.
function originalToMintedTerminable(address) external view returns (address);
/// Map L2 token to Ethereum token - pairs can be only added into this mapping.
function mintedToOriginal(address) external view returns (address);
/// Check if the account has given role - allows to use TokenPairs as an AccessManager for USDC burning ops.
function hasRole(bytes32 role, address account) external view returns (bool);
}