From e41e4d525a9b8b2854826d13de0f374dfe710dd6 Mon Sep 17 00:00:00 2001 From: vindard <17693119+vindard@users.noreply.github.com> Date: Tue, 24 Oct 2023 11:50:40 -0400 Subject: [PATCH] feat: complete PayoutCancelled handler implementation --- core/api/src/servers/event-handlers/bria.ts | 27 ++++++++++++------- .../services/ledger/facade/onchain-send.ts | 24 +++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/core/api/src/servers/event-handlers/bria.ts b/core/api/src/servers/event-handlers/bria.ts index a370395ce40..3f2f8d8e9ff 100644 --- a/core/api/src/servers/event-handlers/bria.ts +++ b/core/api/src/servers/event-handlers/bria.ts @@ -5,7 +5,6 @@ import { CouldNotFindWalletFromOnChainAddressError, LessThanDustThresholdError, NoTransactionToUpdateError, - NotImplementedError, } from "@/domain/errors" import { NoTransactionToSettleError } from "@/services/ledger/domain/errors" @@ -58,7 +57,13 @@ export const briaEventHandler = async (event: BriaEvent): Promise => { - const txns = await LedgerFacade.getTransactionsByPayoutId(event.id) - if (txns instanceof Error) return txns - - if (txns.length !== 0) - return new NotImplementedError( - `Payout cancels not implemented as yet for PayoutId: ${event.id}`, - ) + const res = await LedgerFacade.recordOnChainSendRevert({ + journalId: payoutInfo.externalId as LedgerJournalId, + payoutId: event.id, + }) + if (res instanceof NoTransactionToUpdateError) { + return true + } - return true + return res } export const payoutBroadcastEventHandler = async ({ diff --git a/core/api/src/services/ledger/facade/onchain-send.ts b/core/api/src/services/ledger/facade/onchain-send.ts index 70e1bab1823..af1efd76470 100644 --- a/core/api/src/services/ledger/facade/onchain-send.ts +++ b/core/api/src/services/ledger/facade/onchain-send.ts @@ -1,3 +1,5 @@ +import { JournalNotFoundError } from "medici" + import { MainBook, Transaction } from "../books" import { getBankOwnerWalletId, getNonEndUserWalletIds } from "../caching" @@ -100,6 +102,28 @@ export const setOnChainTxPayoutId = async ({ } } +export const recordOnChainSendRevert = async ({ + journalId, + payoutId, +}: SetOnChainTxPayoutIdArgs): Promise => { + const reason = "Payment canceled" + try { + if (!isValidObjectId(journalId)) { + return new NoTransactionToUpdateError(JSON.stringify({ journalId })) + } + + await MainBook.void(journalId, reason) + + return true + } catch (err) { + if (err instanceof JournalNotFoundError) { + return new NoTransactionToUpdateError(JSON.stringify({ journalId, payoutId })) + } + + return new UnknownLedgerError(err) + } +} + export const setOnChainTxIdByPayoutId = async ({ payoutId, txId,