From c7300714c57652e68fcadcdbcfcce3df8dd7d37b Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 2 Mar 2026 22:50:58 -0800 Subject: fee bumping --- routes/payments.ts | 66 +++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 31 deletions(-) (limited to 'routes/payments.ts') diff --git a/routes/payments.ts b/routes/payments.ts index 91115fc2..043d1c47 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -9,6 +9,7 @@ import { replay } from "$lib/lightning"; import ln from "$lib/ln"; import { err, l, warn } from "$lib/logging"; import mqtt from "$lib/mqtt"; +import { createCpfpChild, estimateBumpCost } from "$lib/cpfp"; import { acquireArkLock, build, @@ -16,7 +17,6 @@ import { createArkPayment, credit, debit, - decode, getUserRate, processWatchedTx, reverse, @@ -665,49 +665,53 @@ export default { return c.json({ ok: true }); }, - async replace(c) { + async bump(c) { const body = await c.req.json(); - const { id } = body; + const { id, targetFeeRate } = body; const user = c.get("user"); try { const p = await gf(`payment:${id}`); if (!p) fail("Payment not found"); if (p.uid !== user.id) fail("unauthorized"); + if (p.confirmed) fail("transaction already confirmed"); + if (p.type !== PaymentType.bitcoin) fail("only bitcoin transactions can be bumped"); + if (!p.bumpReserve || p.bumpReserve <= 0) fail("no bump reserve available"); + if (p.childTxid) fail("transaction already bumped"); - const { tx, type } = await decode(p.hex); - const node = rpc(config[type]); - - const fees: any = await fetch(`${api[type]}/fees/recommended`).then((r) => r.json()); - - const outputs = []; - for (const { - scriptPubKey: { address }, - value, - } of tx.vout) { - if (address && !(await node.getAddressInfo(address)).ismine) - outputs.push({ [address]: value }); - } - - const raw = await node.createRawTransaction(tx.vin, outputs); + const { txid, childFee } = await createCpfpChild(p, targetFeeRate); - const newTx = await node.fundRawTransaction(raw, { - fee_rate: fees.fastestFee + 50, - replaceable: true, - subtractFeeFromOutputs: [], + return c.json({ + txid, + childFee, + remaining: p.bumpReserve - childFee, }); + } catch (e) { + err("failed to bump payment", id, e.message); + return bail(c, e.message); + } + }, - const diff = sats(newTx.fee) - p.fee; - if (diff < 0) fail("fee must increase"); + async bumpEstimate(c) { + const body = await c.req.json(); + const { id, targetFeeRate } = body; + const user = c.get("user"); + try { + const p = await gf(`payment:${id}`); + if (!p) fail("Payment not found"); + if (p.uid !== user.id) fail("unauthorized"); - if (config[type].walletpass) await node.walletPassphrase(config[type].walletpass, 300); - p.hex = (await node.signRawTransactionWithWallet(newTx.hex)).hex; - const r = await node.testMempoolAccept([p.hex]); - if (!r[0].allowed) fail(`transaction rejected ${p.hex}`); - warn("bump", user.username, p.hex); + const cost = estimateBumpCost(p.fee, p.parentVsize, targetFeeRate); - return c.json({ ok: true }); + const fees: any = await fetch(`${api[PaymentType.bitcoin]}/fees/recommended`).then((r) => + r.json(), + ); + + return c.json({ + cost, + fees, + bumpReserve: p.bumpReserve || 0, + }); } catch (e) { - err("failed to bump payment", id, e.message); return bail(c, e.message); } }, -- cgit v1.2.3 ocket.js?h=v1.0.5&id=b4265b73591055adaeca489ceb03adcb27d71a5c'>treecommitdiffstats
path: root/src/lib/socket.js
blob: 672d008c96480607f326a2797bb515c1e2869c61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { get } from 'svelte/store';
import { invoices, newPayment, last, rate, user } from '$lib/store';
import { success } from '$lib/utils';
import { env } from '$env/dynamic/public';

const socketUrl = env.PUBLIC_SOCKET;
const btc = env.PUBLIC_BTC;

let socket, token;

export const auth = () => token && send('login', token);

export const send = (type, data) => {
	socket?.readyState === 1 && socket.send(JSON.stringify({ type, data }));
};

export const messages = (data) => ({
	id() {
		last.set(Date.now());
	},

	rate() {
		rate.set(data);
	},

	async payment() {
		let { amount, invoice } = data;

		if (invoice) {
			invoices.set({ ...get(invoices), [invoice.uuid]: invoice });
		}

		if (get(user).account_id !== data.account_id) return;

		newPayment.set(true);
		if (amount > 0) {
			success(`Received ${amount} sats!`);
		} else {
			success(`Sent ${-amount} sats!`);
		}
	},

	connected: auth
});

const initialReconnectDelay = 1000;
const maxReconnectDelay = 16000;

let currentReconnectDelay = initialReconnectDelay;

export function connect(t) {
	token = t;

	if (socket) return auth();

	socket = new WebSocket(socketUrl);
	socket.addEventListener('open', onWebsocketOpen);
	socket.addEventListener('close', onWebsocketClose);
	socket.addEventListener('message', onWebsocketMessage);
}

export function close() {
	if (socket) socket.close();
}

function onWebsocketMessage(msg) {
	let { type, data } = JSON.parse(msg.data);
	messages(data)[type] && messages(data)[type]();
}

function onWebsocketOpen() {
	currentReconnectDelay = initialReconnectDelay;
}

function onWebsocketClose() {
	socket = null;
	setTimeout(() => {
		reconnectToWebsocket();
	}, currentReconnectDelay + Math.floor(Math.random() * 3000));
}

function reconnectToWebsocket() {
	if (currentReconnectDelay < maxReconnectDelay) {
		currentReconnectDelay *= 2;
	}
	connect();
}