-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): idempotency bug in registerBroadcastHandler (#3558)
* test: add idempotency runs for registerBroadcastedPayout handler * chore: add new 'onchain-reconcile' module to facade * chore: add onChainReconciliationTxn check methods * refactor: change tx filter when setting payoutId * fix: add reconciliation-txn-recorded check to register-broadcast handler This is to fix the idempotency bug introduced in: #3550
- Loading branch information
Showing
7 changed files
with
119 additions
and
41 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
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 @@ | ||
type LedgerFacadeError = import("./errors").LedgerFacadeError |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { MainBook } from "../books" | ||
|
||
import { translateToLedgerTx } from ".." | ||
import { getBankOwnerWalletId } from "../caching" | ||
import { UnknownLedgerError } from "../domain/errors" | ||
import { persistAndReturnEntry } from "../helpers" | ||
import { FeeOnlyEntryBuilder } from "../domain/fee-only-entry-builder" | ||
|
||
import { staticAccountIds } from "./static-account-ids" | ||
|
||
import { LedgerTransactionType, toLiabilitiesWalletId } from "@/domain/ledger" | ||
import { AmountCalculator } from "@/domain/shared" | ||
|
||
const calc = AmountCalculator() | ||
|
||
export const recordReceiveOnChainFeeReconciliation = async ({ | ||
estimatedFee, | ||
actualFee, | ||
metadata, | ||
}: { | ||
estimatedFee: BtcPaymentAmount | ||
actualFee: BtcPaymentAmount | ||
metadata: AddOnChainFeeReconciliationLedgerMetadata | ||
}) => { | ||
const accountIds = await staticAccountIds() | ||
if (accountIds instanceof Error) return accountIds | ||
|
||
let entry = MainBook.entry("") | ||
if (actualFee.amount > estimatedFee.amount) { | ||
const btcFeeDifference = calc.sub(actualFee, estimatedFee) | ||
const builder = FeeOnlyEntryBuilder({ | ||
staticAccountIds: accountIds, | ||
entry, | ||
metadata, | ||
btcFee: btcFeeDifference, | ||
}) | ||
entry = builder.debitBankOwner().creditOnChain() | ||
} else { | ||
const btcFeeDifference = calc.sub(estimatedFee, actualFee) | ||
const builder = FeeOnlyEntryBuilder({ | ||
staticAccountIds: accountIds, | ||
entry, | ||
metadata, | ||
btcFee: btcFeeDifference, | ||
}) | ||
entry = builder.debitOnChain().creditBankOwner() | ||
} | ||
|
||
return persistAndReturnEntry({ | ||
entry, | ||
hash: metadata.hash, | ||
}) | ||
} | ||
|
||
export const isOnChainFeeReconciliationTxn = ( | ||
txn: LedgerTransaction<WalletCurrency>, | ||
): boolean => | ||
txn.type === LedgerTransactionType.OnchainPayment && txn.address === undefined | ||
|
||
export const isOnChainFeeReconciliationRecorded = async ( | ||
payoutId: PayoutId, | ||
): Promise<boolean | LedgerFacadeError> => { | ||
try { | ||
const bankOwnerWalletId = await getBankOwnerWalletId() | ||
const { results } = await MainBook.ledger({ | ||
payout_id: payoutId, | ||
account: toLiabilitiesWalletId(bankOwnerWalletId), | ||
}) | ||
const txns = results.map((tx) => translateToLedgerTx(tx)) | ||
|
||
const reconciliationTxn = txns.find((txn) => isOnChainFeeReconciliationTxn(txn)) | ||
return reconciliationTxn !== undefined | ||
} catch (err) { | ||
return new UnknownLedgerError(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