Skip to content

Commit

Permalink
adjust utxo picking
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrunk committed Jan 9, 2024
1 parent 80ce696 commit 6bb832b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/lib/constructTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function pickUtxos(utxos: utxo[], amount: BigNumber): utxo[] {
// If the "sum of all your UTXO smaller than the Target" doesn't surpass the target, the smallest UTXO greater than your Target will be used.
const utxosBiggestThanTarget = sortedUtxos.filter((utxoX) => {
const utxoAmount = new BigNumber(utxoX.satoshis);
return utxoAmount.isGreaterThan(amount);
return utxoAmount.isGreaterThanOrEqualTo(amount);
});
if (totalAmountSmallerUtxos.isLessThan(amount)) {
if (utxosBiggestThanTarget.length) {
Expand All @@ -302,13 +302,13 @@ function pickUtxos(utxos: utxo[], amount: BigNumber): utxo[] {

// case 4
// If the "sum of all your UTXO smaller than the Target" surpasses the Target, try using the smallest UTXO first and add more UTXO until you reach the Target.
if (totalAmountSmallerUtxos.isGreaterThan(amount)) {
if (totalAmountSmallerUtxos.isGreaterThanOrEqualTo(amount)) {
let totalAmount = new BigNumber(0);
const preselectedUtxos = [];
for (const utxoX of utxosSmallerThanTarget) {
totalAmount = totalAmount.plus(new BigNumber(utxoX.satoshis));
preselectedUtxos.push(utxoX);
if (totalAmount.isGreaterThan(amount)) {
if (totalAmount.isGreaterThanOrEqualTo(amount)) {
selectedUtxos = preselectedUtxos;
break;
}
Expand All @@ -320,13 +320,13 @@ function pickUtxos(utxos: utxo[], amount: BigNumber): utxo[] {

// case 5
// If the "sum of all your UTXO smaller than the Target" surpasses the Target, try using the biggest UTXO first and add more UTXO until you reach the Target.
if (totalAmountSmallerUtxos.isGreaterThan(amount)) {
if (totalAmountSmallerUtxos.isGreaterThanOrEqualTo(amount)) {
let totalAmount = new BigNumber(0);
const preselectedUtxos = [];
for (const utxoX of utxosSmallerThanTarget.reverse()) {
totalAmount = totalAmount.plus(new BigNumber(utxoX.satoshis));
preselectedUtxos.push(utxoX);
if (totalAmount.isGreaterThan(amount)) {
if (totalAmount.isGreaterThanOrEqualTo(amount)) {
selectedUtxos = preselectedUtxos;
break;
}
Expand All @@ -345,19 +345,24 @@ function pickUtxos(utxos: utxo[], amount: BigNumber): utxo[] {
}

// case 7, transaction can't be constructed, tx size would exceed 100kb. This is a limitation of the blockchain. Fallback to case 5
if (totalAmountSmallerUtxos.isGreaterThan(amount)) {
if (totalAmountSmallerUtxos.isGreaterThanOrEqualTo(amount)) {
let totalAmount = new BigNumber(0);
const preselectedUtxos = [];
for (const utxoX of utxosSmallerThanTarget.reverse()) {
totalAmount = totalAmount.plus(new BigNumber(utxoX.satoshis));
preselectedUtxos.push(utxoX);
if (totalAmount.isGreaterThan(amount)) {
if (totalAmount.isGreaterThanOrEqualTo(amount)) {
selectedUtxos = preselectedUtxos;
break;
}
}
}
return selectedUtxos;
if (selectedUtxos.length && selectedUtxos.length <= 670) {
return selectedUtxos;
}
throw new Error(
'Transaction can not be constructed. Try changing the amount.',
);
}

export async function constructAndSignTransaction(
Expand Down

0 comments on commit 6bb832b

Please sign in to comment.