Skip to content
Verified contractExactMIT

DogsHashVault

Address0xa3400fb8de180e775155d4aa160d0f3c139451bf
Balance

0 ETH

 

Transactions

4

 

Compiler

solc 0.8.26

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,030,738
Creation transaction
0xa2022a0a05…0e15b39dbf

src/DogsHashVault.sol

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

/*
 DogsHash — the vault, locked (DRAFT, not audited).

 What the pool mines lands here, on Ethereum, and from here there are exactly two ways out, both written in code:

   reinvest   anyone may call it. It swaps at most half of what is held into USDT on Uniswap and sends the USDT to the
              rental account's deposit address, which is fixed. No more often than every six hours, never more than
              MAX_REINVEST per call, and the trade must return at least 97% of the Chainlink ETH/USD price, so a caller
              cannot drain the vault through a bad trade. That is the whole reinvest loop: the rented cards mine back
              into the pool, the pool pays out into this contract, and the next hour is bought from here.

   release    the operator may announce a payment: to whom, how much, with what call. Anyone may execute it, but only
              seven days after the announcement, and the announcement sits in public on chain the whole time. This is
              how the vault will one day move into the mint contract. It is also the only way money could ever leave
              for anywhere else, and it cannot be quiet and it cannot be fast.

 The operator cannot withdraw, pause, or upgrade, and cannot change the reinvest destination without the same seven
 days' notice. There is no other function that moves ETH.
*/
interface IERC20Min {
    function balanceOf(address who) external view returns (uint256);
    function transfer(address to, uint256 amount) external;     // USDT returns nothing, so nothing is decoded
}

interface ISwapRouter {
    struct ExactInputSingleParams {
        address tokenIn;
        address tokenOut;
        uint24 fee;
        address recipient;
        uint256 deadline;
        uint256 amountIn;
        uint256 amountOutMinimum;
        uint160 sqrtPriceLimitX96;
    }
    function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
}

interface IAggregatorV3 {
    function latestRoundData() external view returns (uint80, int256 answer, uint256, uint256 updatedAt, uint80);
    function decimals() external view returns (uint8);
}

