Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions backend/.env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
NODE_ENV=production
PORT=3000

# Enable local development initialization
INIT_LOCAL_DEV=true

# JWT Secrets - IMPORTANT: Use secure random strings in production
JWT_ACCESS_SECRET=dd01992053448ebc129e4f12366596351e4c2d95ffe9b798104fcc80eff11c28
JWT_REFRESH_SECRET=67bf4928a666ac3d46e991398a0a7e5aa2ec15ca206316b1126f31d2247498d4
Expand All @@ -14,9 +17,10 @@ REDIS_URL=redis://redis:6379
# Database - Use Docker service name and container port
DATABASE_URL=postgresql://postgres:password@postgres:5432/boxmeout_dev?schema=public

# Stellar/Soroban
STELLAR_NETWORK=testnet
STELLAR_SOROBAN_RPC_URL=https://soroban-testnet.stellar.org
# Stellar/Soroban - Local standalone network
STELLAR_NETWORK=standalone
STELLAR_HORIZON_URL=http://soroban:8000
STELLAR_SOROBAN_RPC_URL=http://soroban:8001

# Contract Addresses (set after deployment)
MARKET_CONTRACT_ADDRESS=
Expand Down
197 changes: 197 additions & 0 deletions backend/DOCKER_LOCAL_DEV.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
# Docker Compose Local Development Setup

This document describes the local development environment with Soroban integration.

## Overview

The Docker Compose setup now includes:
- **PostgreSQL** - Main database
- **PostgreSQL Test** - Test database
- **Redis** - Caching and session storage
- **Soroban Node** - Local Stellar blockchain with Soroban smart contracts
- **Backend** - Node.js API server with auto-initialization

## Services

### Soroban (Stellar Quickstart)
- **Image**: `stellar/quickstart:testing`
- **Ports**:
- `8000` - Horizon API (Stellar REST API)
- `8001` - Soroban RPC (Smart contract interaction)
- `11626` - Stellar Core peer port
- `11625` - Stellar Core admin port
- **Network**: Standalone (local development)

### Backend
- **Auto-initialization**: On startup, the backend will:
1. Wait for Soroban node to be ready
2. Generate a deployer identity
3. Deploy test USDC token
4. Build and deploy all 5 Soroban contracts (Oracle, Factory, Treasury, AMM, Market)
5. Initialize contracts with proper configuration
6. Run database migrations
7. Seed database with test markets and users

## Quick Start

### 1. Start all services
```bash
cd backend
docker-compose up -d
```

### 2. View logs
```bash
# All services
docker-compose logs -f

# Specific service
docker-compose logs -f backend
docker-compose logs -f soroban
```

### 3. Check initialization status
The backend container will show initialization progress in the logs:
```bash
docker-compose logs -f backend | grep INIT
```

### 4. Access services
- **Backend API**: http://localhost:3000
- **Horizon API**: http://localhost:8000
- **Soroban RPC**: http://localhost:8001
- **PostgreSQL**: localhost:5434
- **Redis**: localhost:6379

## Test Credentials

After seeding, you can login with:
- **Email**: `admin@boxmeout.com`
- **Password**: `password123`

Other test users:
- `john@example.com` / `password123`
- `sarah@example.com` / `password123`
- `mike@example.com` / `password123`

## Contract Addresses

After initialization, contract addresses are saved to `/app/.env.local` inside the container.

To view them:
```bash
docker-compose exec backend cat /app/.env.local
```

## Manual Contract Interaction

### Using Stellar CLI from host
```bash
# Install stellar CLI locally
curl -L https://github.com/stellar/stellar-cli/releases/download/v21.5.0/stellar-cli-21.5.0-x86_64-unknown-linux-musl.tar.gz | tar xz

# Interact with contracts
./stellar contract invoke \
--id <CONTRACT_ID> \
--network standalone \
--rpc-url http://localhost:8001 \
-- <function_name> <args>
```

### Using Stellar CLI from container
```bash
docker-compose exec backend stellar contract invoke \
--id <CONTRACT_ID> \
--network standalone \
--rpc-url http://soroban:8001 \
-- <function_name> <args>
```

## Troubleshooting

### Backend fails to start
Check if Soroban is healthy:
```bash
curl http://localhost:8000/
```

### Contracts not deploying
1. Check if contracts are built:
```bash
docker-compose exec backend ls -la /app/contracts/contracts/boxmeout/target/wasm32-unknown-unknown/release/
```

2. Rebuild contracts manually:
```bash
docker-compose exec backend bash
cd /app/contracts
cargo build --release --target wasm32-unknown-unknown
```

