// SPDX-License-Identifier: MIT pragma solidity 0.8.28; import {BaseAdapter, IERC20Balance} from "../BaseAdapter.sol"; import {AgentRegistry} from "../AgentRegistry.sol"; /// WETH9's two entry points, and nothing else. Base's predeploy (0x4200…0006) /// and Ethereum's 0xC02a… are WETH9; Arbitrum's 0x82aF… is aeWETH, which keeps /// the same two selectors. interface IWETH { function deposit() external payable; function withdraw(uint256 wad) external; } /// @title WethWrapAdapter — the account wraps its own ETH into WETH, and back. /// /// `weth.wrap` and `weth.unwrap` (adapter `weth_wrap`, operations `wrap` and /// `unwrap`; docs/design/WRAP_UNWRAP_PLAN.md, owner decisions of 2026-10-23). /// /// THE ACCOUNT MAKES THE CALL. `wrap` is the account calling /// `WETH.deposit{value: amount}()`; `unwrap` is the account calling /// `WETH.withdraw(amount)`. Both go through `_exec`, so no value ever passes /// through this contract and there is no balance here to drain. /// /// WHAT THE INTENT CANNOT NAME. A recipient (WETH mints to, and pays, its /// caller, which is the account), a target (the curated `weth` venue), calldata /// (one of two fixed encodings) or an asset (fixed by the operation). The only /// numbers it carries are the amount and, for an unwrap, where the amount comes /// from. /// /// WHERE WETH COMES FROM. Not from this contract: the address is the guardian's /// `weth` venue for this adapter, and it must ALSO be a curated asset. Two /// guardian lists have to agree before any value moves. Nothing chain-specific /// is compiled in, so the initcode, and with it the CREATE2 address, is the same /// on Base, Arbitrum and Ethereum. /// /// THE SCORECARD (docs: the adapter design scorecard, seven points). /// 1. `_consume` is the first line of the one state-changing entry. /// 2. The owner is read from the account; the signed `owner` must match it. /// 3. No recipient: WETH credits and pays `msg.sender`, the account. /// 4. The target is the curated venue; the calldata is fixed per operation. /// 5. Venue by name, the cap charged on the input, the operation allow-listed. /// 6. No allowance is ever made: `deposit` needs none and `withdraw` burns /// the caller's own balance. /// 7. BOTH balances are measured on both sides and must move by exactly the /// amount. Kernel may report a failed call without reverting. /// /// THE INPUT IS CHARGED, as a swap charges its input: a wrap charges the /// 0xEeee… sentinel (the bucket `owner_transfer`'s native send already uses), /// an unwrap charges WETH. /// /// CHAINING. A wrap publishes the WETH it delivered, so a later step may spend /// it. An unwrap may spend what an earlier step delivered in WETH, and publishes /// nothing: native value is not on the bus. For the same reason a wrap cannot /// take its amount from an earlier step. /// /// THE STIPEND. WETH9's `withdraw` pays with `transfer`, which forwards 2,300 /// gas into the account. The account is a solady ERC-1967 proxy whose Kernel /// `receive()` emits `Received`; the proxy's implementation slot and address /// are warm by then, because the account is mid-call in this same transaction. /// `test/WethWrapAdapter.t.sol` proves it against a real Kernel proxy and a WETH9 /// that pays the same way, and `test/WethWrapAdapterFork.t.sol` against each /// chain's real WETH. contract WethWrapAdapter is BaseAdapter { bytes32 public constant OP_WRAP = "wrap"; bytes32 public constant OP_UNWRAP = "unwrap"; /// The venue name. `venueTarget("weth")` is that chain's WETH. bytes32 public constant VENUE_WETH = "weth"; /// THE CHAIN'S OWN CURRENCY as a cap key, the same sentinel /// `OwnerTransferAdapter.NATIVE` is. Not address(0): PolicyGate refuses a /// zero token for an allowance bucket. address public constant NATIVE = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; bytes32 internal constant WRAP_TYPEHASH = keccak256( "Wrap(address account,address owner,bytes32 taskId,uint256 deadline,bool unwrap,uint256 amount,address amountFrom,bytes32 amountFromTask)" ); struct Wrap { address account; /// WHOSE account. Checked against what the account itself says. address owner; bytes32 taskId; uint256 deadline; /// false: ETH → WETH (`wrap`). true: WETH → ETH (`unwrap`). bool unwrap; uint256 amount; /// WHERE THE AMOUNT COMES FROM. Zero means the amount above; non-zero /// names the producing adapter and step (unwrap only). See BaseAdapter. address amountFrom; bytes32 amountFromTask; } event Wrapped(address indexed account, address indexed weth, uint256 amount); event Unwrapped(address indexed account, address indexed weth, uint256 amount); /// The account holds less ETH than the wrap. error InsufficientNative(uint256 held, uint256 amount); /// The account holds less WETH than the unwrap. error InsufficientWrapped(uint256 held, uint256 amount); /// A step of zero. It would move nothing and report success. error NothingToWrap(); /// A wrap sized from an earlier step: nothing publishes native value. error NativeIsNotOnTheBus(); /// One of the two balances did not move by exactly the amount. error WrapNotExact( uint256 amount, uint256 ethBefore, uint256 ethAfter, uint256 wethBefore, uint256 wethAfter ); constructor(AgentRegistry registry_) BaseAdapter(registry_) {} // `_requiresArming` stays BaseAdapter's false, as on AcrossAdapter and // CctpAdapter: neither operation moves value out of the account, so // neither waits on the owner's switch. The install (v1) or the proved pair // (v2) grants both. function _domainNameAndVersion() internal pure override returns (string memory, string memory) { return ("DeFiLoopsWethWrapAdapter", "1"); } function digest(Wrap calldata w) public view returns (bytes32) { return _hashTypedData( keccak256( abi.encode( WRAP_TYPEHASH, w.account, w.owner, w.taskId, w.deadline, w.unwrap, w.amount, w.amountFrom, w.amountFromTask ) ) ); } /// Wrap or unwrap `amount` in the account. Returns the amount delivered: /// WETH for a wrap, ETH for an unwrap, always exactly `amount`. function wrap(Wrap calldata w, bytes calldata signature) external returns (uint256) { _consume(w.account, w.owner, w.taskId, w.deadline, digest(w), signature); _requireOperation(w.account, w.unwrap ? OP_UNWRAP : OP_WRAP); address weth = _venueOf(w.account, VENUE_WETH); _requireCuratedAsset(weth); uint256 amount; if (w.unwrap) { amount = _amountToSpend(w.account, w.amount, w.amountFrom, w.amountFromTask, weth); } else { if (w.amountFrom != address(0)) revert NativeIsNotOnTheBus(); amount = w.amount; } if (amount == 0) revert NothingToWrap(); _charge(w.account, w.unwrap ? weth : NATIVE, amount); uint256 ethBefore = w.account.balance; uint256 wethBefore = IERC20Balance(weth).balanceOf(w.account); if (w.unwrap) { if (wethBefore < amount) revert InsufficientWrapped(wethBefore, amount); _exec(w.account, weth, 0, abi.encodeCall(IWETH.withdraw, (amount))); } else { if (ethBefore < amount) revert InsufficientNative(ethBefore, amount); _exec(w.account, weth, amount, abi.encodeCall(IWETH.deposit, ())); } uint256 ethAfter = w.account.balance; uint256 wethAfter = IERC20Balance(weth).balanceOf(w.account); // BOTH SIDES, EXACTLY. ETH falls and WETH rises by the amount on a // wrap, and the reverse on an unwrap. Either one off means the account // did not get what was charged. bool exact = w.unwrap ? (wethBefore - amount == wethAfter && ethBefore + amount == ethAfter) : (ethBefore - amount == ethAfter && wethBefore + amount == wethAfter); if (!exact) revert WrapNotExact(amount, ethBefore, ethAfter, wethBefore, wethAfter); if (w.unwrap) { emit Unwrapped(w.account, weth, amount); } else { _publishDelivered(w.account, w.taskId, weth, amount); emit Wrapped(w.account, weth, amount); } return amount; } }