gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-core] 02/02: include refund fees in effective refund amoun


From: gnunet
Subject: [taler-wallet-core] 02/02: include refund fees in effective refund amount calculation
Date: Fri, 15 May 2020 13:09:48 +0200

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository wallet-core.

commit 79d0c2f928e3b9a73d07f30a9ab63468c5f3634b
Author: Florian Dold <address@hidden>
AuthorDate: Fri May 15 16:39:40 2020 +0530

    include refund fees in effective refund amount calculation
---
 src/operations/pay.ts          |  1 +
 src/operations/refund.ts       | 31 +++++++++++++++++++++++++++++++
 src/operations/transactions.ts |  8 ++++++++
 src/types/dbTypes.ts           |  5 +++++
 4 files changed, 45 insertions(+)

diff --git a/src/operations/pay.ts b/src/operations/pay.ts
index 20d62dea..372d012d 100644
--- 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
index cbcb9644..b5d611b0 100644
--- 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
index fd767962..9e07d4ff 100644
--- 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
index 79966eb3..82649d9f 100644
--- a/src/types/dbTypes.ts
+++ b/src/types/dbTypes.ts
@@ -1269,6 +1269,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.

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]