Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): add void onchain tx script #4594

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading