Skip to content

Commit

Permalink
refactor(core): use new 'LnPaymentAttemptResult' type in use-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed May 7, 2024
1 parent 3b24b92 commit f3bc86d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 43 deletions.
59 changes: 35 additions & 24 deletions core/api/src/app/payments/send-lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { AccountValidator } from "@/domain/accounts"
import {
decodeInvoice,
defaultTimeToExpiryInSeconds,
LnAlreadyPaidError,
LnPaymentPendingError,
PaymentSendStatus,
LnPaymentAttemptResult,
LnPaymentAttemptResultType,
} from "@/domain/bitcoin/lightning"
import { ResourceExpiredLockServiceError } from "@/domain/lock"
import { AlreadyPaidError, CouldNotFindLightningPaymentFlowError } from "@/domain/errors"
import { DisplayAmountsConverter } from "@/domain/fiat"
import {
Expand Down Expand Up @@ -74,8 +75,6 @@ import {
validateIsUsdWallet,
} from "@/app/wallets"

import { ResourceExpiredLockServiceError } from "@/domain/lock"

const dealer = DealerPriceService()
const paymentFlowRepo = PaymentFlowStateRepository(defaultTimeToExpiryInSeconds)

Expand Down Expand Up @@ -916,7 +915,7 @@ const lockedPaymentViaLnSteps = async ({
const { journalId } = journal

// Execute payment
let payResult: PayInvoiceResult | LightningServiceError
let payResult: LnPaymentAttemptResult
if (rawRoute) {
payResult = await lndService.payInvoiceViaRoutes({
paymentHash,
Expand All @@ -937,28 +936,44 @@ const lockedPaymentViaLnSteps = async ({

payResult =
maxFeeCheck instanceof Error
? maxFeeCheck
? LnPaymentAttemptResult.err(maxFeeCheck)
: await lndService.payInvoiceViaPaymentDetails({
...maxFeeCheckArgs,
decodedInvoice,
})
}

if (!(payResult instanceof LnAlreadyPaidError)) {
if (!(payResult.type === LnPaymentAttemptResultType.AlreadyPaid)) {
await LnPaymentsRepository().persistNew({
paymentHash: decodedInvoice.paymentHash,
paymentRequest: decodedInvoice.paymentRequest,
sentFromPubkey: outgoingNodePubkey,
sentFromPubkey:
payResult.type === LnPaymentAttemptResultType.Error
? outgoingNodePubkey
: payResult.result.sentFromPubkey,
})

if (!(payResult instanceof Error))
if (payResult.type === LnPaymentAttemptResultType.Ok)
await LedgerFacade.updateMetadataByHash({
hash: paymentHash,
revealedPreImage: payResult.revealedPreImage,
revealedPreImage: payResult.result.revealedPreImage,
})
}

if (payResult instanceof LnPaymentPendingError) {
if (
outgoingNodePubkey === undefined &&
!(payResult.type === LnPaymentAttemptResultType.Error)
) {
const updatedPubkey = await LedgerFacade.updatePubkeyByHash({
paymentHash,
pubkey: payResult.result.sentFromPubkey,
})
if (updatedPubkey instanceof Error) {
recordExceptionInCurrentSpan({ error: updatedPubkey })
}
}

if (payResult.type === LnPaymentAttemptResultType.Pending) {
paymentFlow.paymentSentAndPending = true
const updateResult = await paymentFlowRepo.updateLightningPaymentFlow(paymentFlow)
if (updateResult instanceof Error) {
Expand All @@ -977,7 +992,10 @@ const lockedPaymentViaLnSteps = async ({
}

// Settle and record reversion entries
if (payResult instanceof Error) {
if (
payResult.type === LnPaymentAttemptResultType.Error ||
payResult.type === LnPaymentAttemptResultType.AlreadyPaid
) {
const settled = await LedgerFacade.settlePendingLnSend(paymentHash)
if (settled instanceof Error) return LnSendAttemptResult.err(settled)

Expand All @@ -995,31 +1013,24 @@ const lockedPaymentViaLnSteps = async ({
if (updateJournalTxnsState instanceof Error) {
return LnSendAttemptResult.err(updateJournalTxnsState)
}
return payResult instanceof LnAlreadyPaidError

return payResult.type === LnPaymentAttemptResultType.AlreadyPaid
? LnSendAttemptResult.alreadyPaid(journalId)
: LnSendAttemptResult.errWithJournal({ journalId, error: payResult })
: LnSendAttemptResult.errWithJournal({ journalId, error: payResult.error })
}

// Settle and conditionally record reimbursement entries
const settled = await LedgerFacade.settlePendingLnSend(paymentHash)
if (settled instanceof Error) return LnSendAttemptResult.err(settled)

const updatedPubkey = await LedgerFacade.updatePubkeyByHash({
paymentHash,
pubkey: payResult.sentFromPubkey,
})
if (updatedPubkey instanceof Error) {
recordExceptionInCurrentSpan({ error: updatedPubkey })
}

if (!rawRoute) {
const reimbursed = await reimburseFee({
paymentFlow,
senderDisplayAmount: toDisplayBaseAmount(displayAmount),
senderDisplayCurrency,
journalId,
actualFee: payResult.roundedUpFee,
revealedPreImage: payResult.revealedPreImage,
actualFee: payResult.result.roundedUpFee,
revealedPreImage: payResult.result.revealedPreImage,
})
if (reimbursed instanceof Error) return LnSendAttemptResult.err(reimbursed)
}
Expand Down
8 changes: 0 additions & 8 deletions core/api/src/app/payments/update-pending-payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,6 @@ const lockedPendingPaymentSteps = async ({
paymentLogger.error({ error: settled }, "no transaction to update")
return settled
}
const updatedPubkey = await LedgerFacade.updatePubkeyByHash({
paymentHash,
pubkey: lnPaymentLookup.sentFromPubkey,
})
if (updatedPubkey instanceof Error) {
paymentLogger.error({ error: updatedPubkey }, "no transaction to update")
return updatedPubkey
}

let roundedUpFee: Satoshis = toSats(0)
let satsAmount: Satoshis | undefined = undefined
Expand Down
1 change: 1 addition & 0 deletions core/api/src/domain/bitcoin/lightning/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
invoiceExpirationForCurrency,
defaultTimeToExpiryInSeconds,
} from "./invoice-expiration"
export * from "./ln-payment-result"
export * from "./errors"

export const PaymentStatus = {
Expand Down
6 changes: 2 additions & 4 deletions core/api/src/services/lnd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ import {
decodeInvoice,
InvalidInvoiceAmountError,
InvoiceAlreadySettledError,
LnPaymentAttemptResult,
LnPaymentAttemptResultType,
} from "@/domain/bitcoin/lightning"
import { CacheKeys } from "@/domain/cache"
import { LnFees } from "@/domain/payments"
Expand All @@ -87,10 +89,6 @@ import { LocalCacheService } from "@/services/cache"
import { wrapAsyncFunctionsToRunInSpan } from "@/services/tracing"

import { timeoutWithCancel } from "@/utils"
import {
LnPaymentAttemptResult,
LnPaymentAttemptResultType,
} from "@/domain/bitcoin/lightning/ln-payment-result"

const TIMEOUT_PAYMENT = NETWORK !== "regtest" ? 45000 : 3000

Expand Down
19 changes: 13 additions & 6 deletions core/api/test/integration/app/wallets/send-lightning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MaxFeeTooLargeForRoutelessPaymentError,
PaymentSendStatus,
decodeInvoice,
LnPaymentAttemptResultType,
} from "@/domain/bitcoin/lightning"
import { UsdDisplayCurrency, toCents } from "@/domain/fiat"
import { LnPaymentRequestNonZeroAmountRequiredError } from "@/domain/payments"
Expand Down Expand Up @@ -430,9 +431,12 @@ describe("initiated via lightning", () => {
defaultPubkey: (): Pubkey => DEFAULT_PUBKEY,
listAllPubkeys: () => [],
payInvoiceViaPaymentDetails: () => ({
roundedUpFee: toSats(0),
revealedPreImage: "revealedPreImage" as RevealedPreImage,
sentFromPubkey: DEFAULT_PUBKEY,
type: LnPaymentAttemptResultType.Ok,
result: {
roundedUpFee: toSats(0),
revealedPreImage: "revealedPreImage" as RevealedPreImage,
sentFromPubkey: DEFAULT_PUBKEY,
},
}),
})

Expand Down Expand Up @@ -549,9 +553,12 @@ describe("initiated via lightning", () => {
defaultPubkey: (): Pubkey => DEFAULT_PUBKEY,
listAllPubkeys: () => [],
payInvoiceViaPaymentDetails: () => ({
roundedUpFee: toSats(0),
revealedPreImage: "revealedPreImage" as RevealedPreImage,
sentFromPubkey: DEFAULT_PUBKEY,
type: LnPaymentAttemptResultType.Ok,
result: {
roundedUpFee: toSats(0),
revealedPreImage: "revealedPreImage" as RevealedPreImage,
sentFromPubkey: DEFAULT_PUBKEY,
},
}),
})

Expand Down
2 changes: 1 addition & 1 deletion core/api/test/integration/services/lnd-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
PaymentStatus,
RouteNotFoundError,
decodeInvoice,
LnPaymentAttemptResultType,
} from "@/domain/bitcoin/lightning"
import { LnFees } from "@/domain/payments"

Expand All @@ -42,7 +43,6 @@ import {
waitFor,
} from "test/helpers"
import { BitcoindWalletClient } from "test/helpers/bitcoind"
import { LnPaymentAttemptResultType } from "@/domain/bitcoin/lightning/ln-payment-result"

const amountInvoice = toSats(1000)
const btcPaymentAmount = { amount: BigInt(amountInvoice), currency: WalletCurrency.Btc }
Expand Down

0 comments on commit f3bc86d

Please sign in to comment.