UNPKG

@arkade-os/boltz-swap

Version:

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

142 lines (137 loc) 4.78 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/realm/index.ts var realm_exports = {}; __export(realm_exports, { BoltzRealmSchemas: () => BoltzRealmSchemas, BoltzSwapSchema: () => BoltzSwapSchema, RealmSwapRepository: () => RealmSwapRepository }); module.exports = __toCommonJS(realm_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/realm/swap-repository.ts var RealmSwapRepository = class { constructor(realm) { this.realm = realm; } version = 1; // ── Lifecycle ────────────────────────────────────────────────────── async ensureInit() { } async [Symbol.asyncDispose]() { } // ── Swap operations ──────────────────────────────────────────────── async saveSwap(swap) { await this.ensureInit(); this.realm.write(() => { this.realm.create( "BoltzSwap", { id: swap.id, type: swap.type, status: swap.status, createdAt: swap.createdAt, data: JSON.stringify(swap) }, "modified" ); }); } async deleteSwap(id) { await this.ensureInit(); this.realm.write(() => { const toDelete = this.realm.objects("BoltzSwap").filtered("id == $0", id); if (toDelete.length > 0) { this.realm.delete(toDelete); } }); } async getAllSwaps(filter) { await this.ensureInit(); if (hasImpossibleSwapsFilter(filter)) return []; let results = this.realm.objects("BoltzSwap"); if (filter) { const filterParts = []; const filterArgs = []; let argIndex = 0; argIndex = this.addFilterCondition(filterParts, filterArgs, "id", filter.id, argIndex); argIndex = this.addFilterCondition( filterParts, filterArgs, "status", filter.status, argIndex ); this.addFilterCondition(filterParts, filterArgs, "type", filter.type, argIndex); if (filterParts.length > 0) { const query = filterParts.join(" AND "); results = results.filtered(query, ...filterArgs); } } if (filter?.orderBy === "createdAt") { const reverse = filter.orderDirection === "desc"; results = results.sorted("createdAt", reverse); } return [...results].map((obj) => JSON.parse(obj.data)); } async clear() { await this.ensureInit(); this.realm.write(() => { this.realm.delete(this.realm.objects("BoltzSwap")); }); } // ── Helpers ───────────────────────────────────────────────────────── addFilterCondition(parts, args, column, value, argIndex) { if (value === void 0) return argIndex; if (Array.isArray(value)) { if (value.length === 0) return argIndex; const placeholders = value.map((_, i) => `$${argIndex + i}`); parts.push(`${column} IN {${placeholders.join(", ")}}`); args.push(...value); return argIndex + value.length; } else { parts.push(`${column} == $${argIndex}`); args.push(value); return argIndex + 1; } } }; // src/repositories/realm/schemas.ts var BoltzSwapSchema = { name: "BoltzSwap", primaryKey: "id", properties: { id: "string", type: "string", status: "string", createdAt: "int", data: "string" } }; var BoltzRealmSchemas = [BoltzSwapSchema]; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BoltzRealmSchemas, BoltzSwapSchema, RealmSwapRepository });