A comprehensive collateral vault management system built on Solana blockchain using Anchor framework. This system allows users to deposit, withdraw, lock, and unlock collateral tokens while maintaining real-time synchronization between on-chain state and off-chain database.
-
Anchor Program (
programs/goquant_assignment/)- On-chain Solana program handling vault operations
- Manages collateral deposits, withdrawals, locking/unlocking
- Emits events for off-chain processing
-
Backend API (
backend/)- REST API server built with Actix-web
- PostgreSQL database for persistent storage
- Redis-like caching layer
- Real-time WebSocket updates
- Event listener for on-chain synchronization
-
Shared Library (
shared/)- Common data models and utilities
- Used by both program and backend
-
Database Migrations (
migrations/)- SQL migrations for PostgreSQL schema
- Managed with sqlx
User Request β API β Transaction Builder β Solana RPC β On-chain Program
β
Event Listener β WebSocket/Polling β Solana RPC β Events
β
Database Update β Cache Invalidation β WebSocket Broadcast β Frontend
goquant_assignment/
βββ Anchor.toml # Anchor configuration
βββ Cargo.toml # Workspace Cargo.toml
βββ package.json # Node.js dependencies
βββ tsconfig.json # TypeScript configuration
βββ rust-toolchain.toml # Rust toolchain version
βββ setup.sh # Development setup script
βββ README.md # This file
βββ examples/ # API usage examples
β βββ README.md
βββ migrations/ # Database migrations
β βββ 20260111110050_initial_schema.sql
βββ programs/ # Solana programs
β βββ goquant_assignment/
β βββ Cargo.toml
β βββ src/
β βββ lib.rs # Program entry point
β βββ error.rs # Program errors
β βββ instructions/ # Program instructions
β βββ states/ # Program state structs
βββ backend/ # Rust backend API server
β βββ Cargo.toml
β βββ src/
β βββ main.rs # Server entry point
β βββ config.rs # Configuration loading
β βββ database.rs # PostgreSQL database layer
β βββ cache.rs # In-memory caching
β βββ websocket.rs # WebSocket real-time updates
β βββ api/ # REST API endpoints
β βββ services/ # Business logic services
β βββ monitering/ # Monitoring and metrics
β βββ api_tests.rs # Integration tests
βββ shared/ # Shared Rust library
β βββ Cargo.toml
β βββ src/
β βββ lib.rs
β βββ models.rs # Shared data models
β βββ error.rs # Shared error types
β βββ utils.rs # Utility functions
βββ tests/ # TypeScript tests
β βββ goquant_assignment.ts
βββ target/ # Build artifacts
- Rust 1.70+
- Node.js 18+
- PostgreSQL 14+
- Solana CLI tools
- Anchor framework
For a quick development setup, run:
./setup.shThis script will:
- Check prerequisites
- Create environment configuration
- Install dependencies
- Build the Anchor program
- Run all tests
-
Clone the repository
git clone <repository-url> cd goquant_assignment
-
Install dependencies
# Install Anchor cargo install --git https://github.com/coral-xyz/anchor avm --locked --force avm install latest avm use latest # Install Solana CLI sh -c "$(curl -sSfL https://release.solana.com/v1.16.0/install)" # Install Node.js dependencies cd app && npm install
-
Database Setup
# Create PostgreSQL database createdb goquant_vault # Set environment variables export DATABASE_URL="postgresql://username:password@localhost/goquant_vault" export SOLANA_RPC_URL="https://api.devnet.solana.com" export PROGRAM_ID="A9JDc7TrKR5Qyot3W3t6UQaRz4CTgEURemuSUkWfP9hs"
-
Build and Deploy
# Build the Anchor program anchor build # Deploy to devnet anchor deploy # Run database migrations cd backend && cargo run --bin migrate # Start the backend cargo run
http://localhost:3000/api/v1
GET /healthPOST /api/v1/vault/initialize
Content-Type: application/json
{
"vault_pubkey": "string",
"owner_pubkey": "string",
"token_account": "string"
}GET /api/v1/vault/balance/{vault_pubkey}POST /api/v1/vault/deposit
Content-Type: application/json
{
"vault_pubkey": "string",
"amount": 1000000,
"tx_signature": "string"
}POST /api/v1/vault/withdraw
Content-Type: application/json
{
"vault_pubkey": "string",
"amount": 500000,
"tx_signature": "string"
}POST /api/v1/vault/lock
Content-Type: application/json
{
"vault_pubkey": "string",
"amount": 200000,
"tx_signature": "string"
}POST /api/v1/vault/unlock
Content-Type: application/json
{
"vault_pubkey": "string",
"amount": 200000,
"tx_signature": "string"
}POST /api/v1/transaction/deposit
Content-Type: application/json
{
"vault_pubkey": "string",
"user_pubkey": "string",
"amount": 1000000
}POST /api/v1/transaction/withdraw
Content-Type: application/json
{
"vault_pubkey": "string",
"user_pubkey": "string",
"amount": 500000
}Connect to /ws for real-time vault updates:
const ws = new WebSocket("ws://localhost:3000/ws");
ws.onmessage = (event) => {
const update = JSON.parse(event.data);
console.log("Vault update:", update);
};Update types:
balance_update: Vault balance changesdeposit: Deposit eventswithdrawal: Withdrawal eventslock: Collateral lockedunlock: Collateral unlockedtvl_update: Total Value Locked changes
cd backend
cargo testcd backend
cargo run --bin api_testsanchor test| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | Required |
SOLANA_RPC_URL |
Solana RPC endpoint | https://api.devnet.solana.com |
PROGRAM_ID |
Deployed program ID | Required |
HOST |
Server bind address | 0.0.0.0 |
PORT |
Server port | 3000 |
MAX_DB_CONNECTIONS |
Database connection pool size | 50 |
CACHE_TTL_SECONDS |
Cache TTL in seconds | 300 |
RECONCILIATION_INTERVAL_SECONDS |
Balance reconciliation interval | 3600 |
MONITORING_INTERVAL_SECONDS |
Monitoring interval | 60 |
GET /health- Service health statusGET /metrics- Prometheus metrics
- Vault balance reconciliation
- On-chain vs off-chain balance validation
- Transaction monitoring
- Performance metrics
- Alert system for discrepancies
CREATE TABLE vaults (
vault_pubkey VARCHAR PRIMARY KEY,
owner_pubkey VARCHAR NOT NULL,
token_account VARCHAR NOT NULL,
total_balance BIGINT NOT NULL DEFAULT 0,
available_balance BIGINT NOT NULL DEFAULT 0,
locked_balance BIGINT NOT NULL DEFAULT 0,
total_deposited BIGINT NOT NULL DEFAULT 0,
total_withdrawn BIGINT NOT NULL DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
vault_pubkey VARCHAR NOT NULL REFERENCES vaults(vault_pubkey),
tx_signature VARCHAR UNIQUE NOT NULL,
tx_type VARCHAR NOT NULL,
amount BIGINT,
fee BIGINT,
status VARCHAR NOT NULL DEFAULT 'pending',
block_time TIMESTAMP WITH TIME ZONE,
slot BIGINT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);- All transactions require valid signatures
- Balance validation on every operation
- Reconciliation checks for discrepancies
- Audit trail for all operations
- Rate limiting and monitoring
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or issues, please open a GitHub issue or contact the development team.