From 62de27d2acc2a59e8125d2b7d2cbcf6a41bdc62d Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 2 Apr 2020 14:29:16 +0530 Subject: helpers and tests for reserve reconciliation --- src/operations/reserves.ts | 1 + src/types/ReserveTransaction.ts | 22 ++++++------ src/types/dbTypes.ts | 75 ++++++++++++++++++++++++++++++++++++++++- src/util/helpers.ts | 5 +++ 4 files changed, 91 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/operations/reserves.ts b/src/operations/reserves.ts index 5cf189d3b..2e1b487b9 100644 --- a/src/operations/reserves.ts +++ b/src/operations/reserves.ts @@ -115,6 +115,7 @@ export async function createReserve( retryInfo: initRetryInfo(), lastError: undefined, reserveTransactions: [], + history: [], }; const senderWire = req.senderWire; diff --git a/src/types/ReserveTransaction.ts b/src/types/ReserveTransaction.ts index 3b2553def..cebccd2dc 100644 --- a/src/types/ReserveTransaction.ts +++ b/src/types/ReserveTransaction.ts @@ -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( - makeCodecForObject() +export const codecForReserveCreditTransaction = () => + typecheckedCodec( + makeCodecForObject() .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"), ); diff --git a/src/types/dbTypes.ts b/src/types/dbTypes.ts index db71db710..c88148e7e 100644 --- a/src/types/dbTypes.ts +++ b/src/types/dbTypes.ts @@ -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[]; } /** diff --git a/src/util/helpers.ts b/src/util/helpers.ts index 130dcdaef..0e19d7aba 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -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. -- cgit v1.2.3