Transaction Dashboard
Create a transaction dashboard to display locked funds and track transaction status using React Query and SQLite.
Introduction
Database Configuration
1. Create Data Directory
2. Set Up the Database
// src/lib/db.ts
import sqlite from 'better-sqlite3';
import path from 'path';
import { TransactionStatus } from './types';
const dbPath = process.env.SQLITE_DB_PATH || './data/escrow.db';
const db = sqlite(dbPath);
// Initialize database tables
db.exec(`
CREATE TABLE IF NOT EXISTS wallets (
address TEXT PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS transactions (
txHash TEXT PRIMARY KEY,
wallet TEXT NOT NULL,
amount INTEGER NOT NULL,
status TEXT NOT NULL,
timestamp INTEGER NOT NULL,
FOREIGN KEY (wallet) REFERENCES wallets(address)
);
`);
export function upsertWallet(address: string) {
db.prepare(`INSERT OR IGNORE INTO wallets(address) VALUES (?)`).run(address);
}
export function upsertTx(
txHash: string,
wallet: string,
amount: number,
status: TransactionStatus
) {
db.prepare(
`INSERT OR REPLACE INTO transactions(
txHash, wallet, amount, status, timestamp
) VALUES (?, ?, ?, ?, ?)`
).run(txHash, wallet, amount, status, Date.now());
}
export function getTxsByWallet(wallet: string) {
return db
.prepare(`SELECT * FROM transactions WHERE wallet = ? ORDER BY timestamp DESC`)
.all(wallet);
}
export function updateTxStatus(
txHash: string,
status: TransactionStatus
) {
db.prepare(
`UPDATE transactions SET status = ?, timestamp = ? WHERE txHash = ?`
).run(status, Date.now(), txHash);
}
export function getTxsByHash(txHash: string) {
return db
.prepare(`SELECT * FROM transactions WHERE txHash = ?`)
.get(txHash);
}React Query Setup
1. Create the React Query Provider
2. Update Root Layout
Transaction API Endpoint
Update Submit Transaction Endpoint
Update Transaction Hooks
Create the MyTransactions Component
Update the Home Page
Understanding the Transaction Dashboard
Data Flow
Testing Your Transaction Dashboard
Troubleshooting
Database Issues
Transaction Data Not Showing
Last updated
Was this helpful?

