Switch is a DeFi application on the Ethereum Sepolia Testnet. It allows users to cryptographically lock Ether (ETH) for a fixed duration, preventing premature withdrawals.
Unlike standard dApps that query the blockchain directly (which is slow), Switch utilizes a custom Python Indexer and PostgreSQL database to provide instant data retrieval, a "Gasless" dashboard experience, and real-time updates.
- Frontend (Vercel): https://switch-smart-vault.vercel.app
- Backend API (Render): https://switch-api-erex.onrender.com/docs
-
Smart Vaults: Create multiple savings locks with unique titles (e.g., "Car Fund").
-
Real-Time Status: Live countdown timers showing exactly when funds become available.
-
Hybrid Architecture:
-
On-Chain Security: Funds are held by a Solidity Smart Contract.
-
Off-Chain Speed: UI is powered by a high-performance Python backend.
-
-
Custom Indexer: A background worker that watches the blockchain and syncs events to a local database in real-time.
-
SIWE Authentication: Secure "Sign-In With Ethereum" using cryptographic signatures (Nonces) and JWTs—no passwords required.
-
Optimistic UI: Interface updates almost instantly while transactions confirm in the background.
-
Strict Enforcement: No admin keys. No backdoors. You cannot withdraw until the timer hits zero (or if you decide to use the emergency withdrawal function with a 10% penalty)
Frontend:
- Framework: React (Vite)
- Language: TypeScript
- State Management: Zustand
- Styling: SCSS / Glassmorphism UI
- Blockchain Interaction: Ethers.js (v6)
Backend / Smart Contract:
- Smart Contract: Solidity (v0.8.0+), Framework (Brownie (Eth-Brownie))
- API Framework: FastAPI (Python)
- Database: PostgreSQL (via Supabase)
- ORM: SQLAlchemy (Async)
- Blockchain Interface: Web3.py
- Indexer: Custom multi-threaded event listener
If you want to run this code on your own machine:
- Node.js & npm
- Python 3 & pip
eth-brownieinstalled globally- Docker / Docker Compose
This will spin up the Frontend, Backend, and a local PostgreSQL database automatically.
git clone https://github.com/aluyapeter/switch-smart-vault.git
cd switch-smart-vaultThe backend requires a database connection and an RPC URL to talk to the blockchain. We use Docker to spin up a local PostgreSQL instance instantly.
cd backend
# 1. Create a virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# 2. Install dependencies
pip install -r requirements.txt
# 3. Configure Environment Variables
# Create a .env file in the /backend folder and add:
# Database
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DB=switch_db
DATABASE_URL="postgresql+asyncpg://user:password@localhost/switch_db"
# Blockchain (Sepolia)
RPC_URL="https://eth-sepolia.g.alchemy.com/v2/YOUR_API_KEY"
PRIVATE_KEY="your_wallet_private_key" # Needed for Brownie deployment only
# Auth
JWT_SECRET="supersecretkey"
ALGORITHM="HS256"
# Start the database in the background
docker-compose up -dCompile and deploy the contracts to the testnet.
cd backend
# Compile Contracts
brownie compile
# Deploy to Sepolia (Requires .env with PRIVATE_KEY and WEB3_INFURA_PROJECT_ID)
brownie run deploy_sepolia --network sepoliaRun the FastAPI server and Indexer locally.
# ensure DATABASE_URL matches your Docker config (e.g., postgresql+asyncpg://user:password@localhost/switch_db)
# Run the API
uvicorn main:app --reloadAPI is live at http://localhost:8000
cd ../frontend
# 1. Install dependencies
npm install
# 2. Configure Environment Variables (.env)
VITE_API_URL="http://localhost:8000"
# 3. Run the development server
npm run dev -- --port 3000App is live at http://localhost:3000
Since this is running on the Sepolia Testnet, you do not need real money to test it. Follow these steps:
Install the MetaMask browser extension.
You need "fake" ETH to pay for gas fees.
- Go to Alchemy Sepolia Faucet or Google Cloud Web3 Faucet.
- Enter your wallet address and claim ETH.
- Open the App.
- Click "Connect MetaMask".
- Under "Create New Lock", enter:
- Goal Name: "Test Savings"
- Amount: 0.01 (ETH)
- Time: 1 (Days)
- Click "Lock Funds" and approve the transaction in MetaMask.
- Wait for the transaction to confirm. A green "Active Savings" card will appear.
- Watch the countdown reach 0.
- When it says "Ready to Withdraw!", a red Withdraw button will appear.
- Click it to return the ETH to your wallet.
- An emergency withdrawal can be done with a 10% penalty if you want to withdraw before the unlock date
"Authentication Failed" on Mobile: Ensure you are using the browser inside the MetaMask app. Standard mobile browsers (Chrome/Safari) cannot connect to your wallet unless you use WalletConnect.
"Locks not showing up": The dashboard reads from the database. If you minted a lock directly on Etherscan or before the Indexer was deployed, it might not appear on the dashboard. Create a new lock to see the system in action.
Author: Peter Aluya License: MIT