// SPDX-License-Identifier: MIT pragma solidity 0.8.26; import {FoundationTypesV1 as T} from './vendor/FoundationTypesV1.sol'; import {IFoundationModuleV1,IFoundationModuleFactoryV1} from './vendor/IFoundationModuleV1.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol'; import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; interface INoopLedger {function moduleCredited(address) external view returns(uint256);function moduleClaimed(address) external view returns(uint256);function claimModule(uint256) external;} /// @notice Custom Foundation ABI module, NOT a published or admitted Programmable release. /// ERC20 quote budget goes to a fixed operator treasury. This is not automatic IMD payment. contract NoopWorkModule is IFoundationModuleV1,ReentrancyGuard { using SafeERC20 for IERC20; T.ModuleContext private ctx; bytes32 public immutable configurationHash; address public immutable treasury; uint256 public immutable budgetPerBatch; uint64 public immutable cooldown; uint64 public lastBatch; uint256 public batch; uint256 public pendingSwaps; event WorkFunded(uint256 indexed batch,bytes32 indexed poolId,address indexed treasury,address asset,uint256 amount,uint256 swaps); constructor(T.ModuleContext memory c,bytes memory config){ (address recipient,uint256 amount,uint64 delay)=abi.decode(config,(address,uint256,uint64)); require(c.host!=address(0)&&c.creator!=address(0)&&c.token!=address(0)&&c.quote!=address(0)&&c.ledger!=address(0)&&c.poolId!=0&&c.token!=c.quote,'context'); require(recipient!=address(0)&&recipient!=address(this)&&amount>0&&delay>=10 minutes,'configuration'); ctx=c;configurationHash=keccak256(config);treasury=recipient;budgetPerBatch=amount;cooldown=delay; } function context() external view returns(T.ModuleContext memory){return ctx;} function descriptor() external pure returns(T.Descriptor memory){return T.Descriptor(keccak256('noop.work-budget.v1'),1,T.AFTER_SWAP|T.ACTION,T.OWN_QUOTE_BUDGET,0,65000,250000,true,bytes32(0));} modifier onlyHost(){require(msg.sender==ctx.host,'host');_;} function onBeforeSwap(T.SwapContext calldata s) external onlyHost returns(bytes4){require(s.poolId==ctx.poolId,'pool');return this.onBeforeSwap.selector;} function onAfterSwap(T.SwapContext calldata s) external onlyHost returns(bytes4){require(s.poolId==ctx.poolId,'pool');if(s.grossQuote>0)pendingSwaps++;return this.onAfterSwap.selector;} function onAction(address actor,bytes calldata data) external onlyHost nonReentrant returns(bytes4){ require(actor==ctx.creator&&data.length==0,'creator');require(pendingSwaps>0&&block.timestamp>=uint256(lastBatch)+cooldown,'cooldown'); INoopLedger ledger=INoopLedger(ctx.ledger);require(ledger.moduleCredited(address(this))-ledger.moduleClaimed(address(this))>=budgetPerBatch,'budget'); uint256 swaps=pendingSwaps;pendingSwaps=0;lastBatch=uint64(block.timestamp);batch++; IERC20 asset=IERC20(ctx.quote);uint256 own=asset.balanceOf(address(this));uint256 recipient=asset.balanceOf(treasury); ledger.claimModule(budgetPerBatch);require(asset.balanceOf(address(this))==own+budgetPerBatch,'claim');asset.safeTransfer(treasury,budgetPerBatch); require(asset.balanceOf(treasury)==recipient+budgetPerBatch&&asset.balanceOf(address(this))==own,'transfer'); emit WorkFunded(batch,ctx.poolId,treasury,ctx.quote,budgetPerBatch,swaps);return this.onAction.selector; } } contract NoopWorkFactory is IFoundationModuleFactoryV1 { function createModule(T.ModuleContext calldata c,bytes calldata config) external returns(address){require(msg.sender==c.host,'host');return address(new NoopWorkModule(c,config));} }