Skip to content

Commit

Permalink
chore: address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Oct 20, 2023
1 parent 10e4c3f commit 4c19543
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 45 deletions.
16 changes: 5 additions & 11 deletions core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CouldNotFindWalletInvoiceError } from "@/domain/errors"
import { WalletInvoicesRepository } from "@/services/mongoose"

export const getInvoiceForWalletByPaymentHash = async ({
export const getInvoiceForWalletByPaymentHash = ({
walletId,
paymentHash,
}: {
Expand All @@ -10,13 +9,8 @@ export const getInvoiceForWalletByPaymentHash = async ({
}): Promise<WalletInvoice | ApplicationError> => {
const walletInvoicesRepository = WalletInvoicesRepository()

const walletInvoice = await walletInvoicesRepository.findByPaymentHash(paymentHash)

if (walletInvoice instanceof Error) return walletInvoice

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

return walletInvoice
return walletInvoicesRepository.findForWalletByPaymentHash({
walletId,
paymentHash,
})
}
25 changes: 17 additions & 8 deletions core/api/src/app/wallets/get-transaction-by-id.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { memoSharingConfig } from "@/config"
import { WalletTransactionHistory } from "@/domain/wallets"
import {
checkedToLedgerTransactionId,
CouldNotFindTransactionError,
} from "@/domain/ledger"
import { checkedToLedgerTransactionId } from "@/domain/ledger"

import { getNonEndUserWalletIds, LedgerService } from "@/services/ledger"

Expand All @@ -14,10 +11,22 @@ export const getTransactionForWalletById = async ({
walletId: WalletId
transactionId: string
}): Promise<WalletTransaction | ApplicationError> => {
const transaction = await getTransactionById(uncheckedTransactionId)
if (transaction instanceof Error) return transaction
if (transaction.walletId !== walletId) return new CouldNotFindTransactionError()
return transaction
const ledger = LedgerService()

const ledgerTransactionId = checkedToLedgerTransactionId(uncheckedTransactionId)
if (ledgerTransactionId instanceof Error) return ledgerTransactionId

const ledgerTransaction = await ledger.getTransactionForWalletById({
walletId,
transactionId: ledgerTransactionId,
})
if (ledgerTransaction instanceof Error) return ledgerTransaction

return WalletTransactionHistory.fromLedger({
ledgerTransactions: [ledgerTransaction],
nonEndUserWalletIds: Object.values(await getNonEndUserWalletIds()),
memoSharingConfig,
}).transactions[0]
}

export const getTransactionById = async (
Expand Down
20 changes: 9 additions & 11 deletions core/api/src/app/wallets/get-transactions-by-hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ export const getTransactionForWalletByPaymentHash = async ({
paymentHash: PaymentHash
}): Promise<WalletTransaction | undefined | ApplicationError> => {
const ledger = LedgerService()
const ledgerTransactions = await ledger.getTransactionsByHash(paymentHash)
if (ledgerTransactions instanceof Error) return ledgerTransactions
const ledgerTransaction = await ledger.getTransactionForWalletByPaymentHash({
walletId,
paymentHash,
})

const transactions = WalletTransactionHistory.fromLedger({
ledgerTransactions,
if (ledgerTransaction instanceof Error) return ledgerTransaction

return WalletTransactionHistory.fromLedger({
ledgerTransactions: [ledgerTransaction],
nonEndUserWalletIds: Object.values(await getNonEndUserWalletIds()),
memoSharingConfig,
}).transactions

const transaction = transactions.find(
(transaction) => transaction.walletId === walletId,
)

return transaction
}).transactions[0]
}

export const getTransactionsByHash = async (
Expand Down
Empty file.
15 changes: 5 additions & 10 deletions core/api/src/app/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,26 @@ export * from "./update-pending-invoices"
export * from "./validate"
export * from "./get-invoice-for-wallet-by-hash"

import { CouldNotFindWalletFromIdError } from "@/domain/errors"
import { WalletsRepository } from "@/services/mongoose"

export const getWallet = async (walletId: WalletId) => {
const wallets = WalletsRepository()
return wallets.findById(walletId)
}

export const getWalletForAccountById = async ({
export const getWalletForAccountById = ({
accountId,
walletId,
}: {
accountId: AccountId
walletId: WalletId
}): Promise<Wallet | ApplicationError> => {
const wallets = WalletsRepository()
const wallet = await wallets.findById(walletId)

if (wallet instanceof Error) return wallet

if (wallet.accountId !== accountId) {
return new CouldNotFindWalletFromIdError()
}

return wallet
return wallets.findForAccountById({
accountId,
walletId,
})
}

export const listWalletsByAccountId = async (
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/domain/bitcoin/lightning/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type LnInvoice = {
readonly paymentSecret: PaymentIdentifyingSecret | null
readonly features: LnInvoiceFeature[]
readonly expiresAt: Date
readonly isExpired: boolean // Should we remove this because it can become stale?
readonly isExpired: boolean
}

type RegisterInvoiceArgs = {
Expand Down
10 changes: 10 additions & 0 deletions core/api/src/domain/ledger/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,20 @@ interface ILedgerService {
id: LedgerTransactionId,
): Promise<LedgerTransaction<WalletCurrency> | LedgerServiceError>

getTransactionForWalletById(args: {
walletId: WalletId
transactionId: LedgerTransactionId
}): Promise<LedgerTransaction<WalletCurrency> | LedgerServiceError>

getTransactionsByHash(
paymentHash: PaymentHash | OnChainTxHash,
): Promise<LedgerTransaction<WalletCurrency>[] | LedgerServiceError>

getTransactionForWalletByPaymentHash(args: {
walletId: WalletId
paymentHash: PaymentHash
}): Promise<LedgerTransaction<WalletCurrency> | LedgerServiceError>

getTransactionsByWalletId(
walletId: WalletId,
): Promise<LedgerTransaction<WalletCurrency>[] | LedgerServiceError>
Expand Down
9 changes: 9 additions & 0 deletions core/api/src/domain/wallet-invoices/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ type WalletAddressReceiverArgs<S extends WalletCurrency> = {

type WalletInvoicesPersistNewArgs = Omit<WalletInvoice, "createdAt">

type WalletInvoiceFindForWalletByPaymentHashArgs = {
walletId: WalletId
paymentHash: PaymentHash
}

interface IWalletInvoicesRepository {
persistNew: (
invoice: WalletInvoicesPersistNewArgs,
Expand All @@ -178,6 +183,10 @@ interface IWalletInvoicesRepository {
paymentHash: PaymentHash,
) => Promise<WalletInvoice | RepositoryError>

findForWalletByPaymentHash: (
args: WalletInvoiceFindForWalletByPaymentHashArgs,
) => Promise<WalletInvoice | RepositoryError>

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

deleteByPaymentHash: (paymentHash: PaymentHash) => Promise<boolean | RepositoryError>
Expand Down
4 changes: 4 additions & 0 deletions core/api/src/domain/wallets/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ interface IWalletsRepository {
currency,
}: NewWalletInfo): Promise<Wallet | RepositoryError>
findById(walletId: WalletId): Promise<Wallet | RepositoryError>
findForAccountById(args: {
accountId: AccountId
walletId: WalletId
}): Promise<Wallet | RepositoryError>
listByAccountId(accountId: AccountId): Promise<Wallet[] | RepositoryError>
findAccountWalletsByAccountId(
accountId: AccountId,
Expand Down
4 changes: 2 additions & 2 deletions core/api/src/graphql/shared/types/object/ln-invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const LnInvoice = GT.Object<WalletInvoice>({
paymentStatus: {
type: GT.NonNull(InvoicePaymentStatus),
resolve: (source) => {
const statusCheker = WalletInvoiceStatusChecker(source)
const status = statusCheker.status(new Date())
const statusChecker = WalletInvoiceStatusChecker(source)
const status = statusChecker.status(new Date())
return status
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const LnNoAmountInvoice = GT.Object<WalletInvoice>({
paymentStatus: {
type: GT.NonNull(InvoicePaymentStatus),
resolve: (source) => {
const statusCheker = WalletInvoiceStatusChecker(source)
const status = statusCheker.status(new Date())
const statusChecker = WalletInvoiceStatusChecker(source)
const status = statusChecker.status(new Date())
return status
},
},
Expand Down
47 changes: 47 additions & 0 deletions core/api/src/services/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ export const LedgerService = (): ILedgerService => {
}
}

const getTransactionForWalletById = async ({
walletId,
transactionId,
}: {
walletId: WalletId
transactionId: LedgerTransactionId
}): Promise<LedgerTransaction<WalletCurrency> | LedgerServiceError> => {
const liabilitiesWalletId = toLiabilitiesWalletId(walletId)
try {
const _id = toObjectId<LedgerTransactionId>(transactionId)
const { results } = await MainBook.ledger({
account: liabilitiesWalletId,
_id,
})
if (results.length === 1) {
return translateToLedgerTx(results[0])
}
return new CouldNotFindTransactionError()
} catch (err) {
return new UnknownLedgerError(err)
}
}

const getTransactionsByHash = async (
hash: PaymentHash | OnChainTxHash,
): Promise<LedgerTransaction<WalletCurrency>[] | LedgerServiceError> => {
Expand All @@ -104,6 +127,28 @@ export const LedgerService = (): ILedgerService => {
}
}

const getTransactionForWalletByPaymentHash = async ({
walletId,
paymentHash,
}: {
walletId: WalletId
paymentHash: PaymentHash
}): Promise<LedgerTransaction<WalletCurrency> | LedgerError> => {
const liabilitiesWalletId = toLiabilitiesWalletId(walletId)
try {
const { results } = await MainBook.ledger({
account: liabilitiesWalletId,
hash: paymentHash,
})
if (results.length === 1) {
return translateToLedgerTx(results[0])
}
return new CouldNotFindTransactionError()
} catch (err) {
return new UnknownLedgerError(err)
}
}

const getTransactionsByWalletId = async (
walletId: WalletId,
): Promise<LedgerTransaction<WalletCurrency>[] | LedgerError> => {
Expand Down Expand Up @@ -414,7 +459,9 @@ export const LedgerService = (): ILedgerService => {
fns: {
updateMetadataByHash,
getTransactionById,
getTransactionForWalletById,
getTransactionsByHash,
getTransactionForWalletByPaymentHash,
getTransactionsByWalletId,
getTransactionsByWalletIds,
getTransactionsByWalletIdAndContactUsername,
Expand Down
18 changes: 18 additions & 0 deletions core/api/src/services/mongoose/wallet-invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ export const WalletInvoicesRepository = (): IWalletInvoicesRepository => {
}
}

const findForWalletByPaymentHash = async ({
walletId,
paymentHash,
}: WalletInvoiceFindForWalletByPaymentHashArgs): Promise<
WalletInvoice | RepositoryError
> => {
try {
const walletInvoice = await WalletInvoice.findOne({ _id: paymentHash, walletId })
if (!walletInvoice) {
return new CouldNotFindWalletInvoiceError(paymentHash)
}
return ensureWalletInvoiceHasLnInvoice(walletInvoiceFromRaw(walletInvoice))
} catch (err) {
return parseRepositoryError(err)
}
}

async function* yieldPending():
| AsyncGenerator<WalletInvoiceWithOptionalLnInvoice>
| RepositoryError {
Expand Down Expand Up @@ -124,6 +141,7 @@ export const WalletInvoicesRepository = (): IWalletInvoicesRepository => {
persistNew,
markAsPaid,
findByPaymentHash,
findForWalletByPaymentHash,
yieldPending,
deleteByPaymentHash,
deleteUnpaidOlderThan,
Expand Down
22 changes: 22 additions & 0 deletions core/api/src/services/mongoose/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ export const WalletsRepository = (): IWalletsRepository => {
}
}

const findForAccountById = async ({
accountId,
walletId,
}: {
accountId: AccountId
walletId: WalletId
}): Promise<Wallet | RepositoryError> => {
try {
const result: WalletRecord | null = await Wallet.findOne({
id: walletId,
accountId,
})
if (!result) {
return new CouldNotFindWalletFromIdError()
}
return resultToWallet(result)
} catch (err) {
return parseRepositoryError(err)
}
}

const listByAccountId = async (
accountId: AccountId,
): Promise<Wallet[] | RepositoryError> => {
Expand Down Expand Up @@ -146,6 +167,7 @@ export const WalletsRepository = (): IWalletsRepository => {

return {
findById,
findForAccountById,
listByAccountId,
findAccountWalletsByAccountId,
findByAddress,
Expand Down

0 comments on commit 4c19543

Please sign in to comment.