Skip to content
Verified contractExactApache-2.0

EthConverter

Address0x42a8606a0e1e8ff081638cb341ffb02ed45e146c
Balance

0 ETH

 

Transactions

14

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 200 runs
EVM version
osaka
Via IR
yes
License
Apache-2.0
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,330,626
Creation transaction
0x2ca2cb0790…7610a2ccc7

98 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/EthConverter.sol

// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.28;

import {IRollup} from "@aztec/core/interfaces/IRollup.sol";
import {IOutbox} from "@aztec/core/interfaces/messagebridge/IOutbox.sol";
import {IRegistry} from "@aztec/governance/interfaces/IRegistry.sol";
import {Ownable} from "@oz/access/Ownable.sol";
import {SafeCast} from "@oz/utils/math/SafeCast.sol";
import {IEthConverter} from "./interfaces/IEthConverter.sol";
import {ITokenPortal} from "./interfaces/ITokenPortal.sol";
import {IWETH} from "./interfaces/IWETH.sol";
import {OutboxUtils, OutboxMessageMetadata} from "./libraries/OutboxUtils.sol";

/// @title EthConverter
/// @author Nyx.money
/// @notice Converts between ETH and WETH. Can issue deposit/withdraw of WETH attributed to the
/// user interacting with the converter contract to allow for proper tracking.
contract EthConverter is IEthConverter, Ownable {
    using OutboxUtils for IOutbox;
    using OutboxUtils for OutboxMessageMetadata;
    using SafeCast for uint256;

    /// @notice The wrapped ether contract
    IWETH public immutable WETH;

    /// @notice The Aztec registry this converter is bound to
    IRegistry public immutable REGISTRY;

    /// @notice The weth portal for deposits and withdrawals
    ITokenPortal public immutable PORTAL;

    /// @inheritdoc IEthConverter
    IRollup public rollup;

    /// @notice Aztec rollup version
    uint256 public rollupVersion;

    /// @notice Aztec outbox for L2 -> L1 messages
    IOutbox public outbox;

    /// @inheritdoc IEthConverter
    bytes32 public l2Bridge;

    /// @notice Construct the eth converter for weth portal
    /// @param registry - The Aztec registry this converter is bound to
    /// @param portal - The weth portal for deposit/withdraw
    /// @param owner - Converter owner with admin capabilities
    constructor(IRegistry registry, ITokenPortal portal, address owner) Ownable(owner) {
        REGISTRY = registry;
        WETH = IWETH(address(portal.underlying()));
        PORTAL = portal;

        // The portal only ever pulls what this contract wrapped or withdrew in the same call, and
        // nothing is held between calls, so a standing approval is okay.
        WETH.approve(address(portal), type(uint256).max);
    }

    /// @notice Accepts ether from the wrapped ether contract only
    receive() external payable {
        require(msg.sender == address(WETH), UnexpectedEth());
    }

    /// @notice Initialize the converter with an Aztec rollup
    /// @param rollupVersion_ - Aztec rollup version
    /// @param l2Bridge_ - The L2 bridge this converter is paired with
    function initialize(uint256 rollupVersion_, bytes32 l2Bridge_) external onlyOwner {
        rollupVersion = rollupVersion_;
        rollup = IRollup(address(REGISTRY.getRollup(rollupVersion_)));
        assert(rollup.getVersion() == rollupVersion_);
        outbox = rollup.getOutbox();
        l2Bridge = l2Bridge_;

        require(address(PORTAL.rollup()) == address(rollup), TokenPortalRollupMismatch(PORTAL, rollup));
    }

    /// @inheritdoc IEthConverter
    function depositEthToAztecPrivate(bytes32 claimSecretHash)
        external
        payable
        returns (bytes32 message, uint256 leafIndex)
    {
        require(msg.value > 0, ZeroAmount());

        WETH.deposit{value: msg.value}();

        // Attributed to msg.sender, so limits and tracking are for the depositor
        // rather than on this contract
        return PORTAL.depositToAztecPrivate(msg.value.toUint128(), claimSecretHash, msg.sender);
    }

    /// @inheritdoc IEthConverter
    function withdrawEth(
        address recipient,
        uint128 amount,
        bytes32 claimSecretHash,
        bool withCaller,
        address cancelCallerOnL1,
        uint64 unwrapValidDurationSeconds,
        uint64 withdrawValidDurationSeconds,
        OutboxMessageMetadata calldata withdrawWethMessage,
        OutboxMessageMetadata calldata unwrapMessage
    ) external {
        bytes32 unwrapMessageId = unwrapMessage.computeMessageId();
        if (unwrapMessage.isExpired(unwrapValidDurationSeconds, rollup)) {
            revert OrderExpired(unwrapMessageId);
        }

        _consumeOutboxMessage(
            recipient,
            amount,
            claimSecretHash,
            withCaller ? msg.sender : address(0),
            cancelCallerOnL1,
            unwrapValidDurationSeconds,
            unwrapMessage
        );

        // Attributed to recipient, so limits and tracking are for the depositor
        // rather than on this contract
        PORTAL.withdraw(
            address(this),
            amount,
            true,
            address(this),
            claimSecretHash,
            withdrawValidDurationSeconds,
            withdrawWethMessage,
            recipient
        );

        WETH.withdraw(amount);

        (bool sent,) = recipient.call{value: amount}("");
        require(sent, EthTransferFailed());

        emit EthWithdrawal(unwrapMessageId, recipient, amount);
    }

    // solhint-disable function-max-lines

    /// @inheritdoc IEthConverter
    function cancelWithdrawEth(
        address recipient,
        uint128 amount,
        bytes32 claimSecretHash,
        address callerOnL1,
        bool cancelWithCaller,
        uint64 unwrapValidDurationSeconds,
        uint64 withdrawValidDurationSeconds,
        OutboxMessageMetadata calldata withdrawWethMessage,
        OutboxMessageMetadata calldata unwrapMessage
    ) external returns (bytes32 message, uint256 leafIndex) {
        bytes32 unwrapMessageId = unwrapMessage.computeMessageId();
        if (!unwrapMessage.isExpired(unwrapValidDurationSeconds, rollup)) {
            revert OrderNotExpired(unwrapMessageId);
        }

        _consumeOutboxMessage(
            recipient,
            amount,
            claimSecretHash,
            callerOnL1,
            cancelWithCaller ? msg.sender : address(0),
            unwrapValidDurationSeconds,
            unwrapMessage
        );

        // Cancel if portal withdraw is expired otherwise, withdraw and send back to L2
        if (withdrawWethMessage.isExpired(withdrawValidDurationSeconds, rollup)) {
            (message, leafIndex) = PORTAL.cancelWithdraw(
                address(this),
                amount,
                address(this),
                true,
                claimSecretHash,
                withdrawValidDurationSeconds,
                withdrawWethMessage
            );
        } else {
            PORTAL.withdraw(
                address(this),
                amount,
                true,
                address(this),
                claimSecretHash,
                withdrawValidDurationSeconds,
                withdrawWethMessage,
                recipient
            );
            (message, leafIndex) = PORTAL.depositToAztecPrivate(amount, claimSecretHash, recipient);
        }

        emit EthWithdrawalCanceled(unwrapMessageId, recipient, amount, message, leafIndex);
    }

    // solhint-enable function-max-lines

    function _consumeOutboxMessage(
        address recipient,
        uint128 amount,
        bytes32 claimSecretHash,
        address callerOnL1,
        address cancelCallerOnL1,
        uint64 validDurationSeconds,
        OutboxMessageMetadata calldata messageMetadata
    ) internal {
        outbox.consume(
            // solhint-disable-next-line gas-small-strings
            OutboxUtils.buildMessage(
                l2Bridge,
                rollupVersion,
                address(this),
                "withdrawEth(address,uint128,bytes32,address,address,uint64)",
                abi.encode(recipient, amount, claimSecretHash, callerOnL1, cancelCallerOnL1, validDurationSeconds)
            ),
            messageMetadata
        );
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "osaka",
  "remappings": [
    "@aztec-blob-lib/=lib/aztec-packages/l1-contracts/src/core/libraries/rollup/",
    "@test/=lib/aztec-packages/l1-contracts/test/",
    "@openzeppelin/=lib/aztec-packages/l1-contracts/lib/openzeppelin-contracts/",
    "@oz/=lib/aztec-packages/l1-contracts/lib/openzeppelin-contracts/contracts/",
    "@aztec/=lib/aztec-packages/l1-contracts/src/",
    "@aztectest/=lib/aztec-packages/l1-contracts/test/",
    "@permit2/=lib/permit2/src/",
    "forge-std/=lib/aztec-packages/l1-contracts/lib/forge-std/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.