// SPDX-License-Identifier: MIT pragma solidity 0.8.28; import {AgentRegistry} from "../AgentRegistry.sol"; import {VenueLock} from "../VenueLock.sol"; import {HyperCoreAdapter} from "./HyperCoreAdapter.sol"; interface IERC20Approve { function approve(address spender, uint256 amount) external returns (bool); function balanceOf(address owner) external view returns (uint256); } /// Circle's CoreDepositWallet on HyperEVM (H7). `deposit` credits /// `msg.sender` on HyperCore; `depositFor`, which names a recipient, is never /// called and has no path here. interface ICoreDepositWallet { function deposit(uint256 amount, uint32 destinationDex) external; } /// @title HlDepositAdapter — `hl_deposit(amount)`: V's USDC into V's own HyperCore account. /// /// docs/HYPERLIQUID_VENUE_PLAN.md §7.2 and §7.6a item 6. As V: approve the /// CoreDepositWallet for `amount`, `deposit(amount, 0)` (dex 0 = perps), clear /// the approval. The intent carries `amount` and nothing else that acts. /// /// THE RECIPIENT IS NOT A FIELD. `deposit` credits `msg.sender`, which under /// `_exec` is V, so the HyperCore account credited is V's own. The CDW and /// USDC addresses are pinned in this bytecode per chain; the CDW is also /// curated by name (`VENUE_CDW`) and the curated answer must equal the pin. /// /// THE OWNER'S CAP IS ENFORCED ON THIS PATH, AND ONLY ON IT. VenueLock (T-773) /// holds A's cap on money at Hyperliquid. This adapter reads the live lock /// for (V, hyperliquid) — the owner it records must still be V's owner, or /// `lockOf` answers the empty lock — checks it binds an agent, reads /// /// atVenue = accountValue (0x80f, 6 dp, negative = 0) /// + spot USDC (0x801, 8 dp → 6 dp, rounded up) /// /// and then makes V call `venueLock.noteDeposit(HYPERLIQUID, amount, atVenue)` /// BEFORE any money moves. VenueLock refuses a closed lock (zero cap or no /// products), and refuses unless /// /// max(sentOf + amount, atVenue + inFlight + amount) ≤ lock.cap /// /// and records `sentOf += amount` (audit 2026-10-25, findings 3 and 4) and /// `inFlight += amount` (re-audit 2026-10-25, findings 5 and 6). /// /// - `sentOf` is the net money V sent under the current signed lock: only an /// `hl_withdraw` home lowers it, so a LOSS at the venue never frees room. /// Without it, G (or a bad image) could lose the cap, wait, and be sent /// the cap again: N rounds lost N × cap. /// - The credit is ASYNCHRONOUS (the CDW's own CoreWriter action 13, after /// this EVM block, H7, U5), so the precompiles do not see a deposit until /// later. `sentOf` counts it the moment it leaves V, for as long as it /// takes to land or if it never does: no window to expire. The live term /// counts it too, through VenueLock's `inFlight` (the deposits noted in /// its settle window), so several deposits in one block, or a `set` /// between two, are not each checked against the same old `atVenue`. /// - The record is VenueLock's, keyed by (V, venue), not this adapter's, so /// two deposit adapters live at once (a replacement beside the old one) /// share it and cannot each pass the cap. /// - A GAIN at the venue still counts, through `atVenue`. /// - A fresh `set` A signs starts `sentOf` at zero again; `tighten` does not. /// /// NO DEPENDENT ACTION FOLLOWS IN THIS TRANSACTION (§7.6a item 6). This /// adapter queues no CoreWriter action at all; the credit is Circle's. /// /// NOT AN EXIT, so it reads the lock and a missing or unreadable lock refuses. /// The exits (`hl_exit`, `hl_cancel`, `hl_withdraw`, `hl_agent_revoke`) live /// in other contracts and never read it. /// /// READINGS TAKEN WHERE THE PLAN LEFT A GAP (most restrictive; listed for the /// owner in the T-800 report): /// - The lock must bind an AGENT key (`KEY_AGENT`): a Hyperliquid lock that /// names an account wallet or an API key is not one this venue uses. /// - A negative account value counts as zero, never as room. /// - Spot USDC counts its whole `total`, held or not. /// - Only chain 999 (HyperEVM mainnet) is served. Testnet 998's USDC is not /// pinned here, so a testnet deployment is refused rather than guessed. contract HlDepositAdapter is HyperCoreAdapter { bytes32 public constant OP_DEPOSIT = "hl_deposit"; /// The venue name the guardian curates the CoreDepositWallet under. bytes32 public constant VENUE_CDW = "hl_core_deposit_wallet"; /// Destination dex for `deposit`: 0 = perps (H7). uint32 internal constant DEX_PERPS = 0; uint256 internal constant CHAIN_HYPEREVM = 999; bytes32 internal constant DEPOSIT_TYPEHASH = keccak256( "HlDeposit(address account,address owner,bytes32 taskId,uint256 deadline,uint256 amount)" ); struct Deposit { address account; /// WHOSE account. Checked against what the account itself says. address owner; bytes32 taskId; uint256 deadline; /// EVM USDC, 6 dp. uint256 amount; // No recipient, no dex, no venue address: all fixed here. } /// The owner's lock (T-773). Read by this adapter; V calls it for one /// function only, `noteDeposit`, with calldata written here. VenueLock public immutable venueLock; /// Everything the reconciler needs to check the credit next block (§8): /// the pre-state this deposit was capped against, and V's net sent to the /// venue after it (VenueLock's `sentOf`). event HlDeposited( address indexed account, bytes32 indexed taskId, uint256 amount, int64 accountValue, uint64 spotUsdcWei, uint256 sentAfter, uint128 cap ); error ZeroAmount(); error ChainNotServed(uint256 chainId); error NotCoreDepositWallet(address resolved); error BadVenueLock(address venueLock); error NoHyperliquidLock(address account); error LockIsNotAnAgentLock(address account, uint8 kind); error ApproveFailed(); /// What left V is not what was authorised, in either direction. error SentOtherThanAuthorised(uint256 authorised, uint256 sent); constructor(AgentRegistry registry_, VenueLock venueLock_) HyperCoreAdapter(registry_) { address l = address(venueLock_); // A real contract, and never one of the other addresses V is made to // call here. V does call VenueLock now, but only `noteDeposit`, with // calldata fixed below; a lock that WAS CoreWriter, the CDW or USDC // would turn that call, or theirs, into something else. if (l.code.length == 0 || l == CORE_WRITER || l == CDW_HYPEREVM || l == USDC_HYPEREVM) { revert BadVenueLock(l); } venueLock = venueLock_; } function _domainNameAndVersion() internal pure override returns (string memory, string memory) { return ("DeFiLoopsHlDepositAdapter", "1"); } function digest(Deposit calldata d) public view returns (bytes32) { return _hashTypedData( keccak256( abi.encode(DEPOSIT_TYPEHASH, d.account, d.owner, d.taskId, d.deadline, d.amount) ) ); } function deposit(Deposit calldata d, bytes calldata signature) external { _consume(d.account, d.owner, d.taskId, d.deadline, digest(d), signature); _requireOperation(d.account, OP_DEPOSIT); if (d.amount == 0) revert ZeroAmount(); if (block.chainid != CHAIN_HYPEREVM) revert ChainNotServed(block.chainid); address cdw = _venueOf(d.account, VENUE_CDW); if (cdw != CDW_HYPEREVM) revert NotCoreDepositWallet(cdw); // The owner's per-epoch spend cap on USDC (registry), before anything // moves. Uncapped unless the owner set one. _charge(d.account, USDC_HYPEREVM, d.amount); _requireUnderVenueCap(d.account, d.taskId, d.amount); uint256 before = IERC20Approve(USDC_HYPEREVM).balanceOf(d.account); bytes memory ok = _exec( d.account, USDC_HYPEREVM, 0, abi.encodeCall(IERC20Approve.approve, (cdw, d.amount)) ); if (ok.length != 0 && !abi.decode(ok, (bool))) revert ApproveFailed(); _exec(d.account, cdw, 0, abi.encodeCall(ICoreDepositWallet.deposit, (d.amount, DEX_PERPS))); _requireNoResidual(d.account, USDC_HYPEREVM, cdw); // The EVM half is synchronous and measured; the Core credit is // reconciled next block (§8). uint256 sent = before - IERC20Approve(USDC_HYPEREVM).balanceOf(d.account); if (sent != d.amount) revert SentOtherThanAuthorised(d.amount, sent); } /// The VenueLock cap check (see the header). Runs before any money moves: /// V records the amount with VenueLock, which refuses over the cap. function _requireUnderVenueCap(address account, bytes32 taskId, uint256 amount) internal { VenueLock.Lock memory lock = venueLock.lockOf(account, HYPERLIQUID); if (lock.owner == address(0)) revert NoHyperliquidLock(account); if (lock.binding.kind != LOCK_KEY_AGENT) { revert LockIsNotAnAgentLock(account, lock.binding.kind); } int64 value = _accountValue(account); uint64 spotWei = _spotUsdc(account).total; uint256 atVenue = (value > 0 ? uint256(uint64(value)) : 0) + (uint256(spotWei) + 99) / 100; bytes memory ret = _exec( account, address(venueLock), 0, abi.encodeCall(VenueLock.noteDeposit, (HYPERLIQUID, amount, atVenue)) ); uint256 sentAfter = abi.decode(ret, (uint256)); emit HlDeposited(account, taskId, amount, value, spotWei, sentAfter, lock.limits.cap); } }