- Bun runtime installed
- PostgreSQL database server
- Node.js (for npm packages if needed)
- Install dependencies:
bun install- Install additional PostgreSQL dependencies:
bun add pg @types/pg- Setup PostgreSQL Database:
Create a new database:
createdb fum_vault_dbOr using psql:
CREATE DATABASE fum_vault_db;- Configure Environment Variables:
Copy the .env.example to .env:
cp .env.example .envUpdate the .env file with your database credentials:
# Server Configuration
PORT=3002
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=fum_vault_db
DB_USER=postgres
DB_PASSWORD=your_password_here
# Blockchain Configuration
SCROLL_SEPOLIA_RPC_URL=https://sepolia-rpc.scroll.io/Development mode (with hot reload):
bun run devProduction mode:
bun run startThe server will:
- Connect to PostgreSQL
- Create the necessary tables if they don't exist
- Start the HTTP server on port 3002 (or the port specified in .env)
The application will automatically create the following table:
CREATE TABLE vaults (
id SERIAL PRIMARY KEY,
vault_id INTEGER UNIQUE NOT NULL,
vault_title VARCHAR(255) NOT NULL,
commitment_message TEXT NOT NULL,
owner_address VARCHAR(42) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
metadata TEXT,
tx_hash VARCHAR(66)
);fum-services/
├── src/
│ ├── app.ts # Main application setup
│ ├── index.ts # Server entry point
│ ├── config/
│ │ ├── Database.ts # Database connection & initialization
│ │ ├── Contracts.ts # Smart contract configuration
│ │ └── ViemClient.ts # Blockchain client
│ ├── types/
│ │ └── vault.types.ts # TypeScript interfaces
│ ├── models/
│ │ └── vaults.model.ts # Database operations
│ ├── services/
│ │ └── vault.service.ts # Business logic
│ ├── routes/
│ │ └── vault.routes.ts # API endpoints
│ └── schema/
│ └── vault.schema.ts # Validation schemas
├── package.json
├── tsconfig.json
└── .env
Once the server is running, you can test it:
Health Check:
curl http://localhost:3002/Create a Vault:
curl -X POST http://localhost:3002/api/vaults \
-H "Content-Type: application/json" \
-d '{
"vault_id": 1,
"vault_title": "My First Vault",
"commitment_message": "I am committing to hold this vault for the long term",
"owner_address": "0x742d35Cc6634C0532925a3b844Bc9e7595f6E123",
"metadata": {
"aiAgentAdvice": "Based on market analysis, this is a good time to lock.",
"userNote": "Long-term hold"
},
"tx_hash": "0x123abc..."
}'Get All Vaults:
curl http://localhost:3002/api/vaultsGet Vault by ID:
curl http://localhost:3002/api/vaults/1Get Vaults by Owner:
curl http://localhost:3002/api/vaults/user/0x742d35Cc6634C0532925a3b844Bc9e7595f6E123Update a Vault:
curl -X PUT http://localhost:3002/api/vaults/1 \
-H "Content-Type: application/json" \
-d '{
"vault_title": "Updated Vault Title",
"commitment_message": "Updated commitment message"
}'Delete a Vault:
curl -X DELETE http://localhost:3002/api/vaults/1Get Vault Statistics:
curl http://localhost:3002/api/vaults/stats- ✅ PostgreSQL database integration
- ✅ RESTful API endpoints
- ✅ Input validation with Zod
- ✅ Error handling
- ✅ Pagination support
- ✅ Metadata storage for AI agent insights
- ✅ Automatic timestamp tracking (created_at, updated_at)
- ✅ CRUD operations for vaults
- ✅ Vault statistics endpoint
- ✅ Case-insensitive address storage
- Frontend Integration: Update your frontend to call these API endpoints when creating vaults
- Blockchain Event Listener: Consider adding a service to listen to blockchain events and automatically update vault data
- Background Jobs: Add a cron job to periodically check and update vault information
- Monitoring: Add logging and monitoring for production deployment
- API Rate Limiting: Consider adding rate limiting for production use