@arkade-os/boltz-swap
Version:
A production-ready TypeScript package that brings Boltz submarine-swaps to Arkade.
122 lines (120 loc) • 4.5 kB
JavaScript
import {
SWAP_POLL_TASK_TYPE,
swapsPollProcessor
} from "../chunk-HIG2OOAN.js";
import {
BoltzSwapProvider
} from "../chunk-VKHFXGKL.js";
import "../chunk-SJQJQO7P.js";
// src/expo/background.ts
import * as TaskManager from "expo-task-manager";
import * as BackgroundTask from "expo-background-task";
import { runTasks } from "@arkade-os/sdk/worker/expo";
import { ExpoArkProvider, ExpoIndexerProvider } from "@arkade-os/sdk/adapters/expo";
import { getRandomId } from "@arkade-os/sdk";
function createBackgroundWalletShim(args) {
const notImplemented = (method) => {
throw new Error(
`[boltz-swap] Background wallet shim: "${String(method)}" is not implemented`
);
};
return {
identity: args.identity,
getAddress: args.getAddress,
getBoardingAddress: async () => notImplemented("getBoardingAddress"),
getBalance: async () => notImplemented("getBalance"),
getVtxos: async () => notImplemented("getVtxos"),
getBoardingUtxos: async () => notImplemented("getBoardingUtxos"),
getTransactionHistory: async () => notImplemented("getTransactionHistory"),
getActivityHistory: async () => notImplemented("getActivityHistory"),
getContractManager: async () => notImplemented("getContractManager"),
getDelegateManager: async () => notImplemented("getDelegateManager"),
getDelegatorManager: async () => notImplemented("getDelegatorManager"),
sendBitcoin: async () => notImplemented("sendBitcoin"),
send: async () => notImplemented("send"),
settle: async () => notImplemented("settle"),
clear: async () => notImplemented("clear"),
assetManager: new Proxy({}, {
get: () => notImplemented("assetManager")
}),
activity: new Proxy({}, {
get: () => notImplemented("activity")
})
};
}
function defineExpoSwapBackgroundTask(taskName, options) {
const { taskQueue, swapRepository, identityFactory } = options;
TaskManager.defineTask(taskName, async () => {
try {
const config = await taskQueue.loadConfig();
if (!config) {
return BackgroundTask.BackgroundTaskResult.Success;
}
const identity = await identityFactory();
const arkProvider = new ExpoArkProvider(config.arkServerUrl);
const indexerProvider = new ExpoIndexerProvider(config.arkServerUrl);
const swapProvider = new BoltzSwapProvider({
network: config.network,
apiUrl: config.boltzApiUrl
});
const wallet = createBackgroundWalletShim({
identity,
getAddress: async () => {
const { ArkAddress, getNetwork } = await import("@arkade-os/sdk");
const { hex } = await import("@scure/base");
const info = await arkProvider.getInfo();
const pubkey = await identity.xOnlyPublicKey();
const serverPubKey = hex.decode(info.signerPubkey);
const xOnlyServerPubKey = serverPubKey.length === 33 ? serverPubKey.slice(1) : serverPubKey;
const hrp = getNetwork(info.network).hrp;
return new ArkAddress(xOnlyServerPubKey, pubkey, hrp).encode();
}
});
const deps = {
swapRepository,
swapProvider,
arkProvider,
indexerProvider,
identity,
wallet
};
await runTasks(taskQueue, [swapsPollProcessor], deps);
const results = await taskQueue.getResults();
if (results.length > 0) {
await taskQueue.acknowledgeResults(results.map((r) => r.id));
}
const existing = await taskQueue.getTasks(SWAP_POLL_TASK_TYPE);
if (existing.length === 0) {
const task = {
id: getRandomId(),
type: SWAP_POLL_TASK_TYPE,
data: {},
createdAt: Date.now()
};
await taskQueue.addTask(task);
}
return BackgroundTask.BackgroundTaskResult.Success;
} catch (error) {
console.error(
"[boltz-swap] Background task failed:",
error instanceof Error ? error.message : error
);
return BackgroundTask.BackgroundTaskResult.Failed;
}
});
}
async function registerExpoSwapBackgroundTask(taskName, options) {
await BackgroundTask.registerTaskAsync(taskName, {
minimumInterval: options?.minimumInterval ?? 15
});
}
async function unregisterExpoSwapBackgroundTask(taskName) {
await BackgroundTask.unregisterTaskAsync(taskName);
}
export {
SWAP_POLL_TASK_TYPE,
defineExpoSwapBackgroundTask,
registerExpoSwapBackgroundTask,
swapsPollProcessor,
unregisterExpoSwapBackgroundTask
};