### Reset everything
```bash
docker-compose down -v
docker-compose up -d
```

## Environment Variables

Key environment variables in `.env.docker`:
- `INIT_LOCAL_DEV=true` - Enables auto-initialization
- `STELLAR_NETWORK=standalone` - Uses local Soroban node
- `STELLAR_HORIZON_URL=http://soroban:8000` - Horizon API endpoint
- `STELLAR_SOROBAN_RPC_URL=http://soroban:8001` - Soroban RPC endpoint

## Development Workflow

1. **Start services**: `docker-compose up -d`
2. **Make code changes**: Edit files locally
3. **Rebuild backend**: `docker-compose up -d --build backend`
4. **View logs**: `docker-compose logs -f backend`
5. **Run tests**: `docker-compose exec backend npm test`
6. **Stop services**: `docker-compose down`

## Database Management

### Run migrations
```bash
docker-compose exec backend npx prisma migrate dev
```

### Seed database
```bash
docker-compose exec backend npx prisma db seed
```

### Access database
```bash
docker-compose exec postgres psql -U postgres -d boxmeout_dev
```

## Backup and Restore

The `db-backup` service automatically backs up the database every 6 hours.

### Manual backup
```bash
docker-compose exec db-backup /scripts/backup.sh
```

### Restore from backup
```bash
docker-compose exec db-backup /scripts/restore.sh <backup_file>
```

## Production Deployment

For production, disable auto-initialization:
1. Set `INIT_LOCAL_DEV=false` in `.env.docker`
2. Use testnet or mainnet Soroban RPC URLs
3. Deploy contracts manually using `../deploy.sh`
4. Update contract addresses in `.env`

## Additional Resources

- [Stellar Documentation](https://developers.stellar.org/)
- [Soroban Documentation](https://soroban.stellar.org/)
- [Stellar Quickstart](https://github.com/stellar/quickstart)
38 changes: 34 additions & 4 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ FROM node:20-alpine
# Set working directory
WORKDIR /app

# Install OpenSSL for Prisma (use openssl3 for modern Alpine)
RUN apk add --no-cache openssl
# Install system dependencies
# - OpenSSL for Prisma
# - curl for health checks and API calls
# - bash for init scripts
# - cargo/rust for building Soroban contracts (if needed)
RUN apk add --no-cache \
openssl \
curl \
bash \
git \
build-base

# Install Stellar CLI
RUN curl -L https://github.com/stellar/stellar-cli/releases/download/v21.5.0/stellar-cli-21.5.0-x86_64-unknown-linux-musl.tar.gz | tar xz -C /usr/local/bin

# Copy package files
COPY package*.json ./
Expand All @@ -26,8 +38,26 @@ COPY . .
# Build the application
RUN npm run build

# Copy initialization script
COPY scripts/init-local-dev.sh /app/scripts/init-local-dev.sh
RUN chmod +x /app/scripts/init-local-dev.sh

# Expose port
EXPOSE 3000

# Start the application
CMD ["npm", "start"]
# Create entrypoint script
RUN echo '#!/bin/bash\n\
set -e\n\
\n\
# Run initialization if INIT_LOCAL_DEV is set\n\
if [ "$INIT_LOCAL_DEV" = "true" ]; then\n\
echo "Running local development initialization..."\n\
/app/scripts/init-local-dev.sh\n\
fi\n\
\n\
# Start the application\n\
exec npm start\n\
' > /app/entrypoint.sh && chmod +x /app/entrypoint.sh

# Use custom entrypoint
ENTRYPOINT ["/app/entrypoint.sh"]
24 changes: 24 additions & 0 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
version: '3.8'

services:
soroban:
image: stellar/quickstart:testing
container_name: boxmeout_soroban
command: --standalone --enable-soroban-rpc
ports:
- "8000:8000" # Horizon API
- "8001:8001" # Soroban RPC
- "11626:11626" # Stellar Core peer port
- "11625:11625" # Stellar Core admin port
environment:
- NETWORK_PASSPHRASE=Standalone Network ; February 2017
- SOROBAN_RPC_ENABLED=true
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/"]
interval: 10s
timeout: 5s
retries: 10
start_period: 30s
networks:
- boxmeout_network
restart: unless-stopped

backend:
build: .
container_name: boxmeout_backend
Expand All @@ -13,6 +35,8 @@ services:
condition: service_healthy
redis:
condition: service_healthy
soroban:
condition: service_healthy
networks:
- boxmeout_network
restart: unless-stopped
Expand Down
Loading
Loading