@arkade-os/boltz-swap
Version:
A production-ready TypeScript package that brings Boltz submarine-swaps to Arkade.
111 lines (108 loc) • 3.4 kB
JavaScript
import {
hasImpossibleSwapsFilter
} from "../../chunk-SJQJQO7P.js";
// 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];
export {
BoltzRealmSchemas,
BoltzSwapSchema,
RealmSwapRepository
};