# {{title}} - EigenCompute Integration

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

## 📁 Project Structure

```
{{title}}/
├── src/                                 # Rust Server
│   ├── main.rs                          # YOUR CODE HERE (see marked sections)
│   ├── lib.rs                           # Library exports (auto-generated)
│   └── gen/                             # Auto-generated code (DO NOT EDIT)
│       ├── mod.rs                       # Server setup and utilities
│       ├── server.rs                    # Server trait definitions
│       ├── types.rs                     # Generated types
│       ├── {{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
├── Cargo.toml                           # Rust dependencies
├── Dockerfile                           # Container setup
└── README.md                           # This file
```

## 🚀 Quick Start

### Prerequisites
- [Rust](https://rustup.rs/) 1.70+
- [Foundry](https://getfoundry.sh/) installed
- Docker (optional)

### Run Rust Server
```bash
# Run locally
cargo run

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

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

## 🦀 Rust Server

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

{{#methods}}
#### `compute_{{name}}`
{{summary}}

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

{{/methods}}

### Server Endpoints
- **JSON-RPC**: `http://localhost:{{server_port}}/`
- **Health Check**: Call `system_health` method

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

### Customize Implementation
Edit the trait implementation in `src/main.rs`:

```rust
impl {{trait_name}}Server for {{server_name}} {
{{#methods}}
    async fn {{snake_case_name}}(&self{{#params}}, {{name}}: {{rust_type}}{{/params}}) -> RpcResult<{{result_type}}> {
        // TODO: Implement your {{name}} logic here
        
        // Example implementation:
        // 1. Process input parameters
        // 2. Perform computation
        // 3. Return result
        
        Err(ErrorObject::owned(-32603, "Method not implemented", None::<()>).into())
    }

{{/methods}}
}
```

## 📋 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 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}}{{^last}}, {{/last}}{{/params}}) → uint256 taskId`
{{summary}}

**Callback Method**: `_on{{capitalized_name}}(uint256 taskId, {{result_memory_type}} result, string memory error)`

{{/methods}}

## 🔄 Code Regeneration

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

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

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

### 📝 How to Edit main.rs
After first generation, `src/main.rs` becomes **your file**. Simply implement your logic in the trait methods:
```rust
#[async_trait::async_trait]
impl MyServiceRpcServer for ServerImpl {
    async fn my_method(&self, param: String) -> RpcResult<String> {
        // Replace not_implemented() with your actual logic
        Ok("Hello World".to_string())
    }
}
```
**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.rs` and `contracts/src/{{concrete_contract_name}}.sol` will be preserved.

## 🔧 Implementation Workflow

### 1. Implement Rust Server Logic
```rust
// In src/main.rs (between the marked sections)
#[async_trait::async_trait]
impl {{trait_name}}Server for ServerImpl {
{{#methods}}
    async fn {{snake_case_name}}(&self{{#params}}, {{name}}: {{rust_type}}{{/params}}) -> RpcResult<{{result_type}}> {
        // Your computation logic here
        Ok(result)
    }
{{/methods}}
}
```

### 2. Customize Solidity Callbacks
```solidity
// In contracts/src/{{concrete_contract_name}}.sol
{{#methods}}
function _on{{capitalized_name}}(
    uint256 taskId, 
    {{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, 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/)
- [Rust JSON-RPC Guide](https://docs.rs/jsonrpsee/)
- [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}}*
