// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {IIMD} from "./IIMD.sol"; import {IIntake} from "./IIntake.sol"; import {IIntakeReceiver} from "./IIntakeReceiver.sol"; /// @notice Internal-only mainnet job payment binding. No deployment, owner or mutable endpoint. /// @dev The caller must guard its entry point against reentrancy, record the returned request ID, /// and accept callbacks only for its own unsettled requests. Recovery policy belongs to the caller. library IntakeBinding { address internal constant INTAKE = 0xa43e6F75ee006411F79Ac1C84120606C2330DE82; address internal constant IMD = 0xD34a99Bc0f67aE1bbd63C660e6d0b0dd03E263B7; uint256 internal constant CHAIN_ID = 1; uint256 internal constant MAX_BODY_BYTES = 16_384; bytes32 internal constant JOB_OPEN = bytes32("job.open@job-1"); bytes32 internal constant JOB_CONTINUE = bytes32("job.continue@job-1"); uint8 internal constant SUCCESS = 0; uint8 internal constant REFUSED = 1; uint8 internal constant FAILED = 2; error WrongChain(uint256 chainId); error UnsupportedAction(bytes32 action); error ActionNotSold(bytes32 action); error InvalidBodyLength(uint256 length); error PriceChanged(uint256 expected, uint256 live); error ApprovalFailed(); error AllowanceNotConsumed(uint256 remaining); error NotIntake(address caller); error RequestNotCompleted(bytes32 requestId); /// @notice Zero is unavailable, never a free build. Read separately for open and continue. function price(bytes32 action) internal view returns (uint256 amount) { if (block.chainid != CHAIN_ID) revert WrongChain(block.chainid); if (action != JOB_OPEN && action != JOB_CONTINUE) revert UnsupportedAction(action); amount = IIntake(INTAKE).priceOf(action, IMD); if (amount == 0) revert ActionNotSold(action); } /// @notice Pays the current price from this contract, using an exact temporary approval. /// @param expectedPrice Price the caller budgeted; a changed quote reverts atomically. /// @dev body is already encoded UTF-8 JSON; this layer does not interpret wishes or job IDs. function submit(bytes32 action, bytes memory body, uint256 expectedPrice) internal returns (bytes32 requestId) { if (body.length == 0 || body.length > MAX_BODY_BYTES) revert InvalidBodyLength(body.length); uint256 amount = price(action); if (amount != expectedPrice) revert PriceChanged(expectedPrice, amount); IIMD token = IIMD(IMD); if (token.allowance(address(this), INTAKE) != 0) _approve(token, 0); _approve(token, amount); requestId = IIntake(INTAKE) .requestWithFailure( action, body, IIntake.Callback(address(this), IIntakeReceiver.onResult.selector), IIntakeReceiver.onFailure.selector, IMD, amount ); uint256 remaining = token.allowance(address(this), INTAKE); if (remaining != 0) revert AllowanceNotConsumed(remaining); } /// @dev Caller authentication alone is insufficient: also check your own pending request ID and /// requireCompletedOwnRequest. function requireCallbackCaller() internal view { if (msg.sender != INTAKE) revert NotIntake(msg.sender); } /// @notice intake-2: a callback naming `requestId` settles it only if Intake's own record says it /// is this contract's completed request with this contract's result callback. This stops a writer /// completing an outsider request X from settling our still-pending R through X's args. It cannot /// prove which completion carried the args or that the reported result is honest (writer trust). function requireCompletedOwnRequest(bytes32 requestId) internal view { (address payer, address target, bytes4 selector, bool completed) = IIntake(INTAKE).requests(requestId); if ( !completed || payer != address(this) || target != address(this) || selector != IIntakeReceiver.onResult.selector ) revert RequestNotCompleted(requestId); } function _approve(IIMD token, uint256 amount) private { if (!token.approve(INTAKE, amount)) revert ApprovalFailed(); } }