# State Anchoring Service - Deployment Guide

**Date**: 2026-01-18  
**Status**: Ready for Deployment

---

## Overview

The State Anchoring Service monitors ChainID 138 blocks and submits state proofs to the MainnetTether contract on Ethereum Mainnet.

**Location**: `services/state-anchoring-service/`  
**Implementation**: TypeScript (200 lines)  
**Status**: ✅ **READY FOR DEPLOYMENT**

---

## Prerequisites

1. **Node.js** 18+ installed
2. **TypeScript** compiler (tsc)
3. **Environment Variables**:
   - `PRIVATE_KEY` - Private key for signing transactions on Mainnet
   - `CHAIN138_RPC_URL` - RPC endpoint for ChainID 138
   - `MAINNET_RPC_URL` - RPC endpoint for Ethereum Mainnet
   - `TETHER_ADDRESS` - MainnetTether contract address

---

## Deployment Steps

### 1. Install Dependencies

```bash
cd services/state-anchoring-service
npm install
```

### 2. Configure Environment

Create `.env` file:

```bash
PRIVATE_KEY=0x...
CHAIN138_RPC_URL=http://192.168.11.211:8545
MAINNET_RPC_URL=https://eth.llamarpc.com
TETHER_ADDRESS=0x15DF1D5BFDD8Aa4b380445D4e3E9B38d34283619
```

### 3. Build Service

```bash
npm run build
```

### 4. Test Locally

```bash
npm run dev
```

### 5. Deploy to Production

```bash
# Using PM2 (recommended)
pm2 start dist/index.js --name state-anchoring-service

# Or using systemd (see below)
```

---

## Systemd Service Configuration

Create `/etc/systemd/system/state-anchoring-service.service`:

```ini
[Unit]
Description=State Anchoring Service
After=network.target

[Service]
Type=simple
User=node
WorkingDirectory=/path/to/services/state-anchoring-service
Environment=NODE_ENV=production
ExecStart=/usr/bin/node dist/index.js
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
```

Enable and start:

```bash
sudo systemctl enable state-anchoring-service
sudo systemctl start state-anchoring-service
```

---

## Monitoring

### Check Service Status

```bash
# If using PM2
pm2 status state-anchoring-service
pm2 logs state-anchoring-service

# If using systemd
sudo systemctl status state-anchoring-service
sudo journalctl -u state-anchoring-service -f
```

### Verify Operation

Check MainnetTether contract for new state proofs:

```bash
cast logs --address 0x15DF1D5BFDD8Aa4b380445D4e3E9B38d34283619 \
  --event "StateProofAnchored(uint256,bytes32,bytes32,uint256,uint256)" \
  --rpc-url https://eth.llamarpc.com
```

---

## Configuration Options

### Environment Variables

| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `PRIVATE_KEY` | ✅ Yes | - | Private key for Mainnet transactions |
| `CHAIN138_RPC_URL` | ✅ Yes | `http://192.168.11.211:8545` | ChainID 138 RPC endpoint |
| `MAINNET_RPC_URL` | ✅ Yes | `https://eth.llamarpc.com` | Mainnet RPC endpoint |
| `TETHER_ADDRESS` | ✅ Yes | `0x15DF1D5BFDD8Aa4b380445D4e3E9B38d34283619` | MainnetTether contract address |

### Service Configuration

The service monitors ChainID 138 blocks and submits state proofs periodically. Configuration can be adjusted in `src/index.ts`:

- Block polling interval
- Batch size
- Retry logic
- Validator signature collection

---

## Troubleshooting

### Service Not Starting

1. Check environment variables are set correctly
2. Verify RPC endpoints are accessible
3. Check private key format (must start with `0x`)

### No State Proofs Being Submitted

1. Verify MainnetTether contract address is correct
2. Check wallet has sufficient ETH for gas
3. Verify contract has correct permissions

### RPC Connection Issues

1. Test RPC endpoints manually:
   ```bash
   curl -X POST -H "Content-Type: application/json" \
     --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' \
     $CHAIN138_RPC_URL
   ```
2. Check network connectivity
3. Verify firewall rules allow outbound connections

---

## Next Steps

After deployment:

1. Monitor service logs for errors
2. Verify state proofs are being submitted
3. Check MainnetTether contract for new proofs
4. Set up alerts for service failures

---

**Last Updated**: 2026-01-18
