#!/bin/bash

# CarbonSol deployment script
# This script builds and deploys the CarbonSol smart contracts to the Solana network

# Set environment variables
SOLANA_NETWORK="devnet" # Change to "mainnet-beta" for production
SOLANA_RPC_URL="https://api.devnet.solana.com"

# Build contracts
echo "Building CST token contract..."
cd ../contracts/cst_token
cargo build-bpf

echo "Building VCU token contract..."
cd ../vcu_token
cargo build-bpf

echo "Building DEX contract..."
cd ../dex
cargo build-bpf

# Deploy contracts
echo "Deploying contracts to $SOLANA_NETWORK..."

# Deploy CST token contract
echo "Deploying CST token contract..."
CST_PROGRAM_ID=$(solana program deploy \
  --url $SOLANA_RPC_URL \
  ../contracts/cst_token/target/deploy/cst_token.so)
echo "CST token program deployed with ID: $CST_PROGRAM_ID"

# Deploy VCU token contract
echo "Deploying VCU token contract..."
VCU_PROGRAM_ID=$(solana program deploy \
  --url $SOLANA_RPC_URL \
  ../contracts/vcu_token/target/deploy/vcu_token.so)
echo "VCU token program deployed with ID: $VCU_PROGRAM_ID"

# Deploy DEX contract
echo "Deploying DEX contract..."
DEX_PROGRAM_ID=$(solana program deploy \
  --url $SOLANA_RPC_URL \
  ../contracts/dex/target/deploy/carbonsol_dex.so)
echo "DEX program deployed with ID: $DEX_PROGRAM_ID"

# Save program IDs to a config file
echo "Saving program IDs to config file..."
cat > ../frontend/src/config.js << EOL
// CarbonSol program IDs
// Generated by deployment script

export const SOLANA_NETWORK = "$SOLANA_NETWORK";
export const SOLANA_RPC_URL = "$SOLANA_RPC_URL";
export const CST_PROGRAM_ID = "$CST_PROGRAM_ID";
export const VCU_PROGRAM_ID = "$VCU_PROGRAM_ID";
export const DEX_PROGRAM_ID = "$DEX_PROGRAM_ID";
EOL

echo "Deployment completed successfully!" 