aboutsummaryrefslogtreecommitdiff
path: root/src/operations/transactions.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-05-12 14:08:58 +0530
committerFlorian Dold <florian.dold@gmail.com>2020-05-12 14:09:10 +0530
commit6206b418ff88a238762a18e7b6eeaceafc5de294 (patch)
treec4d947770ccf50e362abf083953f55140046fc2a /src/operations/transactions.ts
parent857a2b9dcaf64d4298027644f8e6716fa22db941 (diff)
downloadwallet-core-6206b418ff88a238762a18e7b6eeaceafc5de294.tar.xz
new transactions API: withdrawal
Diffstat (limited to 'src/operations/transactions.ts')
-rw-r--r--src/operations/transactions.ts130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/operations/transactions.ts b/src/operations/transactions.ts
new file mode 100644
index 000000000..8333b66c6
--- /dev/null
+++ b/src/operations/transactions.ts
@@ -0,0 +1,130 @@
+/*
+ This file is part of GNU Taler
+ (C) 2019 Taler Systems S.A.
+
+ 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.
+ */
+import { InternalWalletState } from "./state";
+import { Stores, ProposalRecord, ReserveRecordStatus } from "../types/dbTypes";
+import { Amounts } from "../util/amounts";
+import { timestampCmp } from "../util/time";
+import {
+ TransactionsRequest,
+ TransactionsResponse,
+ Transaction,
+ TransactionType,
+} from "../types/transactions";
+import { OrderShortInfo } from "../types/history";
+
+/**
+ * Create an event ID from the type and the primary key for the event.
+ */
+function makeEventId(type: TransactionType, ...args: string[]): string {
+ return type + ";" + args.map((x) => encodeURIComponent(x)).join(";");
+}
+
+function getOrderShortInfo(
+ proposal: ProposalRecord,
+): OrderShortInfo | undefined {
+ const download = proposal.download;
+ if (!download) {
+ return undefined;
+ }
+ return {
+ amount: Amounts.stringify(download.contractData.amount),
+ fulfillmentUrl: download.contractData.fulfillmentUrl,
+ orderId: download.contractData.orderId,
+ merchantBaseUrl: download.contractData.merchantBaseUrl,
+ proposalId: proposal.proposalId,
+ summary: download.contractData.summary,
+ };
+}
+
+/**
+ * Retrive the full event history for this wallet.
+ */
+export async function getTransactions(
+ ws: InternalWalletState,
+ transactionsRequest?: TransactionsRequest,
+): Promise<TransactionsResponse> {
+ const transactions: Transaction[] = [];
+
+ await ws.db.runWithReadTransaction(
+ [
+ Stores.currencies,
+ Stores.coins,
+ Stores.denominations,
+ Stores.proposals,
+ Stores.purchases,
+ Stores.refreshGroups,
+ Stores.reserves,
+ Stores.reserveHistory,
+ Stores.tips,
+ Stores.withdrawalGroups,
+ Stores.payEvents,
+ Stores.planchets,
+ Stores.refundEvents,
+ Stores.reserveUpdatedEvents,
+ Stores.recoupGroups,
+ ],
+ async (tx) => {
+ tx.iter(Stores.withdrawalGroups).forEach((wsr) => {
+ if (wsr.timestampFinish) {
+ transactions.push({
+ type: TransactionType.Withdrawal,
+ amountEffective: Amounts.stringify(wsr.denomsSel.totalWithdrawCost),
+ amountRaw: Amounts.stringify(wsr.denomsSel.totalCoinValue),
+ confirmed: true,
+ exchangeBaseUrl: wsr.exchangeBaseUrl,
+ pending: !wsr.timestampFinish,
+ timestamp: wsr.timestampStart,
+ transactionId: makeEventId(
+ TransactionType.Withdrawal,
+ wsr.withdrawalGroupId,
+ ),
+ });
+ }
+ });
+
+ tx.iter(Stores.reserves).forEach((r) => {
+ if (r.reserveStatus !== ReserveRecordStatus.WAIT_CONFIRM_BANK) {
+ return;
+ }
+ if (!r.bankInfo) {
+ return;
+ }
+ transactions.push({
+ type: TransactionType.Withdrawal,
+ confirmed: false,
+ amountRaw: Amounts.stringify(r.bankInfo.amount),
+ amountEffective: undefined,
+ exchangeBaseUrl: undefined,
+ pending: true,
+ timestamp: r.timestampCreated,
+ bankConfirmationUrl: r.bankInfo.confirmUrl,
+ transactionId: makeEventId(
+ TransactionType.Withdrawal,
+ r.bankInfo.bankWithdrawalGroupId,
+ ),
+ });
+ });
+ },
+ );
+
+ transactions.sort((h1, h2) => timestampCmp(h1.timestamp, h2.timestamp));
+
+ return { transactions };
+}