diff options
author | Florian Dold <florian@dold.me> | 2024-04-08 19:56:54 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-04-08 19:56:54 +0200 |
commit | 85b6213333b211bbfae9209630d8446108c4ce56 (patch) | |
tree | 90456a87b674a223e4565a0fc8ae6219703cb2ac | |
parent | 988911236823c0314f3c01ad219374c34ff21433 (diff) | |
download | wallet-core-85b6213333b211bbfae9209630d8446108c4ce56.tar.gz wallet-core-85b6213333b211bbfae9209630d8446108c4ce56.tar.bz2 wallet-core-85b6213333b211bbfae9209630d8446108c4ce56.zip |
wallet-core: refactor getTotalRefreshCost
-rw-r--r-- | packages/taler-wallet-core/src/deposits.ts | 12 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/pay-merchant.ts | 11 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/pay-peer-common.ts | 13 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/refresh.ts | 44 |
4 files changed, 27 insertions, 53 deletions
diff --git a/packages/taler-wallet-core/src/deposits.ts b/packages/taler-wallet-core/src/deposits.ts index e24401ed6..50f26ea9c 100644 --- a/packages/taler-wallet-core/src/deposits.ts +++ b/packages/taler-wallet-core/src/deposits.ts @@ -110,7 +110,6 @@ import { parseTransactionIdentifier, } from "./transactions.js"; import { WalletExecutionContext, getDenomInfo } from "./wallet.js"; -import { getCandidateWithdrawalDenomsTx } from "./withdraw.js"; /** * Logger. @@ -1712,19 +1711,12 @@ async function getTotalFeesForDepositAmount( } coinFee.push(Amounts.parseOrThrow(denom.feeDeposit)); exchangeSet.add(pcs[i].exchangeBaseUrl); - - const allDenoms = await getCandidateWithdrawalDenomsTx( + const amountLeft = Amounts.sub(denom.value, pcs[i].contribution).amount; + const refreshCost = await getTotalRefreshCost( wex, tx, - pcs[i].exchangeBaseUrl, - currency, - ); - const amountLeft = Amounts.sub(denom.value, pcs[i].contribution).amount; - const refreshCost = getTotalRefreshCost( - allDenoms, denom, amountLeft, - wex.ws.config.testing.denomselAllowLate, ); refreshFee.push(refreshCost); } diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts index 5b4db4931..2a01481b0 100644 --- a/packages/taler-wallet-core/src/pay-merchant.ts +++ b/packages/taler-wallet-core/src/pay-merchant.ts @@ -143,7 +143,6 @@ import { getDenomInfo, WalletExecutionContext, } from "./wallet.js"; -import { getCandidateWithdrawalDenomsTx } from "./withdraw.js"; /** * Logger. @@ -468,18 +467,12 @@ export async function getTotalPaymentCost( "can't calculate payment cost, denomination for coin not found", ); } - const allDenoms = await getCandidateWithdrawalDenomsTx( + const amountLeft = Amounts.sub(denom.value, pcs[i].contribution).amount; + const refreshCost = await getTotalRefreshCost( wex, tx, - pcs[i].exchangeBaseUrl, - currency, - ); - const amountLeft = Amounts.sub(denom.value, pcs[i].contribution).amount; - const refreshCost = getTotalRefreshCost( - allDenoms, DenominationRecord.toDenomInfo(denom), amountLeft, - wex.ws.config.testing.denomselAllowLate, ); costs.push(Amounts.parseOrThrow(pcs[i].contribution)); costs.push(refreshCost); diff --git a/packages/taler-wallet-core/src/pay-peer-common.ts b/packages/taler-wallet-core/src/pay-peer-common.ts index 6ad8ecb70..05091c5cf 100644 --- a/packages/taler-wallet-core/src/pay-peer-common.ts +++ b/packages/taler-wallet-core/src/pay-peer-common.ts @@ -34,7 +34,6 @@ import { SpendCoinDetails } from "./crypto/cryptoImplementation.js"; import { DbPeerPushPaymentCoinSelection, ReserveRecord } from "./db.js"; import { getTotalRefreshCost } from "./refresh.js"; import { WalletExecutionContext, getDenomInfo } from "./wallet.js"; -import { getCandidateWithdrawalDenomsTx } from "./withdraw.js"; /** * Get information about the coin selected for signatures. @@ -91,21 +90,15 @@ export async function getTotalPeerPaymentCost( "can't calculate payment cost, denomination for coin not found", ); } - const allDenoms = await getCandidateWithdrawalDenomsTx( - wex, - tx, - pcs[i].exchangeBaseUrl, - currency, - ); const amountLeft = Amounts.sub( denomInfo.value, pcs[i].contribution, ).amount; - const refreshCost = getTotalRefreshCost( - allDenoms, + const refreshCost = await getTotalRefreshCost( + wex, + tx, denomInfo, amountLeft, - wex.ws.config.testing.denomselAllowLate, ); costs.push(Amounts.parseOrThrow(pcs[i].contribution)); costs.push(refreshCost); diff --git a/packages/taler-wallet-core/src/refresh.ts b/packages/taler-wallet-core/src/refresh.ts index fb2cdba93..7c5f75625 100644 --- a/packages/taler-wallet-core/src/refresh.ts +++ b/packages/taler-wallet-core/src/refresh.ts @@ -309,6 +309,21 @@ export class RefreshTransactionContext implements TransactionContext { } } +export async function getTotalRefreshCost( + wex: WalletExecutionContext, + tx: WalletDbReadOnlyTransaction<["denominations"]>, + refreshedDenom: DenominationInfo, + amountLeft: AmountJson, +): Promise<AmountJson> { + const allDenoms = await getCandidateWithdrawalDenomsTx( + wex, + tx, + refreshedDenom.exchangeBaseUrl, + Amounts.currencyOf(amountLeft), + ); + return getTotalRefreshCostInternal(allDenoms, refreshedDenom, amountLeft); +} + /** * Get the amount that we lose when refreshing a coin of the given denomination * with a certain amount left. @@ -320,11 +335,10 @@ export class RefreshTransactionContext implements TransactionContext { * Considers refresh fees, withdrawal fees after refresh and amounts too small * to refresh. */ -export function getTotalRefreshCost( +export function getTotalRefreshCostInternal( denoms: DenominationRecord[], refreshedDenom: DenominationInfo, amountLeft: AmountJson, - denomselAllowLate: boolean, ): AmountJson { const withdrawAmount = Amounts.sub( amountLeft, @@ -334,7 +348,7 @@ export function getTotalRefreshCost( const withdrawDenoms = selectWithdrawalDenominations( withdrawAmount, denoms, - denomselAllowLate, + false, ); const resultingAmount = Amounts.add( Amounts.zeroOfCurrency(withdrawAmount.currency), @@ -1438,23 +1452,6 @@ export async function calculateRefreshOutput( const infoPerExchange: Record<string, RefreshGroupPerExchangeInfo> = {}; - // FIXME: Use denom groups instead of querying all denominations! - const getDenoms = async ( - exchangeBaseUrl: string, - ): Promise<DenominationRecord[]> => { - if (denomsPerExchange[exchangeBaseUrl]) { - return denomsPerExchange[exchangeBaseUrl]; - } - const allDenoms = await getCandidateWithdrawalDenomsTx( - wex, - tx, - exchangeBaseUrl, - currency, - ); - denomsPerExchange[exchangeBaseUrl] = allDenoms; - return allDenoms; - }; - for (const ocp of oldCoinPubs) { const coin = await tx.coins.get(ocp.coinPub); checkDbInvariant(!!coin, "coin must be in database"); @@ -1469,12 +1466,11 @@ export async function calculateRefreshOutput( "denomination for existing coin must be in database", ); const refreshAmount = ocp.amount; - const denoms = await getDenoms(coin.exchangeBaseUrl); - const cost = getTotalRefreshCost( - denoms, + const cost = await getTotalRefreshCost( + wex, + tx, denom, Amounts.parseOrThrow(refreshAmount), - wex.ws.config.testing.denomselAllowLate, ); const output = Amounts.sub(refreshAmount, cost).amount; let exchInfo = infoPerExchange[coin.exchangeBaseUrl]; |