# {{title}} - EigenCompute Integration

This project contains an auto-generated **Python server** and **Solidity contracts** for integrating **{{title}}** with the EigenCompute network.

## 📁 Project Structure

```
{{title}}/
├── src/                                 # Python Server
│   ├── main.py                          # YOUR CODE HERE (see marked sections){{#has_types}}
│   ├── types.py                         # Generated Pydantic types (auto-generated){{/has_types}}
│   └── gen/                             # Auto-generated code (DO NOT EDIT)
│       ├── __init__.py                  # Package initialization
│       ├── {{openrpc_spec_file}}        # OpenRPC specification
│       └── {{abi_file}}                 # Contract ABI
├── contracts/                           # Solidity Contracts
│   ├── src/
│   │   ├── {{concrete_contract_name}}.sol # YOUR CONTRACT CODE
│   │   └── gen/                         # Auto-generated (DO NOT EDIT)
│   │       ├── {{contract_name}}.sol    # Abstract receiver contract
│   │       └── interfaces/
│   │           ├── {{prefixed_interface_name}}.sol # Async interface
│   │           └── IExecutionCallback.sol # Callback interface
│   ├── script/
│   │   └── Deploy{{concrete_contract_name}}.s.sol # Deployment script
│   └── foundry.toml                     # Foundry configuration
├── Dockerfile                           # Container setup
├── package.json                         # NPM scripts for deployment
├── pyproject.toml                       # Python dependencies & config
└── README.md                            # This file
```

## 🚀 Quick Start

### Prerequisites
- Python 3.9+
- [Foundry](https://getfoundry.sh/) installed
- Docker (optional)

### Run Python Server
```bash
# Install dependencies
pip install .

# Run locally
uvicorn src.main:app --host 0.0.0.0 --port {{server_port}} --reload

# Or with Docker
docker build -t {{package_name}} .
docker run -p {{server_port}}:{{server_port}} {{package_name}}
```

### Setup Solidity Contracts
```bash
cd contracts
git init # if not in a git repo
forge soldeer install
forge build
forge test
```

## 🐍 Python Server

### Implementation
The server implements JSON-RPC methods with a `compute_` prefix:

{{#methods}}
#### `compute_{{original_name}}`
{{summary}}

* **Parameters**: {{#params}}`{{name}}: {{python_type}}`{{^last}}, {{/last}}{{/params}}
* **Returns**: `{{result_type}}`

{{/methods}}

### Server Endpoints
- **JSON-RPC**: `http://localhost:{{server_port}}/`
- **Interactive Docs**: `http://localhost:{{server_port}}/docs`

### Example Client Calls
{{#methods}}
```bash
# Call the {{original_name}} method
curl -X POST http://localhost:{{server_port}} \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "compute_{{original_name}}",
    "params": [{{#params}}"example_{{name}}"{{^last}}, {{/last}}{{/params}}],
    "id": 1
  }'
```
{{/methods}}

### Customize Implementation
Edit the method implementations in `src/main.py`:

```python
{{#methods}}
@rpc.method()
async def compute_{{original_name}}({{#params}}{{name}}: {{python_type}}{{#params_has_next}}, {{/params_has_next}}{{/params}}) -> {{result_type}}:
    # TODO: Implement your {{original_name}} logic here

    # Example implementation:
    # 1. Process input parameters
    # 2. Perform computation
    # 3. Return result

    raise NotImplementedError()

{{/methods}}
```

## 🔄 Code Regeneration

This project uses a clean separation between generated code and user code:

### ✅ Safe to Edit (Your Code)
- **`src/main.py`** - Implement your logic in the method implementations
- **`contracts/src/{{concrete_contract_name}}.sol`** - Your Solidity contract customizations

### ⚠️ Auto-Generated (DO NOT EDIT)
- `src/gen/` - All generated Python code (types, specifications, ABIs)
- `contracts/src/gen/` - All generated Solidity contracts and interfaces
- `Dockerfile`, `package.json`, `pyproject.toml` - Build configurations
- `contracts/foundry.toml`, `contracts/script/` - Contract deployment setup

### 📝 How to Edit main.py
After first generation, `src/main.py` becomes **your file**. Simply implement your logic in the method implementations:
```python
@rpc.method()
async def compute_my_method(param: str) -> str:
    # Replace NotImplementedError() with your actual logic
    return "Hello World"
```
**The file won't be overwritten on regeneration** - only `src/gen/` gets updated.

### Regenerating Your Project
When you need to update your project (e.g., after changing your Solidity interface):

```bash
# This will regenerate all auto-generated files while preserving your code
npm run regenerate
```

Your implementations in `src/main.py` and `contracts/src/{{concrete_contract_name}}.sol` will be preserved.

## 📋 Solidity Contracts

### {{contract_name}}
**Abstract receiver contract** that handles EigenCompute integration:
- ✅ Implements `IExecutionCallback` for async result handling
- ✅ Implements `{{prefixed_interface_name}}` for the async interface
- ✅ Routes all function calls through the EigenCompute network
- ✅ Provides abstract callback methods for result handling

### {{concrete_contract_name}}
**Concrete implementation** ready for customization:
- ✅ Extends `{{contract_name}}`
- ✅ Provides template implementations for all callback methods
- 🔧 **TODO**: Implement your business logic in the callback methods

{{#methods}}
### `{{original_name}}({{#params}}{{solidity_type}} {{name}}{{#params_has_next}}, {{/params_has_next}}{{/params}}) → uint256 taskId`
{{summary}}

**Callback Method**: `_on{{capitalized_name}}(uint256 taskId{{#params}}, {{memory_type}} {{name}}{{/params}}, {{result_memory_type}} result, string memory error)`

{{/methods}}

## 🔧 Implementation Workflow

### 1. Implement Python Server Logic
```python
# In src/main.py
{{#methods}}
@rpc.method()
async def compute_{{original_name}}({{#params}}{{name}}: {{python_type}}{{#params_has_next}}, {{/params_has_next}}{{/params}}) -> {{result_type}}:
    # Your computation logic here
    # e.g., return param1 + param2
    result = ...
    return result
{{/methods}}
```

### 2. Customize Solidity Callbacks
```solidity
// In contracts/src/{{concrete_contract_name}}.sol
{{#methods}}
function _on{{capitalized_name}}(
    uint256 taskId{{#params}},
    {{memory_type}} {{name}}{{/params}},
    {{result_memory_type}} result,
    string memory error
) internal override {
    if (bytes(error).length > 0) {
        emit {{capitalized_name}}Error(taskId, error);
        return;
    }

    // Handle successful result
    emit {{capitalized_name}}Success(taskId{{#params}}, {{name}}{{/params}}, result);
}
{{/methods}}
```

### 3. Deploy Your Service

This project includes pre-configured npm scripts for easy deployment using the EigenCompute CLI:

```bash
# Set your private key (recommended approach)
export PRIVATE_KEY=0x123...

# Interactive deployment (recommended) - prompts for deployment type
npm run deploy

# Deploy only offchain component (Docker to EigenDA)
npm run deploy:offchain

# Deploy only onchain component (smart contracts)
npm run deploy:onchain

# Deploy both components
npm run deploy:both
```

**Alternative: Direct CLI usage**
```bash
# Interactive deployment - prompts to choose between offchain, onchain, or both
export PRIVATE_KEY=0x123...
npx @eigenlayer/compute-cli deploy --dockerfile ./Dockerfile --contract-path ./contracts/src/{{concrete_contract_name}}.sol

# Deploy to custom network
export PRIVATE_KEY=0x123...
npx @eigenlayer/compute-cli deploy --dockerfile ./Dockerfile --contract-path ./contracts/src/{{concrete_contract_name}}.sol --network 1337 --rpc http://localhost:8545
```

**Manual contract deployment (if needed)**
```bash
cd contracts
forge script script/Deploy{{concrete_contract_name}}.s.sol --rpc-url <rpc_url> --private-key <private_key> --broadcast
```

### 4. Test Integration
{{#methods}}
```solidity
// Call {{original_name}} async function
uint256 taskId = {{concrete_contract_name}}.{{original_name}}({{#params}}example{{capitalized_name}}{{^last}}, {{/last}}{{/params}});
// Result delivered via _on{{capitalized_name}} callback automatically
```
{{/methods}}

## 📊 Package Information
- **Package Name**: `{{package_name}}`
- **Version**: `{{version}}`
- **Generated**: {{generation_timestamp}}

## 📚 Learn More

- [EigenCompute Documentation](https://docs.eigencloud.xyz/)
- [FastAPI Documentation](https://fastapi.tiangolo.com/)
- [Foundry Book](https://book.getfoundry.sh/)
- [Solidity Documentation](https://docs.soliditylang.org/)

---

*Generated by [EigenCompute Codegen](https://github.com/Layr-Labs/compute-app-avs) on {{generation_timestamp}}*
