aboutsummaryrefslogtreecommitdiff
path: root/src/operations/balance.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-03-06 19:39:55 +0530
committerFlorian Dold <florian.dold@gmail.com>2020-03-06 19:39:55 +0530
commit4e76edf129229823281c2a662392249c32a1c7a2 (patch)
treeacc1c8b68176978362f45608ae0efc9b729ddde7 /src/operations/balance.ts
parent7c7d3e001ec3fa066ad7a41876142c331e3e2f0f (diff)
downloadwallet-core-4e76edf129229823281c2a662392249c32a1c7a2.tar.xz
include (pending) wallet balance in pending ops response
Diffstat (limited to 'src/operations/balance.ts')
-rw-r--r--src/operations/balance.ts148
1 files changed, 79 insertions, 69 deletions
diff --git a/src/operations/balance.ts b/src/operations/balance.ts
index d12fbaf7c..03d1b2a9f 100644
--- a/src/operations/balance.ts
+++ b/src/operations/balance.ts
@@ -18,7 +18,7 @@
* Imports.
*/
import { WalletBalance, WalletBalanceEntry } from "../types/walletTypes";
-import { Database } from "../util/query";
+import { Database, TransactionHandle } from "../util/query";
import { InternalWalletState } from "./state";
import { Stores, TipRecord, CoinStatus } from "../types/dbTypes";
import * as Amounts from "../util/amounts";
@@ -28,13 +28,14 @@ import { Logger } from "../util/logging";
const logger = new Logger("withdraw.ts");
/**
- * Get detailed balance information, sliced by exchange and by currency.
+ * Get balance information.
*/
-export async function getBalances(
+export async function getBalancesInsideTransaction(
ws: InternalWalletState,
+ tx: TransactionHandle,
): Promise<WalletBalance> {
- logger.trace("starting to compute balance");
- /**
+
+ /**
* Add amount to a balance field, both for
* the slicing by exchange and currency.
*/
@@ -73,76 +74,85 @@ export async function getBalances(
byExchange: {},
};
- await ws.db.runWithReadTransaction(
- [Stores.coins, Stores.refreshGroups, Stores.reserves, Stores.purchases, Stores.withdrawalSession],
- async tx => {
- await tx.iter(Stores.coins).forEach(c => {
- if (c.suspended) {
- return;
- }
- if (c.status === CoinStatus.Fresh) {
- addTo(balanceStore, "available", c.currentAmount, c.exchangeBaseUrl);
- }
- });
- await tx.iter(Stores.refreshGroups).forEach(r => {
- // Don't count finished refreshes, since the refresh already resulted
- // in coins being added to the wallet.
- if (r.timestampFinished) {
- return;
- }
- for (let i = 0; i < r.oldCoinPubs.length; i++) {
- const session = r.refreshSessionPerCoin[i];
- if (session) {
- addTo(
- balanceStore,
- "pendingIncoming",
- session.amountRefreshOutput,
- session.exchangeBaseUrl,
- );
- addTo(
- balanceStore,
- "pendingIncomingRefresh",
- session.amountRefreshOutput,
- session.exchangeBaseUrl,
- );
- }
- }
- });
-
- await tx.iter(Stores.withdrawalSession).forEach(wds => {
- let w = wds.totalCoinValue;
- for (let i = 0; i < wds.planchets.length; i++) {
- if (wds.withdrawn[i]) {
- const p = wds.planchets[i];
- if (p) {
- w = Amounts.sub(w, p.coinValue).amount;
- }
- }
- }
+ await tx.iter(Stores.coins).forEach(c => {
+ if (c.suspended) {
+ return;
+ }
+ if (c.status === CoinStatus.Fresh) {
+ addTo(balanceStore, "available", c.currentAmount, c.exchangeBaseUrl);
+ }
+ });
+ await tx.iter(Stores.refreshGroups).forEach(r => {
+ // Don't count finished refreshes, since the refresh already resulted
+ // in coins being added to the wallet.
+ if (r.timestampFinished) {
+ return;
+ }
+ for (let i = 0; i < r.oldCoinPubs.length; i++) {
+ const session = r.refreshSessionPerCoin[i];
+ if (session) {
addTo(
balanceStore,
"pendingIncoming",
- w,
- wds.exchangeBaseUrl,
+ session.amountRefreshOutput,
+ session.exchangeBaseUrl,
+ );
+ addTo(
+ balanceStore,
+ "pendingIncomingRefresh",
+ session.amountRefreshOutput,
+ session.exchangeBaseUrl,
);
- });
+ }
+ }
+ });
- await tx.iter(Stores.purchases).forEach(t => {
- if (t.timestampFirstSuccessfulPay) {
- return;
- }
- for (const c of t.payReq.coins) {
- addTo(
- balanceStore,
- "pendingPayment",
- Amounts.parseOrThrow(c.contribution),
- c.exchange_url,
- );
+ await tx.iter(Stores.withdrawalSession).forEach(wds => {
+ let w = wds.totalCoinValue;
+ for (let i = 0; i < wds.planchets.length; i++) {
+ if (wds.withdrawn[i]) {
+ const p = wds.planchets[i];
+ if (p) {
+ w = Amounts.sub(w, p.coinValue).amount;
}
- });
- },
- );
+ }
+ }
+ addTo(balanceStore, "pendingIncoming", w, wds.exchangeBaseUrl);
+ });
+
+ await tx.iter(Stores.purchases).forEach(t => {
+ if (t.timestampFirstSuccessfulPay) {
+ return;
+ }
+ for (const c of t.payReq.coins) {
+ addTo(
+ balanceStore,
+ "pendingPayment",
+ Amounts.parseOrThrow(c.contribution),
+ c.exchange_url,
+ );
+ }
+ });
- logger.trace("computed balances:", balanceStore);
return balanceStore;
}
+
+/**
+ * Get detailed balance information, sliced by exchange and by currency.
+ */
+export async function getBalances(
+ ws: InternalWalletState,
+): Promise<WalletBalance> {
+ logger.trace("starting to compute balance");
+
+ return await ws.db.runWithReadTransaction([
+ Stores.coins,
+ Stores.refreshGroups,
+ Stores.reserves,
+ Stores.purchases,
+ Stores.withdrawalSession,
+ ],
+ async tx => {
+ return getBalancesInsideTransaction(ws, tx);
+ });
+}