Skip to content

Commit

Permalink
chore: remove LnAndWalletInvoice type
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Oct 17, 2023
1 parent 5aae4f8 commit a0b3481
Show file tree
Hide file tree
Showing 32 changed files with 418 additions and 407 deletions.
25 changes: 12 additions & 13 deletions core/api/src/app/wallets/add-invoice-for-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const addInvoiceForSelf = async ({
walletAmount,
memo = "",
expiresIn,
}: AddInvoiceForSelfArgs): Promise<LnInvoice | ApplicationError> =>
}: AddInvoiceForSelfArgs): Promise<WalletInvoice | ApplicationError> =>
addInvoice({
walletId,
limitCheckFn: checkSelfWalletIdRateLimits,
Expand All @@ -45,7 +45,7 @@ const addInvoiceForSelf = async ({

export const addInvoiceForSelfForBtcWallet = async (
args: AddInvoiceForSelfForBtcWalletArgs,
): Promise<LnInvoice | ApplicationError> => {
): Promise<WalletInvoice | ApplicationError> => {
const walletId = checkedToWalletId(args.walletId)
if (walletId instanceof Error) return walletId

Expand All @@ -63,7 +63,7 @@ export const addInvoiceForSelfForBtcWallet = async (

export const addInvoiceForSelfForUsdWallet = async (
args: AddInvoiceForSelfForUsdWalletArgs,
): Promise<LnInvoice | ApplicationError> => {
): Promise<WalletInvoice | ApplicationError> => {
const walletId = checkedToWalletId(args.walletId)
if (walletId instanceof Error) return walletId

Expand All @@ -83,7 +83,7 @@ export const addInvoiceNoAmountForSelf = async ({
walletId,
memo = "",
expiresIn,
}: AddInvoiceNoAmountForSelfArgs): Promise<LnInvoice | ApplicationError> => {
}: AddInvoiceNoAmountForSelfArgs): Promise<WalletInvoice | ApplicationError> => {
const walletIdChecked = checkedToWalletId(walletId)
if (walletIdChecked instanceof Error) return walletIdChecked

Expand Down Expand Up @@ -118,7 +118,7 @@ const addInvoiceForRecipient = async ({
memo = "",
descriptionHash,
expiresIn,
}: AddInvoiceForRecipientArgs): Promise<LnInvoice | ApplicationError> =>
}: AddInvoiceForRecipientArgs): Promise<WalletInvoice | ApplicationError> =>
addInvoice({
walletId: recipientWalletId,
limitCheckFn: checkRecipientWalletIdRateLimits,
Expand All @@ -136,7 +136,7 @@ const addInvoiceForRecipient = async ({

export const addInvoiceForRecipientForBtcWallet = async (
args: AddInvoiceForRecipientForBtcWalletArgs,
): Promise<LnInvoice | ApplicationError> => {
): Promise<WalletInvoice | ApplicationError> => {
const recipientWalletId = checkedToWalletId(args.recipientWalletId)
if (recipientWalletId instanceof Error) return recipientWalletId

Expand All @@ -160,7 +160,7 @@ export const addInvoiceForRecipientForBtcWallet = async (

export const addInvoiceForRecipientForUsdWallet = async (
args: AddInvoiceForRecipientForUsdWalletArgs,
): Promise<LnInvoice | ApplicationError> => {
): Promise<WalletInvoice | ApplicationError> => {
const recipientWalletId = checkedToWalletId(args.recipientWalletId)
if (recipientWalletId instanceof Error) return recipientWalletId

Expand All @@ -184,7 +184,7 @@ export const addInvoiceForRecipientForUsdWallet = async (

export const addInvoiceForRecipientForUsdWalletAndBtcAmount = async (
args: AddInvoiceForRecipientForUsdWalletArgs,
): Promise<LnInvoice | ApplicationError> => {
): Promise<WalletInvoice | ApplicationError> => {
const recipientWalletId = checkedToWalletId(args.recipientWalletId)
if (recipientWalletId instanceof Error) return recipientWalletId

Expand All @@ -210,7 +210,7 @@ export const addInvoiceNoAmountForRecipient = async ({
recipientWalletId,
memo = "",
expiresIn,
}: AddInvoiceNoAmountForRecipientArgs): Promise<LnInvoice | ApplicationError> => {
}: AddInvoiceNoAmountForRecipientArgs): Promise<WalletInvoice | ApplicationError> => {
const walletId = checkedToWalletId(recipientWalletId)
if (walletId instanceof Error) return walletId

Expand Down Expand Up @@ -243,7 +243,7 @@ const addInvoice = async ({
walletId,
limitCheckFn,
buildWIBWithAmountFn,
}: AddInvoiceArgs): Promise<LnInvoice | ApplicationError> => {
}: AddInvoiceArgs): Promise<WalletInvoice | ApplicationError> => {
const wallet = await WalletsRepository().findById(walletId)
if (wallet instanceof Error) return wallet
const account = await AccountsRepository().findById(wallet.accountId)
Expand Down Expand Up @@ -275,12 +275,11 @@ const addInvoice = async ({

const invoice = await walletIBWithAmount.registerInvoice()
if (invoice instanceof Error) return invoice
const { walletInvoice, lnInvoice } = invoice

const persistedInvoice = await WalletInvoicesRepository().persistNew(walletInvoice)
const persistedInvoice = await WalletInvoicesRepository().persistNew(invoice)
if (persistedInvoice instanceof Error) return persistedInvoice

return lnInvoice
return invoice
}

const checkSelfWalletIdRateLimits = async (
Expand Down
22 changes: 5 additions & 17 deletions core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { decodeInvoice } from "@/domain/bitcoin/lightning"
import { CouldNotFindWalletInvoiceError } from "@/domain/errors"
import { ensureWalletInvoiceHasPaymentRequest } from "@/domain/wallet-invoices"
import { WalletInvoicesRepository } from "@/services/mongoose"

export const getInvoiceForWalletByPaymentHash = async ({
Expand All @@ -9,26 +7,16 @@ export const getInvoiceForWalletByPaymentHash = async ({
}: {
walletId: WalletId
paymentHash: PaymentHash
}): Promise<LnInvoice | ApplicationError> => {
}): Promise<WalletInvoice | ApplicationError> => {
const walletInvoicesRepository = WalletInvoicesRepository()

const walletInvoiceWithOptionalPaymentRequest =
await walletInvoicesRepository.findByPaymentHash(paymentHash)
const walletInvoice = await walletInvoicesRepository.findByPaymentHash(paymentHash)

if (walletInvoiceWithOptionalPaymentRequest instanceof Error)
return walletInvoiceWithOptionalPaymentRequest
if (walletInvoice instanceof Error) return walletInvoice

if (walletInvoiceWithOptionalPaymentRequest.recipientWalletDescriptor.id !== walletId) {
if (walletInvoice.recipientWalletDescriptor.id !== walletId) {
return new CouldNotFindWalletInvoiceError()
}

const walletInvoice = ensureWalletInvoiceHasPaymentRequest(
walletInvoiceWithOptionalPaymentRequest,
)

if (walletInvoice instanceof Error) return walletInvoice

const lnInvoice = decodeInvoice(walletInvoice.paymentRequest)

return lnInvoice
return walletInvoice
}
11 changes: 7 additions & 4 deletions core/api/src/app/wallets/update-pending-invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export const handleHeldInvoices = async (logger: Logger): Promise<void> => {
await runInParallel({
iterator: pendingInvoices,
logger,
processor: async (walletInvoice: WalletInvoice, index: number) => {
processor: async (
walletInvoice: WalletInvoiceWithOptionalLnInvoice,
index: number,
) => {
logger.trace("updating pending invoices %s in worker %d", index)

return updateOrDeclinePendingInvoice({
Expand Down Expand Up @@ -158,7 +161,7 @@ const updateOrDeclinePendingInvoice = async ({
walletInvoice,
logger,
}: {
walletInvoice: WalletInvoice
walletInvoice: WalletInvoiceWithOptionalLnInvoice
logger: Logger
}): Promise<boolean | ApplicationError> =>
WalletInvoiceChecker(walletInvoice).shouldDecline()
Expand All @@ -173,7 +176,7 @@ const updatePendingInvoiceBeforeFinally = async ({
walletInvoice,
logger,
}: {
walletInvoice: WalletInvoiceWithOptionalPaymentRequest
walletInvoice: WalletInvoiceWithOptionalLnInvoice
logger: Logger
}): Promise<boolean | ApplicationError> => {
addAttributesToCurrentSpan({
Expand Down Expand Up @@ -419,7 +422,7 @@ export const updatePendingInvoice = wrapAsyncToRunInSpan({
walletInvoice,
logger,
}: {
walletInvoice: WalletInvoiceWithOptionalPaymentRequest
walletInvoice: WalletInvoiceWithOptionalLnInvoice
logger: Logger
}): Promise<boolean | ApplicationError> => {
const result = await updatePendingInvoiceBeforeFinally({
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/domain/bitcoin/lightning/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type LnInvoice = {
readonly paymentSecret: PaymentIdentifyingSecret | null
readonly features: LnInvoiceFeature[]
readonly expiresAt: Date
readonly isExpired: boolean
readonly isExpired: boolean // Should we remove this because it can become stale?
}

type RegisterInvoiceArgs = {
Expand Down
1 change: 1 addition & 0 deletions core/api/src/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class DbConnectionClosedError extends RepositoryError {
level = ErrorLevel.Critical
}
export class MultipleWalletsFoundForAccountIdAndCurrency extends RepositoryError {}
export class WalletInvoiceMissingLnInvoiceError extends RepositoryError {}

export class CouldNotUnsetPhoneFromUserError extends CouldNotUpdateError {}

Expand Down
4 changes: 1 addition & 3 deletions core/api/src/domain/wallet-invoices/errors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ValidationError, ErrorLevel, DomainError } from "@/domain/shared"
import { ValidationError, ErrorLevel } from "@/domain/shared"

export class SubOneCentSatAmountForUsdReceiveError extends ValidationError {}
export class InvalidWalletInvoiceBuilderStateError extends ValidationError {
level = ErrorLevel.Critical
}

export class WalletInvoiceMissingPaymentRequestError extends DomainError {}
12 changes: 0 additions & 12 deletions core/api/src/domain/wallet-invoices/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
import { WalletInvoiceMissingPaymentRequestError } from "./errors"

export * from "./wallet-invoice-checker"

export const ensureWalletInvoiceHasPaymentRequest = (
walletInvoiceWithOptionalPaymentRequest: WalletInvoiceWithOptionalPaymentRequest,
): WalletInvoice | WalletInvoiceMissingPaymentRequestError => {
if (!walletInvoiceWithOptionalPaymentRequest.paymentRequest) {
return new WalletInvoiceMissingPaymentRequestError()
}

return walletInvoiceWithOptionalPaymentRequest as WalletInvoice
}
23 changes: 9 additions & 14 deletions core/api/src/domain/wallet-invoices/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,11 @@ type WIBWithAmountState = WIBWithExpirationState & {
usdAmount?: UsdPaymentAmount
}

type LnAndWalletInvoice = {
walletInvoice: WalletInvoice & { paymentRequest: EncodedPaymentRequest }
lnInvoice: LnInvoice
}

type WIBWithAmount = {
registerInvoice: () => Promise<LnAndWalletInvoice | LightningServiceError>
registerInvoice: () => Promise<WalletInvoice | LightningServiceError>
}

type WalletInvoiceWithOptionalPaymentRequest = {
type WalletInvoiceWithOptionalLnInvoice = {
paymentHash: PaymentHash
secret: SecretPreImage
selfGenerated: boolean
Expand All @@ -91,11 +86,11 @@ type WalletInvoiceWithOptionalPaymentRequest = {
recipientWalletDescriptor: PartialWalletDescriptor<WalletCurrency>
paid: boolean
createdAt: Date
paymentRequest?: EncodedPaymentRequest // Payment request is optional because some older invoices don't have it
lnInvoice?: LnInvoice // LnInvoice is optional because some older invoices don't have it
}

type WalletInvoice = WalletInvoiceWithOptionalPaymentRequest & {
paymentRequest: EncodedPaymentRequest
type WalletInvoice = WalletInvoiceWithOptionalLnInvoice & {
lnInvoice: LnInvoice
}

type WalletAddress<S extends WalletCurrency> = {
Expand Down Expand Up @@ -144,7 +139,7 @@ type WalletInvoiceReceiverArgs = {
receivedBtc: BtcPaymentAmount
satsFee?: BtcPaymentAmount

walletInvoice: WalletInvoiceWithOptionalPaymentRequest
walletInvoice: WalletInvoiceWithOptionalLnInvoice
recipientWalletDescriptors: AccountWalletDescriptors
}

Expand All @@ -170,13 +165,13 @@ interface IWalletInvoicesRepository {

markAsPaid: (
paymentHash: PaymentHash,
) => Promise<WalletInvoiceWithOptionalPaymentRequest | RepositoryError>
) => Promise<WalletInvoiceWithOptionalLnInvoice | RepositoryError>

findByPaymentHash: (
paymentHash: PaymentHash,
) => Promise<WalletInvoiceWithOptionalPaymentRequest | RepositoryError>
) => Promise<WalletInvoice | RepositoryError>

yieldPending: () => AsyncGenerator<WalletInvoice> | RepositoryError
yieldPending: () => AsyncGenerator<WalletInvoiceWithOptionalLnInvoice> | RepositoryError

deleteByPaymentHash: (paymentHash: PaymentHash) => Promise<boolean | RepositoryError>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,9 @@ export const WIBWithAmount = (state: WIBWithAmountState): WIBWithAmount => {
recipientWalletDescriptor: state.recipientWalletDescriptor,
paid: false,
createdAt: new Date(),
paymentRequest: registeredInvoice.invoice.paymentRequest,
}
return {
walletInvoice,
lnInvoice: registeredInvoice.invoice,
}
return walletInvoice
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CouldNotFindWalletInvoiceError } from "@/domain/errors"
import { WalletCurrency } from "@/domain/shared"

export const WalletInvoiceChecker = (
walletInvoice: WalletInvoiceWithOptionalPaymentRequest | RepositoryError,
walletInvoice: WalletInvoiceWithOptionalLnInvoice | RepositoryError,
): WalletInvoiceChecker => {
const shouldDecline = (): boolean => {
if (walletInvoice instanceof CouldNotFindWalletInvoiceError) {
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions core/api/src/graphql/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export const mapError = (error: ApplicationError): CustomApolloError => {
message = `Account does not exist for username ${error.message}`
return new NotFoundError({ message, logger: baseLogger })

case "WalletInvoiceMissingPaymentRequestError":
message = `Invoice is missing its 'paymentRequest' value likely becuase it is an older invoice`
case "WalletInvoiceMissingLnInvoiceError":
message = `The associated lightning invoice could not be found.`
return new NotFoundError({ message, logger: baseLogger })

case "CouldNotFindTransactionsForAccountError":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ const LnInvoiceCreateMutation = GT.Field({
}
}

const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({
const invoice = await Wallets.addInvoiceForSelfForBtcWallet({
walletId,
amount,
memo,
expiresIn,
})

if (lnInvoice instanceof Error) {
return { errors: [mapAndParseErrorForGqlResponse(lnInvoice)] }
if (invoice instanceof Error) {
return { errors: [mapAndParseErrorForGqlResponse(invoice)] }
}

return {
errors: [],
invoice: lnInvoice,
invoice,
}
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,19 @@ const LnNoAmountInvoiceCreateOnBehalfOfRecipientMutation = GT.Field({
}
}

const result = await Wallets.addInvoiceNoAmountForRecipient({
const invoice = await Wallets.addInvoiceNoAmountForRecipient({
recipientWalletId,
memo,
expiresIn,
})

if (result instanceof Error) {
return { errors: [mapAndParseErrorForGqlResponse(result)] }
if (invoice instanceof Error) {
return { errors: [mapAndParseErrorForGqlResponse(invoice)] }
}

const { paymentRequest, paymentHash, paymentSecret } = result

return {
errors: [],
invoice: {
paymentRequest,
paymentHash,
paymentSecret,
},
invoice,
}
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ const LnNoAmountInvoiceCreateMutation = GT.Field({
}
}

const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({
const invoice = await Wallets.addInvoiceNoAmountForSelf({
walletId,
memo,
expiresIn,
})

if (lnInvoice instanceof Error) {
return { errors: [mapAndParseErrorForGqlResponse(lnInvoice)] }
if (invoice instanceof Error) {
return { errors: [mapAndParseErrorForGqlResponse(invoice)] }
}

return {
errors: [],
invoice: lnInvoice,
invoice,
}
},
})
Expand Down
Loading

0 comments on commit a0b3481

Please sign in to comment.