Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-zebras-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'magic-supplier': patch
---

Automatically consolidate UTXOs when redeeming inbound HTLCs
27 changes: 22 additions & 5 deletions src/processors/redeem-htlc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getBtcAddress, getBtcSigner, getSupplierId } from '../config';
import { networks, Psbt, script as bScript, payments, opcodes } from 'bitcoinjs-lib';
import { getRedeemedHTLC, setRedeemedHTLC, RedisClient } from '../store';
import { logger as _logger } from '../logger';
import { getFeeRate, tryBroadcast, withElectrumClient } from '../wallet';
import { getFeeRate, listUnspent, tryBroadcast, txWeight, withElectrumClient } from '../wallet';
import { bridgeContract, stacksProvider } from '../stacks';
import { bytesToHex, hexToBytes } from 'micro-stacks/common';
import { getBtcTxUrl, satsToBtc } from '../utils';
Expand Down Expand Up @@ -59,9 +59,10 @@ export async function redeem(txid: string, preimage: Uint8Array) {
const psbt = new Psbt({ network: networks.regtest });
const signer = getBtcSigner();
const address = getBtcAddress();
const weight = 312;
const feeRate = await getFeeRate(client);
const fee = weight * feeRate;
const baseWeight = 312n;
const [unspents, feeRate] = await Promise.all([listUnspent(client), getFeeRate(client)]);
const size = baseWeight + txWeight(unspents.length, 1);
const fee = size * BigInt(feeRate);

psbt.addInput({
hash: txid,
Expand All @@ -70,9 +71,25 @@ export async function redeem(txid: string, preimage: Uint8Array) {
redeemScript: Buffer.from(swap.redeemScript),
});

let consolidateTotal = 0n;
psbt.addInputs(
await Promise.all(
unspents.map(async coin => {
const txHex = await client.blockchain_transaction_get(coin.tx_hash);
consolidateTotal += BigInt(coin.value);
return {
hash: coin.tx_hash,
index: coin.tx_pos,
nonWitnessUtxo: Buffer.from(txHex, 'hex'),
};
})
)
);

const outputAmount = consolidateTotal + swap.sats - fee;
psbt.addOutput({
address,
value: Number(swap.sats) - fee,
value: Number(outputAmount),
});
await psbt.signInputAsync(0, signer);

Expand Down