> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zkterm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# zkCompression

> Light Protocol integration for Solana ZK Compression

## Overview

zkCompression integrates Light Protocol to enable ZK Compression on Solana, allowing you to compress SPL tokens and reduce storage costs by up to 5000x.

## How It Works

1. **Compress:** Regular SPL tokens are converted to compressed tokens stored in a Merkle tree state.

2. **Store:** Only the Merkle root is stored on-chain, dramatically reducing costs.

3. **Decompress:** Compressed tokens can be decompressed back to regular SPL tokens at any time.

## Cost Comparison

| Operation         | Regular SPL    | Compressed     |
| ----------------- | -------------- | -------------- |
| Token Account     | \~0.002 SOL    | \~0.00002 SOL  |
| Transfer          | \~0.000005 SOL | \~0.000001 SOL |
| Storage per token | \~165 bytes    | \~0.165 bytes  |

Compressed tokens are 5000x cheaper to create and 98% cheaper to transfer.

## Terminal Commands

### Show Help

```bash theme={null}
zk light help
```

Shows all available Light Protocol commands and cost savings info.

### Compress SOL

```bash theme={null}
zk light compress <amount> --from <wallet>
```

Convert regular SOL to compressed SOL. Compressed SOL has NO rent costs.

**Example:**

```bash theme={null}
zk light compress 0.1 --from mysol
```

### Decompress SOL

```bash theme={null}
zk light decompress <amount> --from <wallet>
```

Convert compressed SOL back to regular SOL.

**Example:**

```bash theme={null}
zk light decompress 0.1 --from mysol
```

### Create Compressed Token

```bash theme={null}
zk light create-token --decimals <n> --from <wallet>
```

Create a new compressed SPL token mint (5000x cheaper than standard).

**Example:**

```bash theme={null}
zk light create-token --decimals 9 --from mysol
```

### Mint Compressed Tokens

```bash theme={null}
zk light mint <token> <amount> <address> --from <wallet>
```

Mint compressed tokens to a specified address.

### Transfer Compressed Tokens

```bash theme={null}
zk light transfer <token> <amount> <address> --from <wallet>
```

Transfer compressed tokens to another address.

### Check Balance

```bash theme={null}
zk light balance <address> [token]
```

Check compressed token balances for an address.

### Account Info

```bash theme={null}
zk light account-info <address>
```

Get detailed compressed account information including all token balances.

### Airdrop

```bash theme={null}
zk light airdrop <token> <amount> --recipients <addr1,addr2,...> --from <wallet>
```

Mass token distribution to multiple recipients.

## View Token Portfolio

```bash theme={null}
zk tokens
```

Display unified portfolio view of all tokens (regular + compressed):

```
Token Portfolio
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Token           Balance      Type         Mint
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
USDC            1,234.56     Regular      EPjFWdd5...
USDC            500.00       [C]          EPjFWdd5...
SOL             10.5         Regular      So11111...
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[C] = Compressed Token
```

## API Reference

### Compress SOL

```bash theme={null}
POST /api/light/compress
Content-Type: application/json

{
  "amount": 0.1,
  "walletId": "wallet-uuid"
}
```

**Response:**

```json theme={null}
{
  "success": true,
  "txHash": "5abc...",
  "amount": 0.1,
  "costSavings": {
    "standardRent": 2039280,
    "compressedRent": 0
  }
}
```

### Decompress SOL

```bash theme={null}
POST /api/light/decompress
Content-Type: application/json

{
  "amount": 0.1,
  "walletId": "wallet-uuid"
}
```

**Response:**

```json theme={null}
{
  "success": true,
  "txHash": "6def...",
  "amount": 0.1
}
```

### Create Compressed Token

```bash theme={null}
POST /api/light/create-token
Content-Type: application/json

{
  "decimals": 9,
  "walletId": "wallet-uuid"
}
```

**Response:**

```json theme={null}
{
  "success": true,
  "mintAddress": "CompMint123...",
  "txHash": "7ghi..."
}
```

### Get Account Info

```bash theme={null}
GET /api/light/account-info/<address>
```

**Response:**

```json theme={null}
{
  "address": "YourAddress123...",
  "compressedBalance": "0.5",
  "tokenBalances": [
    {
      "mint": "EPjFWdd5...",
      "symbol": "USDC",
      "balance": "100.00"
    }
  ]
}
```

## Technical Details

### Light Protocol Integration

zkCompression uses the following Light Protocol packages:

```typescript theme={null}
import { Rpc, createRpc } from "@lightprotocol/stateless.js";
import { compress, decompress } from "@lightprotocol/compressed-token";
```

### Wallet Security

zkCompression implements wallet mutex locking and private key zeroization for security:

1. Mutex locking prevents concurrent wallet access
2. Private keys are zeroized from memory after use
3. Automatic retry polling for RPC indexer sync
4. Auto-create Associated Token Account during decompression

### RPC Configuration

zkCompression uses Helius RPC with Light Protocol extensions:

```typescript theme={null}
const connection = createRpc(
  process.env.HELIUS_RPC_URL,
  process.env.HELIUS_RPC_URL,  // Compression RPC
  process.env.HELIUS_RPC_URL   // Prover RPC
);
```

## Error Handling

| Error                  | Cause             | Solution                             |
| ---------------------- | ----------------- | ------------------------------------ |
| `Account not found`    | ATA doesn't exist | Auto-created during decompression    |
| `Insufficient balance` | Not enough tokens | Check balance with `zk tokens`       |
| `RPC indexer sync`     | Indexer lag       | Automatic retry polling handles this |

## Example: Full Compression Flow

```bash theme={null}
# 1. Check current balances
> zk tokens
USDC: 1000.00 (Regular)

# 2. Compress 0.1 SOL
> zk light compress 0.1 --from mysol
Compressing SOL...
Transaction: 5abc...

COMPRESSION SUCCESSFUL
  Amount:    0.1 SOL
  TxHash:    5abc...

  RENT SAVINGS:
  Standard:     2039280 lamports
  Compressed:   0 lamports (NO RENT!)

# 3. Check account info
> zk light account-info <your-address>

COMPRESSED ACCOUNT INFO
  Address:         <your-address>
  SOL Balance:     0.1 SOL

# 4. Decompress when needed
> zk light decompress 0.05 --from mysol
Decompressing SOL...
Transaction: 6def...

DECOMPRESSION SUCCESSFUL
  Amount:    0.05 SOL
  TxHash:    6def...
```
