Skip to content
Verified contractExactMIT

WeekendVault

Address0x787e78f4ea64a340612e6fd83bb7694051f20e4b
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.34

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.34+commit.80d5c536
Language
solidity
Optimization
No
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,393,674
Creation transaction
0xfbafe7ef9b…00f1188ab9

WeekendVault.sol

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

/**
 * @title WeekendVault
 * @notice ETH, USDT and USDC vault on Ethereum Mainnet.
 *
 * Withdrawal window:
 * Saturday 08:30 UTC -> Sunday 08:30 UTC
 *
 * Wallet 1 and Wallet 2:
 * Shared weekly limit of 50% per asset.
 *
 * Wallet 3:
 * Independent weekly limit of 10% per asset.
 *
 * Weekly limits are based on the balance at
 * the first withdrawal of each asset that week.
 *
 * Deposits are open at all times.
 */
contract WeekendVault {

    // =====================================
    // TOKEN ADDRESSES - ETHEREUM MAINNET
    // =====================================

    address public constant USDT =
        0xdAC17F958D2ee523a2206206994597C13D831ec7;

    address public constant USDC =
        0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;

    // ETH is represented by address(0).
    address public constant ETH_ASSET = address(0);

    // =====================================
    // AUTHORIZED WALLETS
    // =====================================

    address public constant WALLET_1 =
        0x4Da05bee5B51fa9682AbcB3EE76EB5239A772000;

    address public constant WALLET_2 =
        0x4516BBFEc869b94Ddac838F39dB68B0eD6E35cA7;

    address public constant WALLET_3 =
        0x54407D628De35622ACfDEc9ea6231bf5f7e24Fb6;

    // =====================================
    // WEEKLY LIMITS
    // =====================================

    uint256 public constant PRIMARY_LIMIT = 50;
    uint256 public constant THIRD_LIMIT = 10;

    uint256 private constant PERCENT_BASE = 100;

    // =====================================
    // TIME SETTINGS
    // =====================================

    uint256 private constant TIME_OFFSET =
        3 hours + 30 minutes;

    uint256 private constant ONE_DAY = 1 days;

    uint256 private constant NOON = 12 hours;

    // First Saturday after Unix epoch,
    // at 08:30 UTC: January 3, 1970.
    uint256 private constant FIRST_WINDOW_START =
        2 days + 8 hours + 30 minutes;

    // =====================================
    // REENTRANCY PROTECTION
    // =====================================

    uint256 private locked = 1;

    // =====================================
    // WEEKLY ACCOUNTING
    // =====================================

    struct WeeklyState {
        uint256 weekId;
        uint256 startingBalance;
        uint256 primaryWithdrawn;
        uint256 thirdWithdrawn;
        bool initialized;
    }

    // ETH = address(0)
    // USDT = USDT token address
    // USDC = USDC token address
    mapping(address => WeeklyState) private weeklyStates;

    // =====================================
    // EVENTS
    // =====================================

    event ETHDeposited(
        address indexed sender,
        uint256 amount
    );

    event TokenDeposited(
        address indexed token,
        address indexed sender,
        uint256 amount
    );

    event AssetWithdrawn(
        address indexed asset,
        address indexed recipient,
        uint256 amount,
        uint256 weekId
    );

    event WeeklySnapshotCreated(
        address indexed asset,
        uint256 indexed weekId,
        uint256 startingBalance
    );

    // =====================================
    // MODIFIERS
    // =====================================

    modifier onlyAuthorized() {
        require(
            isAuthorized(msg.sender),
            "Unauthorized wallet"
        );
        _;
    }

    modifier onlyWithdrawalWindow() {
        require(
            isWithdrawalOpen(),
            "Withdrawals are closed"
        );
        _;
    }

    modifier nonReentrant() {
        require(
            locked == 1,
            "Reentrancy blocked"
        );

        locked = 2;
        _;
        locked = 1;
    }

    // =====================================
    // CONSTRUCTOR
    // =====================================

    constructor() {
        require(
            block.chainid == 1,
            "Ethereum Mainnet only"
        );
    }

    // =====================================
    // AUTHORIZATION
    // =====================================

    function isAuthorized(address account)
        public
        pure
        returns (bool)
    {
        return (
            account == WALLET_1 ||
            account == WALLET_2 ||
            account == WALLET_3
        );
    }

    function isPrimaryWallet(address account)
        public
        pure
        returns (bool)
    {
        return (
            account == WALLET_1 ||
            account == WALLET_2
        );
    }

    // =====================================
    // TIME MANAGEMENT
    // =====================================

    function isWithdrawalOpen()
        public
        view
        returns (bool)
    {
        uint256 localTime =
            block.timestamp + TIME_OFFSET;

        // Sunday = 0, Saturday = 6
        uint256 dayOfWeek =
            ((localTime / ONE_DAY) + 4) % 7;

        uint256 timeOfDay =
            localTime % ONE_DAY;

        if (
            dayOfWeek == 6 &&
            timeOfDay >= NOON
        ) {
            return true;
        }

        if (
            dayOfWeek == 0 &&
            timeOfDay < NOON
        ) {
            return true;
        }

        return false;
    }

    function getDayAndTime()
        external
        view
        returns (
            uint256 dayOfWeek,
            uint256 hour,
            uint256 minute
        )
    {
        uint256 localTime =
            block.timestamp + TIME_OFFSET;

        dayOfWeek =
            ((localTime / ONE_DAY) + 4) % 7;

        hour =
            (localTime % ONE_DAY) / 1 hours;

        minute =
            (localTime % 1 hours) / 1 minutes;
    }

    function getCurrentWeek()
        public
        view
        returns (uint256)
    {
        return (
            block.timestamp - FIRST_WINDOW_START
        ) / 1 weeks;
    }

    // =====================================
    // ETH DEPOSITS
    // =====================================

    receive() external payable {
        require(
            msg.value > 0,
            "Zero ETH deposit"
        );

        emit ETHDeposited(
            msg.sender,
            msg.value
        );
    }

    function depositETH()
        external
        payable
    {
        require(
            msg.value > 0,
            "Zero ETH deposit"
        );

        emit ETHDeposited(
            msg.sender,
            msg.value
        );
    }

    // =====================================
    // USDT AND USDC DEPOSITS
    // =====================================

    function depositUSDT(uint256 amount)
        external
        nonReentrant
    {
        _depositToken(USDT, amount);
    }

    function depositUSDC(uint256 amount)
        external
        nonReentrant
    {
        _depositToken(USDC, amount);
    }

    function _depositToken(
        address token,
        uint256 amount
    )
        private
    {
        require(
            amount > 0,
            "Zero token deposit"
        );

        uint256 balanceBefore =
            _tokenBalance(token);

        _safeTokenCall(
            token,
            abi.encodeWithSelector(
                bytes4(0x23b872dd),
                msg.sender,
                address(this),
                amount
            )
        );

        uint256 balanceAfter =
            _tokenBalance(token);

        require(
            balanceAfter >= balanceBefore &&
            balanceAfter - balanceBefore == amount,
            "Incorrect token received"
        );

        emit TokenDeposited(
            token,
            msg.sender,
            amount
        );
    }

    // =====================================
    // WEEKLY SNAPSHOT
    // =====================================

    function _syncWeeklyState(
        address asset,
        uint256 currentBalance
    )
        private
    {
        uint256 currentWeek = getCurrentWeek();

        WeeklyState storage state =
            weeklyStates[asset];

        if (
            !state.initialized ||
            state.weekId != currentWeek
        ) {
            state.weekId = currentWeek;
            state.startingBalance = currentBalance;
            state.primaryWithdrawn = 0;
            state.thirdWithdrawn = 0;
            state.initialized = true;

            emit WeeklySnapshotCreated(
                asset,
                currentWeek,
                currentBalance
            );
        }
    }

    // =====================================
    // WEEKLY LIMIT CALCULATION
    // =====================================

    function _remainingAllowance(
        address asset,
        address account,
        uint256 currentBalance
    )
        private
        view
        returns (uint256)
    {
        require(
            isAuthorized(account),
            "Unauthorized wallet"
        );

        WeeklyState storage state =
            weeklyStates[asset];

        uint256 baseline;
        uint256 primaryUsed;
        uint256 thirdUsed;

        if (
            state.initialized &&
            state.weekId == getCurrentWeek()
        ) {
            baseline = state.startingBalance;
            primaryUsed = state.primaryWithdrawn;
            thirdUsed = state.thirdWithdrawn;
        } else {
            baseline = currentBalance;
        }

        uint256 limit;
        uint256 used;

        if (isPrimaryWallet(account)) {
            limit =
                (baseline * PRIMARY_LIMIT) /
                PERCENT_BASE;

            used = primaryUsed;
        } else {
            limit =
                (baseline * THIRD_LIMIT) /
                PERCENT_BASE;

            used = thirdUsed;
        }

        if (used >= limit) {
            return 0;
        }

        uint256 remaining = limit - used;

        // Cannot withdraw more than the
        // actual balance currently available.
        if (remaining > currentBalance) {
            return currentBalance;
        }

        return remaining;
    }

    function _recordWithdrawal(
        address asset,
        address account,
        uint256 amount,
        uint256 currentBalance
    )
        private
    {
        require(
            amount > 0,
            "Invalid amount"
        );

        require(
            currentBalance >= amount,
            "Insufficient balance"
        );

        _syncWeeklyState(
            asset,
            currentBalance
        );

        uint256 remaining =
            _remainingAllowance(
                asset,
                account,
                currentBalance
            );

        require(
            amount <= remaining,
            "Weekly withdrawal limit exceeded"
        );

        WeeklyState storage state =
            weeklyStates[asset];

        if (isPrimaryWallet(account)) {
            state.primaryWithdrawn += amount;
        } else {
            state.thirdWithdrawn += amount;
        }
    }

    // =====================================
    // ETH WITHDRAWALS
    // =====================================

    function withdrawETH(uint256 amount)
        external
        onlyAuthorized
        onlyWithdrawalWindow
        nonReentrant
    {
        _withdrawETH(amount);
    }

    function withdrawMaxETH()
        external
        onlyAuthorized
        onlyWithdrawalWindow
        nonReentrant
    {
        uint256 amount =
            _remainingAllowance(
                ETH_ASSET,
                msg.sender,
                address(this).balance
            );

        _withdrawETH(amount);
    }

    function _withdrawETH(uint256 amount)
        private
    {
        uint256 currentBalance =
            address(this).balance;

        _recordWithdrawal(
            ETH_ASSET,
            msg.sender,
            amount,
            currentBalance
        );

        (bool success, ) =
            payable(msg.sender).call{
                value: amount
            }("");

        require(
            success,
            "ETH transfer failed"
        );

        emit AssetWithdrawn(
            ETH_ASSET,
            msg.sender,
            amount,
            getCurrentWeek()
        );
    }

    // =====================================
    // USDT WITHDRAWALS
    // =====================================

    function withdrawUSDT(uint256 amount)
        external
        onlyAuthorized
        onlyWithdrawalWindow
        nonReentrant
    {
        _withdrawToken(USDT, amount);
    }

    function withdrawMaxUSDT()
        external
        onlyAuthorized
        onlyWithdrawalWindow
        nonReentrant
    {
        uint256 amount =
            _remainingAllowance(
                USDT,
                msg.sender,
                usdtBalance()
            );

        _withdrawToken(USDT, amount);
    }

    // =====================================
    // USDC WITHDRAWALS
    // =====================================

    function withdrawUSDC(uint256 amount)
        external
        onlyAuthorized
        onlyWithdrawalWindow
        nonReentrant
    {
        _withdrawToken(USDC, amount);
    }

    function withdrawMaxUSDC()
        external
        onlyAuthorized
        onlyWithdrawalWindow
        nonReentrant
    {
        uint256 amount =
            _remainingAllowance(
                USDC,
                msg.sender,
                usdcBalance()
            );

        _withdrawToken(USDC, amount);
    }

    // =====================================
    // INTERNAL TOKEN WITHDRAWAL
    // =====================================

    function _withdrawToken(
        address token,
        uint256 amount
    )
        private
    {
        uint256 currentBalance =
            _tokenBalance(token);

        _recordWithdrawal(
            token,
            msg.sender,
            amount,
            currentBalance
        );

        _safeTokenCall(
            token,
            abi.encodeWithSelector(
                bytes4(0xa9059cbb),
                msg.sender,
                amount
            )
        );

        emit AssetWithdrawn(
            token,
            msg.sender,
            amount,
            getCurrentWeek()
        );
    }

    // =====================================
    // BALANCES
    // =====================================

    function ethBalance()
        external
        view
        returns (uint256)
    {
        return address(this).balance;
    }

    function usdtBalance()
        public
        view
        returns (uint256)
    {
        return _tokenBalance(USDT);
    }

    function usdcBalance()
        public
        view
        returns (uint256)
    {
        return _tokenBalance(USDC);
    }

    function _tokenBalance(address token)
        private
        view
        returns (uint256)
    {
        (bool success, bytes memory data) =
            token.staticcall(
                abi.encodeWithSelector(
                    bytes4(0x70a08231),
                    address(this)
                )
            );

        require(
            success && data.length >= 32,
            "Token balance query failed"
        );

        return abi.decode(
            data,
            (uint256)
        );
    }

    // =====================================
    // REMAINING WEEKLY ALLOWANCES
    // =====================================

    function remainingETH(address account)
        external
        view
        returns (uint256)
    {
        return _remainingAllowance(
            ETH_ASSET,
            account,
            address(this).balance
        );
    }

    function remainingUSDT(address account)
        external
        view
        returns (uint256)
    {
        return _remainingAllowance(
            USDT,
            account,
            usdtBalance()
        );
    }

    function remainingUSDC(address account)
        external
        view
        returns (uint256)
    {
        return _remainingAllowance(
            USDC,
            account,
            usdcBalance()
        );
    }

    // =====================================
    // WEEKLY STATUS
    // =====================================

    function getWeeklyStatus(address asset)
        external
        view
        returns (
            uint256 weekId,
            uint256 startingBalance,
            uint256 primaryLimit,
            uint256 primaryWithdrawn,
            uint256 thirdLimit,
            uint256 thirdWithdrawn
        )
    {
        require(
            asset == ETH_ASSET ||
            asset == USDT ||
            asset == USDC,
            "Unsupported asset"
        );

        WeeklyState storage state =
            weeklyStates[asset];

        weekId = getCurrentWeek();

        if (
            state.initialized &&
            state.weekId == weekId
        ) {
            startingBalance =
                state.startingBalance;

            primaryWithdrawn =
                state.primaryWithdrawn;

            thirdWithdrawn =
                state.thirdWithdrawn;
        } else {
            if (asset == ETH_ASSET) {
                startingBalance =
                    address(this).balance;
            } else {
                startingBalance =
                    _tokenBalance(asset);
            }

            primaryWithdrawn = 0;
            thirdWithdrawn = 0;
        }

        primaryLimit =
            (startingBalance * PRIMARY_LIMIT) /
            PERCENT_BASE;

        thirdLimit =
            (startingBalance * THIRD_LIMIT) /
            PERCENT_BASE;
    }

    // =====================================
    // SAFE ERC20 TRANSFER
    // =====================================

    function _safeTokenCall(
        address token,
        bytes memory data
    )
        private
    {
        require(
            token == USDT ||
            token == USDC,
            "Unsupported token"
        );

        (bool success, bytes memory result) =
            token.call(data);

        require(
            success,
            "Token call failed"
        );

        if (result.length > 0) {
            require(
                result.length == 32,
                "Invalid token response"
            );

            require(
                abi.decode(result, (bool)),
                "Token transfer failed"
            );
        }
    }

    // =====================================
    // FALLBACK
    // =====================================

    fallback() external payable {
        revert("Unsupported function");
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.