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.
|
|
|
|
*/
|
2019-12-12 20:53:15 +01:00
|
|
|
import { WalletBalance, WalletBalanceEntry } from "../types/walletTypes";
|
2020-04-07 10:07:32 +02:00
|
|
|
import { TransactionHandle } from "../util/query";
|
2019-12-02 00:42:40 +01:00
|
|
|
import { InternalWalletState } from "./state";
|
2020-04-07 10:07:32 +02:00
|
|
|
import { Stores, CoinStatus } from "../types/dbTypes";
|
2019-12-02 00:42:40 +01:00
|
|
|
import * as Amounts from "../util/amounts";
|
|
|
|
import { AmountJson } from "../util/amounts";
|
|
|
|
import { Logger } from "../util/logging";
|
|
|
|
|
|
|
|
const logger = new Logger("withdraw.ts");
|
|
|
|
|
|
|
|
/**
|
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,
|
2020-03-06 15:09:55 +01:00
|
|
|
tx: TransactionHandle,
|
2019-12-02 00:42:40 +01:00
|
|
|
): Promise<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.
|
|
|
|
*/
|
|
|
|
function addTo(
|
|
|
|
balance: WalletBalance,
|
|
|
|
field: keyof WalletBalanceEntry,
|
|
|
|
amount: AmountJson,
|
|
|
|
exchange: string,
|
|
|
|
): void {
|
|
|
|
const z = Amounts.getZero(amount.currency);
|
|
|
|
const balanceIdentity = {
|
|
|
|
available: z,
|
|
|
|
paybackAmount: z,
|
|
|
|
pendingIncoming: z,
|
|
|
|
pendingPayment: z,
|
|
|
|
pendingIncomingDirty: z,
|
|
|
|
pendingIncomingRefresh: z,
|
|
|
|
pendingIncomingWithdraw: z,
|
|
|
|
};
|
|
|
|
let entryCurr = balance.byCurrency[amount.currency];
|
|
|
|
if (!entryCurr) {
|
|
|
|
balance.byCurrency[amount.currency] = entryCurr = {
|
|
|
|
...balanceIdentity,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let entryEx = balance.byExchange[exchange];
|
|
|
|
if (!entryEx) {
|
|
|
|
balance.byExchange[exchange] = entryEx = { ...balanceIdentity };
|
|
|
|
}
|
|
|
|
entryCurr[field] = Amounts.add(entryCurr[field], amount).amount;
|
|
|
|
entryEx[field] = Amounts.add(entryEx[field], amount).amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
const balanceStore = {
|
|
|
|
byCurrency: {},
|
|
|
|
byExchange: {},
|
|
|
|
};
|
|
|
|
|
2020-05-15 13:43:30 +02:00
|
|
|
await tx.iter(Stores.reserves).forEach((r) => {
|
|
|
|
const z = Amounts.getZero(r.currency);
|
|
|
|
addTo(balanceStore, "available", z, r.exchangeBaseUrl);
|
|
|
|
});
|
|
|
|
|
2020-03-30 12:39:32 +02:00
|
|
|
await tx.iter(Stores.coins).forEach((c) => {
|
2020-03-06 15:09:55 +01:00
|
|
|
if (c.suspended) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c.status === CoinStatus.Fresh) {
|
|
|
|
addTo(balanceStore, "available", c.currentAmount, c.exchangeBaseUrl);
|
|
|
|
}
|
|
|
|
});
|
2020-05-15 13:43:30 +02:00
|
|
|
|
2020-03-30 12:39:32 +02:00
|
|
|
await tx.iter(Stores.refreshGroups).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) {
|
2019-12-03 01:33:25 +01:00
|
|
|
addTo(
|
|
|
|
balanceStore,
|
|
|
|
"pendingIncoming",
|
2020-03-06 15:09:55 +01:00
|
|
|
session.amountRefreshOutput,
|
|
|
|
session.exchangeBaseUrl,
|
|
|
|
);
|
|
|
|
addTo(
|
|
|
|
balanceStore,
|
|
|
|
"pendingIncomingRefresh",
|
|
|
|
session.amountRefreshOutput,
|
|
|
|
session.exchangeBaseUrl,
|
2019-12-03 01:33:25 +01:00
|
|
|
);
|
2020-03-06 15:09:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-12-03 01:33:25 +01:00
|
|
|
|
2020-05-11 14:33:25 +02:00
|
|
|
// FIXME: re-implement
|
|
|
|
// await tx.iter(Stores.withdrawalGroups).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);
|
|
|
|
// });
|
2020-03-06 15:09:55 +01:00
|
|
|
|
2020-03-30 12:39:32 +02:00
|
|
|
await tx.iter(Stores.purchases).forEach((t) => {
|
2020-03-06 15:09:55 +01:00
|
|
|
if (t.timestampFirstSuccessfulPay) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-21 08:53:48 +02:00
|
|
|
for (const c of t.coinDepositPermissions) {
|
2020-03-06 15:09:55 +01:00
|
|
|
addTo(
|
|
|
|
balanceStore,
|
|
|
|
"pendingPayment",
|
|
|
|
Amounts.parseOrThrow(c.contribution),
|
|
|
|
c.exchange_url,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
return balanceStore;
|
|
|
|
}
|
2020-03-06 15:09:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get detailed balance information, sliced by exchange and by currency.
|
|
|
|
*/
|
|
|
|
export async function getBalances(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
): Promise<WalletBalance> {
|
|
|
|
logger.trace("starting to compute balance");
|
|
|
|
|
2020-05-11 18:17:35 +02:00
|
|
|
const wbal = await ws.db.runWithReadTransaction(
|
2020-03-30 12:39:32 +02:00
|
|
|
[
|
|
|
|
Stores.coins,
|
|
|
|
Stores.refreshGroups,
|
|
|
|
Stores.reserves,
|
|
|
|
Stores.purchases,
|
2020-04-02 17:03:01 +02:00
|
|
|
Stores.withdrawalGroups,
|
2020-03-30 12:39:32 +02:00
|
|
|
],
|
|
|
|
async (tx) => {
|
|
|
|
return getBalancesInsideTransaction(ws, tx);
|
|
|
|
},
|
|
|
|
);
|
2020-05-11 18:17:35 +02:00
|
|
|
|
|
|
|
logger.trace("finished computing wallet balance");
|
|
|
|
|
|
|
|
return wbal;
|
2020-03-06 15:09:55 +01:00
|
|
|
}
|