# Payment and State Channel Manager Deployment

**Status**: Ready for deployment  
**Contracts**: `PaymentChannelManager` (payment channels), optional `GenericStateChannelManager` (state channels with stateHash). Both deployable on Mainnet and Chain-138.

---

## Overview

PaymentChannelManager enables payment channels on each chain. Deploy once per chain. Channel open/close/update transactions on Chain-138 are mirrored and anchored by the existing TransactionMirror and MainnetTether (no changes to those contracts).

---

## Environment Variables

| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `PRIVATE_KEY` | Yes | - | Deployer private key (hex, no 0x prefix or with 0x) |
| `CHANNEL_ADMIN` | No | Deployer address | Admin for pause, setChallengeWindow, setAdmin (multisig recommended) |
| `CHALLENGE_WINDOW_SECONDS` | No | 86400 (24h) | Challenge period for unilateral close (seconds) |
| `ETH_MAINNET_RPC_URL` | Yes (Mainnet) | - | RPC URL for Ethereum Mainnet |
| `CHAIN138_RPC_URL` | Yes (Chain-138) | - | RPC URL for Chain-138 |

---

## Deploy to Ethereum Mainnet

```bash
export PRIVATE_KEY=<deployer_private_key>
export CHANNEL_ADMIN=0x...   # optional; multisig recommended
export CHALLENGE_WINDOW_SECONDS=86400   # optional; 24h default

forge script script/DeployPaymentChannelManager.s.sol \
  --rpc-url $ETH_MAINNET_RPC_URL \
  --broadcast \
  --verify
```

After deployment, set in frontend config:

- `CONTRACT_ADDRESSES.mainnet.PAYMENT_CHANNEL_MANAGER = <deployed_address>`

**Optional – Generic state channels (stateHash):**

```bash
forge script script/DeployGenericStateChannelManager.s.sol \
  --rpc-url $ETH_MAINNET_RPC_URL \
  --broadcast \
  --verify
```

Set `CONTRACT_ADDRESSES.mainnet.GENERIC_STATE_CHANNEL_MANAGER` (and chain138 if deployed) if you use the generic state channel manager in the frontend.

---

## Deploy to Chain-138

```bash
export PRIVATE_KEY=<deployer_private_key>
export CHANNEL_ADMIN=0x...
export CHALLENGE_WINDOW_SECONDS=86400   # optional

forge script script/DeployPaymentChannelManager.s.sol \
  --rpc-url $CHAIN138_RPC_URL \
  --broadcast \
  --verify
```

Then set:

- `CONTRACT_ADDRESSES.chain138.PAYMENT_CHANNEL_MANAGER = <deployed_address>`

---

## Verification

Verification uses the same chain as the deployment RPC. If the chain has a block explorer (Etherscan, Blockscout, etc.), pass `--verify` and ensure the appropriate API key is set for that chain.

### Ethereum Mainnet

- Use `--verify` with Forge; set `ETHERSCAN_API_KEY` in the environment.
- Example: `export ETHERSCAN_API_KEY=...` then run the deploy script with `--verify`.

### Chain-138

Chain-138 may use a different block explorer (e.g. Blockscout or a custom explorer). Verification steps:

1. **Identify the explorer** for Chain-138 (e.g. Blockscout instance URL).
2. **Set the API key** for that explorer. For Blockscout-compatible explorers, Forge often uses:
   - `ETHERSCAN_API_KEY` if the explorer is Etherscan-compatible, or
   - A chain-specific key (e.g. `BLOCKSCOUT_API_KEY` or the explorer’s env var documented in [Foundry book – Verify](https://book.getfoundry.sh/forge/verify)).
3. **RPC and chain ID**: Use `--rpc-url $CHAIN138_RPC_URL` so Forge uses Chain-138 for verification. Ensure the deployment script’s chain ID matches (e.g. 138).
4. If the explorer is not Etherscan-compatible, check Foundry’s `foundry.toml` or docs for `[etherscan]` blocks and the correct `key` / `url` for your chain.

Document the exact env vars and `foundry.toml` snippet for Chain-138 in your runbook once the explorer is chosen.

---

## Security Recommendations

- Use a **multisig** (e.g. Gnosis Safe) for `CHANNEL_ADMIN`.
- Keep **challenge window** short enough to limit griefing (e.g. 24h); document that participants or a watchtower must respond within the window.
- Test on testnets (Mainnet fork and Chain-138 testnet if available) before mainnet.

---

## Gas (Approximate)

- Deployment: ~1.5M gas
- openChannel: ~175k gas
- fundChannel: ~60k gas
- closeChannelCooperative: ~240k gas
- submitClose: ~150k gas
- challengeClose: ~180k gas
- finalizeClose: ~100k gas

---

## Related

- [MAINNET_TETHER_AND_TRANSACTION_MIRROR.md](MAINNET_TETHER_AND_TRANSACTION_MIRROR.md) – Tether and mirror (unchanged)
- [docs/channels/PAYMENT_CHANNELS_ARCHITECTURE.md](../channels/PAYMENT_CHANNELS_ARCHITECTURE.md) – Architecture and data flow
- [docs/channels/PRE_DEPLOYMENT_RECOMMENDATIONS.md](../channels/PRE_DEPLOYMENT_RECOMMENDATIONS.md) – Pre-deployment checklist and recommendations
- [docs/channels/WATCHTOWER_AND_INDEXER.md](../channels/WATCHTOWER_AND_INDEXER.md) – Optional watchtower and indexer
- [docs/channels/GAS_REPORT.md](../channels/GAS_REPORT.md) – Gas report from tests
- [docs/operations/CHANNEL_INCIDENT_RUNBOOK.md](../operations/CHANNEL_INCIDENT_RUNBOOK.md) – Pause, unpause, replace admin
