diff options
author | Florian Dold <florian@dold.me> | 2023-09-15 16:45:12 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-09-15 16:45:12 +0200 |
commit | a15eec55d3136b4f737c68ac41e3042624b8e25f (patch) | |
tree | b35a2d328ec329e4fa96471af070d8508428a496 /packages/taler-wallet-core/src/util/coinSelection.ts | |
parent | de117e375a2a3cfa312acf4176276092f55205e0 (diff) | |
download | wallet-core-a15eec55d3136b4f737c68ac41e3042624b8e25f.tar.gz wallet-core-a15eec55d3136b4f737c68ac41e3042624b8e25f.tar.bz2 wallet-core-a15eec55d3136b4f737c68ac41e3042624b8e25f.zip |
wallet-core: correctly consider deposit fee in p2p coin selection
Diffstat (limited to 'packages/taler-wallet-core/src/util/coinSelection.ts')
-rw-r--r-- | packages/taler-wallet-core/src/util/coinSelection.ts | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/packages/taler-wallet-core/src/util/coinSelection.ts b/packages/taler-wallet-core/src/util/coinSelection.ts index 0b1be881f..8c90f26f1 100644 --- a/packages/taler-wallet-core/src/util/coinSelection.ts +++ b/packages/taler-wallet-core/src/util/coinSelection.ts @@ -419,8 +419,10 @@ interface SelResult { }; } -export function testing_selectGreedy(...args: Parameters<typeof selectGreedy>): ReturnType<typeof selectGreedy>{ - return selectGreedy(...args) +export function testing_selectGreedy( + ...args: Parameters<typeof selectGreedy> +): ReturnType<typeof selectGreedy> { + return selectGreedy(...args); } function selectGreedy( req: SelectPayCoinRequestNg, @@ -900,9 +902,12 @@ interface PeerCoinSelectionTally { /** * exporting for testing */ -export function testing_greedySelectPeer(...args: Parameters<typeof greedySelectPeer>): ReturnType<typeof greedySelectPeer> { - return greedySelectPeer(...args) +export function testing_greedySelectPeer( + ...args: Parameters<typeof greedySelectPeer> +): ReturnType<typeof greedySelectPeer> { + return greedySelectPeer(...args); } + function greedySelectPeer( candidates: AvailableDenom[], instructedAmount: AmountLike, @@ -921,11 +926,16 @@ function greedySelectPeer( instructedAmount, tally.amountAcc, ).amount; - const coinContrib = Amounts.sub(denom.value, denom.feeDeposit).amount + // Maximum amount the coin could effectively contribute. + const maxCoinContrib = Amounts.sub(denom.value, denom.feeDeposit).amount; + + const coinSpend = Amounts.min( + Amounts.add(amountPayRemaining, denom.feeDeposit).amount, + maxCoinContrib, + ); - const coinSpend = Amounts.min(amountPayRemaining, coinContrib) - tally.amountAcc = Amounts.add(tally.amountAcc, coinSpend).amount; + tally.amountAcc = Amounts.sub(tally.amountAcc, denom.feeDeposit).amount; tally.depositFeesAcc = Amounts.add( tally.depositFeesAcc, |