contract DogsHashVault {
    // ------------------------------------------------------------------ the rule, fixed at deploy
    uint256 public constant SHARE_BPS = 5000;             // at most half of what is held, per reinvest
    uint256 public constant REINVEST_EVERY = 6 hours;
    uint256 public constant MIN_REINVEST = 0.004 ether;   // about ten dollars, the smallest deposit the rental account credits
    uint256 public constant MAX_REINVEST = 0.1 ether;     // per call: one call can never be large
    uint256 public constant PRICE_TOLERANCE_BPS = 300;    // the swap must return at least 97% of the oracle price
    uint256 public constant PRICE_MAX_AGE = 2 hours;      // an oracle answer older than this stops reinvest, not money
    uint256 public constant NOTICE = 7 days;
    uint24 public constant POOL_FEE = 500;                // Uniswap v3 WETH/USDT 0.05%

    ISwapRouter public immutable router;
    address public immutable weth;
    IERC20Min public immutable usdt;
    IAggregatorV3 public immutable feed;
    uint256 private immutable feedScale;                  // wei * price / feedScale = USDT units (6 decimals)

    address public operator;
    address public pendingOperator;
    address public clore;                                 // the rental account's deposit address
    address public pendingClore;
    uint64 public pendingCloreAt;

    uint64 public lastReinvest;
    uint256 public reinvestCalls;
    uint256 public reinvestedWei;                         // lifetime
    uint256 public reinvestedUsdt;                        // lifetime, 6 decimals

    struct Release { address to; uint256 value; bytes data; uint64 executeAfter; }
    Release public pendingRelease;

    event Received(address indexed from, uint256 amount, uint256 held);
    event Reinvested(address indexed caller, uint256 weiIn, uint256 usdtOut, uint256 oraclePrice, address to);
    event ReleaseProposed(address indexed to, uint256 value, bytes data, uint64 executeAfter);
    event ReleaseCancelled();
    event Released(address indexed to, uint256 value);
    event CloreProposed(address indexed next, uint64 executeAfter);
    event CloreSet(address indexed clore);
    event OperatorHandover(address indexed from, address indexed to);

    error NotOperator();
    error BadInput();
    error TooSoon();
    error TooMuch();
    error Nothing();
    error StalePrice();
    error CallFailed();

    modifier onlyOperator() {
        if (msg.sender != operator) revert NotOperator();
        _;
    }

    constructor(address router_, address weth_, address usdt_, address feed_, address clore_, address operator_) {
        if (router_ == address(0) || weth_ == address(0) || usdt_ == address(0) || feed_ == address(0)
            || clore_ == address(0) || operator_ == address(0)) revert BadInput();
        router = ISwapRouter(router_);
        weth = weth_;
        usdt = IERC20Min(usdt_);
        feed = IAggregatorV3(feed_);
        feedScale = 10 ** (18 + IAggregatorV3(feed_).decimals() - 6);
        clore = clore_;
        operator = operator_;
        emit CloreSet(clore_);
        emit OperatorHandover(address(0), operator_);
    }

    receive() external payable {
        emit Received(msg.sender, msg.value, address(this).balance);
    }

    // ------------------------------------------------------------------ door one: reinvest, anyone
    /// @notice What may go to the rental account right now, in wei: zero inside the cooldown.
    function reinvestable() public view returns (uint256) {
        if (block.timestamp < lastReinvest + REINVEST_EVERY) return 0;
        uint256 cap = address(this).balance * SHARE_BPS / 10000;
        if (cap > MAX_REINVEST) cap = MAX_REINVEST;
        return cap < MIN_REINVEST ? 0 : cap;
    }

    /// @notice The least USDT the vault accepts for `amountWei` at the oracle price, with the tolerance taken off.
    function minUsdtFor(uint256 amountWei) public view returns (uint256 minOut, uint256 price) {
        (, int256 answer,, uint256 updatedAt,) = feed.latestRoundData();
        if (answer <= 0 || block.timestamp - updatedAt > PRICE_MAX_AGE) revert StalePrice();
        price = uint256(answer);
        minOut = amountWei * price * (10000 - PRICE_TOLERANCE_BPS) / 10000 / feedScale;
    }

    /// @notice Swap `amountWei` of the vault into USDT and send it to the rental account. Anyone may call.
    function reinvest(uint256 amountWei) external returns (uint256 usdtOut) {
        if (amountWei < MIN_REINVEST) revert BadInput();
        if (amountWei > reinvestable()) revert TooMuch();
        (uint256 minOut, uint256 price) = minUsdtFor(amountWei);
        lastReinvest = uint64(block.timestamp);
        router.exactInputSingle{value: amountWei}(ISwapRouter.ExactInputSingleParams({
            tokenIn: weth, tokenOut: address(usdt), fee: POOL_FEE, recipient: address(this),
            deadline: block.timestamp, amountIn: amountWei, amountOutMinimum: minOut, sqrtPriceLimitX96: 0
        }));
        usdtOut = usdt.balanceOf(address(this));         // everything, including any USDT that landed here by mistake
        if (usdtOut < minOut) revert TooMuch();
        usdt.transfer(clore, usdtOut);
        reinvestCalls += 1;
        reinvestedWei += amountWei;
        reinvestedUsdt += usdtOut;
        emit Reinvested(msg.sender, amountWei, usdtOut, price, clore);
    }

    // ------------------------------------------------------------------ door two: a release, announced seven days ahead
    function proposeRelease(address to, uint256 value, bytes calldata data) external onlyOperator {
        if (to == address(0) || value == 0) revert BadInput();
        pendingRelease = Release(to, value, data, uint64(block.timestamp + NOTICE));
        emit ReleaseProposed(to, value, data, pendingRelease.executeAfter);
    }

    function cancelRelease() external onlyOperator {
        if (pendingRelease.executeAfter == 0) revert Nothing();
        delete pendingRelease;
        emit ReleaseCancelled();
    }

    /// @notice Anyone may execute a release once its notice has run out; the vault must still hold the amount.
    function executeRelease() external {
        Release memory r = pendingRelease;
        if (r.executeAfter == 0) revert Nothing();
        if (block.timestamp < r.executeAfter) revert TooSoon();
        delete pendingRelease;
        (bool ok,) = r.to.call{value: r.value}(r.data);
        if (!ok) revert CallFailed();
        emit Released(r.to, r.value);
    }

    // ------------------------------------------------------------------ the reinvest destination, same notice
    function proposeClore(address next) external onlyOperator {
        if (next == address(0)) revert BadInput();
        pendingClore = next;
        pendingCloreAt = uint64(block.timestamp + NOTICE);
        emit CloreProposed(next, pendingCloreAt);
    }

    function setClore() external {
        if (pendingClore == address(0)) revert Nothing();
        if (block.timestamp < pendingCloreAt) revert TooSoon();
        clore = pendingClore;
        pendingClore = address(0);
        pendingCloreAt = 0;
        emit CloreSet(clore);
    }

    // ------------------------------------------------------------------ operator, two-step
    function proposeOperator(address next) external onlyOperator {
        pendingOperator = next;
    }

    function acceptOperator() external {
        if (msg.sender != pendingOperator) revert NotOperator();
        emit OperatorHandover(operator, msg.sender);
        operator = msg.sender;
        pendingOperator = address(0);
    }

    // ------------------------------------------------------------------ views for the site
    function held() external view returns (uint256) {
        return address(this).balance;
    }

    function nextReinvestAt() external view returns (uint256) {
        return uint256(lastReinvest) + REINVEST_EVERY;
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.