-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add settle pending onchain payments script (#4668)
* chore: add settle pending onchain payments script * fix: how to run comment * fix: lint issues * fix: handle proto generator default values issue * fix: spell lint
- Loading branch information
Showing
7 changed files
with
187 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* how to run: | ||
* | ||
* pnpm tsx src/debug/settle-pending-onchain-payments.ts | ||
* | ||
*/ | ||
|
||
import { Wallets } from "@/app" | ||
|
||
import { OnChainService } from "@/services/bria" | ||
import { LedgerService } from "@/services/ledger" | ||
import * as LedgerFacade from "@/services/ledger/facade" | ||
import { lndsConnect } from "@/services/lnd/auth" | ||
import { isUp } from "@/services/lnd/health" | ||
import { setupMongoConnection } from "@/services/mongodb" | ||
|
||
const onChainService = OnChainService() | ||
|
||
const processPayment = async (payment: LedgerTransaction<WalletCurrency>) => { | ||
const payout = await onChainService.findPayoutByLedgerJournalId(payment.journalId) | ||
if (payout instanceof Error) { | ||
return new Error(`Failed to get payout: ${payout.name} - ${payout.message}`) | ||
} | ||
if (!payout.batchId || !payout.txId || payout.vout === undefined) { | ||
return new Error("Missing required payout details") | ||
} | ||
|
||
const setTxIdResult = await LedgerFacade.setOnChainTxIdByPayoutId({ | ||
payoutId: payout.id, | ||
txId: payout.txId, | ||
vout: payout.vout, | ||
}) | ||
if (setTxIdResult instanceof Error) { | ||
return new Error( | ||
`Failed to set transaction ID: ${setTxIdResult.name} - ${setTxIdResult.message}`, | ||
) | ||
} | ||
|
||
const settledPayout = await Wallets.settlePayout(payout.id) | ||
if (settledPayout instanceof Error) { | ||
return new Error( | ||
`Failed to settle payout: ${settledPayout.name} - ${settledPayout.message}`, | ||
) | ||
} | ||
|
||
return true | ||
} | ||
|
||
const settlePendingOnchainPayments = async () => { | ||
const pendingPayments = LedgerService().listPendingOnchainPayments() | ||
if (pendingPayments instanceof Error) return pendingPayments | ||
|
||
let totalPayments = 0 | ||
let successCount = 0 | ||
let errorCount = 0 | ||
const errors = [] | ||
|
||
for await (const payment of pendingPayments) { | ||
totalPayments++ | ||
console.log(`Processing payment ${totalPayments}`) | ||
|
||
const result = await processPayment(payment) | ||
if (result instanceof Error) { | ||
errorCount++ | ||
errors.push({ | ||
journalId: payment.journalId, | ||
payoutId: payment.payoutId, | ||
error: result.message, | ||
}) | ||
console.error(`Failed to process payment: ${result.message}`) | ||
continue | ||
} | ||
|
||
successCount++ | ||
console.log( | ||
`Successfully processed payout ${payment.payoutId} for journal ${payment.journalId}`, | ||
) | ||
} | ||
|
||
return { | ||
successCount, | ||
errorCount, | ||
total: totalPayments, | ||
errors, | ||
} | ||
} | ||
|
||
const main = async () => { | ||
const result = await settlePendingOnchainPayments() | ||
if (result instanceof Error) { | ||
console.error("Error:", result) | ||
return | ||
} | ||
console.log("Settlement process completed") | ||
console.log(`Total Processed: ${result.total}`) | ||
console.log(`Successful: ${result.successCount}`) | ||
console.log(`Failed: ${result.errorCount}`) | ||
|
||
if (result.errors.length > 0) { | ||
console.log("\nErrors:") | ||
result.errors.forEach(({ journalId, payoutId, error }) => { | ||
console.log( | ||
`- Journal ${journalId}${payoutId ? ` (Payout ${payoutId})` : ""}: ${error}`, | ||
) | ||
}) | ||
} | ||
} | ||
|
||
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
files.extend-exclude = [ | ||
"dev", | ||
"core/api/dev", | ||
"core/api/src/domain/users/languages.ts", | ||
"core/api/docker-compose.yml", | ||
"quickstart/*.yml", | ||
"quickstart/galoy", | ||
"quickstart/dev", | ||
"prelude", | ||
"core/notifications/locales/*.yml", | ||
"apps/admin-panel/components/notification/languages.ts", | ||
"apps/pay/components/success-animation.json", | ||
"dev", | ||
"core/api/dev", | ||
"core/api/src/domain/users/languages.ts", | ||
"core/api/docker-compose.yml", | ||
"quickstart/*.yml", | ||
"quickstart/galoy", | ||
"quickstart/dev", | ||
"prelude", | ||
"core/notifications/locales/*.yml", | ||
"apps/admin-panel/components/notification/languages.ts", | ||
"apps/pay/components/success-animation.json", | ||
] | ||
|
||
default.extend-ignore-identifiers-re = [ | ||
"^bc1\\w+", | ||
"^lnbc\\w+", | ||
"[Ww][Ss]", | ||
] | ||
default.extend-ignore-identifiers-re = ["^bc1\\w+", "^lnbc\\w+", "[Ww][Ss]"] | ||
|
||
default.extend-ignore-re = ["\"eyJ[a-zA-Z0-9_\\-\\.]+\"", | ||
"moneyImportantGovernement", | ||
"GovernementCanPrintMoney", | ||
"moneySocialAggrement", | ||
default.extend-ignore-re = [ | ||
"\"eyJ[a-zA-Z0-9_\\-\\.]+\"", | ||
"moneyImportantGovernement", | ||
"GovernementCanPrintMoney", | ||
"moneySocialAggrement", | ||
] | ||
|
||
[default.extend-words] | ||
# returned from twilio API referenced in src/debug/text-sms.ts | ||
Ons = "Ons" | ||
Lsat = "Lsat" | ||
quizs = "quizs" | ||
nin = "nin" |