Skip to content
Verified contractExactLGPL-3.0-only

MinimalProxy

Address0x61bd69b0e73c7c40bf2cbef98959c3800dd5414d
Balance

0 ETH

 

Transactions

5

 

Compiler

solc 0.8.30

 

Same code

3 more contracts

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 10,000 runs
EVM version
cancun
Via IR
yes
License
LGPL-3.0-only
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
23,596,380
Creation transaction
0xa0eb1a7abb…df569cbaa4

src/proxy/MinimalProxy.sol

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.30;

import { VmErrors } from '../VmErrors.sol';
import { Tstorish } from './TStorish.sol';

/// @title Minimal Proxy
/// @custom:version 1.0.0
/// @dev A minimal proxy that authenticates the sender and forwards all calls to the VM address
contract MinimalProxy is Tstorish {
    address public immutable owner;
    address public immutable vmAddress;
    // Storage slot used for the reentrancy guard, whether using TSTORE or SSTORE.
    uint256 private constant _REENTRANCY_GUARD_SLOT = uint256(keccak256('lifi.minimal-proxy.tstorish-slot'));

    modifier onlyOwner() {
        if (msg.sender != owner) revert VmErrors.Unauthorized();
        _;
    }

    modifier setReentrancyGuard() {
        _setReentrancyGuard();
        _;
    }

    /**
     * @notice Internal function to set the reentrancy guard using either TSTORE or SSTORE.
     * Called as part of functions that require reentrancy protection. Reverts if called
     * again before the reentrancy guard has been cleared.
     * @dev Note that the caller is set to the value; this enables external contracts to
     * ascertain the account originating the ongoing call while handling the call using
     * exttload. Also note that the value is actually set to a value of 1 when cleared;
     * this results in a significant efficiency improvement for environments that do not
     * yet support tstore, and additionally provides a mechanism to determine whether the
     * contract has been entered in a previous stage of the current transaction for
     * environments that do support it.
     */
    function _setReentrancyGuard() internal {
        // Retrieve the current reentrancy sentinel value.
        uint256 entered = _getTstorish(_REENTRANCY_GUARD_SLOT);
        assembly ("memory-safe") {
            // Consider any value over 1 as indicating that reentrancy is disallowed.
            if gt(entered, 1) {
                // revert ReentrantCall(address existingCaller)
                mstore(0, 0xf57c448b)
                mstore(0x20, entered)
                revert(0x1c, 0x24)
            }

            // Use the address of the caller for the updated sentinel value.
            entered := caller()
        }

        // Store the updated sentinel value.
        _setTstorish(_REENTRANCY_GUARD_SLOT, entered);
    }

    /**
     * @notice Internal function to clear the reentrancy guard using either TSTORE or SSTORE.
     * Called as part of functions that require reentrancy protection.
     */
    function _clearReentrancyGuard() internal {
        // Store a value of 1 for the updated sentinel value. This indicates that the
        // contract can be entered again while keeping the sentinel storage slot dirty.
        _setTstorish(_REENTRANCY_GUARD_SLOT, 1);
    }

    constructor(address _owner, address _vmAddress) Tstorish() {
        owner = _owner;
        vmAddress = _vmAddress;
    }

    function _forward() internal setReentrancyGuard returns (bytes memory out) {
        (bool ok, bytes memory ret) = vmAddress.delegatecall(msg.data);
        if (!ok) {
            assembly {
                revert(add(ret, 0x20), mload(ret))
            }
        }
        _clearReentrancyGuard();
        return ret;
    }

    fallback() external payable onlyOwner {
        bytes memory out = _forward();
        assembly ("memory-safe") {
            return(add(out, 0x20), mload(out))
        }
    }

    receive() external payable { }
}

src/VmErrors.sol

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.30;

