aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/refresh.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2024-04-08 19:56:54 +0200
committerFlorian Dold <florian@dold.me>2024-04-08 19:56:54 +0200
commit85b6213333b211bbfae9209630d8446108c4ce56 (patch)
tree90456a87b674a223e4565a0fc8ae6219703cb2ac /packages/taler-wallet-core/src/refresh.ts
parent988911236823c0314f3c01ad219374c34ff21433 (diff)
downloadwallet-core-85b6213333b211bbfae9209630d8446108c4ce56.tar.xz
wallet-core: refactor getTotalRefreshCost
Diffstat (limited to 'packages/taler-wallet-core/src/refresh.ts')
-rw-r--r--packages/taler-wallet-core/src/refresh.ts44
1 files changed, 20 insertions, 24 deletions
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];