commit 79d0c2f928e3b9a73d07f30a9ab63468c5f3634b
parent 3eb88574bcf327672c34120becfb511eac4e06cd
Author: Florian Dold <florian.dold@gmail.com>
Date: Fri, 15 May 2020 16:39:40 +0530
include refund fees in effective refund amount calculation
Diffstat:
4 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/src/operations/pay.ts b/src/operations/pay.ts
@@ -456,6 +456,7 @@ async function recordConfirmPay(
refundsDone: {},
refundsFailed: {},
refundsPending: {},
+ refundsRefreshCost: {},
};
await ws.db.runWithWriteTransaction(
diff --git a/src/operations/refund.ts b/src/operations/refund.ts
@@ -144,6 +144,8 @@ async function acceptRefundResponse(
const unfinishedRefunds: MerchantRefundDetails[] = [];
const failedRefunds: MerchantRefundDetails[] = [];
+ const refundsRefreshCost: { [refundKey: string]: AmountJson } = {};
+
for (const rd of refunds) {
if (rd.exchange_http_status === 200) {
// FIXME: also verify signature if necessary.
@@ -158,6 +160,33 @@ async function acceptRefundResponse(
}
}
+ for (const rd of [...finishedRefunds, ...unfinishedRefunds]) {
+ const key = getRefundKey(rd);
+ const coin = await ws.db.get(Stores.coins, rd.coin_pub);
+ if (!coin) {
+ continue;
+ }
+ const denom = await ws.db.getIndexed(
+ Stores.denominations.denomPubHashIndex,
+ coin.denomPubHash,
+ );
+ if (!denom) {
+ throw Error("inconsistent database");
+ }
+ const amountLeft = Amounts.sub(
+ Amounts.add(coin.currentAmount, Amounts.parseOrThrow(rd.refund_amount))
+ .amount,
+ Amounts.parseOrThrow(rd.refund_fee),
+ ).amount;
+ const allDenoms = await ws.db
+ .iterIndex(
+ Stores.denominations.exchangeBaseUrlIndex,
+ coin.exchangeBaseUrl,
+ )
+ .toArray();
+ refundsRefreshCost[key] = getTotalRefreshCost(allDenoms, denom, amountLeft);
+ }
+
const now = getTimestampNow();
await ws.db.runWithWriteTransaction(
@@ -282,6 +311,8 @@ async function acceptRefundResponse(
console.log("refund query not done");
}
+ p.refundsRefreshCost = {...p.refundsRefreshCost, ...refundsRefreshCost };
+
await tx.put(Stores.purchases, p);
const coinsPubsToBeRefreshed = Object.values(refreshCoinsMap);
diff --git a/src/operations/transactions.ts b/src/operations/transactions.ts
@@ -63,6 +63,8 @@ function getRefundStats(
.amount;
}
+ // Subtract fees from effective refund amount
+
for (const rk of Object.keys(pr.refundsDone)) {
const perm = pr.refundsDone[rk].perm;
if (pr.refundsDone[rk].refundGroupId !== refundGroupId) {
@@ -72,6 +74,12 @@ function getRefundStats(
amountEffective,
Amounts.parseOrThrow(perm.refund_fee),
).amount;
+ if (pr.refundsRefreshCost[rk]) {
+ amountEffective = Amounts.sub(
+ amountEffective,
+ pr.refundsRefreshCost[rk],
+ ).amount;
+ }
}
for (const rk of Object.keys(pr.refundsFailed)) {
diff --git a/src/types/dbTypes.ts b/src/types/dbTypes.ts
@@ -1270,6 +1270,11 @@ export interface PurchaseRecord {
refundsFailed: { [refundKey: string]: RefundInfo };
/**
+ * Refresh cost for each refund permission.
+ */
+ refundsRefreshCost: { [refundKey: string]: AmountJson };
+
+ /**
* When was the last refund made?
* Set to 0 if no refund was made on the purchase.
*/