From 5d5ce2ac859fe3ce960c4514c9a4be4bb2d15f86 Mon Sep 17 00:00:00 2001 From: c4605 Date: Sun, 27 Apr 2025 12:07:30 +0200 Subject: [PATCH] fix: prevent dust change output --- src/sdkUtils/bridgeFromBRC20.ts | 14 ++++++++++---- src/sdkUtils/bridgeFromRunes.ts | 18 ++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/sdkUtils/bridgeFromBRC20.ts b/src/sdkUtils/bridgeFromBRC20.ts index 5f0ae35..d6579e4 100644 --- a/src/sdkUtils/bridgeFromBRC20.ts +++ b/src/sdkUtils/bridgeFromBRC20.ts @@ -723,12 +723,17 @@ async function constructBRC20Transaction( }> { const txOptions = await prepareBRC20Transaction(sdkContext, info) + const recipients = + txOptions.changeAmount > 0n + ? txOptions.recipients.concat({ + addressScriptPubKey: info.networkFeeChangeAddressScriptPubKey, + satsAmount: txOptions.changeAmount, + }) + : txOptions.recipients + const tx = createTransaction( txOptions.inputs, - txOptions.recipients.concat({ - addressScriptPubKey: info.networkFeeChangeAddressScriptPubKey, - satsAmount: txOptions.changeAmount, - }), + recipients, txOptions.opReturnScripts ?? [], ) @@ -812,6 +817,7 @@ export type PrepareBRC20TransactionInput = KnownRoute_FromBRC20 & { * * Peg-in order data * * Bridge fee (optional) * * Hard linkage (optional) + * * ...extra outputs * * BTC change (optional) * * (with bridge fee example tx) https://mempool.space/testnet/tx/e127a2d3c343675a1cde8ca8d10ae5621b40d309ce44b4f45bedc10499f8d596 diff --git a/src/sdkUtils/bridgeFromRunes.ts b/src/sdkUtils/bridgeFromRunes.ts index 2734c3a..0991b66 100644 --- a/src/sdkUtils/bridgeFromRunes.ts +++ b/src/sdkUtils/bridgeFromRunes.ts @@ -669,12 +669,17 @@ async function constructRunesTransaction( info, ) + const recipients = + txOptions.changeAmount > 0n + ? txOptions.recipients.concat({ + addressScriptPubKey: info.networkFeeChangeAddressScriptPubKey, + satsAmount: txOptions.changeAmount, + }) + : txOptions.recipients + const tx = createTransaction( txOptions.inputs, - txOptions.recipients.concat({ - addressScriptPubKey: info.networkFeeChangeAddressScriptPubKey, - satsAmount: txOptions.changeAmount, - }), + recipients, txOptions.opReturnScripts ?? [], ) @@ -760,6 +765,7 @@ export type PrepareRunesTransactionInput = KnownRoute_FromRunes & { * * Bridge fee (optional) * * Hard linkage (optional) * * Peg-in Rune tokens + * * ...extra outputs * * BTC change (optional) * * Runestone * @@ -943,6 +949,10 @@ export async function prepareRunesTransaction( }), ), }, + ...info.extraOutputs.map(o => ({ + addressScriptPubKey: o.address.scriptPubKey, + satsAmount: o.satsAmount, + })), ], changeAddressScriptPubKey: info.networkFeeChangeAddressScriptPubKey, feeRate: info.networkFeeRate,