UNPKG

@arkade-os/boltz-swap

Version:

A production-ready TypeScript package that brings Boltz submarine-swaps to Arkade.

136 lines (132 loc) 4.83 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/repositories/sqlite/index.ts var sqlite_exports = {}; __export(sqlite_exports, { SQLiteSwapRepository: () => SQLiteSwapRepository }); module.exports = __toCommonJS(sqlite_exports); // src/repositories/swap-repository.ts function hasImpossibleSwapsFilter(filter) { if (!filter) return false; return Array.isArray(filter.id) && filter.id.length === 0 || Array.isArray(filter.status) && filter.status.length === 0 || Array.isArray(filter.type) && filter.type.length === 0; } // src/repositories/sqlite/swap-repository.ts var SQLiteSwapRepository = class { constructor(executor) { this.executor = executor; } version = 1; initPromise = null; // ── Lifecycle ────────────────────────────────────────────────────── ensureInit() { if (!this.initPromise) { this.initPromise = this.init(); } return this.initPromise; } async init() { await this.executor.run(` CREATE TABLE IF NOT EXISTS boltz_swaps ( id TEXT PRIMARY KEY, type TEXT NOT NULL, status TEXT NOT NULL, created_at INTEGER NOT NULL, data TEXT NOT NULL ) `); await this.executor.run( `CREATE INDEX IF NOT EXISTS idx_boltz_swaps_status ON boltz_swaps(status)` ); await this.executor.run( `CREATE INDEX IF NOT EXISTS idx_boltz_swaps_type ON boltz_swaps(type)` ); await this.executor.run( `CREATE INDEX IF NOT EXISTS idx_boltz_swaps_created_at ON boltz_swaps(created_at)` ); } async [Symbol.asyncDispose]() { } // ── Swap operations ──────────────────────────────────────────────── async saveSwap(swap) { await this.ensureInit(); await this.executor.run( `INSERT OR REPLACE INTO boltz_swaps (id, type, status, created_at, data) VALUES (?, ?, ?, ?, ?)`, [swap.id, swap.type, swap.status, swap.createdAt, JSON.stringify(swap)] ); } async deleteSwap(id) { await this.ensureInit(); await this.executor.run(`DELETE FROM boltz_swaps WHERE id = ?`, [id]); } async getAllSwaps(filter) { await this.ensureInit(); if (hasImpossibleSwapsFilter(filter)) return []; const conditions = []; const params = []; if (filter) { if (filter.id !== void 0) { if (Array.isArray(filter.id)) { conditions.push(`id IN (${filter.id.map(() => "?").join(",")})`); params.push(...filter.id); } else { conditions.push(`id = ?`); params.push(filter.id); } } if (filter.status !== void 0) { if (Array.isArray(filter.status)) { conditions.push(`status IN (${filter.status.map(() => "?").join(",")})`); params.push(...filter.status); } else { conditions.push(`status = ?`); params.push(filter.status); } } if (filter.type !== void 0) { if (Array.isArray(filter.type)) { conditions.push(`type IN (${filter.type.map(() => "?").join(",")})`); params.push(...filter.type); } else { conditions.push(`type = ?`); params.push(filter.type); } } } let sql = `SELECT data FROM boltz_swaps`; if (conditions.length > 0) { sql += ` WHERE ${conditions.join(" AND ")}`; } if (filter?.orderBy === "createdAt") { const direction = filter.orderDirection === "desc" ? "DESC" : "ASC"; sql += ` ORDER BY created_at ${direction}`; } const rows = await this.executor.all(sql, params); return rows.map((row) => JSON.parse(row.data)); } async clear() { await this.ensureInit(); await this.executor.run(`DELETE FROM boltz_swaps`); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SQLiteSwapRepository });