A secure, non-custodial collateral vault management system for decentralized perpetual futures exchanges built on Solana.
This system provides a foundation for managing user collateral (USDT) in program-controlled vaults, enabling secure deposits, withdrawals, and cross-program invocations for trading operations.
-
Anchor Program (
programs/collateral_vault/)- Solana smart contract for vault management
- PDA-based vault accounts
- SPL Token integration
- Cross-program invocation support
-
Rust Backend Service (
backend/)- Vault management service
- Balance tracking and reconciliation
- REST API endpoints
- WebSocket streams for real-time updates
- Database integration (PostgreSQL)
-
Database Schema
- Vault records
- Transaction history
- Balance snapshots
- ✅ Secure PDA-based vault accounts
- ✅ Deposit and withdraw collateral
- ✅ Lock/unlock collateral for positions
- ✅ Transfer collateral between vaults
- ✅ Real-time balance tracking
- ✅ Transaction history
- ✅ Total Value Locked (TVL) tracking
- ✅ WebSocket support for real-time updates
- Rust 1.75+
- Anchor 0.30.1+
- Solana CLI tools (including
solana-test-validatorandspl-token) - PostgreSQL (optional for localnet, required for backend)
- Node.js (for tests)
- Clone the repository:
git clone <repository-url>
cd GoQuant- Install dependencies:
# Install Anchor
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
avm install latest
avm use latest
# Build the program
anchor build- Set up the database:
# Create PostgreSQL database
createdb vault_db
# Set environment variables
export DATABASE_URL="postgresql://localhost/vault_db"
export SOLANA_RPC_URL="https://api.devnet.solana.com"
export PROGRAM_ID="<your-program-id>"
export USDT_MINT="<usdt-mint-address>"- Run migrations:
cd backend
cargo run --bin migrate- Start the backend server:
cd backend
cargo runThe system can run entirely on Solana localnet with fake SOL and fake USDT tokens. This uses REAL Solana programs, REAL PDAs, and REAL transactions - it's not a mock.
- Setup localnet environment:
./scripts/setup_localnet.shThis script will:
- Start
solana-test-validatoron port 8899 - Create a fake USDT SPL token mint
- Fund test accounts with fake SOL and USDT
- Generate
.env.localnetconfiguration file
- Deploy the program:
anchor deploy --provider.cluster localnet- Start the backend (in localnet mode):
cd backend
source ../.env.localnet
cargo run- Run the demo:
./scripts/demo_localnet.sh- ✅ Unlimited fake SOL - No need to worry about fees
- ✅ Fake USDT mint - Locally deployed SPL token
- ✅ Real transactions - Uses actual Solana programs
- ✅ Real PDAs - Same derivation as mainnet/devnet
- ✅ Real CPIs - Actual cross-program invocations
- ✅ Identical logic - No code changes, same security
After running setup_localnet.sh, you'll have:
.env.localnet- Configuration file with all addresses.localnet_usdt_mint.json- Fake USDT mint keypair.localnet_test_user.json- Test user keypair.validator.pid- Validator process ID
./scripts/stop_localnet.shOr manually:
kill $(cat .validator.pid)| Feature | Localnet | Devnet/Mainnet |
|---|---|---|
| SOL | Unlimited fake | Real (limited) |
| USDT | Fake mint | Real USDT mint |
| RPC | http://127.0.0.1:8899 |
Public RPC |
| Speed | Instant | Network dependent |
| Cost | Free | Transaction fees |
| Logic | Identical | Identical |
Important: The Anchor program code is identical for localnet and mainnet. Only the mint address changes, which is passed as a parameter (no code changes needed).
curl -X POST http://localhost:3000/vault/initialize \
-H "Content-Type: application/json" \
-d '{"user": "<user-public-key>"}'curl -X POST http://localhost:3000/vault/deposit \
-H "Content-Type: application/json" \
-d '{
"user": "<user-public-key>",
"amount": 1000000000
}'curl -X POST http://localhost:3000/vault/withdraw \
-H "Content-Type: application/json" \
-d '{
"user": "<user-public-key>",
"amount": 500000000
}'curl http://localhost:3000/vault/balance/<user-public-key>curl http://localhost:3000/vault/transactions/<user-public-key>curl http://localhost:3000/vault/tvl| Method | Endpoint | Description |
|---|---|---|
| POST | /vault/initialize |
Initialize a new vault |
| POST | /vault/deposit |
Deposit collateral |
| POST | /vault/withdraw |
Withdraw collateral |
| GET | /vault/balance/:user |
Get vault balance |
| GET | /vault/transactions/:user |
Get transaction history |
| GET | /vault/tvl |
Get total value locked |
| GET | /ws |
WebSocket connection |
anchor testcd backend
cargo test- All vault operations are secured with PDA-based accounts
- Only vault owners can withdraw
- Only authorized programs can lock/unlock collateral
- Integer overflow/underflow protection
- Atomic state updates
See docs/ for detailed documentation:
MIT