aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/operations/pay-merchant.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2022-10-15 11:52:07 +0200
committerFlorian Dold <florian@dold.me>2022-10-15 11:53:16 +0200
commite075134ffc94fda3582b179122bda594d91a962b (patch)
tree547920b2aa07bdb9f2c87a0c1f8c35dbcd64c8f7 /packages/taler-wallet-core/src/operations/pay-merchant.ts
parent4d70391f3db386766a516bdecc3d1d265c5d49a1 (diff)
downloadwallet-core-e075134ffc94fda3582b179122bda594d91a962b.tar.xz
wallet-core: simplify coin record
we only track the allocation now, not the remaining amount
Diffstat (limited to 'packages/taler-wallet-core/src/operations/pay-merchant.ts')
-rw-r--r--packages/taler-wallet-core/src/operations/pay-merchant.ts46
1 files changed, 18 insertions, 28 deletions
diff --git a/packages/taler-wallet-core/src/operations/pay-merchant.ts b/packages/taler-wallet-core/src/operations/pay-merchant.ts
index 6b14b60c6..2b0ea1f96 100644
--- a/packages/taler-wallet-core/src/operations/pay-merchant.ts
+++ b/packages/taler-wallet-core/src/operations/pay-merchant.ts
@@ -40,7 +40,8 @@ import {
codecForMerchantPayResponse,
codecForProposal,
CoinDepositPermission,
- CoinPublicKey,
+ CoinRefreshRequest,
+ CoinStatus,
ConfirmPayResult,
ConfirmPayResultType,
ContractTerms,
@@ -78,7 +79,6 @@ import {
AllowedExchangeInfo,
BackupProviderStateTag,
CoinRecord,
- CoinStatus,
DenominationRecord,
PurchaseRecord,
PurchaseStatus,
@@ -2084,7 +2084,7 @@ async function applySuccessfulRefund(
denominations: typeof WalletStoresV1.denominations;
}>,
p: PurchaseRecord,
- refreshCoinsMap: Record<string, { coinPub: string }>,
+ refreshCoinsMap: Record<string, CoinRefreshRequest>,
r: MerchantCoinRefundSuccessStatus,
): Promise<void> {
// FIXME: check signature before storing it as valid!
@@ -2102,31 +2102,23 @@ async function applySuccessfulRefund(
if (!denom) {
throw Error("inconsistent database");
}
- refreshCoinsMap[coin.coinPub] = { coinPub: coin.coinPub };
const refundAmount = Amounts.parseOrThrow(r.refund_amount);
const refundFee = denom.fees.feeRefund;
+ const amountLeft = Amounts.sub(refundAmount, refundFee).amount;
coin.status = CoinStatus.Dormant;
- coin.currentAmount = Amounts.add(coin.currentAmount, refundAmount).amount;
- coin.currentAmount = Amounts.sub(coin.currentAmount, refundFee).amount;
- logger.trace(`coin amount after is ${Amounts.stringify(coin.currentAmount)}`);
await tx.coins.put(coin);
const allDenoms = await tx.denominations.indexes.byExchangeBaseUrl
.iter(coin.exchangeBaseUrl)
.toArray();
-
- const amountLeft = Amounts.sub(
- Amounts.add(coin.currentAmount, Amounts.parseOrThrow(r.refund_amount))
- .amount,
- denom.fees.feeRefund,
- ).amount;
-
const totalRefreshCostBound = getTotalRefreshCost(
allDenoms,
DenominationRecord.toDenomInfo(denom),
amountLeft,
);
+ refreshCoinsMap[coin.coinPub] = { coinPub: coin.coinPub, amount: amountLeft };
+
p.refunds[refundKey] = {
type: RefundState.Applied,
obtainedTime: AbsoluteTime.toTimestamp(AbsoluteTime.now()),
@@ -2167,9 +2159,9 @@ async function storePendingRefund(
.iter(coin.exchangeBaseUrl)
.toArray();
+ // Refunded amount after fees.
const amountLeft = Amounts.sub(
- Amounts.add(coin.currentAmount, Amounts.parseOrThrow(r.refund_amount))
- .amount,
+ Amounts.parseOrThrow(r.refund_amount),
denom.fees.feeRefund,
).amount;
@@ -2197,7 +2189,7 @@ async function storeFailedRefund(
denominations: typeof WalletStoresV1.denominations;
}>,
p: PurchaseRecord,
- refreshCoinsMap: Record<string, { coinPub: string }>,
+ refreshCoinsMap: Record<string, CoinRefreshRequest>,
r: MerchantCoinRefundFailureStatus,
): Promise<void> {
const refundKey = getRefundKey(r);
@@ -2221,8 +2213,7 @@ async function storeFailedRefund(
.toArray();
const amountLeft = Amounts.sub(
- Amounts.add(coin.currentAmount, Amounts.parseOrThrow(r.refund_amount))
- .amount,
+ Amounts.parseOrThrow(r.refund_amount),
denom.fees.feeRefund,
).amount;
@@ -2246,6 +2237,7 @@ async function storeFailedRefund(
if (p.purchaseStatus === PurchaseStatus.AbortingWithRefund) {
// Refund failed because the merchant didn't even try to deposit
// the coin yet, so we try to refresh.
+ // FIXME: Is this case tested?!
if (r.exchange_code === TalerErrorCode.EXCHANGE_REFUND_DEPOSIT_NOT_FOUND) {
const coin = await tx.coins.get(r.coin_pub);
if (!coin) {
@@ -2271,14 +2263,11 @@ async function storeFailedRefund(
contrib = payCoinSelection.coinContributions[i];
}
}
- if (contrib) {
- coin.currentAmount = Amounts.add(coin.currentAmount, contrib).amount;
- coin.currentAmount = Amounts.sub(
- coin.currentAmount,
- denom.fees.feeRefund,
- ).amount;
- }
- refreshCoinsMap[coin.coinPub] = { coinPub: coin.coinPub };
+ // FIXME: Is this case tested?!
+ refreshCoinsMap[coin.coinPub] = {
+ coinPub: coin.coinPub,
+ amount: amountLeft,
+ };
await tx.coins.put(coin);
}
}
@@ -2308,7 +2297,7 @@ async function acceptRefunds(
return;
}
- const refreshCoinsMap: Record<string, CoinPublicKey> = {};
+ const refreshCoinsMap: Record<string, CoinRefreshRequest> = {};
for (const refundStatus of refunds) {
const refundKey = getRefundKey(refundStatus);
@@ -2350,6 +2339,7 @@ async function acceptRefunds(
}
const refreshCoinsPubs = Object.values(refreshCoinsMap);
+ logger.info(`refreshCoinMap ${j2s(refreshCoinsMap)}`);
if (refreshCoinsPubs.length > 0) {
await createRefreshGroup(
ws,