diff options
author | Florian Dold <florian@dold.me> | 2022-10-15 11:52:07 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-10-15 11:53:16 +0200 |
commit | e075134ffc94fda3582b179122bda594d91a962b (patch) | |
tree | 547920b2aa07bdb9f2c87a0c1f8c35dbcd64c8f7 /packages/taler-wallet-core/src/operations/common.ts | |
parent | 4d70391f3db386766a516bdecc3d1d265c5d49a1 (diff) | |
download | wallet-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/common.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/common.ts | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/packages/taler-wallet-core/src/operations/common.ts b/packages/taler-wallet-core/src/operations/common.ts index d17530c7f..5e02f3d7b 100644 --- a/packages/taler-wallet-core/src/operations/common.ts +++ b/packages/taler-wallet-core/src/operations/common.ts @@ -20,6 +20,8 @@ import { AmountJson, Amounts, + CoinRefreshRequest, + CoinStatus, j2s, Logger, RefreshReason, @@ -29,7 +31,7 @@ import { TransactionIdStr, TransactionType, } from "@gnu-taler/taler-util"; -import { WalletStoresV1, CoinStatus, CoinRecord } from "../db.js"; +import { WalletStoresV1, CoinRecord } from "../db.js"; import { makeErrorDetail, TalerError } from "../errors.js"; import { InternalWalletState } from "../internal-wallet-state.js"; import { checkDbInvariant, checkLogicInvariant } from "../util/invariants.js"; @@ -103,11 +105,19 @@ export async function spendCoins( }>, csi: CoinsSpendInfo, ): Promise<void> { + let refreshCoinPubs: CoinRefreshRequest[] = []; for (let i = 0; i < csi.coinPubs.length; i++) { const coin = await tx.coins.get(csi.coinPubs[i]); if (!coin) { throw Error("coin allocated for payment doesn't exist anymore"); } + const denom = await ws.getDenomInfo( + ws, + tx, + coin.exchangeBaseUrl, + coin.denomPubHash, + ); + checkDbInvariant(!!denom); const coinAvailability = await tx.coinAvailability.get([ coin.exchangeBaseUrl, coin.denomPubHash, @@ -116,7 +126,7 @@ export async function spendCoins( checkDbInvariant(!!coinAvailability); const contrib = csi.contributions[i]; if (coin.status !== CoinStatus.Fresh) { - const alloc = coin.allocation; + const alloc = coin.spendAllocation; if (!alloc) { continue; } @@ -131,15 +141,18 @@ export async function spendCoins( continue; } coin.status = CoinStatus.Dormant; - coin.allocation = { + coin.spendAllocation = { id: csi.allocationId, amount: Amounts.stringify(contrib), }; - const remaining = Amounts.sub(coin.currentAmount, contrib); + const remaining = Amounts.sub(denom.value, contrib); if (remaining.saturated) { throw Error("not enough remaining balance on coin for payment"); } - coin.currentAmount = remaining.amount; + refreshCoinPubs.push({ + amount: remaining.amount, + coinPub: coin.coinPub, + }); checkDbInvariant(!!coinAvailability); if (coinAvailability.freshCoinCount === 0) { throw Error( @@ -150,9 +163,6 @@ export async function spendCoins( await tx.coins.put(coin); await tx.coinAvailability.put(coinAvailability); } - const refreshCoinPubs = csi.coinPubs.map((x) => ({ - coinPub: x, - })); await ws.refreshOps.createRefreshGroup( ws, tx, |