Skip to content

Commit

Permalink
[Issue-66] Fix wrong transfer amount when transfer all
Browse files Browse the repository at this point in the history
  • Loading branch information
S2kael committed May 29, 2024
1 parent c407759 commit e950faf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,9 @@ export default class KoniExtension {
utxos
};
const { fee: _estimatedFee, inputs, outputs } = transferAll ? determineUtxosForSpendAll(determineUtxosArgs) : determineUtxosForSpend(determineUtxosArgs);

maxTransferable = inputs.reduce((previous, input) => previous.plus(input.value), new BigN(0));

const recipients = outputs.filter((o) => o.address).map((o) => o.address || '');
const { txVBytes: vSize } = getSizeInfo({
inputLength: inputs.length,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ export async function getBitcoinTransactionObject ({ bitcoinApi,
? determineUtxosForSpendAll(determineUtxosArgs)
: determineUtxosForSpend(determineUtxosArgs);

console.log(inputs, outputs);
console.log(inputs, inputs.reduce((v, i) => v + i.value, 0));
console.log(outputs, (outputs as Array<{value: number}>).reduce((v, i) => v + i.value, 0));
console.log(fee, bitcoinFee);

const pair = keyring.getPair(from);
const tx = new Psbt({ network });
const transferAmount = new BigN(0);
let transferAmount = new BigN(0);

for (const input of inputs) {
if (pair.type === 'bitcoin-44' || pair.type === 'bittest-44') {
Expand All @@ -74,8 +79,6 @@ export async function getBitcoinTransactionObject ({ bitcoinApi,
}
});
}

transferAmount.plus(input.value);
}

for (const output of outputs) {
Expand All @@ -84,17 +87,15 @@ export async function getBitcoinTransactionObject ({ bitcoinApi,
value: output.value
});

if (output.address) {
transferAmount.minus(output.value);
if (output.address === to) {
transferAmount = transferAmount.plus(output.value);
}
}

console.log(inputs, inputs.reduce((v, i) => v + i.value, 0));
console.log(outputs, (outputs as Array<{value: number}>).reduce((v, i) => v + i.value, 0));
console.log(fee, bitcoinFee);

transferAmount.minus(fee);

console.log('Transfer Amount:', transferAmount.toString());

return [tx, transferAmount.toString()];
Expand Down

1 comment on commit e950faf

@saltict
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.