2019-12-02 00:42:40 +01:00
|
|
|
/*
|
|
|
|
This file is part of GNU Taler
|
|
|
|
(C) 2019 GNUnet e.V.
|
|
|
|
|
|
|
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
2021-06-09 15:26:18 +02:00
|
|
|
import {
|
|
|
|
AmountJson,
|
|
|
|
BalancesResponse,
|
|
|
|
Amounts,
|
|
|
|
Logger,
|
|
|
|
} from "@gnu-taler/taler-util";
|
2021-06-09 15:14:17 +02:00
|
|
|
import { CoinStatus, WalletStoresV1 } from "../db.js";
|
|
|
|
import { GetReadOnlyAccess } from "../util/query.js";
|
2022-03-23 13:11:36 +01:00
|
|
|
import { InternalWalletState } from "../internal-wallet-state.js";
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2021-06-17 15:49:05 +02:00
|
|
|
const logger = new Logger("operations/balance.ts");
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-07-28 19:47:12 +02:00
|
|
|
interface WalletBalance {
|
|
|
|
available: AmountJson;
|
|
|
|
pendingIncoming: AmountJson;
|
|
|
|
pendingOutgoing: AmountJson;
|
|
|
|
}
|
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
/**
|
2020-03-06 15:09:55 +01:00
|
|
|
* Get balance information.
|
2019-12-02 00:42:40 +01:00
|
|
|
*/
|
2020-03-06 15:09:55 +01:00
|
|
|
export async function getBalancesInsideTransaction(
|
2019-12-02 00:42:40 +01:00
|
|
|
ws: InternalWalletState,
|
2021-06-09 15:14:17 +02:00
|
|
|
tx: GetReadOnlyAccess<{
|
|
|
|
reserves: typeof WalletStoresV1.reserves;
|
|
|
|
coins: typeof WalletStoresV1.coins;
|
|
|
|
refreshGroups: typeof WalletStoresV1.refreshGroups;
|
|
|
|
withdrawalGroups: typeof WalletStoresV1.withdrawalGroups;
|
|
|
|
}>,
|
2020-07-28 19:47:12 +02:00
|
|
|
): Promise<BalancesResponse> {
|
|
|
|
const balanceStore: Record<string, WalletBalance> = {};
|
|
|
|
|
2020-03-30 12:39:32 +02:00
|
|
|
/**
|
2019-12-02 00:42:40 +01:00
|
|
|
* Add amount to a balance field, both for
|
|
|
|
* the slicing by exchange and currency.
|
|
|
|
*/
|
2020-07-28 19:47:12 +02:00
|
|
|
const initBalance = (currency: string): WalletBalance => {
|
|
|
|
const b = balanceStore[currency];
|
|
|
|
if (!b) {
|
|
|
|
balanceStore[currency] = {
|
|
|
|
available: Amounts.getZero(currency),
|
|
|
|
pendingIncoming: Amounts.getZero(currency),
|
|
|
|
pendingOutgoing: Amounts.getZero(currency),
|
2019-12-02 00:42:40 +01:00
|
|
|
};
|
|
|
|
}
|
2020-07-28 19:47:12 +02:00
|
|
|
return balanceStore[currency];
|
2020-07-29 11:03:59 +02:00
|
|
|
};
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-07-28 19:47:12 +02:00
|
|
|
// Initialize balance to zero, even if we didn't start withdrawing yet.
|
2021-06-09 15:14:17 +02:00
|
|
|
await tx.reserves.iter().forEach((r) => {
|
2020-07-29 11:03:59 +02:00
|
|
|
const b = initBalance(r.currency);
|
|
|
|
if (!r.initialWithdrawalStarted) {
|
|
|
|
b.pendingIncoming = Amounts.add(
|
|
|
|
b.pendingIncoming,
|
|
|
|
r.initialDenomSel.totalCoinValue,
|
|
|
|
).amount;
|
|
|
|
}
|
2020-05-15 13:43:30 +02:00
|
|
|
});
|
|
|
|
|
2021-06-09 15:14:17 +02:00
|
|
|
await tx.coins.iter().forEach((c) => {
|
2020-07-28 19:47:12 +02:00
|
|
|
// Only count fresh coins, as dormant coins will
|
|
|
|
// already be in a refresh session.
|
2020-03-06 15:09:55 +01:00
|
|
|
if (c.status === CoinStatus.Fresh) {
|
2020-07-28 19:47:12 +02:00
|
|
|
const b = initBalance(c.currentAmount.currency);
|
|
|
|
b.available = Amounts.add(b.available, c.currentAmount).amount;
|
2020-03-06 15:09:55 +01:00
|
|
|
}
|
|
|
|
});
|
2020-05-15 13:43:30 +02:00
|
|
|
|
2021-06-09 15:14:17 +02:00
|
|
|
await tx.refreshGroups.iter().forEach((r) => {
|
2020-03-06 15:09:55 +01:00
|
|
|
// 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) {
|
2020-07-28 19:47:12 +02:00
|
|
|
const b = initBalance(session.amountRefreshOutput.currency);
|
|
|
|
// We are always assuming the refresh will succeed, thus we
|
|
|
|
// report the output as available balance.
|
2020-09-04 08:34:11 +02:00
|
|
|
b.available = Amounts.add(
|
|
|
|
b.available,
|
|
|
|
session.amountRefreshOutput,
|
|
|
|
).amount;
|
2020-09-01 19:31:44 +02:00
|
|
|
} else {
|
|
|
|
const b = initBalance(r.inputPerCoin[i].currency);
|
2020-09-04 08:34:11 +02:00
|
|
|
b.available = Amounts.add(
|
|
|
|
b.available,
|
|
|
|
r.estimatedOutputPerCoin[i],
|
|
|
|
).amount;
|
2020-03-06 15:09:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-12-03 01:33:25 +01:00
|
|
|
|
2021-06-09 15:14:17 +02:00
|
|
|
await tx.withdrawalGroups.iter().forEach((wds) => {
|
2020-07-28 19:47:12 +02:00
|
|
|
if (wds.timestampFinish) {
|
2020-03-06 15:09:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-07-28 19:47:12 +02:00
|
|
|
const b = initBalance(wds.denomsSel.totalWithdrawCost.currency);
|
2020-07-29 11:03:59 +02:00
|
|
|
b.pendingIncoming = Amounts.add(
|
|
|
|
b.pendingIncoming,
|
|
|
|
wds.denomsSel.totalCoinValue,
|
|
|
|
).amount;
|
2020-03-06 15:09:55 +01:00
|
|
|
});
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-07-28 19:47:12 +02:00
|
|
|
const balancesResponse: BalancesResponse = {
|
|
|
|
balances: [],
|
|
|
|
};
|
|
|
|
|
2020-07-29 11:03:59 +02:00
|
|
|
Object.keys(balanceStore)
|
|
|
|
.sort()
|
|
|
|
.forEach((c) => {
|
|
|
|
const v = balanceStore[c];
|
|
|
|
balancesResponse.balances.push({
|
|
|
|
available: Amounts.stringify(v.available),
|
|
|
|
pendingIncoming: Amounts.stringify(v.pendingIncoming),
|
|
|
|
pendingOutgoing: Amounts.stringify(v.pendingOutgoing),
|
|
|
|
hasPendingTransactions: false,
|
|
|
|
requiresUserInput: false,
|
|
|
|
});
|
2020-07-28 19:47:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return balancesResponse;
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2020-03-06 15:09:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get detailed balance information, sliced by exchange and by currency.
|
|
|
|
*/
|
|
|
|
export async function getBalances(
|
|
|
|
ws: InternalWalletState,
|
2020-07-28 19:47:12 +02:00
|
|
|
): Promise<BalancesResponse> {
|
2020-03-06 15:09:55 +01:00
|
|
|
logger.trace("starting to compute balance");
|
|
|
|
|
2021-06-09 15:14:17 +02:00
|
|
|
const wbal = await ws.db
|
|
|
|
.mktx((x) => ({
|
|
|
|
coins: x.coins,
|
|
|
|
refreshGroups: x.refreshGroups,
|
|
|
|
reserves: x.reserves,
|
|
|
|
purchases: x.purchases,
|
|
|
|
withdrawalGroups: x.withdrawalGroups,
|
|
|
|
}))
|
|
|
|
.runReadOnly(async (tx) => {
|
2020-03-30 12:39:32 +02:00
|
|
|
return getBalancesInsideTransaction(ws, tx);
|
2021-06-09 15:14:17 +02:00
|
|
|
});
|
2020-05-11 18:17:35 +02:00
|
|
|
|
|
|
|
logger.trace("finished computing wallet balance");
|
|
|
|
|
|
|
|
return wbal;
|
2020-03-06 15:09:55 +01:00
|
|
|
}
|