From 79d0c2f928e3b9a73d07f30a9ab63468c5f3634b Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 15 May 2020 16:39:40 +0530 Subject: include refund fees in effective refund amount calculation --- src/operations/refund.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/operations/refund.ts') diff --git a/src/operations/refund.ts b/src/operations/refund.ts index cbcb96446..b5d611b07 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); -- cgit v1.2.3