Backend API for the Saiv platform - a Web3 savings and group management platform with 100% gasless transactions.
- Users pay ZERO gas fees
- Registration, group creation, and all blockchain operations are FREE
- Backend wallet pays all gas fees
- See GASLESS_TRANSACTIONS.md for details
- Email Registration: Passwordless authentication via Web3Auth
- Wallet Registration: Connect with existing EOA wallets
- Automatic wallet creation (main + savings wallets)
- JWT authentication
- Create savings groups with pool addresses
- Join/leave groups
- Member management (admin/member roles)
- Configurable group settings (max members, min contribution)
- Check ETH/token balances
- Send ETH and ERC-20 tokens
- Transaction history
- Gas estimation
- Multi-token support
- Real-time backend wallet balance
- Gas cost estimates
- Service status checks
npm installcp .env.example .envEdit .env:
# Database
MONGODB_URI=mongodb://localhost:27017/saiv_platform
# Security
JWT_SECRET=your_secret_key_here_at_least_32_characters
# Blockchain (Polygon Mumbai Testnet)
RPC_URL=https://rpc-mumbai.maticvigil.com
# Backend Wallet (Pays all gas fees)
ADMIN_PRIVATE_KEY=0xYourPrivateKeyHere
# Deployed Contract
ADDRESS_MANAGER_CONTRACT=0xYourContractAddressHeremongodcd ../contract
npx hardhat run scripts/deploy.js --network polygonMumbaiCopy the contract address to .env.
Get testnet MATIC from: https://faucet.polygon.technology/
Send to your backend wallet address.
npm run devYou should see:
β
GASLESS SERVICE ENABLED - Users pay NO gas fees
- Registration: FREE (backend pays gas)
- Create Group: FREE (backend pays gas)
- Join Group: FREE (backend pays gas)
Backend wallet balance: 5.0 MATIC
Server is running on port 3001
# Register with Email (GASLESS)
POST /api/auth/register/email
{
"email": "[email protected]"
}
# Register with Wallet (GASLESS)
POST /api/auth/register/wallet
{
"eoaAddress": "0x742d35Cc6634C0532925a3b8D0Ed62FDa2c0e7A6"
}
# Get Profile
GET /api/auth/profile
Authorization: Bearer <jwt_token># Create Group (GASLESS)
POST /api/groups
Authorization: Bearer <jwt_token>
{
"name": "Monthly Savings",
"description": "Save $100/month",
"paymentWindowDuration": 2592000
}
# Get User Groups
GET /api/groups
Authorization: Bearer <jwt_token>
# Join Group (GASLESS)
POST /api/groups/:groupId/join
Authorization: Bearer <jwt_token># Get Balance
GET /api/wallet/balance
Authorization: Bearer <jwt_token>
# Send ETH
POST /api/wallet/send-eth
Authorization: Bearer <jwt_token>
{
"to": "0x742d35Cc6634C0532925a3b8D0Ed62FDa2c0e7A6",
"amount": "0.1"
}
# Get Token Balance
GET /api/wallet/token-balance?tokenAddress=0x...
Authorization: Bearer <jwt_token># Check Gasless Status
GET /api/gas/status
# Get Backend Wallet Balance
GET /api/gas/backend-wallet
Authorization: Bearer <jwt_token>
# Get Gas Estimates
GET /api/gas/estimatesbackend/
βββ src/
β βββ config/
β β βββ database.js # MongoDB connection
β β βββ web3auth.js # Web3Auth config
β βββ controllers/
β β βββ authController.js # User registration/login
β β βββ groupController.js # Group management
β β βββ walletController.js # Wallet operations
β β βββ gasController.js # Gas monitoring
β βββ middleware/
β β βββ auth.js # JWT authentication
β β βββ validation.js # Input validation
β βββ models/
β β βββ User.js # User schema
β β βββ Group.js # Group schema
β βββ routes/
β β βββ auth.js # Auth routes
β β βββ groups.js # Group routes
β β βββ wallet.js # Wallet routes
β β βββ gas.js # Gas routes
β β βββ index.js # Route aggregator
β βββ services/
β β βββ gaslessService.js # π Gasless transaction handler
β β βββ contractService.js # Smart contract interactions
β β βββ walletService.js # Wallet operations
β βββ app.js # Express app setup
β βββ server.js # Server entry point
βββ tests/ # Test files
βββ .env.example # Environment template
βββ GASLESS_TRANSACTIONS.md # Gasless guide
βββ README.md # This file
βββ package.json # Dependencies
- Framework: Express.js
- Database: MongoDB + Mongoose
- Authentication: JWT + Web3Auth
- Blockchain: Ethers.js v6
- Security: Helmet, CORS
- Validation: Express-validator
The backend interacts with deployed smart contracts:
- AddressManager: Creates user wallets and group pools
- UserWallet: Individual wallet contracts (main + savings)
- GroupPool: Group savings pool contracts
All contract interactions are gasless - the backend wallet pays all gas fees.
# Run in development mode
npm run dev
# Run in production mode
npm start
# Run tests
npm testPer Operation:
- Registration: ~$0.01
- Create Group: ~$0.02
- Join Group: ~$0.002
Monthly Estimates:
- 1,000 users: ~$10
- 500 groups: ~$10
- 5,000 joins: ~$10
- Total: ~$30/month
- Use Layer 2: Polygon, Arbitrum, Optimism
- Batch Operations: Combine multiple operations when possible
- Off-peak Times: Execute transactions when gas is low
- Monitor Usage: Set up alerts for unusual activity
-
Private Key Security
- Never commit
.envto version control - Use secrets management in production
- Rotate keys regularly
- Never commit
-
Rate Limiting
- Limit registration attempts per IP
- Prevent spam group creation
- Monitor unusual patterns
-
Input Validation
- Validate all user inputs
- Sanitize email addresses
- Check wallet address formats
-
Monitoring
- Track backend wallet balance
- Alert on low balance
- Log all gasless transactions
- Set
NODE_ENV=production - Use strong
JWT_SECRET - Configure production MongoDB
- Deploy contracts to mainnet
- Fund backend wallet with sufficient MATIC
- Set up wallet balance monitoring
- Configure rate limiting
- Enable HTTPS
- Set up error tracking (Sentry, etc.)
- Configure backup strategy
- Backend: AWS EC2, DigitalOcean, or Heroku
- Database: MongoDB Atlas
- Secrets: AWS Secrets Manager or Vault
- Monitoring: CloudWatch, DataDog
- Blockchain: Alchemy or Infura RPC
Cause: Backend wallet not configured
Fix:
- Add
ADMIN_PRIVATE_KEYto.env - Fund the wallet with MATIC
- Restart server
Cause: Backend wallet balance too low
Fix:
- Check balance:
GET /api/gas/backend-wallet - Send MATIC to backend wallet
- Verify transaction on block explorer
Cause: Missing or invalid contract address
Fix:
- Deploy contracts:
cd ../contract && npx hardhat run scripts/deploy.js - Add contract address to
.env - Restart server
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
ISC
For questions or issues:
- Check GASLESS_TRANSACTIONS.md
- Review API documentation above
- Check server logs for errors
- Monitor gas usage via
/api/gas/*endpoints
Built with β€οΈ for the Web3 community