@arkade-os/boltz-swap
Version:
A production-ready TypeScript package that brings Boltz submarine-swaps to Arkade.
107 lines (105 loc) • 3.54 kB
JavaScript
import {
hasImpossibleSwapsFilter
} from "../../chunk-SJQJQO7P.js";
// 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`);
}
};
export {
SQLiteSwapRepository
};