Skip to content
Verified contractExactUNLICENSED

DayAaveV3Engine

Address0x1e119a8e44a8e4eb1bb2589aebff33ee70bfc132
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.24

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
UNLICENSED
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,984,403
Creation transaction
0x25faed5c98…77890f687a
Main copy (most transactions)
0x8d27aacf…717a484a

src/DayAaveV3Engine.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.24;

import {IVaultEngine} from "./IVaultEngine.sol";

interface IERC20DayAaveEngine {
    function balanceOf(address account) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
}

interface IAaveV3PoolDayAaveEngine {
    function supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode) external;
    function withdraw(address asset, uint256 amount, address to) external returns (uint256);
}

interface IAaveScaledTokenDayAaveEngine {
    function scaledBalanceOf(address account) external view returns (uint256);
}

/// @title DayAaveV3Engine
/// @notice Per-chain VaultHub engine that deploys prefunded USDC into Aave V3.
/// @dev Only the local VaultHub calls this engine. The engine is prefunded with
///      USDC by its liquidity operator; principal moves separately from messaging,
///      so `onDeposit` supplies from its own balance and `onWithdraw` leaves the
///      withdrawn USDC on the engine for downstream CREDIT settlement.
contract DayAaveV3Engine is IVaultEngine {
    address public constant BASE_POOL = 0xA238Dd80C259a72e81d7e4664a9801593F98d1c5;
    address public constant BASE_USDC = 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913;
    address public constant BASE_A_USDC = 0x4e65fE4DbA92790696d040ac24Aa414708F5c0AB;
    address public constant ARBITRUM_POOL = 0x794a61358D6845594F94dc1DB02A252b5b4814aD;
    address public constant ARBITRUM_USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831;
    address public constant ARBITRUM_A_USDC = 0x724dc807b04555b71ed48a6896b6F41593b8C637;
    address public constant ETHEREUM_POOL = 0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2;
    address public constant ETHEREUM_USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
    address public constant ETHEREUM_A_USDC = 0x98C23E9d8f34FEFb1B7BD6a91B7FF122F4e16F5c;
    address public constant POLYGON_POOL = 0x794a61358D6845594F94dc1DB02A252b5b4814aD;
    address public constant POLYGON_USDC = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359;
    address public constant POLYGON_A_USDC = 0xA4D94019934D8333Ef880ABFFbF2FDd611C762BD;
    address public constant BSC_POOL = 0x6807dc923806fE8Fd134338EABCA509979a7e0cB;
    address public constant BSC_USDC = 0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d;
    address public constant BSC_A_USDC = 0x00901a076785e0906d1028c7d6372d247bec7d61;

    struct Deposit {
        address sender;
        uint256 scaledPosition;
    }

    struct Withdrawal {
        address sender;
        uint256 outputAmount;
    }

    address public immutable hub;
    address public immutable pool;
    address public immutable usdc;
    address public immutable aUsdc;

    mapping(bytes32 => Deposit) public deposits;
    mapping(bytes32 => Withdrawal) public withdrawals;

    bool private _locked;

    error ZeroAddress();
    error AddressHasNoCode();
    error UnsupportedChain();
    error OnlyHub();
    error ZeroAmount();
    error RequestAlreadySeen();
    error InsufficientLiquidity();
    error BalanceDeltaMismatch();
    error TransferFailed();
    error Reentrancy();

    event DepositExecuted(bytes32 indexed requestId, address indexed sender, uint256 amount, uint256 scaledPosition);
    event WithdrawalExecuted(bytes32 indexed requestId, address indexed sender, uint256 amount, uint256 outputAmount);

    modifier onlyHub() {
        if (msg.sender != hub) revert OnlyHub();
        _;
    }

    modifier nonReentrant() {
        if (_locked) revert Reentrancy();
        _locked = true;
        _;
        _locked = false;
    }

    /// @notice Deploys the engine against the Aave V3 route for this chain.
    /// @param hub_ Local VaultHub authorized to call the engine.
    constructor(address hub_) {
        if (hub_ == address(0)) revert ZeroAddress();
        if (hub_.code.length == 0) revert AddressHasNoCode();
        (address pool_, address usdc_, address aUsdc_) = _routeForChain();
        if (pool_.code.length == 0 || usdc_.code.length == 0 || aUsdc_.code.length == 0) {
            revert AddressHasNoCode();
        }
        hub = hub_;
        pool = pool_;
        usdc = usdc_;
        aUsdc = aUsdc_;
    }

    /// @notice Supplies prefunded USDC into Aave V3 for a cross-chain deposit request.
    /// @param requestId Replay-proof identifier derived on the source chain.
    /// @param sender Originator of the request on the source chain.
    /// @param amount Requested deposit amount in vault principal units.
    /// @param extraOptions Direction-prefixed option bytes forwarded from the REQUEST.
    /// @return scaledPosition Measured aUSDC scaled-balance delta opened by the supply.
    function onDeposit(bytes32 requestId, address sender, uint256 amount, bytes calldata extraOptions)
        external
        override
        onlyHub
        nonReentrant
        returns (uint256 scaledPosition)
    {
        extraOptions;
        if (amount == 0) revert ZeroAmount();
        if (deposits[requestId].sender != address(0) || deposits[requestId].scaledPosition != 0) {
            revert RequestAlreadySeen();
        }
        if (_balanceOf(usdc, address(this)) < amount) revert InsufficientLiquidity();
        uint256 scaledBefore = _scaledBalanceOf(aUsdc, address(this));
        _approveExact(pool, amount);
        IAaveV3PoolDayAaveEngine(pool).supply(usdc, amount, address(this), 0);
        _approveExact(pool, 0);
        scaledPosition = _scaledBalanceOf(aUsdc, address(this)) - scaledBefore;
        if (scaledPosition == 0) revert BalanceDeltaMismatch();
        deposits[requestId] = Deposit({sender: sender, scaledPosition: scaledPosition});
        emit DepositExecuted(requestId, sender, amount, scaledPosition);
    }

    /// @notice Withdraws USDC from Aave V3 for a cross-chain withdraw request.
    /// @dev Funds stay on the engine as USDC for downstream CREDIT settlement.
    /// @param requestId Replay-proof identifier derived on the source chain.
    /// @param sender Originator of the request on the source chain.
    /// @param amount Requested withdraw amount in vault principal units.
    /// @param extraOptions Direction-prefixed option bytes forwarded from the REQUEST.
    /// @return outputAmount USDC released to the engine by the withdraw.
    function onWithdraw(bytes32 requestId, address sender, uint256 amount, bytes calldata extraOptions)
        external
        override
        onlyHub
        nonReentrant
        returns (uint256 outputAmount)
    {
        extraOptions;
        if (amount == 0) revert ZeroAmount();
        if (withdrawals[requestId].sender != address(0) || withdrawals[requestId].outputAmount != 0) {
            revert RequestAlreadySeen();
        }
        uint256 assetBefore = _balanceOf(usdc, address(this));
        uint256 reported = IAaveV3PoolDayAaveEngine(pool).withdraw(usdc, amount, address(this));
        uint256 received = _balanceOf(usdc, address(this)) - assetBefore;
        if (reported == 0 || reported != received) revert BalanceDeltaMismatch();
        outputAmount = reported;
        withdrawals[requestId] = Withdrawal({sender: sender, outputAmount: outputAmount});
        emit WithdrawalExecuted(requestId, sender, amount, outputAmount);
    }

    /// @notice Returns the recorded deposit for a request id.
    /// @param requestId Replay-proof identifier derived on the source chain.
    /// @return sender Originator recorded at deposit time.
    /// @return scaledPosition Measured aUSDC scaled-balance delta opened by the supply.
    function depositOf(bytes32 requestId) external view returns (address sender, uint256 scaledPosition) {
        Deposit memory recorded = deposits[requestId];
        return (recorded.sender, recorded.scaledPosition);
    }

    /// @notice Returns the recorded withdrawal for a request id.
    /// @param requestId Replay-proof identifier derived on the source chain.
    /// @return sender Originator recorded at withdraw time.
    /// @return outputAmount USDC released to the engine by the withdraw.
    function withdrawalOf(bytes32 requestId) external view returns (address sender, uint256 outputAmount) {
        Withdrawal memory recorded = withdrawals[requestId];
        return (recorded.sender, recorded.outputAmount);
    }

    function _routeForChain() private view returns (address pool_, address usdc_, address aUsdc_) {
        if (block.chainid == 8453) return (BASE_POOL, BASE_USDC, BASE_A_USDC);
        if (block.chainid == 42161) return (ARBITRUM_POOL, ARBITRUM_USDC, ARBITRUM_A_USDC);
        if (block.chainid == 1) return (ETHEREUM_POOL, ETHEREUM_USDC, ETHEREUM_A_USDC);
        if (block.chainid == 137) return (POLYGON_POOL, POLYGON_USDC, POLYGON_A_USDC);
        if (block.chainid == 56) return (BSC_POOL, BSC_USDC, BSC_A_USDC);
        revert UnsupportedChain();
    }

    function _approveExact(address spender, uint256 amount) private {
        _callOptionalReturn(usdc, abi.encodeCall(IERC20DayAaveEngine.approve, (spender, 0)));
        if (amount != 0) _callOptionalReturn(usdc, abi.encodeCall(IERC20DayAaveEngine.approve, (spender, amount)));
    }

    function _balanceOf(address token, address account) private view returns (uint256) {
        (bool ok, bytes memory data) = token.staticcall(abi.encodeCall(IERC20DayAaveEngine.balanceOf, (account)));
        if (!ok || data.length < 32) revert TransferFailed();
        return abi.decode(data, (uint256));
    }

    function _scaledBalanceOf(address token, address account) private view returns (uint256) {
        (bool ok, bytes memory data) =
            token.staticcall(abi.encodeCall(IAaveScaledTokenDayAaveEngine.scaledBalanceOf, (account)));
        if (!ok || data.length < 32) revert TransferFailed();
        return abi.decode(data, (uint256));
    }

    function _callOptionalReturn(address token, bytes memory data) private {
        (bool ok, bytes memory result) = token.call(data);
        if (!ok || (result.length != 0 && (result.length != 32 || !abi.decode(result, (bool))))) {
            revert TransferFailed();
        }
    }
}

