Back
Version 19
12/2/2024
import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
const SCHEMA_VERSION = 9; // Increased due to transaction handling improvement
const KEY = new URL(import.meta.url).pathname.split("/").at(-1);
const TABLE_NAME = `${KEY}_wallets_${SCHEMA_VERSION}`;
// accounts start with this much money
const startingBalance = 100;
interface ApiResponse {
status: number;
body: any;
headers?: Record<string, string>;
}
function normalizeAddress(address: string): string {
return address.toLowerCase();
}
async function initializeDatabase() {
await sqlite.execute(`
CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (
address TEXT PRIMARY KEY,
balance INTEGER NOT NULL DEFAULT 0
)
`);
}
async function getBalance(address: string): Promise<number> {
const normalizedAddress = normalizeAddress(address);
const result = await sqlite.execute(
`SELECT balance FROM ${TABLE_NAME} WHERE address = ?`,
[normalizedAddress],
);
if (result.rows.length === 0) {
await incrementBalance(normalizedAddress, startingBalance);
jamiedubs-glifbux.web.val.run
Updated: December 3, 2024