/// @title VmErrors
/// @custom:version 1.0.0
/// @notice Shared error definitions for the Virtual Machine
library VmErrors {
    /// @notice Thrown when a disallowed operation is executed
    error Disallowed();

    /// @notice Thrown when a surgery operation exceeds the bounds of the source data
    error OutOfBounds();

    /// @notice Thrown when replacement data exceeds surgery length
    error ReplacementTooLarge();

    /// @notice Thrown when too many surgeries are requested (maximum 6)
    error TooManySurgeries();

    /// @notice Thrown when an invalid log variant is provided
    error InvalidLogVariant();

    /// @notice Thrown when an invalid call type is provided
    error InvalidCallType();

    /// @notice Thrown when a blueprint exceeds the maximum allowed size
    error BlueprintTooLarge();

    /// @notice Thrown when source registers exceed 208 bits for log packing
    error SourceRegistersExceed208Bits();

    /// @notice Thrown when no tokens are approved for deposit
    error NoApprovedTokens();

    /// @notice Thrown when token transfer fails during deposit
    error TokenTransferFailed();

    /// @notice Thrown when an invalid opcode is encountered
    error InvalidOpcode(uint8 opcode);

    /// @notice Thrown when bytes are attempted to be read as address
    error InvalidAddressBytes();

    /// @notice Thrown when calldata is too short (must be at least 36 bytes: 32 for length + 4 for selector)
    error InvalidCallDataLength();

    /// @notice Thrown when value data is too short (must be at least 32 bytes)
    error InvalidValueLength();

    /// @notice Thrown when too many operations are requested (maximum 32)
    error TooManyOperations();

    /// @notice Thrown when an invalid user address (zero address) is provided
    error InvalidUserAddress();

    /// @notice Thrown when a proxy has already been deployed for the user
    error ProxyAlreadyDeployed();

    /// @notice Thrown when unauthorized access is attempted
    error Unauthorized();

    /// @notice Thrown when attempting to call a non-contract address
    error CallToNonContract();

    /// @notice Thrown when register data has invalid length
    error InvalidRegisterLength();

    /// @notice Thrown when destination register count is outwith allowed range
    error DestinationCountOutOfBounds(uint8 count);

    /// @notice Thrown when destination register count does not match the specified count
    error DestinationCountMismatch(uint8 expected, uint256 found);

    /// @notice Thrown when an invalid address is provided
    error InvalidAddress();

    /// @notice Thrown when an invalid token address is provided
    error InvalidTokenAddress();

    /// @notice Thrown when GetBalance call fails (in DepositApproved)
    error GetBalanceFailed();
}

src/proxy/EfficiencyLib.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

import { Scope, ResetPeriod } from './Types.sol';

/// @title EfficiencyLib
/// @custom:version 1.0.0
/// @notice Library contract implementing logic for efficient value comparisons,
/// conversions, typecasting, and sanitization. Also provides functions to prevent
/// the function specializer from being triggered when using static arguments.
library EfficiencyLib {
    /**
     * @notice Internal pure function that checks if an address has its lower 160
     * bits set to zero.
     * @param account The address to check.
     * @return isNull Whether the address is null.
     */
    function isNullAddress(address account) internal pure returns (bool isNull) {
        assembly ("memory-safe") {
            isNull := iszero(shl(96, account))
        }
    }

    /**
     * @notice Internal pure function that converts a boolean to a uint256.
     * @param a  The boolean to convert.
     * @return b The resulting uint256.
     */
    function asUint256(bool a) internal pure returns (uint256 b) {
        assembly ("memory-safe") {
            b := a
        }
    }

    /**
     * @notice Internal pure function that converts a uint96 to a uint256.
     * @param a  The uint96 to convert.
     * @return b The resulting uint256.
     */
    function asUint256(uint96 a) internal pure returns (uint256 b) {
        assembly ("memory-safe") {
            b := a
        }
    }

    /**
     * @notice Internal pure function that converts a bytes12 to a uint256.
     * @param a  The bytes12 to convert.
     * @return b The resulting uint256.
     */
    function asUint256(bytes12 a) internal pure returns (uint256 b) {
        assembly ("memory-safe") {
            b := a
        }
    }

    /**
     * @notice Internal pure function that converts a Scope enum to a uint256.
     * @param a  The Scope enum to convert.
     * @return b The resulting uint256.
     */
    function asUint256(Scope a) internal pure returns (uint256 b) {
        assembly ("memory-safe") {
            b := a
        }
    }

    /**
     * @notice Internal pure function that converts an address to a uint256.
     * @param a  The address to convert.
     * @return b The resulting uint256.
     */
    function asUint256(address a) internal pure returns (uint256 b) {
        assembly ("memory-safe") {
            b := a
        }
    }

    /**
     * @notice Internal pure function that converts a ResetPeriod enum to a uint256.
     * @param a  The ResetPeriod enum to convert.
     * @return b The resulting uint256.
     */
    function asUint256(ResetPeriod a) internal pure returns (uint256 b) {
        assembly ("memory-safe") {
            b := a
        }
    }

    /**
     * @notice Internal pure function that prevents the function specializer from
     * optimizing uint256 arguments. XORs the value with calldatasize(), which
     * will always be non-zero in a real call.
     * @param a  The uint256 value to make stubborn.
     * @return b The original value, preventing specialization.
     */
    function asStubborn(uint256 a) internal pure returns (uint256 b) {
        assembly ("memory-safe") {
            b := or(iszero(calldatasize()), a)
        }
    }

    /**
     * @notice Internal pure function that prevents the function specializer from
     * inlining functions that take fixed boolean arguments. Since calldatasize()
     * will always be non-zero when making a standard function call, an OR
     * against iszero(calldatasize()) will always result in the original value.
     * @param a  The boolean value to make stubborn.
     * @return b The original value, preventing specialization.
     */
    function asStubborn(bool a) internal pure returns (bool b) {
        assembly ("memory-safe") {
            b := or(iszero(calldatasize()), a)
        }
    }

    /**
     * @notice Internal pure function that performs a bitwise OR on two booleans.
     * Avoids Solidity's conditional evaluation of logical OR. Only safe when
     * inputs are known to be exactly 0 or 1 with no dirty bits.
     * @param a  The first boolean value.
     * @param b  The second boolean value.
     * @return c The result of the bitwise OR.
     */
    function or(bool a, bool b) internal pure returns (bool c) {
        assembly ("memory-safe") {
            c := or(a, b)
        }
    }
}

