// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; {{#has_structs}}import "./interfaces/{{prefixed_interface_name}}Types.sol";{{/has_structs}} import "./interfaces/IExecutionCallback.sol"; import "./interfaces/IImageArchive.sol"; /** * Abstract EigenCompute Receiver contract that implements IExecutionCallback{{#has_structs}} and uses {{prefixed_interface_name}}Types{{/has_structs}} * * This contract combines service functions and callback handling in one abstract contract. * Users should extend this contract and override the abstract callback methods they need. * This contract automatically handles onExecutionResult and onExecutionError routing from EigenCompute. * * Generated by EigenCompute on {{generation_timestamp}} * Functions: {{#has_methods}}{{methods.length}}{{/has_methods}}{{^has_methods}}0{{/has_methods}} */ abstract contract {{contract_name}} is IExecutionCallback{{#has_structs}}, {{prefixed_interface_name}}Types{{/has_structs}} { /// @notice Request data for EigenCompute execution struct ExecutionRequest { bytes32 configID; address callbackReceiver; string functionName; bytes encodedParams; } // The EigenCompute reservation ID uint256 public immutable reservationID; /** * Constructor to initialize the reservation ID * @param _reservationID The EigenCompute reservation ID */ constructor(uint256 _reservationID) { reservationID = _reservationID; } /** * Modifier to ensure only EigenCompute can call callback functions */ modifier onlyEigenCompute() { require(msg.sender == _getEigenCompute(), "Only EigenCompute can call this function"); _; } /** * Implementation of IExecutionCallback.onExecutionResult * Automatically routes to typed callback methods * Can only be called by EigenCompute contract */ function onExecutionResult(uint256 taskId, string memory functionName, bytes memory result) external override onlyEigenCompute { _handleExecutionCallback(taskId, functionName, result, ""); } /** * Implementation of IExecutionCallback.onExecutionError * Automatically routes to typed callback methods with error message * Can only be called by EigenCompute contract */ function onExecutionError(uint256 taskId, string memory functionName, string memory error) external override onlyEigenCompute { _handleExecutionErrorCallback(taskId, functionName, error); } /** * Internal callback router for type-safe callbacks */ function _handleExecutionCallback( uint256 taskId, string memory functionName, bytes memory result, string memory error ) internal { {{#methods}} if (keccak256(bytes(functionName)) == keccak256(bytes("compute_{{name}}"))) { // Decode the execution result {{#has_multiple_outputs}} ({{#result_params}}{{type}} {{name}}{{^last}}, {{/last}}{{/result_params}}) = abi.decode(result, {{result_type}}); _on{{capitalized_name}}(taskId, {{#result_params}}{{name}}{{^last}}, {{/last}}{{/result_params}}, error); {{/has_multiple_outputs}} {{^has_multiple_outputs}} {{result_memory_type}} resultValue = abi.decode(result, ({{result_type}})); _on{{capitalized_name}}(taskId, resultValue, error); {{/has_multiple_outputs}} return; } {{/methods}} revert("Unknown function"); } /** * Internal error callback router for type-safe error callbacks */ function _handleExecutionErrorCallback( uint256 taskId, string memory functionName, string memory error ) internal { {{#methods}} if (keccak256(bytes(functionName)) == keccak256(bytes("compute_{{name}}"))) { {{#has_multiple_outputs}} _on{{capitalized_name}}(taskId, {{#result_params}}{{{default_value}}}{{^last}}, {{/last}}{{/result_params}}, error); {{/has_multiple_outputs}} {{^has_multiple_outputs}} _on{{capitalized_name}}(taskId, {{{default_result_value}}}, error); {{/has_multiple_outputs}} return; } {{/methods}} revert("Unknown function"); } /** * Get the EigenCompute contract address * @return address EigenCompute contract address * @dev This function reverts if EigenCompute is not deployed on this chain */ function _getEigenCompute() internal view virtual returns (address) { _checkDeployed(); return address({{execution_endpoint_address}}); } /** * Get the EigenCompute ImageArchive contract * @return IImageArchive EigenCompute ImageArchive contract * @dev This function reverts if EigenCompute is not deployed on this chain */ function _getImageArchive() internal view virtual returns (IImageArchive) { _checkDeployed(); return IImageArchive(address({{image_archive_address}})); } /** * Check if EigenCompute is deployed on this chain * @dev This function reverts if EigenCompute is not deployed on this chain */ function _checkDeployed() internal view virtual { require(block.chainid == 84532, "EigenCompute not deployed on this chain"); } // ================== SERVICE FUNCTIONS ================== {{^has_methods}} // No functions found in the contract interface {{/has_methods}} {{#methods}} /** * Call {{name}} with callback (internal async version) * {{summary}} * The calling contract (address(this)) will receive the callback. {{#params}} * @param {{name}} Parameter of type {{solidity_type}} {{/params}} * @return taskId Unique identifier for tracking this task */ function _{{name}}Async({{#params}}{{calldata_type}} {{name}}{{^last}}, {{/last}}{{/params}}) internal returns (uint256 taskId) { {{#has_params}} bytes memory encodedParams = abi.encode({{#params}}{{name}}{{^last}}, {{/last}}{{/params}}); {{/has_params}} {{^has_params}} bytes memory encodedParams = ""; {{/has_params}} return _submitTaskWithCallback( "compute_{{name}}", encodedParams, address(this) ); } {{/methods}} /** * Internal function to handle asynchronous task submission with callback * @param functionName The name of the function to execute * @param encodedParams ABI-encoded parameters * @param callbackReceiver Address that will receive the callback * @return taskId Unique identifier for tracking this task */ function _submitTaskWithCallback( string memory functionName, bytes memory encodedParams, address callbackReceiver ) internal returns (uint256) { // Get the active reservation config ID - this auto-updates with image changes // Alternatively, use a fixed configID for execution guarantees bytes32 configID = _getImageArchive().getReservationConfigID(reservationID); bytes memory callData = abi.encodeWithSignature( "requestExecution((bytes32,address,string,bytes))", ExecutionRequest({ configID: configID, callbackReceiver: callbackReceiver, functionName: functionName, encodedParams: encodedParams }) ); (bool success, bytes memory returnData) = _getEigenCompute().call(callData); require(success, "EigenCompute call failed"); uint256 taskId = abi.decode(returnData, (uint256)); return taskId; } // ================== ABSTRACT CALLBACK METHODS ================== {{^has_methods}} // No callback methods needed - no functions found in the contract interface {{/has_methods}} {{#methods}} /** * Abstract callback for {{name}} function completion * Override this function in your contract to handle {{name}} results * @param taskId The unique identifier for the completed task {{#has_multiple_outputs}} {{#result_params}} * @param {{name}} The result returned by {{../name}} (original type: {{type}}) {{/result_params}} {{/has_multiple_outputs}} {{^has_multiple_outputs}} * @param result The result returned by {{name}} (original type: {{result_type}}) {{/has_multiple_outputs}} * @param error Error message if the function failed, empty string if successful */ function _on{{capitalized_name}}(uint256 taskId, {{result_param_signature}}, string memory error) internal virtual; {{/methods}} }