diff --git a/core/api/src/app/wallets/add-invoice-for-wallet.ts b/core/api/src/app/wallets/add-invoice-for-wallet.ts index 9a9a2dc3c84..d3a1ca75603 100644 --- a/core/api/src/app/wallets/add-invoice-for-wallet.ts +++ b/core/api/src/app/wallets/add-invoice-for-wallet.ts @@ -27,7 +27,7 @@ const addInvoiceForSelf = async ({ walletAmount, memo = "", expiresIn, -}: AddInvoiceForSelfArgs): Promise => +}: AddInvoiceForSelfArgs): Promise => addInvoice({ walletId, limitCheckFn: checkSelfWalletIdRateLimits, @@ -45,7 +45,7 @@ const addInvoiceForSelf = async ({ export const addInvoiceForSelfForBtcWallet = async ( args: AddInvoiceForSelfForBtcWalletArgs, -): Promise => { +): Promise => { const walletId = checkedToWalletId(args.walletId) if (walletId instanceof Error) return walletId @@ -63,7 +63,7 @@ export const addInvoiceForSelfForBtcWallet = async ( export const addInvoiceForSelfForUsdWallet = async ( args: AddInvoiceForSelfForUsdWalletArgs, -): Promise => { +): Promise => { const walletId = checkedToWalletId(args.walletId) if (walletId instanceof Error) return walletId @@ -83,7 +83,7 @@ export const addInvoiceNoAmountForSelf = async ({ walletId, memo = "", expiresIn, -}: AddInvoiceNoAmountForSelfArgs): Promise => { +}: AddInvoiceNoAmountForSelfArgs): Promise => { const walletIdChecked = checkedToWalletId(walletId) if (walletIdChecked instanceof Error) return walletIdChecked @@ -118,7 +118,7 @@ const addInvoiceForRecipient = async ({ memo = "", descriptionHash, expiresIn, -}: AddInvoiceForRecipientArgs): Promise => +}: AddInvoiceForRecipientArgs): Promise => addInvoice({ walletId: recipientWalletId, limitCheckFn: checkRecipientWalletIdRateLimits, @@ -136,7 +136,7 @@ const addInvoiceForRecipient = async ({ export const addInvoiceForRecipientForBtcWallet = async ( args: AddInvoiceForRecipientForBtcWalletArgs, -): Promise => { +): Promise => { const recipientWalletId = checkedToWalletId(args.recipientWalletId) if (recipientWalletId instanceof Error) return recipientWalletId @@ -160,7 +160,7 @@ export const addInvoiceForRecipientForBtcWallet = async ( export const addInvoiceForRecipientForUsdWallet = async ( args: AddInvoiceForRecipientForUsdWalletArgs, -): Promise => { +): Promise => { const recipientWalletId = checkedToWalletId(args.recipientWalletId) if (recipientWalletId instanceof Error) return recipientWalletId @@ -184,7 +184,7 @@ export const addInvoiceForRecipientForUsdWallet = async ( export const addInvoiceForRecipientForUsdWalletAndBtcAmount = async ( args: AddInvoiceForRecipientForUsdWalletArgs, -): Promise => { +): Promise => { const recipientWalletId = checkedToWalletId(args.recipientWalletId) if (recipientWalletId instanceof Error) return recipientWalletId @@ -210,7 +210,7 @@ export const addInvoiceNoAmountForRecipient = async ({ recipientWalletId, memo = "", expiresIn, -}: AddInvoiceNoAmountForRecipientArgs): Promise => { +}: AddInvoiceNoAmountForRecipientArgs): Promise => { const walletId = checkedToWalletId(recipientWalletId) if (walletId instanceof Error) return walletId @@ -243,7 +243,7 @@ const addInvoice = async ({ walletId, limitCheckFn, buildWIBWithAmountFn, -}: AddInvoiceArgs): Promise => { +}: AddInvoiceArgs): Promise => { const wallet = await WalletsRepository().findById(walletId) if (wallet instanceof Error) return wallet const account = await AccountsRepository().findById(wallet.accountId) @@ -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 ( diff --git a/core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts b/core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts index 45e50fd1580..86808caa01a 100644 --- a/core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts +++ b/core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts @@ -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 ({ @@ -9,26 +7,16 @@ export const getInvoiceForWalletByPaymentHash = async ({ }: { walletId: WalletId paymentHash: PaymentHash -}): Promise => { +}): Promise => { 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 } diff --git a/core/api/src/app/wallets/update-pending-invoices.ts b/core/api/src/app/wallets/update-pending-invoices.ts index afc7563b976..6c543ddf549 100644 --- a/core/api/src/app/wallets/update-pending-invoices.ts +++ b/core/api/src/app/wallets/update-pending-invoices.ts @@ -51,7 +51,10 @@ export const handleHeldInvoices = async (logger: Logger): Promise => { 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({ @@ -158,7 +161,7 @@ const updateOrDeclinePendingInvoice = async ({ walletInvoice, logger, }: { - walletInvoice: WalletInvoice + walletInvoice: WalletInvoiceWithOptionalLnInvoice logger: Logger }): Promise => WalletInvoiceChecker(walletInvoice).shouldDecline() @@ -173,7 +176,7 @@ const updatePendingInvoiceBeforeFinally = async ({ walletInvoice, logger, }: { - walletInvoice: WalletInvoiceWithOptionalPaymentRequest + walletInvoice: WalletInvoiceWithOptionalLnInvoice logger: Logger }): Promise => { addAttributesToCurrentSpan({ @@ -419,7 +422,7 @@ export const updatePendingInvoice = wrapAsyncToRunInSpan({ walletInvoice, logger, }: { - walletInvoice: WalletInvoiceWithOptionalPaymentRequest + walletInvoice: WalletInvoiceWithOptionalLnInvoice logger: Logger }): Promise => { const result = await updatePendingInvoiceBeforeFinally({ diff --git a/core/api/src/domain/bitcoin/lightning/index.types.d.ts b/core/api/src/domain/bitcoin/lightning/index.types.d.ts index 7003bce5ca0..1d97d795ffb 100644 --- a/core/api/src/domain/bitcoin/lightning/index.types.d.ts +++ b/core/api/src/domain/bitcoin/lightning/index.types.d.ts @@ -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 = { diff --git a/core/api/src/domain/errors.ts b/core/api/src/domain/errors.ts index 1a79e3e16bf..1fd4d8006f3 100644 --- a/core/api/src/domain/errors.ts +++ b/core/api/src/domain/errors.ts @@ -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 {} diff --git a/core/api/src/domain/wallet-invoices/errors.ts b/core/api/src/domain/wallet-invoices/errors.ts index 2b1156f12aa..4bcc5209806 100644 --- a/core/api/src/domain/wallet-invoices/errors.ts +++ b/core/api/src/domain/wallet-invoices/errors.ts @@ -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 {} diff --git a/core/api/src/domain/wallet-invoices/index.ts b/core/api/src/domain/wallet-invoices/index.ts index 9a55d8ab3f4..5f108baa07c 100644 --- a/core/api/src/domain/wallet-invoices/index.ts +++ b/core/api/src/domain/wallet-invoices/index.ts @@ -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 -} diff --git a/core/api/src/domain/wallet-invoices/index.types.d.ts b/core/api/src/domain/wallet-invoices/index.types.d.ts index e89d1d24ac9..4b5f2bc242e 100644 --- a/core/api/src/domain/wallet-invoices/index.types.d.ts +++ b/core/api/src/domain/wallet-invoices/index.types.d.ts @@ -73,16 +73,11 @@ type WIBWithAmountState = WIBWithExpirationState & { usdAmount?: UsdPaymentAmount } -type LnAndWalletInvoice = { - walletInvoice: WalletInvoice & { paymentRequest: EncodedPaymentRequest } - lnInvoice: LnInvoice -} - type WIBWithAmount = { - registerInvoice: () => Promise + registerInvoice: () => Promise } -type WalletInvoiceWithOptionalPaymentRequest = { +type WalletInvoiceWithOptionalLnInvoice = { paymentHash: PaymentHash secret: SecretPreImage selfGenerated: boolean @@ -91,11 +86,11 @@ type WalletInvoiceWithOptionalPaymentRequest = { recipientWalletDescriptor: PartialWalletDescriptor 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 = { @@ -144,7 +139,7 @@ type WalletInvoiceReceiverArgs = { receivedBtc: BtcPaymentAmount satsFee?: BtcPaymentAmount - walletInvoice: WalletInvoiceWithOptionalPaymentRequest + walletInvoice: WalletInvoiceWithOptionalLnInvoice recipientWalletDescriptors: AccountWalletDescriptors } @@ -170,13 +165,13 @@ interface IWalletInvoicesRepository { markAsPaid: ( paymentHash: PaymentHash, - ) => Promise + ) => Promise findByPaymentHash: ( paymentHash: PaymentHash, - ) => Promise + ) => Promise - yieldPending: () => AsyncGenerator | RepositoryError + yieldPending: () => AsyncGenerator | RepositoryError deleteByPaymentHash: (paymentHash: PaymentHash) => Promise diff --git a/core/api/src/domain/wallet-invoices/wallet-invoice-builder.ts b/core/api/src/domain/wallet-invoices/wallet-invoice-builder.ts index 3630db30e80..21bb416da24 100644 --- a/core/api/src/domain/wallet-invoices/wallet-invoice-builder.ts +++ b/core/api/src/domain/wallet-invoices/wallet-invoice-builder.ts @@ -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 { diff --git a/core/api/src/domain/wallet-invoices/wallet-invoice-checker.ts b/core/api/src/domain/wallet-invoices/wallet-invoice-checker.ts index 3b468c7dfef..72ba8bdf359 100644 --- a/core/api/src/domain/wallet-invoices/wallet-invoice-checker.ts +++ b/core/api/src/domain/wallet-invoices/wallet-invoice-checker.ts @@ -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) { diff --git a/core/api/src/domain/wallet-invoices/wallet-invoice-status.ts b/core/api/src/domain/wallet-invoices/wallet-invoice-status.ts new file mode 100644 index 00000000000..e69de29bb2d diff --git a/core/api/src/graphql/error-map.ts b/core/api/src/graphql/error-map.ts index 3f6c2ecc62b..11d5b60557e 100644 --- a/core/api/src/graphql/error-map.ts +++ b/core/api/src/graphql/error-map.ts @@ -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": diff --git a/core/api/src/graphql/public/root/mutation/ln-invoice-create.ts b/core/api/src/graphql/public/root/mutation/ln-invoice-create.ts index 703b812fc45..eb93143887a 100644 --- a/core/api/src/graphql/public/root/mutation/ln-invoice-create.ts +++ b/core/api/src/graphql/public/root/mutation/ln-invoice-create.ts @@ -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, } }, }) diff --git a/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-create-on-behalf-of-recipient.ts b/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-create-on-behalf-of-recipient.ts index 7ca5d8a177c..0da78153ce1 100644 --- a/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-create-on-behalf-of-recipient.ts +++ b/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-create-on-behalf-of-recipient.ts @@ -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, } }, }) diff --git a/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-create.ts b/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-create.ts index 2218c2becf5..091bcce4ec0 100644 --- a/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-create.ts +++ b/core/api/src/graphql/public/root/mutation/ln-noamount-invoice-create.ts @@ -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, } }, }) diff --git a/core/api/src/graphql/public/root/mutation/ln-usd-invoice-create.ts b/core/api/src/graphql/public/root/mutation/ln-usd-invoice-create.ts index 8518335457d..4138777b2de 100644 --- a/core/api/src/graphql/public/root/mutation/ln-usd-invoice-create.ts +++ b/core/api/src/graphql/public/root/mutation/ln-usd-invoice-create.ts @@ -47,20 +47,20 @@ const LnUsdInvoiceCreateMutation = GT.Field({ } } - const lnInvoice = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoice = await Wallets.addInvoiceForSelfForUsdWallet({ 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, } }, }) diff --git a/core/api/src/graphql/shared/types/abstract/invoice.ts b/core/api/src/graphql/shared/types/abstract/invoice.ts index 085295e7784..96081531f4c 100644 --- a/core/api/src/graphql/shared/types/abstract/invoice.ts +++ b/core/api/src/graphql/shared/types/abstract/invoice.ts @@ -1,6 +1,7 @@ +import LnInvoice from "../object/ln-invoice" import { GT } from "@/graphql/index" -import LnInvoice from "../object/ln-invoice" + import LnNoAmountInvoice from "@/graphql/shared/types/object/ln-noamount-invoice" const Invoice = GT.Union({ diff --git a/core/api/src/graphql/shared/types/object/ln-invoice.ts b/core/api/src/graphql/shared/types/object/ln-invoice.ts index 9f5adbfe504..df1dfb172da 100644 --- a/core/api/src/graphql/shared/types/object/ln-invoice.ts +++ b/core/api/src/graphql/shared/types/object/ln-invoice.ts @@ -5,22 +5,25 @@ import SatAmount from "../scalar/sat-amount" import { GT } from "@/graphql/index" -const LnInvoice = GT.Object({ +const LnInvoice = GT.Object({ name: "LnInvoice", - isTypeOf: (source) => !!source.amount, + isTypeOf: (source) => !!source.lnInvoice.amount, fields: () => ({ paymentRequest: { type: GT.NonNull(LnPaymentRequest), + resolve: (source) => source.lnInvoice.paymentRequest, }, paymentHash: { type: GT.NonNull(PaymentHash), + resolve: (source) => source.lnInvoice.paymentHash, }, paymentSecret: { type: GT.NonNull(LnPaymentSecret), + resolve: (source) => source.lnInvoice.paymentSecret, }, satoshis: { type: SatAmount, - resolve: (source) => source.amount, + resolve: (source) => source.lnInvoice.amount, }, }), }) diff --git a/core/api/src/graphql/shared/types/object/ln-noamount-invoice.ts b/core/api/src/graphql/shared/types/object/ln-noamount-invoice.ts index 2a7000d7e67..ddbd7f04c07 100644 --- a/core/api/src/graphql/shared/types/object/ln-noamount-invoice.ts +++ b/core/api/src/graphql/shared/types/object/ln-noamount-invoice.ts @@ -3,27 +3,24 @@ import PaymentHash from "../scalar/payment-hash" import LnPaymentSecret from "../scalar/ln-payment-secret" import { GT } from "@/graphql/index" -import InvoicePaymentStatus from "../scalar/invoice-payment-status" +// import InvoicePaymentStatus from "../scalar/invoice-payment-status" -const LnNoAmountInvoice = GT.Object({ +const LnNoAmountInvoice = GT.Object({ name: "LnNoAmountInvoice", - isTypeOf: (source) => !source.amount, + isTypeOf: (source) => !source.lnInvoice.amount, fields: () => ({ paymentRequest: { type: GT.NonNull(LnPaymentRequest), + resolve: (source) => source.lnInvoice.paymentRequest, }, paymentHash: { type: GT.NonNull(PaymentHash), + resolve: (source) => source.lnInvoice.paymentHash, }, paymentSecret: { type: GT.NonNull(LnPaymentSecret), + resolve: (source) => source.lnInvoice.paymentSecret, }, - status: { - type: GT.NonNull(InvoicePaymentStatus), - resolve: (source) => { - - } - } }), }) diff --git a/core/api/src/services/mongoose/wallet-invoices.ts b/core/api/src/services/mongoose/wallet-invoices.ts index 7db3f23c43f..4e29b92196e 100644 --- a/core/api/src/services/mongoose/wallet-invoices.ts +++ b/core/api/src/services/mongoose/wallet-invoices.ts @@ -2,10 +2,13 @@ import { WalletInvoice } from "./schema" import { parseRepositoryError } from "./utils" +import { decodeInvoice } from "@/domain/bitcoin/lightning" + import { CouldNotFindWalletInvoiceError, RepositoryError, UnknownRepositoryError, + WalletInvoiceMissingLnInvoiceError, } from "@/domain/errors" import { UsdPaymentAmount } from "@/domain/shared" @@ -18,7 +21,7 @@ export const WalletInvoicesRepository = (): IWalletInvoicesRepository => { pubkey, paid, usdAmount, - paymentRequest, + lnInvoice, }: WalletInvoicesPersistNewArgs): Promise => { try { const walletInvoice = await new WalletInvoice({ @@ -30,9 +33,9 @@ export const WalletInvoicesRepository = (): IWalletInvoicesRepository => { paid, cents: usdAmount ? Number(usdAmount.amount) : undefined, currency: recipientWalletDescriptor.currency, - paymentRequest, + paymentRequest: lnInvoice.paymentRequest, }).save() - return walletInvoiceFromRaw(walletInvoice) + return ensureWalletInvoiceHasLnInvoice(walletInvoiceFromRaw(walletInvoice)) } catch (err) { return parseRepositoryError(err) } @@ -40,7 +43,7 @@ export const WalletInvoicesRepository = (): IWalletInvoicesRepository => { const markAsPaid = async ( paymentHash: PaymentHash, - ): Promise => { + ): Promise => { try { const walletInvoice = await WalletInvoice.findOneAndUpdate( { _id: paymentHash }, @@ -66,13 +69,15 @@ export const WalletInvoicesRepository = (): IWalletInvoicesRepository => { if (!walletInvoice) { return new CouldNotFindWalletInvoiceError(paymentHash) } - return walletInvoiceFromRaw(walletInvoice) + return ensureWalletInvoiceHasLnInvoice(walletInvoiceFromRaw(walletInvoice)) } catch (err) { return parseRepositoryError(err) } } - async function* yieldPending(): AsyncGenerator | RepositoryError { + async function* yieldPending(): + | AsyncGenerator + | RepositoryError { let pending try { pending = WalletInvoice.find({ paid: false }).cursor({ @@ -125,17 +130,37 @@ export const WalletInvoicesRepository = (): IWalletInvoicesRepository => { } } -const walletInvoiceFromRaw = (result: WalletInvoiceRecord): WalletInvoice => ({ - paymentHash: result._id as PaymentHash, - secret: result.secret as SecretPreImage, - recipientWalletDescriptor: { - id: result.walletId as WalletId, - currency: result.currency as WalletCurrency, - }, - selfGenerated: result.selfGenerated, - pubkey: result.pubkey as Pubkey, - paid: result.paid as boolean, - usdAmount: result.cents ? UsdPaymentAmount(BigInt(result.cents)) : undefined, - createdAt: new Date(result.timestamp.getTime()), - paymentRequest: result.paymentRequest as EncodedPaymentRequest, -}) +const walletInvoiceFromRaw = ( + result: WalletInvoiceRecord, +): WalletInvoiceWithOptionalLnInvoice => { + const lnInvoice = result.paymentRequest + ? decodeInvoice(result.paymentRequest) + : undefined + + if (lnInvoice instanceof Error) throw new Error("Corrupt payment request in db") + + return { + paymentHash: result._id as PaymentHash, + secret: result.secret as SecretPreImage, + recipientWalletDescriptor: { + id: result.walletId as WalletId, + currency: result.currency as WalletCurrency, + }, + selfGenerated: result.selfGenerated, + pubkey: result.pubkey as Pubkey, + paid: result.paid as boolean, + usdAmount: result.cents ? UsdPaymentAmount(BigInt(result.cents)) : undefined, + createdAt: new Date(result.timestamp.getTime()), + lnInvoice, + } +} + +const ensureWalletInvoiceHasLnInvoice = ( + walletInvoiceWithOptionalLnInvoice: WalletInvoiceWithOptionalLnInvoice, +) => { + if (!walletInvoiceWithOptionalLnInvoice.lnInvoice) { + return new WalletInvoiceMissingLnInvoiceError() + } + + return walletInvoiceWithOptionalLnInvoice as WalletInvoice +} diff --git a/core/api/test/helpers/lightning.ts b/core/api/test/helpers/lightning.ts index 2b2f5a67832..e1b35ad593b 100644 --- a/core/api/test/helpers/lightning.ts +++ b/core/api/test/helpers/lightning.ts @@ -470,12 +470,12 @@ export const fundWalletIdFromLightning = async ({ : await Wallets.addInvoiceForSelfForUsdWallet({ walletId, amount }) if (invoice instanceof Error) return invoice - safePay({ lnd: lndOutside1, request: invoice.paymentRequest }) + safePay({ lnd: lndOutside1, request: invoice.lnInvoice.paymentRequest }) // TODO: we could use an event instead of a sleep await sleep(500) - const hash = getHash(invoice.paymentRequest) + const hash = getHash(invoice.lnInvoice.paymentRequest) expect( await Wallets.updatePendingInvoiceByPaymentHash({ diff --git a/core/api/test/helpers/user.ts b/core/api/test/helpers/user.ts index 4788df1b095..2d0ac2eec89 100644 --- a/core/api/test/helpers/user.ts +++ b/core/api/test/helpers/user.ts @@ -340,20 +340,20 @@ export const fundWallet = async ({ wallet.currency === WalletCurrency.Btc ? Wallets.addInvoiceForSelfForBtcWallet : Wallets.addInvoiceForSelfForUsdWallet - const lnInvoice = await addInvoiceFn({ + const invoice = await addInvoiceFn({ walletId: wallet.id, amount: Number(balanceAmount.amount), memo: `Fund new wallet ${wallet.id}`, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: invoice, paymentHash } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest, paymentHash } = invoice.lnInvoice const updateInvoice = () => Wallets.updatePendingInvoiceByPaymentHash({ paymentHash, logger: baseLogger, }) const promises = Promise.all([ - safePay({ lnd: lndOutside1, request: invoice }), + safePay({ lnd: lndOutside1, request: paymentRequest }), (async () => { // TODO: we could use event instead of a sleep to lower test latency await sleep(500) diff --git a/core/api/test/integration/app/wallets/send-lightning.spec.ts b/core/api/test/integration/app/wallets/send-lightning.spec.ts index 8410d95c7cc..f8af335c647 100644 --- a/core/api/test/integration/app/wallets/send-lightning.spec.ts +++ b/core/api/test/integration/app/wallets/send-lightning.spec.ts @@ -512,7 +512,7 @@ describe("initiated via lightning", () => { pubkey: destination, recipientWalletDescriptor, paid: false, - paymentRequest: lnInvoice.paymentRequest, + lnInvoice, }) if (persisted instanceof Error) throw persisted @@ -562,7 +562,7 @@ describe("initiated via lightning", () => { pubkey: lnInvoice.destination, recipientWalletDescriptor: newWalletDescriptor, paid: false, - paymentRequest: lnInvoice.paymentRequest, + lnInvoice, }) if (persisted instanceof Error) throw persisted @@ -634,7 +634,7 @@ describe("initiated via lightning", () => { recipientWalletDescriptor: usdWalletDescriptor, paid: false, usdAmount, - paymentRequest: lnInvoice.paymentRequest, + lnInvoice, }) if (persisted instanceof Error) throw persisted @@ -655,7 +655,7 @@ describe("initiated via lightning", () => { pubkey: noAmountLnInvoice.destination, recipientWalletDescriptor: usdWalletDescriptor, paid: false, - paymentRequest: lnInvoice.paymentRequest, + lnInvoice, }) if (noAmountPersisted instanceof Error) throw noAmountPersisted @@ -714,7 +714,7 @@ describe("initiated via lightning", () => { pubkey: largeWithAmountLnInvoice.destination, recipientWalletDescriptor: otherWalletDescriptor, paid: false, - paymentRequest: lnInvoice.paymentRequest, + lnInvoice, }) if (persisted instanceof Error) throw persisted @@ -735,7 +735,7 @@ describe("initiated via lightning", () => { pubkey: noAmountLnInvoice.destination, recipientWalletDescriptor: otherWalletDescriptor, paid: false, - paymentRequest: lnInvoice.paymentRequest, + lnInvoice, }) if (noAmountPersisted instanceof Error) throw noAmountPersisted @@ -788,7 +788,7 @@ describe("initiated via lightning", () => { pubkey: noAmountLnInvoice.destination, recipientWalletDescriptor: usdWalletDescriptor, paid: false, - paymentRequest: lnInvoice.paymentRequest, + lnInvoice, }) if (persisted instanceof Error) throw persisted diff --git a/core/api/test/integration/app/wallets/update-pending-invoices.spec.ts b/core/api/test/integration/app/wallets/update-pending-invoices.spec.ts index bdaa4c14f3f..74ae7ecc579 100644 --- a/core/api/test/integration/app/wallets/update-pending-invoices.spec.ts +++ b/core/api/test/integration/app/wallets/update-pending-invoices.spec.ts @@ -8,7 +8,11 @@ import { WalletCurrency } from "@/domain/shared" import { baseLogger } from "@/services/logger" import { WalletInvoicesRepository } from "@/services/mongoose" import { WalletInvoice } from "@/services/mongoose/schema" -import { getSecretAndPaymentHash } from "@/domain/bitcoin/lightning" +import { decodeInvoice, getSecretAndPaymentHash } from "@/domain/bitcoin/lightning" + +const mockLnInvoice = decodeInvoice( + "lnbc1pjjahwgpp5zzh9s6tkhpk7heu8jt4l7keuzg7v046p0lzx2hvy3jf6a56w50nqdp82pshjgr5dusyymrfde4jq4mpd3kx2apq24ek2uscqzpuxqyz5vqsp5vl4zmuvhl8rzy4rmq0g3j28060pv3gqp22rh8l7u45xwyu27928q9qyyssqn9drylhlth9ee320e4ahz52y9rklujqgw0kj9ce2gcmltqk6uuay5yv8vgks0y5tggndv0kek2m2n02lf43znx50237mglxsfw4au2cqqr6qax", +) as LnInvoice afterEach(async () => { await WalletInvoice.deleteMany({}) @@ -35,7 +39,7 @@ describe("update pending invoices", () => { currency: WalletCurrency.Usd, }, paid: false, - paymentRequest: "paymentRequest" as EncodedPaymentRequest, + lnInvoice: mockLnInvoice, } const persisted = await WalletInvoicesRepository().persistNew( expiredUsdWalletInvoice, @@ -85,7 +89,7 @@ describe("update pending invoices", () => { currency: WalletCurrency.Btc, }, paid: false, - paymentRequest: "paymentRequest" as EncodedPaymentRequest, + lnInvoice: mockLnInvoice, } const persisted = await WalletInvoicesRepository().persistNew( expiredBtcWalletInvoice, diff --git a/core/api/test/legacy-integration/02-user-wallet/02-receive-lightning.spec.ts b/core/api/test/legacy-integration/02-user-wallet/02-receive-lightning.spec.ts index 606b42af541..7aa2c33a807 100644 --- a/core/api/test/legacy-integration/02-user-wallet/02-receive-lightning.spec.ts +++ b/core/api/test/legacy-integration/02-user-wallet/02-receive-lightning.spec.ts @@ -37,13 +37,13 @@ describe("UserWallet - Lightning", () => { const sats = 500_000 const memo = "myMemo" - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: walletIdB, amount: toSats(sats), memo, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest, paymentHash } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest, paymentHash } = invoice.lnInvoice const balanceBefore = await getBalanceHelper(walletIdB) const updateInvoice = async () => { diff --git a/core/api/test/legacy-integration/02-user-wallet/02-send-lightning-limits.spec.ts b/core/api/test/legacy-integration/02-user-wallet/02-send-lightning-limits.spec.ts index 484144db120..f1e421780bf 100644 --- a/core/api/test/legacy-integration/02-user-wallet/02-send-lightning-limits.spec.ts +++ b/core/api/test/legacy-integration/02-user-wallet/02-send-lightning-limits.spec.ts @@ -153,12 +153,12 @@ describe("UserWallet Limits - Lightning Pay", () => { ) if (senderAccount instanceof Error) throw senderAccount - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: otherBtcWallet.id, amount: Number(btcThresholdAmount.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest: request, @@ -209,12 +209,12 @@ describe("UserWallet Limits - Lightning Pay", () => { // Test BTC -> USD limits { - const lnInvoice = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoice = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: usdWalletDescriptor.id, amount: Number(usdPaymentAmount.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, @@ -231,12 +231,12 @@ describe("UserWallet Limits - Lightning Pay", () => { // Test USD -> BTC limits { - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: btcWalletDescriptor.id, amount: Number(btcThresholdAmount.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequestBtc } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequestBtc } = invoice.lnInvoice const paymentResultUsd = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest: uncheckedPaymentRequestBtc, @@ -314,12 +314,12 @@ describe("UserWallet Limits - Lightning Pay", () => { numPayments, }) for (let i = 0; i < numPayments; i++) { - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: otherBtcWallet.id, amount: Number(partialBtcSendAmount.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest: request, @@ -340,12 +340,12 @@ describe("UserWallet Limits - Lightning Pay", () => { { // Fails for payment just above limit - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: otherBtcWallet.id, amount: Number(btcAmountAboveThreshold.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest: request, @@ -391,12 +391,12 @@ describe("UserWallet Limits - Lightning Pay", () => { { // Succeeds for same payment just above tradeIntraAccount limit - const lnInvoice = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoice = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: usdWalletDescriptor.id, amount: Number(usdAmountAboveThreshold.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, @@ -444,12 +444,12 @@ describe("UserWallet Limits - Lightning Pay", () => { numPayments, }) { - const lnInvoice = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoice = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: usdWalletDescriptor.id, amount: Number(partialUsdSendAmount.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, @@ -461,12 +461,12 @@ describe("UserWallet Limits - Lightning Pay", () => { expect(paymentResult).toBe(PaymentSendStatus.Success) } { - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: btcWalletDescriptor.id, amount: Number(partialBtcSendAmount.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, @@ -487,12 +487,12 @@ describe("UserWallet Limits - Lightning Pay", () => { { // Fails for payment just above limit, from btc - const lnInvoice = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoice = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: usdWalletDescriptor.id, amount: Number(usdAmountAboveThreshold.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, @@ -509,12 +509,12 @@ describe("UserWallet Limits - Lightning Pay", () => { { // Fails for payment just above limit, from usd - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: btcWalletDescriptor.id, amount: Number(btcAmountAboveThreshold.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, @@ -548,12 +548,12 @@ describe("UserWallet Limits - Lightning Pay", () => { { // Succeeds for same payment just above intraledger limit - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: otherBtcWallet.id, amount: Number(btcAmountAboveThreshold.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, @@ -645,12 +645,12 @@ describe("UserWallet Limits - Lightning Pay", () => { { // Succeeds for same payment just above intraledger limit - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: otherBtcWallet.id, amount: Number(btcAmountAboveThreshold.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, @@ -664,12 +664,12 @@ describe("UserWallet Limits - Lightning Pay", () => { { // Succeeds for same payment just above tradeIntraAccount limit - const lnInvoice = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoice = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: usdWalletDescriptor.id, amount: Number(usdAmountAboveThreshold.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: uncheckedPaymentRequest } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ uncheckedPaymentRequest, diff --git a/core/api/test/legacy-integration/02-user-wallet/02-tx-display.spec.ts b/core/api/test/legacy-integration/02-user-wallet/02-tx-display.spec.ts index 80d033cb776..0d55fa96a89 100644 --- a/core/api/test/legacy-integration/02-user-wallet/02-tx-display.spec.ts +++ b/core/api/test/legacy-integration/02-user-wallet/02-tx-display.spec.ts @@ -212,16 +212,16 @@ describe("Display properties on transactions", () => { // Receive payment const memo = "invoiceMemo #" + (Math.random() * 1_000_000).toFixed() - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: recipientWalletId, amount: amountInvoice, memo, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: invoice, paymentHash } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest, paymentHash } = invoice.lnInvoice const [outsideLndpayResult, updateResult] = await Promise.all([ - safePay({ lnd: lndOutside1, request: invoice }), + safePay({ lnd: lndOutside1, request: paymentRequest }), (async () => { // TODO: we could use event instead of a sleep to lower test latency await sleep(500) @@ -285,16 +285,16 @@ describe("Display properties on transactions", () => { // Send payment const memo = "invoiceMemo #" + (Math.random() * 1_000_000).toFixed() - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: recipientWalletId, amount: amountInvoice, memo, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: invoice } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest } = invoice.lnInvoice const paymentResult = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: invoice, + uncheckedPaymentRequest: paymentRequest, memo: null, senderWalletId: senderWalletId, senderAccount, @@ -302,7 +302,7 @@ describe("Display properties on transactions", () => { if (paymentResult instanceof Error) throw paymentResult // Check entries - const txns = await getAllTransactionsByHash(lnInvoice.paymentHash) + const txns = await getAllTransactionsByHash(invoice.paymentHash) if (txns instanceof Error) throw txns const senderTxn = txns.find(({ walletId }) => walletId === senderWalletId) @@ -347,12 +347,12 @@ describe("Display properties on transactions", () => { // Send payment const memo = "invoiceMemo #" + (Math.random() * 1_000_000).toFixed() - const request = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: recipientWalletId, memo, }) - if (request instanceof Error) throw request - const { paymentRequest: uncheckedPaymentRequest, paymentHash } = request + if (invoice instanceof Error) throw invoice + const { paymentRequest: uncheckedPaymentRequest, paymentHash } = invoice.lnInvoice const paymentResult = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ uncheckedPaymentRequest, diff --git a/core/api/test/legacy-integration/app/wallets/invoice.spec.ts b/core/api/test/legacy-integration/app/wallets/invoice.spec.ts index 80f2f20d06c..97cce063891 100644 --- a/core/api/test/legacy-integration/app/wallets/invoice.spec.ts +++ b/core/api/test/legacy-integration/app/wallets/invoice.spec.ts @@ -59,12 +59,12 @@ describe("Wallet - addInvoice BTC", () => { it("add a self generated invoice", async () => { const amountInput = 1000 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: walletIdBtc, amount: amountInput, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice expect(request.startsWith("lnbcrt10")).toBeTruthy() const result = await walletInvoices.findByPaymentHash(getHash(request)) @@ -85,11 +85,11 @@ describe("Wallet - addInvoice BTC", () => { }) it("add a self generated invoice without amount", async () => { - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: walletIdBtc, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice const result = await walletInvoices.findByPaymentHash(getHash(request)) if (result instanceof Error) throw result @@ -105,12 +105,12 @@ describe("Wallet - addInvoice BTC", () => { it("adds a public with amount invoice", async () => { const amountInput = 10 - const lnInvoice = await Wallets.addInvoiceForRecipientForBtcWallet({ + const invoice = await Wallets.addInvoiceForRecipientForBtcWallet({ recipientWalletId: walletIdBtc, amount: amountInput, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice expect(request.startsWith("lnbcrt1")).toBeTruthy() const result = await walletInvoices.findByPaymentHash(getHash(request)) @@ -132,11 +132,11 @@ describe("Wallet - addInvoice BTC", () => { }) it("adds a public no amount invoice", async () => { - const lnInvoice = await Wallets.addInvoiceNoAmountForRecipient({ + const invoice = await Wallets.addInvoiceNoAmountForRecipient({ recipientWalletId: walletIdBtc, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice expect(request.startsWith("lnbcrt1")).toBeTruthy() const result = await walletInvoices.findByPaymentHash(getHash(request)) @@ -161,12 +161,12 @@ describe("Wallet - addInvoice USD", () => { if (btcAmount instanceof Error) throw btcAmount const sats = Number(btcAmount.amount) - const lnInvoice = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoice = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: walletIdUsd, amount: toCents(centsInput), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice expect(request.startsWith("lnbcrt")).toBeTruthy() const result = await walletInvoices.findByPaymentHash(getHash(request)) @@ -188,11 +188,11 @@ describe("Wallet - addInvoice USD", () => { }) it("add a self generated invoice without amount", async () => { - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: walletIdUsd, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice const result = await walletInvoices.findByPaymentHash(getHash(request)) if (result instanceof Error) throw result @@ -214,12 +214,12 @@ describe("Wallet - addInvoice USD", () => { if (btcAmount instanceof Error) throw btcAmount const sats = Number(btcAmount.amount) - const lnInvoice = await Wallets.addInvoiceForRecipientForUsdWallet({ + const invoice = await Wallets.addInvoiceForRecipientForUsdWallet({ recipientWalletId: walletIdUsd, amount: toCents(centsInput), }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice expect(request.startsWith("lnbcrt")).toBeTruthy() const result = await walletInvoices.findByPaymentHash(getHash(request)) @@ -242,11 +242,11 @@ describe("Wallet - addInvoice USD", () => { }) it("adds a public invoice", async () => { - const lnInvoice = await Wallets.addInvoiceNoAmountForRecipient({ + const invoice = await Wallets.addInvoiceNoAmountForRecipient({ recipientWalletId: walletIdUsd, }) - if (lnInvoice instanceof Error) throw lnInvoice - const { paymentRequest: request } = lnInvoice + if (invoice instanceof Error) throw invoice + const { paymentRequest: request } = invoice.lnInvoice expect(request.startsWith("lnbcrt1")).toBeTruthy() const result = await walletInvoices.findByPaymentHash(getHash(request)) @@ -272,17 +272,17 @@ describe("Wallet - rate limiting test", () => { // Create max number of invoices const limitsNum = getInvoiceCreateAttemptLimits().points - const promises: Promise[] = [] + const promises: Promise[] = [] for (let i = 0; i < limitsNum; i++) { - const lnInvoicePromise = Wallets.addInvoiceForSelfForBtcWallet({ + const invoicePromise = Wallets.addInvoiceForSelfForBtcWallet({ walletId: walletIdBtc, amount: 1000, }) - promises.push(lnInvoicePromise) + promises.push(invoicePromise) } - const lnInvoices = await Promise.all(promises) + const invoices = await Promise.all(promises) const isNotError = (item: unknown) => !(item instanceof Error) - expect(lnInvoices.every(isNotError)).toBe(true) + expect(invoices.every(isNotError)).toBe(true) return testPastSelfInvoiceLimits({ walletId: walletIdBtc, accountId: accountIdB }) }) @@ -295,16 +295,16 @@ describe("Wallet - rate limiting test", () => { // Create max number of invoices const limitsNum = getInvoiceCreateAttemptLimits().points - const promises: Promise[] = [] + const promises: Promise[] = [] for (let i = 0; i < limitsNum; i++) { - const lnInvoicePromise = Wallets.addInvoiceNoAmountForSelf({ + const invoicePromise = Wallets.addInvoiceNoAmountForSelf({ walletId: walletIdBtc, }) - promises.push(lnInvoicePromise) + promises.push(invoicePromise) } - const lnInvoices = await Promise.all(promises) + const invoices = await Promise.all(promises) const isNotError = (item: unknown) => !(item instanceof Error) - expect(lnInvoices.every(isNotError)).toBe(true) + expect(invoices.every(isNotError)).toBe(true) return testPastSelfInvoiceLimits({ walletId: walletIdBtc, accountId: accountIdB }) }) @@ -317,17 +317,17 @@ describe("Wallet - rate limiting test", () => { // Create max number of invoices const limitsNum = getInvoiceCreateForRecipientAttemptLimits().points - const promises: Promise[] = [] + const promises: Promise[] = [] for (let i = 0; i < limitsNum; i++) { - const lnInvoicePromise = Wallets.addInvoiceForRecipientForBtcWallet({ + const invoicePromise = Wallets.addInvoiceForRecipientForBtcWallet({ recipientWalletId: walletIdBtc, amount: 1000, }) - promises.push(lnInvoicePromise) + promises.push(invoicePromise) } - const lnInvoices = await Promise.all(promises) + const invoices = await Promise.all(promises) const isNotError = (item: unknown) => !(item instanceof Error) - expect(lnInvoices.every(isNotError)).toBe(true) + expect(invoices.every(isNotError)).toBe(true) return testPastRecipientInvoiceLimits({ walletId: walletIdBtc, @@ -343,16 +343,16 @@ describe("Wallet - rate limiting test", () => { // Create max number of invoices const limitsNum = getInvoiceCreateForRecipientAttemptLimits().points - const promises: Promise[] = [] + const promises: Promise[] = [] for (let i = 0; i < limitsNum; i++) { - const lnInvoicePromise = Wallets.addInvoiceNoAmountForRecipient({ + const invoicePromise = Wallets.addInvoiceNoAmountForRecipient({ recipientWalletId: walletIdBtc, }) - promises.push(lnInvoicePromise) + promises.push(invoicePromise) } - const lnInvoices = await Promise.all(promises) + const invoices = await Promise.all(promises) const isNotError = (item: unknown) => !(item instanceof Error) - expect(lnInvoices.every(isNotError)).toBe(true) + expect(invoices.every(isNotError)).toBe(true) return testPastRecipientInvoiceLimits({ walletId: walletIdBtc, @@ -369,11 +369,11 @@ const testPastSelfInvoiceLimits = async ({ accountId: AccountId }) => { // Test that first invoice past the limit fails - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId, amount: 1000, }) - expect(lnInvoice).toBeInstanceOf(InvoiceCreateRateLimiterExceededError) + expect(invoice).toBeInstanceOf(InvoiceCreateRateLimiterExceededError) const lnNoAmountInvoice = await Wallets.addInvoiceNoAmountForSelf({ walletId, @@ -425,12 +425,12 @@ const testPastRecipientInvoiceLimits = async ({ ) // Test that recipient invoices still work - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId, amount: 1000, }) - expect(lnInvoice).not.toBeInstanceOf(Error) - expect(lnInvoice).toHaveProperty("paymentRequest") + expect(invoice).not.toBeInstanceOf(Error) + expect(invoice).toHaveProperty("paymentRequest") const lnNoAmountInvoice = await Wallets.addInvoiceNoAmountForSelf({ walletId, diff --git a/core/api/test/legacy-integration/services/dealer/hedge-price.spec.ts b/core/api/test/legacy-integration/services/dealer/hedge-price.spec.ts index 6c9be077939..3fdacfb27fa 100644 --- a/core/api/test/legacy-integration/services/dealer/hedge-price.spec.ts +++ b/core/api/test/legacy-integration/services/dealer/hedge-price.spec.ts @@ -133,15 +133,15 @@ const getUsdEquivalentForWithAmountInvoiceSendToBtc = async ({ accountAndWallets: AccountAndWallets }): Promise => { const { newBtcWallet, newUsdWallet, newAccount } = accountAndWallets - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(btcPaymentAmount.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const beforeUsd = await getBalanceHelper(newUsdWallet.id) const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -160,20 +160,20 @@ const getUsdEquivalentForWithAmountInvoiceProbeAndSendToBtc = async ({ }): Promise => { const { newBtcWallet, newUsdWallet, newAccount } = accountAndWallets - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(btcPaymentAmount.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const beforeUsd = await getBalanceHelper(newUsdWallet.id) const probeResult = await Payments.getLightningFeeEstimationForUsdWallet({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probeResult instanceof Error) throw probeResult const payResult = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -193,15 +193,15 @@ const getBtcEquivalentForNoAmountInvoiceSendToUsd = async ({ const { newBtcWallet, newUsdWallet, newAccount } = accountAndWallets const beforeRecipientUsd = await getBalanceHelper(newUsdWallet.id) - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const beforeBtc = await getBalanceHelper(newBtcWallet.id) const result = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(btcPaymentAmount.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -230,16 +230,16 @@ const getBtcEquivalentForNoAmountInvoiceProbeAndSendToUsd = async ({ const { newBtcWallet, newUsdWallet, newAccount } = accountAndWallets const beforeRecipientUsd = await getBalanceHelper(newUsdWallet.id) - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const beforeBtc = await getBalanceHelper(newBtcWallet.id) const probe = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ amount: Number(btcPaymentAmount.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probe instanceof SubOneCentSatAmountForUsdSelfSendError) { @@ -249,7 +249,7 @@ const getBtcEquivalentForNoAmountInvoiceProbeAndSendToUsd = async ({ const result = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(btcPaymentAmount.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -421,15 +421,15 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -437,14 +437,14 @@ describe("arbitrage strategies", () => { if (result instanceof Error) throw result // Step 3: Replenish USD from BTC wallet with $0.01 invoice - const lnInvoiceUsd = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoiceUsd = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: newUsdWallet.id, amount: toCents(1), }) - if (lnInvoiceUsd instanceof Error) throw lnInvoiceUsd + if (invoiceUsd instanceof Error) throw invoiceUsd const resultUsd = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoiceUsd.paymentRequest, + uncheckedPaymentRequest: invoiceUsd.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -485,15 +485,15 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -501,20 +501,20 @@ describe("arbitrage strategies", () => { if (result instanceof Error) throw result // Step 3: Replenish USD from BTC wallet with $0.01 invoice - const lnInvoiceUsd = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoiceUsd = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: newUsdWallet.id, amount: toCents(1), }) - if (lnInvoiceUsd instanceof Error) throw lnInvoiceUsd + if (invoiceUsd instanceof Error) throw invoiceUsd const probe = await Payments.getLightningFeeEstimationForBtcWallet({ - uncheckedPaymentRequest: lnInvoiceUsd.paymentRequest, + uncheckedPaymentRequest: invoiceUsd.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probe instanceof Error) throw probe const resultUsd = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoiceUsd.paymentRequest, + uncheckedPaymentRequest: invoiceUsd.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -566,15 +566,15 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -638,15 +638,15 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -654,14 +654,14 @@ describe("arbitrage strategies", () => { if (result instanceof Error) throw result // Step 3: Replenish USD from BTC wallet with discovered 'minBtcAmountToSpend' for $0.01 - const lnInvoiceNoAmountUsd = await Wallets.addInvoiceNoAmountForSelf({ + const invoiceNoAmountUsd = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoiceNoAmountUsd instanceof Error) throw lnInvoiceNoAmountUsd + if (invoiceNoAmountUsd instanceof Error) throw invoiceNoAmountUsd const repaid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: toSats(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoiceNoAmountUsd.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountUsd.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -710,15 +710,15 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -726,21 +726,21 @@ describe("arbitrage strategies", () => { if (result instanceof Error) throw result // Step 3: Replenish USD from BTC wallet with discovered 'minBtcAmountToSpend' for $0.01 - const lnInvoiceNoAmountUsd = await Wallets.addInvoiceNoAmountForSelf({ + const invoiceNoAmountUsd = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoiceNoAmountUsd instanceof Error) throw lnInvoiceNoAmountUsd + if (invoiceNoAmountUsd instanceof Error) throw invoiceNoAmountUsd const probe = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ amount: toSats(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoiceNoAmountUsd.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountUsd.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probe instanceof Error) throw probe const repaid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: toSats(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoiceNoAmountUsd.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountUsd.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -785,21 +785,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const probe = await Payments.getLightningFeeEstimationForUsdWallet({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probe instanceof Error) throw probe const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -807,14 +807,14 @@ describe("arbitrage strategies", () => { if (result instanceof Error) throw result // Step 3: Replenish USD from BTC wallet with $0.01 invoice - const lnInvoiceUsd = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoiceUsd = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: newUsdWallet.id, amount: toCents(1), }) - if (lnInvoiceUsd instanceof Error) throw lnInvoiceUsd + if (invoiceUsd instanceof Error) throw invoiceUsd const resultUsd = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoiceUsd.paymentRequest, + uncheckedPaymentRequest: invoiceUsd.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -855,21 +855,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const probe = await Payments.getLightningFeeEstimationForUsdWallet({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probe instanceof Error) throw probe const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -877,20 +877,20 @@ describe("arbitrage strategies", () => { if (result instanceof Error) throw result // Step 3: Replenish USD from BTC wallet with $0.01 invoice - const lnInvoiceUsd = await Wallets.addInvoiceForSelfForUsdWallet({ + const invoiceUsd = await Wallets.addInvoiceForSelfForUsdWallet({ walletId: newUsdWallet.id, amount: toCents(1), }) - if (lnInvoiceUsd instanceof Error) throw lnInvoiceUsd + if (invoiceUsd instanceof Error) throw invoiceUsd const probeUsd = await Payments.getLightningFeeEstimationForBtcWallet({ - uncheckedPaymentRequest: lnInvoiceUsd.paymentRequest, + uncheckedPaymentRequest: invoiceUsd.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probeUsd instanceof Error) throw probeUsd const resultUsd = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoiceUsd.paymentRequest, + uncheckedPaymentRequest: invoiceUsd.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -942,21 +942,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const probeResult = await Payments.getLightningFeeEstimationForUsdWallet({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probeResult instanceof Error) throw probeResult const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1020,21 +1020,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const probe = await Payments.getLightningFeeEstimationForUsdWallet({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probe instanceof Error) throw probe const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1042,14 +1042,14 @@ describe("arbitrage strategies", () => { if (result instanceof Error) throw result // Step 3: Replenish USD from BTC wallet with discovered 'minBtcAmountToSpend' for $0.01 - const lnInvoiceNoAmountUsd = await Wallets.addInvoiceNoAmountForSelf({ + const invoiceNoAmountUsd = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoiceNoAmountUsd instanceof Error) throw lnInvoiceNoAmountUsd + if (invoiceNoAmountUsd instanceof Error) throw invoiceNoAmountUsd const repaid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: toSats(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoiceNoAmountUsd.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountUsd.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1098,21 +1098,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Create invoice from BTC Wallet using discovered 'maxBtcAmountToEarn' from $0.01 - const lnInvoice = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoice = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice // Step 2: Pay invoice from USD wallet at favourable rate const probe = await Payments.getLightningFeeEstimationForUsdWallet({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probe instanceof Error) throw probe const result = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1120,21 +1120,21 @@ describe("arbitrage strategies", () => { if (result instanceof Error) throw result // Step 3: Replenish USD from BTC wallet with discovered 'minBtcAmountToSpend' for $0.01 - const lnInvoiceNoAmountUsd = await Wallets.addInvoiceNoAmountForSelf({ + const invoiceNoAmountUsd = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoiceNoAmountUsd instanceof Error) throw lnInvoiceNoAmountUsd + if (invoiceNoAmountUsd instanceof Error) throw invoiceNoAmountUsd const probeUsd = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ amount: toSats(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoiceNoAmountUsd.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountUsd.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probeUsd instanceof Error) throw probeUsd const repaid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: toSats(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoiceNoAmountUsd.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountUsd.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1239,14 +1239,14 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1293,14 +1293,14 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1308,14 +1308,14 @@ describe("arbitrage strategies", () => { if (paid instanceof Error) throw paid // Step 2: Replenish BTC from USD wallet with $0.01 - const lnInvoiceNoAmountBtc = await Wallets.addInvoiceNoAmountForSelf({ + const invoiceNoAmountBtc = await Wallets.addInvoiceNoAmountForSelf({ walletId: newBtcWallet.id, }) - if (lnInvoiceNoAmountBtc instanceof Error) throw lnInvoiceNoAmountBtc + if (invoiceNoAmountBtc instanceof Error) throw invoiceNoAmountBtc const repaid = await Payments.payNoAmountInvoiceByWalletIdForUsdWallet({ amount: toCents(1), - uncheckedPaymentRequest: lnInvoiceNoAmountBtc.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountBtc.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1352,14 +1352,14 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1367,21 +1367,21 @@ describe("arbitrage strategies", () => { if (paid instanceof Error) throw paid // Step 2: Replenish BTC from USD wallet with $0.01 - const lnInvoiceNoAmountBtc = await Wallets.addInvoiceNoAmountForSelf({ + const invoiceNoAmountBtc = await Wallets.addInvoiceNoAmountForSelf({ walletId: newBtcWallet.id, }) - if (lnInvoiceNoAmountBtc instanceof Error) throw lnInvoiceNoAmountBtc + if (invoiceNoAmountBtc instanceof Error) throw invoiceNoAmountBtc const probeBtc = await Payments.getNoAmountLightningFeeEstimationForUsdWallet({ amount: toCents(1), - uncheckedPaymentRequest: lnInvoiceNoAmountBtc.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountBtc.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probeBtc instanceof Error) throw probeBtc const repaid = await Payments.payNoAmountInvoiceByWalletIdForUsdWallet({ amount: toCents(1), - uncheckedPaymentRequest: lnInvoiceNoAmountBtc.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountBtc.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1432,14 +1432,14 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1447,14 +1447,14 @@ describe("arbitrage strategies", () => { if (paid instanceof Error) throw paid // Step 2: Pay back $0.01 from USD to BTC wallet - const lnInvoiceBtc = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoiceBtc = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoiceBtc instanceof Error) throw lnInvoiceBtc + if (invoiceBtc instanceof Error) throw invoiceBtc const repaid = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoiceBtc.paymentRequest, + uncheckedPaymentRequest: invoiceBtc.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1503,14 +1503,14 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1518,20 +1518,20 @@ describe("arbitrage strategies", () => { if (paid instanceof Error) throw paid // Step 2: Pay back $0.01 from USD to BTC wallet - const lnInvoiceBtc = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoiceBtc = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoiceBtc instanceof Error) throw lnInvoiceBtc + if (invoiceBtc instanceof Error) throw invoiceBtc const probeUsd = await Payments.getLightningFeeEstimationForUsdWallet({ - uncheckedPaymentRequest: lnInvoiceBtc.paymentRequest, + uncheckedPaymentRequest: invoiceBtc.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probeUsd instanceof Error) throw probeUsd const repaid = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoiceBtc.paymentRequest, + uncheckedPaymentRequest: invoiceBtc.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1572,21 +1572,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const probe = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probe instanceof Error) throw probe const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1633,21 +1633,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const probe = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probe instanceof Error) throw probe const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1655,14 +1655,14 @@ describe("arbitrage strategies", () => { if (paid instanceof Error) throw paid // Step 2: Replenish BTC from USD wallet with $0.01 - const lnInvoiceNoAmountBtc = await Wallets.addInvoiceNoAmountForSelf({ + const invoiceNoAmountBtc = await Wallets.addInvoiceNoAmountForSelf({ walletId: newBtcWallet.id, }) - if (lnInvoiceNoAmountBtc instanceof Error) throw lnInvoiceNoAmountBtc + if (invoiceNoAmountBtc instanceof Error) throw invoiceNoAmountBtc const repaid = await Payments.payNoAmountInvoiceByWalletIdForUsdWallet({ amount: toCents(1), - uncheckedPaymentRequest: lnInvoiceNoAmountBtc.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountBtc.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1699,21 +1699,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const probe = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probe instanceof Error) throw probe const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1721,21 +1721,21 @@ describe("arbitrage strategies", () => { if (paid instanceof Error) throw paid // Step 2: Replenish BTC from USD wallet with $0.01 - const lnInvoiceNoAmountBtc = await Wallets.addInvoiceNoAmountForSelf({ + const invoiceNoAmountBtc = await Wallets.addInvoiceNoAmountForSelf({ walletId: newBtcWallet.id, }) - if (lnInvoiceNoAmountBtc instanceof Error) throw lnInvoiceNoAmountBtc + if (invoiceNoAmountBtc instanceof Error) throw invoiceNoAmountBtc const probeBtc = await Payments.getNoAmountLightningFeeEstimationForUsdWallet({ amount: toCents(1), - uncheckedPaymentRequest: lnInvoiceNoAmountBtc.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountBtc.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probeBtc instanceof Error) throw probeBtc const repaid = await Payments.payNoAmountInvoiceByWalletIdForUsdWallet({ amount: toCents(1), - uncheckedPaymentRequest: lnInvoiceNoAmountBtc.paymentRequest, + uncheckedPaymentRequest: invoiceNoAmountBtc.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1786,21 +1786,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const probe = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probe instanceof Error) throw probe const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1808,14 +1808,14 @@ describe("arbitrage strategies", () => { if (paid instanceof Error) throw paid // Step 2: Pay back $0.01 from USD to BTC wallet - const lnInvoiceBtc = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoiceBtc = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoiceBtc instanceof Error) throw lnInvoiceBtc + if (invoiceBtc instanceof Error) throw invoiceBtc const repaid = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoiceBtc.paymentRequest, + uncheckedPaymentRequest: invoiceBtc.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, @@ -1864,21 +1864,21 @@ describe("arbitrage strategies", () => { const usdBalanceBefore = await getBalanceHelper(newUsdWallet.id) // Step 1: Pay min sats via no-amount invoice to USD wallet - const lnInvoice = await Wallets.addInvoiceNoAmountForSelf({ + const invoice = await Wallets.addInvoiceNoAmountForSelf({ walletId: newUsdWallet.id, }) - if (lnInvoice instanceof Error) throw lnInvoice + if (invoice instanceof Error) throw invoice const probe = await Payments.getNoAmountLightningFeeEstimationForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, walletId: newBtcWallet.id, }) if (probe instanceof Error) throw probe const paid = await Payments.payNoAmountInvoiceByWalletIdForBtcWallet({ amount: Number(minBtcAmountToSpend.amount), - uncheckedPaymentRequest: lnInvoice.paymentRequest, + uncheckedPaymentRequest: invoice.lnInvoice.paymentRequest, memo: null, senderWalletId: newBtcWallet.id, senderAccount: newAccount, @@ -1886,20 +1886,20 @@ describe("arbitrage strategies", () => { if (paid instanceof Error) throw paid // Step 2: Pay back $0.01 from USD to BTC wallet - const lnInvoiceBtc = await Wallets.addInvoiceForSelfForBtcWallet({ + const invoiceBtc = await Wallets.addInvoiceForSelfForBtcWallet({ walletId: newBtcWallet.id, amount: toSats(maxBtcAmountToEarn.amount), }) - if (lnInvoiceBtc instanceof Error) throw lnInvoiceBtc + if (invoiceBtc instanceof Error) throw invoiceBtc const probeUsd = await Payments.getLightningFeeEstimationForUsdWallet({ - uncheckedPaymentRequest: lnInvoiceBtc.paymentRequest, + uncheckedPaymentRequest: invoiceBtc.lnInvoice.paymentRequest, walletId: newUsdWallet.id, }) if (probeUsd instanceof Error) throw probeUsd const repaid = await Payments.payInvoiceByWalletId({ - uncheckedPaymentRequest: lnInvoiceBtc.paymentRequest, + uncheckedPaymentRequest: invoiceBtc.lnInvoice.paymentRequest, memo: null, senderWalletId: newUsdWallet.id, senderAccount: newAccount, diff --git a/core/api/test/legacy-integration/services/mongoose/wallet-invoices.spec.ts b/core/api/test/legacy-integration/services/mongoose/wallet-invoices.spec.ts index 82e9e38fcf3..d00b68e7f7a 100644 --- a/core/api/test/legacy-integration/services/mongoose/wallet-invoices.spec.ts +++ b/core/api/test/legacy-integration/services/mongoose/wallet-invoices.spec.ts @@ -1,7 +1,7 @@ import crypto from "crypto" import { WalletCurrency } from "@/domain/shared" -import { getSecretAndPaymentHash } from "@/domain/bitcoin/lightning" +import { decodeInvoice, getSecretAndPaymentHash } from "@/domain/bitcoin/lightning" import { WalletInvoicesRepository } from "@/services/mongoose" import { createUserAndWalletFromPhone, randomPhone } from "test/helpers" @@ -27,7 +27,9 @@ const createTestWalletInvoice = () => { amount: 10n, }, createdAt: new Date(), - paymentRequest: "paymentRequest" as EncodedPaymentRequest, + lnInvoice: decodeInvoice( + "lnbc1pjjahwgpp5zzh9s6tkhpk7heu8jt4l7keuzg7v046p0lzx2hvy3jf6a56w50nqdp82pshjgr5dusyymrfde4jq4mpd3kx2apq24ek2uscqzpuxqyz5vqsp5vl4zmuvhl8rzy4rmq0g3j28060pv3gqp22rh8l7u45xwyu27928q9qyyssqn9drylhlth9ee320e4ahz52y9rklujqgw0kj9ce2gcmltqk6uuay5yv8vgks0y5tggndv0kek2m2n02lf43znx50237mglxsfw4au2cqqr6qax", + ) as LnInvoice, // Use a real invoice to test decoding } } diff --git a/core/api/test/unit/domain/wallet-invoices/wallet-invoice-builder.spec.ts b/core/api/test/unit/domain/wallet-invoices/wallet-invoice-builder.spec.ts index 210467d40fc..84023245dae 100644 --- a/core/api/test/unit/domain/wallet-invoices/wallet-invoice-builder.spec.ts +++ b/core/api/test/unit/domain/wallet-invoices/wallet-invoice-builder.spec.ts @@ -76,8 +76,8 @@ describe("WalletInvoiceBuilder", () => { lnRegisterInvoice: registerInvoice, }) - const checkSecretAndHash = ({ lnInvoice, walletInvoice }: LnAndWalletInvoice) => { - const { secret } = walletInvoice + const checkSecretAndHash = (walletInvoice: WalletInvoice) => { + const { secret, lnInvoice } = walletInvoice const hashFromSecret = sha256(Buffer.from(secret, "hex")) expect(hashFromSecret).toEqual(walletInvoice.paymentHash) expect(walletInvoice.paymentHash).toEqual(lnInvoice.paymentHash) @@ -88,14 +88,14 @@ describe("WalletInvoiceBuilder", () => { const WIBWithDescription = WIB.withDescription({ description: testDescription, }) - const checkDescription = ({ lnInvoice }: LnAndWalletInvoice) => { + const checkDescription = ({ lnInvoice }: WalletInvoice) => { expect(lnInvoice.description).toEqual(testDescription) } const expirationInMinutes = checkedToMinutes(3) if (expirationInMinutes instanceof Error) throw expirationInMinutes const testExpiration = new Date("2000-01-01T00:03:00.000Z") - const checkExpiration = ({ lnInvoice }: LnAndWalletInvoice) => { + const checkExpiration = ({ lnInvoice }: WalletInvoice) => { expect(lnInvoice.expiresAt).toEqual(testExpiration) } @@ -110,13 +110,13 @@ describe("WalletInvoiceBuilder", () => { describe("generated for self", () => { const WIBWithCreator = WIBWithDescription.generatedForSelf() - const checkCreator = ({ walletInvoice }: LnAndWalletInvoice) => { + const checkCreator = (walletInvoice: WalletInvoice) => { expect(walletInvoice.selfGenerated).toEqual(true) } describe("with btc recipient wallet", () => { const WIBWithRecipient = WIBWithCreator.withRecipientWallet(recipientBtcWallet) - const checkRecipientWallet = ({ walletInvoice }: LnAndWalletInvoice) => { + const checkRecipientWallet = (walletInvoice: WalletInvoice) => { expect(walletInvoice.recipientWalletDescriptor).toEqual(recipientBtcWallet) } @@ -130,8 +130,8 @@ describe("WalletInvoiceBuilder", () => { ) if (WIBWithAmount instanceof Error) throw WIBWithAmount - const checkAmount = ({ lnInvoice, walletInvoice }: LnAndWalletInvoice) => { - expect(lnInvoice).toEqual( + const checkAmount = (walletInvoice: WalletInvoice) => { + expect(walletInvoice.lnInvoice).toEqual( expect.objectContaining({ amount: uncheckedAmount as Satoshis, paymentAmount: btcCheckedAmount, @@ -174,8 +174,8 @@ describe("WalletInvoiceBuilder", () => { await WIBWithRecipient.withExpiration(expirationInMinutes).withoutAmount() if (WIBWithAmount instanceof Error) throw WIBWithAmount - const checkAmount = ({ lnInvoice, walletInvoice }: LnAndWalletInvoice) => { - expect(lnInvoice).toEqual( + const checkAmount = (walletInvoice: WalletInvoice) => { + expect(walletInvoice.lnInvoice).toEqual( expect.objectContaining({ amount: 0 as Satoshis, paymentAmount: BtcPaymentAmount(BigInt(0)), @@ -205,7 +205,7 @@ describe("WalletInvoiceBuilder", () => { describe("with usd recipient wallet", () => { const WIBWithRecipient = WIBWithCreator.withRecipientWallet(recipientUsdWallet) - const checkRecipientWallet = ({ walletInvoice }: LnAndWalletInvoice) => { + const checkRecipientWallet = (walletInvoice: WalletInvoice) => { expect(walletInvoice.recipientWalletDescriptor).toEqual(recipientUsdWallet) } @@ -219,9 +219,9 @@ describe("WalletInvoiceBuilder", () => { ) if (WIBWithAmount instanceof Error) throw WIBWithAmount - const checkAmount = ({ lnInvoice, walletInvoice }: LnAndWalletInvoice) => { + const checkAmount = (walletInvoice: WalletInvoice) => { const convertedAmount = BigInt(uncheckedAmount) * dealerPriceRatio - expect(lnInvoice).toEqual( + expect(walletInvoice.lnInvoice).toEqual( expect.objectContaining({ amount: Number(convertedAmount) as Satoshis, paymentAmount: BtcPaymentAmount(convertedAmount), @@ -256,8 +256,8 @@ describe("WalletInvoiceBuilder", () => { ) if (WIBWithAmount instanceof Error) throw WIBWithAmount - const checkAmount = ({ lnInvoice, walletInvoice }: LnAndWalletInvoice) => { - expect(lnInvoice).toEqual( + const checkAmount = (walletInvoice: WalletInvoice) => { + expect(walletInvoice.lnInvoice).toEqual( expect.objectContaining({ amount: Number(uncheckedAmount) as Satoshis, paymentAmount: btcCheckedAmount, @@ -303,8 +303,8 @@ describe("WalletInvoiceBuilder", () => { await WIBWithRecipient.withExpiration(expirationInMinutes).withoutAmount() if (WIBWithAmount instanceof Error) throw WIBWithAmount - const checkAmount = ({ lnInvoice, walletInvoice }: LnAndWalletInvoice) => { - expect(lnInvoice).toEqual( + const checkAmount = (walletInvoice: WalletInvoice) => { + expect(walletInvoice.lnInvoice).toEqual( expect.objectContaining({ amount: 0 as Satoshis, paymentAmount: BtcPaymentAmount(BigInt(0)), diff --git a/core/api/test/unit/domain/wallet-invoices/wallet-invoice-receiver.spec.ts b/core/api/test/unit/domain/wallet-invoices/wallet-invoice-receiver.spec.ts index 1aedb8461ab..51edc1a2f5b 100644 --- a/core/api/test/unit/domain/wallet-invoices/wallet-invoice-receiver.spec.ts +++ b/core/api/test/unit/domain/wallet-invoices/wallet-invoice-receiver.spec.ts @@ -53,6 +53,22 @@ describe("WalletInvoiceReceiver", () => { [WalletCurrency.Usd]: recipientUsdWalletDescriptor, } + const mockLnInvoice: LnInvoice = { + destination: "destination" as Pubkey, + paymentHash: "paymentHash" as PaymentHash, + paymentRequest: "paymentRequest" as EncodedPaymentRequest, + paymentSecret: "paymentSecret" as PaymentIdentifyingSecret, + milliSatsAmount: 0 as MilliSatoshis, + description: "description", + routeHints: [] as Hop[][], + features: [] as LnInvoiceFeature[], + expiresAt: new Date(), + isExpired: false, + cltvDelta: null, + amount: null, + paymentAmount: null, + } + describe("for btc invoice", () => { const btcInvoice: WalletInvoice = { paymentHash: "paymentHash" as PaymentHash, @@ -63,7 +79,7 @@ describe("WalletInvoiceReceiver", () => { paid: false, recipientWalletDescriptor: partialRecipientBtcWalletDescriptor, createdAt: new Date(), - paymentRequest: "paymentRequest" as EncodedPaymentRequest, + lnInvoice: mockLnInvoice, } it("returns correct amounts", async () => { @@ -104,7 +120,7 @@ describe("WalletInvoiceReceiver", () => { usdAmount: UsdPaymentAmount(BigInt(100)), paid: false, createdAt: new Date(), - paymentRequest: "paymentRequest" as EncodedPaymentRequest, + lnInvoice: mockLnInvoice, } it("returns correct amounts", async () => { @@ -138,7 +154,7 @@ describe("WalletInvoiceReceiver", () => { pubkey: "pubkey" as Pubkey, paid: false, createdAt: new Date(), - paymentRequest: "paymentRequest" as EncodedPaymentRequest, + lnInvoice: mockLnInvoice, } it("returns correct amounts", async () => {