|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.17; |
| 3 | + |
| 4 | +/// @author Modified from https://github.com/unruggable-labs/CCIPReader.sol/blob/341576fe7ff2b6e0c93fc08f37740cf6439f5873/contracts/CCIPReader.sol |
| 5 | + |
| 6 | +/// MIT License |
| 7 | +/// Portions Copyright (c) 2025 Unruggable |
| 8 | +/// Portions Copyright (c) 2025 ENS Labs Ltd |
| 9 | + |
| 10 | +/// @dev Instructions: |
| 11 | +/// 1. inherit this contract |
| 12 | +/// 2. call `ccipRead()` similar to `staticcall()` |
| 13 | +/// 3. do not put logic after this invocation |
| 14 | +/// 4. implement all response logic in callback |
| 15 | +/// 5. ensure that return type of calling function == callback function |
| 16 | + |
| 17 | +import {EIP3668, OffchainLookup} from "./EIP3668.sol"; |
| 18 | +import {BytesUtils} from "../utils/BytesUtils.sol"; |
| 19 | + |
| 20 | +contract CCIPReader { |
| 21 | + /// @dev A recursive CCIP-Read session. |
| 22 | + struct Context { |
| 23 | + address target; |
| 24 | + bytes4 callbackFunction; |
| 25 | + bytes extraData; |
| 26 | + bytes4 myCallbackFunction; |
| 27 | + bytes myExtraData; |
| 28 | + } |
| 29 | + |
| 30 | + /// @dev Special-purpose value for identity callback: `f(x) = x`. |
| 31 | + bytes4 constant IDENTITY_FUNCTION = bytes4(0); |
| 32 | + |
| 33 | + /// @dev Same as `ccipRead()` but the callback function is the identity. |
| 34 | + function ccipRead(address target, bytes memory call) internal view { |
| 35 | + ccipRead(target, call, IDENTITY_FUNCTION, ""); |
| 36 | + } |
| 37 | + |
| 38 | + /// @dev Performs a CCIP-Read and handles internal recursion. |
| 39 | + /// Reverts `OffchainLookup` if necessary. |
| 40 | + /// @param target The contract address. |
| 41 | + /// @param call The calldata to `staticcall()` on `target`. |
| 42 | + /// @param callbackFunction The function selector of callback. |
| 43 | + /// @param extraData The contextual data relayed to `callbackFunction`. |
| 44 | + function ccipRead( |
| 45 | + address target, |
| 46 | + bytes memory call, |
| 47 | + bytes4 callbackFunction, |
| 48 | + bytes memory extraData |
| 49 | + ) internal view { |
| 50 | + // We call the intended function that **could** revert with an `OffchainLookup` |
| 51 | + // We destructure the response into an execution status bool and our return bytes |
| 52 | + (bool ok, bytes memory v) = _safeCall( |
| 53 | + _detectEIP140(target), |
| 54 | + target, |
| 55 | + call |
| 56 | + ); |
| 57 | + // IF the function reverted with an `OffchainLookup` |
| 58 | + if (!ok && bytes4(v) == OffchainLookup.selector) { |
| 59 | + // We decode the response error into a tuple |
| 60 | + // tuples allow flexibility noting stack too deep constraints |
| 61 | + EIP3668.Params memory p = decodeOffchainLookup(v); |
| 62 | + if (p.sender == target) { |
| 63 | + // We then wrap the error data in an `OffchainLookup` sent/'owned' by this contract |
| 64 | + revert OffchainLookup( |
| 65 | + address(this), |
| 66 | + p.urls, |
| 67 | + p.callData, |
| 68 | + this.ccipReadCallback.selector, |
| 69 | + abi.encode( |
| 70 | + Context( |
| 71 | + target, |
| 72 | + p.callbackFunction, |
| 73 | + p.extraData, |
| 74 | + callbackFunction, |
| 75 | + extraData |
| 76 | + ) |
| 77 | + ) |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + // IF we have gotten here, the 'real' target does not revert with an `OffchainLookup` error |
| 82 | + if (ok && callbackFunction != IDENTITY_FUNCTION) { |
| 83 | + // The exit point of this architecture is OUR callback in the 'real' |
| 84 | + // We pass through the response to that callback |
| 85 | + (ok, v) = address(this).staticcall( |
| 86 | + abi.encodeWithSelector(callbackFunction, v, extraData) |
| 87 | + ); |
| 88 | + } |
| 89 | + // OR the call to the 'real' target reverts with a different error selector |
| 90 | + // OR the call to OUR callback reverts with ANY error selector |
| 91 | + if (ok) { |
| 92 | + assembly { |
| 93 | + return(add(v, 32), mload(v)) |
| 94 | + } |
| 95 | + } else { |
| 96 | + assembly { |
| 97 | + revert(add(v, 32), mload(v)) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// @dev CCIP-Read callback for `ccipRead()`. |
| 103 | + /// @param response The response from offchain. |
| 104 | + /// @param extraData The contextual data passed from `ccipRead()`. |
| 105 | + /// @dev The return type of this function is polymorphic depending on the caller. |
| 106 | + function ccipReadCallback( |
| 107 | + bytes memory response, |
| 108 | + bytes memory extraData |
| 109 | + ) external view { |
| 110 | + Context memory ctx = abi.decode(extraData, (Context)); |
| 111 | + // Since the callback can revert too (but has the same return structure) |
| 112 | + // We can reuse the calling infrastructure to call the callback |
| 113 | + ccipRead( |
| 114 | + ctx.target, |
| 115 | + abi.encodeWithSelector( |
| 116 | + ctx.callbackFunction, |
| 117 | + response, |
| 118 | + ctx.extraData |
| 119 | + ), |
| 120 | + ctx.myCallbackFunction, |
| 121 | + ctx.myExtraData |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + /// @dev Decode `OffchainLookup` error data into a struct. |
| 126 | + /// @param v The error data of the revert. |
| 127 | + /// @return p The decoded `OffchainLookup` params. |
| 128 | + function decodeOffchainLookup( |
| 129 | + bytes memory v |
| 130 | + ) internal pure returns (EIP3668.Params memory p) { |
| 131 | + p = EIP3668.decode(BytesUtils.substring(v, 4, v.length - 4)); |
| 132 | + } |
| 133 | + |
| 134 | + /// @dev Determine if `target` uses `revert()` instead of `invalid()`. |
| 135 | + // Assumption: only newer contracts revert `OffchainLookup`. |
| 136 | + /// @param target The contract to test. |
| 137 | + /// @return safe True if safe to call. |
| 138 | + function _detectEIP140(address target) internal view returns (bool safe) { |
| 139 | + if (target == address(this)) return true; |
| 140 | + // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-140.md |
| 141 | + assembly { |
| 142 | + let G := 5000 |
| 143 | + let g := gas() |
| 144 | + pop(staticcall(G, target, 0, 0, 0, 0)) |
| 145 | + safe := lt(sub(g, gas()), G) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /// @dev Same as `staticcall()` but prevents OOG when not `safe`. |
| 150 | + function _safeCall( |
| 151 | + bool safe, |
| 152 | + address target, |
| 153 | + bytes memory call |
| 154 | + ) internal view returns (bool ok, bytes memory v) { |
| 155 | + (ok, v) = target.staticcall{gas: safe ? gasleft() : 50000}(call); |
| 156 | + } |
| 157 | +} |
0 commit comments