src/IVaultEngine.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.24;

/// @title IVaultEngine
/// @notice Per-chain opportunity logic executed by VaultHub on REQUEST arrival.
/// @dev Only the local VaultHub calls the engine. Implementations are per-chain
///      and carry the opportunity (Morpho, perps, lending); this signature is fixed.
interface IVaultEngine {
    /// @notice Runs the deposit leg for a cross-chain request.
    /// @param requestId Replay-proof identifier derived on the source chain.
    /// @param sender Originator of the request on the source chain.
    /// @param amount Requested deposit amount in vault principal units.
    /// @param extraOptions Direction-prefixed option bytes forwarded from the REQUEST.
    /// @return positionAmount Position opened on this chain.
    function onDeposit(bytes32 requestId, address sender, uint256 amount, bytes calldata extraOptions)
        external
        returns (uint256 positionAmount);

    /// @notice Runs the withdraw leg for a cross-chain request.
    /// @param requestId Replay-proof identifier derived on the source chain.
    /// @param sender Originator of the request on the source chain.
    /// @param amount Requested withdraw amount in vault principal units.
    /// @param extraOptions Direction-prefixed option bytes forwarded from the REQUEST.
    /// @return returnAmount Amount released on this chain.
    function onWithdraw(bytes32 requestId, address sender, uint256 amount, bytes calldata extraOptions)
        external
        returns (uint256 returnAmount);
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@layerzerolabs/lz-evm-protocol-v2/=lib/layerzero-v2/packages/layerzero-v2/evm/protocol/",
    "ds-test/=lib/layerzero-v2/lib/forge-std/lib/ds-test/src/",
    "forge-std/=lib/layerzero-v2/lib/forge-std/src/",
    "layerzero-v2/=lib/layerzero-v2/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.