Skip to content

Commit

Permalink
chore(core): add void onchain tx script (#4594)
Browse files Browse the repository at this point in the history
* chore(core): add void onchain tx script

* fix: lint issues
  • Loading branch information
dolcalmi authored Oct 8, 2024
1 parent 39ee56a commit 3c48af2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
59 changes: 59 additions & 0 deletions core/api/src/debug/void-onchain-payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* how to run:
*
* pnpm tsx src/debug/void-onchain-payment.ts <journal id> <payout id>
*
* <journal id>: journal id to void.
* <payout id>: bria payout id
*/

import { isUp } from "@/services/lnd/health"
import { lndsConnect } from "@/services/lnd/auth"
import * as LedgerFacade from "@/services/ledger/facade"
import { setupMongoConnection } from "@/services/mongodb"

const voidOnchainPayment = async ({
journalId,
payoutId,
}: {
journalId: LedgerJournalId
payoutId: PayoutId
}) => {
const ledgerTransactions = await LedgerFacade.getTransactionsByPayoutId(payoutId)
if (ledgerTransactions instanceof Error) return ledgerTransactions

const payment = ledgerTransactions.find(
(tx) => tx.pendingConfirmation && tx.journalId === journalId,
)
if (!payment) return new Error("Invalid payment payoutId or payment has been handled")

const reverted = await LedgerFacade.recordOnChainSendRevert({
journalId,
payoutId,
})
if (reverted instanceof Error) return reverted

return true
}

const main = async () => {
const args = process.argv.slice(-2)
const params = {
journalId: args[0] as LedgerJournalId,
payoutId: args[1] as PayoutId,
}
const result = await voidOnchainPayment(params)
if (result instanceof Error) {
console.error("Error:", result)
return
}
console.log(`Voided payment for payout id ${params.payoutId}: `, result)
}

setupMongoConnection()
.then(async (mongoose) => {
await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams)))
await main()
if (mongoose) await mongoose.connection.close()
})
.catch((err) => console.log(err))
3 changes: 3 additions & 0 deletions core/api/src/services/ledger/facade/onchain-send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ export const recordOnChainSendRevert = async ({
return new NoTransactionToUpdateError(JSON.stringify({ journalId }))
}

// pending update must be before void to avoid pending voided records
await Transaction.updateMany({ _journal: toObjectId(journalId) }, { pending: false })

const savedEntry = await MainBook.void(journalId, reason)

const journalEntry = translateToLedgerJournal(savedEntry)
Expand Down

0 comments on commit 3c48af2

Please sign in to comment.