src/proxy/TStorish.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

import { EfficiencyLib } from './EfficiencyLib.sol';

/// @title Tstorish
/// @custom:version 1.0.0
/// @notice Inheritable contract implementing logic for determining whether or not
/// transient storage is available in the current EVM environment and utilizing it
/// if it has been confirmed to be available (either at time of deployment or by
/// explicitly activating it at a later point).
contract Tstorish {
    using EfficiencyLib for bool;
    using EfficiencyLib for address;

    /*
     * ------------------------------------------------------------------------+
     * Opcode      | Mnemonic         | Stack              | Memory            |
     * ------------------------------------------------------------------------|
     * 60 0x02     | PUSH1 0x02       | 0x02               |                   |
     * 60 0x1e     | PUSH1 0x1e       | 0x1e 0x02          |                   |
     * 61 0x3d5c   | PUSH2 0x3d5c     | 0x3d5c 0x1e 0x02   |                   |
     * 3d          | RETURNDATASIZE   | 0 0x3d5c 0x1e 0x02 |                   |
     *                                                                         |
     * :: store deployed bytecode in memory: (3d) RETURNDATASIZE (5c) TLOAD :: |
     * 52          | MSTORE           | 0x1e 0x02          | [0..0x20): 0x3d5c |
     * f3          | RETURN           |                    | [0..0x20): 0x3d5c |
     * ------------------------------------------------------------------------+
     */
    uint80 private constant _TLOAD_TEST_PAYLOAD = 0x6002_601e_613d5c_3d_52_f3;
    uint8 private constant _TLOAD_TEST_PAYLOAD_LENGTH = 0x0a;
    uint8 private constant _TLOAD_TEST_PAYLOAD_OFFSET = 0x16;

    // Declare an immutable variable to store the tstore test contract address.
    address private immutable _tloadTestContract;

    // Declare an immutable variable to store the initial TSTORE support status.
    bool private immutable _tstoreInitialSupport;

    // Declare a storage variable indicating when TSTORE support will be
    // activated assuming it was not already active at initial deployment.
    uint256 private _tstoreSupportActiveAt;

    // Declare a few custom errors.
    error TStoreAlreadyActivated();
    error TStoreNotSupported();
    error TloadTestContractDeploymentFailed();

    /**
     * @notice Determine TSTORE availability during deployment. This involves
     * attempting to deploy a contract that utilizes TLOAD as part of the contract
     * construction bytecode, and configuring initial support for using TSTORE in
     * place of SSTORE based on the result.
     */
    constructor() {
        // Deploy the contract testing TLOAD support and store the address.
        address tloadTestContract = _deployTloadTest();

        // Ensure the deployment was successful.
        if (tloadTestContract.isNullAddress()) {
            revert TloadTestContractDeploymentFailed();
        }

        // Determine if TSTORE is supported.
        _tstoreInitialSupport = _testTload(tloadTestContract);

        // Set the address of the deployed TLOAD test contract as an immutable.
        _tloadTestContract = tloadTestContract;
    }

    /**
     * @notice External function to activate TSTORE usage. Does not need to be called
     * if TSTORE is supported from deployment, and only needs to be called once.
     * Reverts if TSTORE has already been activated or if the opcode is not available.
     */
    function __activateTstore() external {
        // Determine if TSTORE can potentially be activated.
        if (_tstoreInitialSupport.or(_tstoreSupportActiveAt != 0)) {
            assembly ("memory-safe") {
                mstore(0, 0xf45b98b0) // `TStoreAlreadyActivated()`.
                revert(0x1c, 0x04)
            }
        }

        // Determine if TSTORE can be activated and revert if not.
        if (!_testTload(_tloadTestContract)) {
            assembly ("memory-safe") {
                mstore(0, 0x70a4078f) // `TStoreNotSupported()`.
                revert(0x1c, 0x04)
            }
        }

        // Mark TSTORE as activated as of the next block.
        unchecked {
            _tstoreSupportActiveAt = block.number + 1;
        }
    }

    /**
     * @notice Internal function to write a value to a given slot using either transient
     * storage or standard storage depending on the activation status.
     *
     * @param slot  The slot to write the value to.
     * @param value The value to write to the given slot.
     */
    function _setTstorish(uint256 slot, uint256 value) internal {
        // Retrieve initial support status from the immutable variable and place it on the stack.
        bool tstoreInitialSupport = _tstoreInitialSupport;

        assembly ("memory-safe") {
            // Use a faux loop to support breaking early.
            for { } 1 { } {
                if iszero(tstoreInitialSupport) {
                    // Load the storage slot tracking the tstore activation block number.
                    let tstoreSupportActiveAt := sload(_tstoreSupportActiveAt.slot)

                    // Use sstore if no value is set or if value is greater than current block number.
                    let useSstore := or(iszero(tstoreSupportActiveAt), gt(tstoreSupportActiveAt, number()))

                    if useSstore {
                        sstore(slot, value)
                        break
                    }
                }

                tstore(slot, value)
                break
            }
        }
    }

    /**
     * @notice Internal view function to read the value of a given slot using either transient
     * storage or standard storage depending on the activation status.
     *
     * @param slot   The slot to read the value from.
     * @return value The value at the given slot.
     */
    function _getTstorish(uint256 slot) internal view returns (uint256 value) {
        // Retrieve initial support status from the immutable variable and place it on the stack.
        bool tstoreInitialSupport = _tstoreInitialSupport;

        assembly ("memory-safe") {
            // Use a faux loop to support breaking early.
            for { } 1 { } {
                if iszero(tstoreInitialSupport) {
                    // Load the storage slot tracking the tstore activation block number.
                    let tstoreSupportActiveAt := sload(_tstoreSupportActiveAt.slot)

                    // Use sstore if no value is set or if value is greater than current block number.
                    let useSstore := or(iszero(tstoreSupportActiveAt), gt(tstoreSupportActiveAt, number()))

                    if useSstore {
                        value := sload(slot)
                        break
                    }
                }

                value := tload(slot)
                break
            }
        }
    }

    /**
     * @notice Internal view function to determine if TSTORE/TLOAD are supported by
     * the current EVM implementation by attempting to call the test contract, which
     * utilizes TLOAD as part of its fallback logic. The function is marked as
     * *internal virtual* to facilitate overriding as part of tests.
     */
    function _testTload(address tloadTestContract) internal view virtual returns (bool ok) {
        // Call the test contract, which will perform a TLOAD test. If the call
        // does not revert, then TLOAD/TSTORE is supported. Do not forward all
        // available gas, as all forwarded gas will be consumed on revert.
        // Note that this assumes that the contract was successfully deployed.
        assembly ("memory-safe") {
            ok := staticcall(div(gas(), 10), tloadTestContract, 0, 0, 0, 0)
        }
    }

    /**
     * @notice Private function to deploy a test contract that utilizes TLOAD as
     * part of its fallback logic.
     */
    function _deployTloadTest() private returns (address contractAddress) {
        // Utilize assembly to deploy a contract testing TLOAD support.
        assembly ("memory-safe") {
            // Write the contract deployment code payload to scratch space.
            mstore(0, _TLOAD_TEST_PAYLOAD)

            // Deploy the contract.
            contractAddress := create(0, _TLOAD_TEST_PAYLOAD_OFFSET, _TLOAD_TEST_PAYLOAD_LENGTH)
        }
    }
}

src/proxy/Types.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

enum Scope {
    Multichain,
    ChainSpecific
}

enum ResetPeriod {
    OneSecond,
    FifteenSeconds,
    OneMinute,
    TenMinutes,
    OneHourAndFiveMinutes,
    OneDay,
    SevenDaysAndOneHour,
    ThirtyDays
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "libraries": {},
  "optimizer": {
    "runs": 10000,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@chimera/=lib/setup-helpers/lib/chimera/src/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@recon/=lib/setup-helpers/src/",
    "chimera/=lib/chimera/src/",
    "create3-factory/=lib/create3-factory/",
    "createx-forge/=lib/createx-forge/",
    "ds-test/=lib/chimera/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "setup-helpers/=lib/setup-helpers/src/",
    "solady-test/=lib/solady/test/",
    "solady/=lib/solady/src/",
    "solmate/=lib/create3-factory/lib/solmate/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.