helpers and tests for reserve reconciliation

This commit is contained in:
Florian Dold 2020-04-02 14:29:16 +05:30
parent 63cf437633
commit 62de27d2ac
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
5 changed files with 93 additions and 12 deletions

View File

@ -115,6 +115,7 @@ export async function createReserve(
retryInfo: initRetryInfo(),
lastError: undefined,
reserveTransactions: [],
history: [],
};
const senderWire = req.senderWire;

View File

@ -39,7 +39,7 @@ import { Timestamp, codecForTimestamp } from "../util/time";
export const enum ReserveTransactionType {
Withdraw = "WITHDRAW",
Deposit = "CREDIT",
Credit = "CREDIT",
Recoup = "RECOUP",
Closing = "CLOSING",
}
@ -74,8 +74,8 @@ export interface ReserveWithdrawTransaction {
withdraw_fee: AmountString;
}
export interface ReserveDepositTransaction {
type: ReserveTransactionType.Deposit;
export interface ReserveCreditTransaction {
type: ReserveTransactionType.Credit;
/**
* Amount withdrawn.
@ -175,7 +175,7 @@ export interface ReserveRecoupTransaction {
*/
export type ReserveTransaction =
| ReserveWithdrawTransaction
| ReserveDepositTransaction
| ReserveCreditTransaction
| ReserveClosingTransaction
| ReserveRecoupTransaction;
@ -194,15 +194,15 @@ export const codecForReserveWithdrawTransaction = () =>
.build("ReserveWithdrawTransaction"),
);
export const codecForReserveDepositTransaction = () =>
typecheckedCodec<ReserveDepositTransaction>(
makeCodecForObject<ReserveDepositTransaction>()
export const codecForReserveCreditTransaction = () =>
typecheckedCodec<ReserveCreditTransaction>(
makeCodecForObject<ReserveCreditTransaction>()
.property("amount", codecForString)
.property("sender_account_url", codecForString)
.property("timestamp", codecForTimestamp)
.property("wire_reference", codecForString)
.property("type", makeCodecForConstString(ReserveTransactionType.Deposit))
.build("ReserveDepositTransaction"),
.property("type", makeCodecForConstString(ReserveTransactionType.Credit))
.build("ReserveCreditTransaction"),
);
export const codecForReserveClosingTransaction = () =>
@ -248,8 +248,8 @@ export const codecForReserveTransaction = () =>
codecForReserveRecoupTransaction(),
)
.alternative(
ReserveTransactionType.Deposit,
codecForReserveDepositTransaction(),
ReserveTransactionType.Credit,
codecForReserveCreditTransaction(),
)
.build<ReserveTransaction>("ReserveTransaction"),
);

View File

@ -35,8 +35,9 @@ import {
import { Index, Store } from "../util/query";
import { OperationError, RefreshReason } from "./walletTypes";
import { ReserveTransaction } from "./ReserveTransaction";
import { ReserveTransaction, ReserveCreditTransaction, ReserveWithdrawTransaction, ReserveClosingTransaction, ReserveRecoupTransaction } from "./ReserveTransaction";
import { Timestamp, Duration, getTimestampNow } from "../util/time";
import { Wallet } from "../wallet";
export enum ReserveRecordStatus {
/**
@ -131,6 +132,71 @@ export function initRetryInfo(
return info;
}
export const enum WalletReserveHistoryItemType {
Credit = "credit",
Withdraw = "withdraw",
Closing = "closing",
Recoup = "recoup",
}
export interface WalletReserveHistoryCreditItem {
type: WalletReserveHistoryItemType.Credit;
/**
* Amount we expect to see credited.
*/
expectedAmount?: string;
/**
* Item from the reserve transaction history that this
* wallet reserve history item matches up with.
*/
matchedExchangeTransaction?: ReserveCreditTransaction;
}
export interface WalletReserveHistoryWithdrawItem {
expectedAmount?: string;
type: WalletReserveHistoryItemType.Withdraw;
/**
* Item from the reserve transaction history that this
* wallet reserve history item matches up with.
*/
matchedExchangeTransaction?: ReserveWithdrawTransaction;
}
export interface WalletReserveHistoryClosingItem {
type: WalletReserveHistoryItemType.Closing;
/**
* Item from the reserve transaction history that this
* wallet reserve history item matches up with.
*/
matchedExchangeTransaction?: ReserveClosingTransaction;
}
export interface WalletReserveHistoryRecoupItem {
type: WalletReserveHistoryItemType.Recoup;
/**
* Amount we expect to see recouped.
*/
expectedAmount?: string;
/**
* Item from the reserve transaction history that this
* wallet reserve history item matches up with.
*/
matchedExchangeTransaction?: ReserveRecoupTransaction;
}
export type WalletReserveHistoryItem =
| WalletReserveHistoryCreditItem
| WalletReserveHistoryWithdrawItem
| WalletReserveHistoryRecoupItem
| WalletReserveHistoryClosingItem;
/**
* A reserve record as stored in the wallet's database.
*/
@ -234,6 +300,13 @@ export interface ReserveRecord {
lastError: OperationError | undefined;
reserveTransactions: ReserveTransaction[];
/**
* History of the reserve as modeled by the wallet.
* Reconciled with the history kept by the exchange
* when we request the reserve status.
*/
history: WalletReserveHistoryItem[];
}
/**

View File

@ -102,6 +102,11 @@ export function deepEquals(x: any, y: any): boolean {
);
}
export function deepCopy(x: any): any {
// FIXME: this has many issues ...
return JSON.parse(JSON.stringify(x));
}
/**
* Map from a collection to a list or results and then
* concatenate the results.

View File

@ -86,6 +86,8 @@
"src/util/payto.ts",
"src/util/promiseUtils.ts",
"src/util/query.ts",
"src/util/reserveHistoryUtil-test.ts",
"src/util/reserveHistoryUtil.ts",
"src/util/talerconfig.ts",
"src/util/taleruri-test.ts",
"src/util/taleruri.ts",