@arkade-os/boltz-swap
Version:
A production-ready TypeScript package that brings Boltz submarine-swaps to Arkade.
99 lines (96 loc) • 3.11 kB
JavaScript
import {
ArkadeSwaps,
isPendingReverseSwap,
isPendingSubmarineSwap,
isReverseClaimableStatus,
isReverseFinalStatus,
isSubmarineFinalStatus,
isSubmarineSwapRefundable,
logger
} from "./chunk-VKHFXGKL.js";
// src/expo/swapsPollProcessor.ts
var SWAP_POLL_TASK_TYPE = "swap-poll";
var swapsPollProcessor = {
taskType: SWAP_POLL_TASK_TYPE,
async execute(item, deps) {
const { swapRepository, swapProvider, wallet, arkProvider, indexerProvider } = deps;
const allSwaps = await swapRepository.getAllSwaps();
const pendingSwaps = allSwaps.filter((swap) => {
if (isPendingReverseSwap(swap)) return !isReverseFinalStatus(swap.status);
if (isPendingSubmarineSwap(swap)) return !isSubmarineFinalStatus(swap.status);
return false;
});
let polled = 0;
let updated = 0;
let claimed = 0;
let refunded = 0;
let errors = 0;
const tempSwaps = new ArkadeSwaps({
wallet,
arkProvider,
indexerProvider,
swapProvider,
swapManager: false,
swapRepository
});
try {
for (const swap of pendingSwaps) {
try {
const { status: currentStatus } = await swapProvider.getSwapStatus(swap.id);
polled++;
if (currentStatus !== swap.status) {
await swapRepository.saveSwap({
...swap,
status: currentStatus
});
updated++;
}
if (isPendingReverseSwap(swap) && isReverseClaimableStatus(currentStatus)) {
if (!swap.preimage) {
logger.warn(`[swap-poll] Skipping claim for ${swap.id}: no preimage`);
continue;
}
try {
await tempSwaps.claimVHTLC(swap);
claimed++;
} catch (claimError) {
logger.error(`[swap-poll] Claim failed for ${swap.id}:`, claimError);
errors++;
}
}
const swapWithStatus = isPendingSubmarineSwap(swap) ? { ...swap, status: currentStatus } : null;
if (isPendingSubmarineSwap(swap) && isSubmarineSwapRefundable(swapWithStatus)) {
if (!swap.request.invoice && !swap.preimageHash) {
logger.warn(
`[swap-poll] Skipping refund for ${swap.id}: no invoice or preimageHash`
);
continue;
}
try {
await tempSwaps.refundVHTLC(swapWithStatus);
refunded++;
} catch (refundError) {
logger.error(`[swap-poll] Refund failed for ${swap.id}:`, refundError);
errors++;
}
}
} catch (swapError) {
logger.error(`[swap-poll] Error processing swap ${swap.id}:`, swapError);
errors++;
}
}
} finally {
await tempSwaps.dispose();
}
return {
taskItemId: item.id,
type: SWAP_POLL_TASK_TYPE,
status: errors > 0 && polled === 0 ? "failed" : "success",
data: { polled, updated, claimed, refunded, errors }
};
}
};
export {
SWAP_POLL_TASK_TYPE,
swapsPollProcessor
};