# Local Deployment Guide (Much Cheaper!)

## 🚀 Deploy Locally with Anchor CLI

### **Cost Comparison:**
- **Solana Playground**: 1.8 SOL
- **Local Anchor CLI**: 0.01-0.05 SOL
- **Savings**: 95-98% cheaper!

## **Step 1: Install Prerequisites**

```bash
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env

# Install Solana CLI
sh -c "$(curl -sSfL https://release.solana.com/stable/install)"
export PATH="$HOME/.local/share/solana/install/active_release/bin:$PATH"

# Install Anchor CLI
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
avm install latest
avm use latest
```

## **Step 2: Setup Solana Wallet**

```bash
# Create new wallet
solana-keygen new

# Check wallet
solana address

# Switch to devnet
solana config set --url devnet

# Get devnet SOL
solana airdrop 2
```

## **Step 3: Deploy Contract**

```bash
# Build the program
anchor build

# Deploy to devnet
anchor deploy --provider.cluster devnet

# Get your Program ID
solana address -k target/deploy/solana_ico-keypair.json
```

## **Step 4: Update Configuration**

1. **Copy Program ID** from deployment
2. **Update Anchor.toml** with your Program ID
3. **Update frontend** with new Program ID
4. **Redeploy** if needed

## **Step 5: Test Deployment**

```bash
# Run tests
anchor test

# Check program
solana program show YOUR_PROGRAM_ID
```

## **Alternative Deployment Platforms:**

### **1. Helius (Recommended)**
- **Cost**: 0.5-1.0 SOL
- **Features**: Better than Playground
- **URL**: [helius.xyz](https://helius.xyz)

### **2. QuickNode**
- **Cost**: 0.3-0.8 SOL
- **Features**: Fast deployment
- **URL**: [quicknode.com](https://quicknode.com)

### **3. GenesysGo**
- **Cost**: 0.2-0.5 SOL
- **Features**: Budget option
- **URL**: [genesysgo.com](https://genesysgo.com)

## **Gas Optimization Tips:**

### **1. Use Efficient Data Types:**
```rust
// Good
pub amount: u64,        // 8 bytes
pub is_active: bool,    // 1 byte

// Avoid
pub amount: String,     // Variable size
pub timestamp: i64,     // 8 bytes (if needed)
```

### **2. Minimize Account Creation:**
```rust
// Use PDAs for derived addresses
let [presale_pda] = Pubkey::find_program_address(
    &[b"presale", token_mint.key().as_ref()],
    program_id
);
```

### **3. Batch Operations:**
```rust
// Process multiple operations in one transaction
// Reduces overall gas costs
```

## **Deployment Commands:**

```bash
# Build
anchor build

# Deploy to devnet
anchor deploy --provider.cluster devnet

# Deploy to mainnet
anchor deploy --provider.cluster mainnet

# Verify deployment
solana program show YOUR_PROGRAM_ID
```

## **Cost Breakdown:**

| Method | Cost | Savings |
|--------|------|---------|
| Solana Playground | 1.8 SOL | - |
| Local Anchor CLI | 0.01-0.05 SOL | 95-98% |
| Helius | 0.5-1.0 SOL | 44-72% |
| QuickNode | 0.3-0.8 SOL | 56-83% |

**Local deployment is the clear winner for cost savings!** 🎯

