-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8779294
commit d26a48b
Showing
21 changed files
with
360 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 25 additions & 15 deletions
40
core/api/src/app/accounts/get-transactions-for-account.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,44 @@ | ||
import { getTransactionsForWallets } from "../wallets" | ||
|
||
import { PartialResult } from "../partial-result" | ||
|
||
import { AccountValidator } from "@/domain/accounts" | ||
import { RepositoryError } from "@/domain/errors" | ||
import { WalletsRepository } from "@/services/mongoose" | ||
|
||
export const getTransactionsForAccountByWalletIds = async ({ | ||
account, | ||
walletIds, | ||
paginationArgs, | ||
rawPaginationArgs, | ||
}: { | ||
account: Account | ||
walletIds: WalletId[] | ||
paginationArgs?: PaginationArgs | ||
}): Promise<PartialResult<PaginatedArray<WalletTransaction>>> => { | ||
walletIds?: WalletId[] | ||
rawPaginationArgs: { | ||
first?: number | null | ||
last?: number | null | ||
before?: string | null | ||
after?: string | null | ||
} | ||
}): Promise<PaginatedQueryResult<WalletTransaction> | ApplicationError> => { | ||
const walletsRepo = WalletsRepository() | ||
|
||
const wallets: Wallet[] = [] | ||
for (const walletId of walletIds) { | ||
const wallet = await walletsRepo.findById(walletId) | ||
if (wallet instanceof RepositoryError) return PartialResult.err(wallet) | ||
|
||
const accountValidator = AccountValidator(account) | ||
if (accountValidator instanceof Error) return PartialResult.err(accountValidator) | ||
const validateWallet = accountValidator.validateWalletForAccount(wallet) | ||
if (validateWallet instanceof Error) return PartialResult.err(validateWallet) | ||
if (walletIds) { | ||
for (const walletId of walletIds) { | ||
const wallet = await walletsRepo.findById(walletId) | ||
if (wallet instanceof RepositoryError) return wallet | ||
|
||
const accountValidator = AccountValidator(account) | ||
if (accountValidator instanceof Error) return accountValidator | ||
const validateWallet = accountValidator.validateWalletForAccount(wallet) | ||
if (validateWallet instanceof Error) return validateWallet | ||
|
||
wallets.push(wallet) | ||
wallets.push(wallet) | ||
} | ||
} else { | ||
const accountWallets = await walletsRepo.listByAccountId(account.id) | ||
if (accountWallets instanceof RepositoryError) return accountWallets | ||
wallets.push(...accountWallets) | ||
} | ||
|
||
return getTransactionsForWallets({ wallets, paginationArgs }) | ||
return getTransactionsForWallets({ wallets, rawPaginationArgs }) | ||
} |
75 changes: 37 additions & 38 deletions
75
core/api/src/app/wallets/get-transactions-by-addresses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,60 @@ | ||
import { memoSharingConfig } from "@/config" | ||
import { PartialResult } from "@/app/partial-result" | ||
import { MAX_PAGINATION_PAGE_SIZE, memoSharingConfig } from "@/config" | ||
|
||
import { LedgerError } from "@/domain/ledger" | ||
import { WalletTransactionHistory } from "@/domain/wallets" | ||
import { CouldNotFindError } from "@/domain/errors" | ||
|
||
import { getNonEndUserWalletIds, LedgerService } from "@/services/ledger" | ||
import { WalletOnChainPendingReceiveRepository } from "@/services/mongoose" | ||
import { checkedToPaginatedQueryArgs } from "@/domain/primitives" | ||
|
||
export const getTransactionsForWalletsByAddresses = async ({ | ||
wallets, | ||
addresses, | ||
paginationArgs, | ||
rawPaginationArgs, | ||
}: { | ||
wallets: Wallet[] | ||
addresses: OnChainAddress[] | ||
paginationArgs?: PaginationArgs | ||
}): Promise<PartialResult<PaginatedArray<WalletTransaction>>> => { | ||
const walletIds = wallets.map((wallet) => wallet.id) | ||
rawPaginationArgs: { | ||
first?: number | null | ||
last?: number | null | ||
before?: string | null | ||
after?: string | null | ||
} | ||
}): Promise<PaginatedQueryResult<WalletTransaction> | ApplicationError> => { | ||
const paginationArgs = checkedToPaginatedQueryArgs({ | ||
paginationArgs: rawPaginationArgs, | ||
maxPageSize: MAX_PAGINATION_PAGE_SIZE, | ||
}) | ||
|
||
let pendingHistory = | ||
await WalletOnChainPendingReceiveRepository().listByWalletIdsAndAddresses({ | ||
walletIds, | ||
addresses, | ||
}) | ||
if (pendingHistory instanceof Error) { | ||
if (pendingHistory instanceof CouldNotFindError) { | ||
pendingHistory = [] | ||
} else { | ||
return PartialResult.err(pendingHistory) | ||
} | ||
if (paginationArgs instanceof Error) { | ||
return paginationArgs | ||
} | ||
|
||
const confirmedLedgerTxns = await LedgerService().getTransactionsByWalletIds({ | ||
const walletIds = wallets.map((wallet) => wallet.id) | ||
|
||
const ledgerTxs = await LedgerService().getTransactionsByWalletIdsAndAddresses({ | ||
walletIds, | ||
paginationArgs, | ||
addresses, | ||
}) | ||
if (confirmedLedgerTxns instanceof LedgerError) { | ||
return PartialResult.partial( | ||
{ slice: pendingHistory, total: pendingHistory.length }, | ||
confirmedLedgerTxns, | ||
) | ||
|
||
if (ledgerTxs instanceof LedgerError) { | ||
return ledgerTxs | ||
} | ||
const ledgerTransactions = confirmedLedgerTxns.slice.filter( | ||
(tx) => tx.address && addresses.includes(tx.address), | ||
) | ||
|
||
const confirmedHistory = WalletTransactionHistory.fromLedger({ | ||
ledgerTransactions, | ||
nonEndUserWalletIds: Object.values(await getNonEndUserWalletIds()), | ||
memoSharingConfig, | ||
}) | ||
|
||
const transactions = [...pendingHistory, ...confirmedHistory.transactions] | ||
const nonEndUserWalletIds = Object.values(await getNonEndUserWalletIds()) | ||
|
||
const txEdges = ledgerTxs.edges.map((edge) => { | ||
const { transactions } = WalletTransactionHistory.fromLedger({ | ||
ledgerTransactions: [edge.node], | ||
nonEndUserWalletIds, | ||
memoSharingConfig, | ||
}) | ||
|
||
return PartialResult.ok({ | ||
slice: transactions, | ||
total: transactions.length, | ||
return { | ||
cursor: edge.cursor, | ||
node: transactions[0], | ||
} | ||
}) | ||
|
||
return { ...ledgerTxs, edges: txEdges } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,53 @@ | ||
import { memoSharingConfig } from "@/config" | ||
import { PartialResult } from "@/app/partial-result" | ||
import { MAX_PAGINATION_PAGE_SIZE, memoSharingConfig } from "@/config" | ||
|
||
import { LedgerError } from "@/domain/ledger" | ||
import { WalletTransactionHistory } from "@/domain/wallets" | ||
import { CouldNotFindError } from "@/domain/errors" | ||
|
||
import { getNonEndUserWalletIds, LedgerService } from "@/services/ledger" | ||
import { WalletOnChainPendingReceiveRepository } from "@/services/mongoose" | ||
import { checkedToPaginatedQueryArgs } from "@/domain/primitives" | ||
|
||
export const getTransactionsForWallets = async ({ | ||
wallets, | ||
paginationArgs, | ||
rawPaginationArgs, | ||
}: { | ||
wallets: Wallet[] | ||
paginationArgs?: PaginationArgs | ||
}): Promise<PartialResult<PaginatedArray<WalletTransaction>>> => { | ||
const walletIds = wallets.map((wallet) => wallet.id) | ||
|
||
let pendingHistory = await WalletOnChainPendingReceiveRepository().listByWalletIds({ | ||
walletIds, | ||
}) | ||
if (pendingHistory instanceof Error) { | ||
if (pendingHistory instanceof CouldNotFindError) { | ||
pendingHistory = [] | ||
} else { | ||
return PartialResult.err(pendingHistory) | ||
} | ||
rawPaginationArgs: { | ||
first?: number | null | ||
last?: number | null | ||
before?: string | null | ||
after?: string | null | ||
} | ||
}): Promise<PaginatedQueryResult<WalletTransaction> | ApplicationError> => { | ||
const paginationArgs = checkedToPaginatedQueryArgs({ | ||
paginationArgs: rawPaginationArgs, | ||
maxPageSize: MAX_PAGINATION_PAGE_SIZE, | ||
}) | ||
|
||
if (paginationArgs instanceof Error) return paginationArgs | ||
|
||
const confirmedLedgerTxns = await LedgerService().getTransactionsByWalletIds({ | ||
const walletIds = wallets.map((wallet) => wallet.id) | ||
|
||
const ledgerTxs = await LedgerService().getTransactionsByWalletIds({ | ||
walletIds, | ||
paginationArgs, | ||
}) | ||
|
||
if (confirmedLedgerTxns instanceof LedgerError) { | ||
return PartialResult.partial( | ||
{ slice: pendingHistory, total: pendingHistory.length }, | ||
confirmedLedgerTxns, | ||
) | ||
} | ||
if (ledgerTxs instanceof LedgerError) return ledgerTxs | ||
|
||
const confirmedHistory = WalletTransactionHistory.fromLedger({ | ||
ledgerTransactions: confirmedLedgerTxns.slice, | ||
nonEndUserWalletIds: Object.values(await getNonEndUserWalletIds()), | ||
memoSharingConfig, | ||
}) | ||
const nonEndUserWalletIds = Object.values(await getNonEndUserWalletIds()) | ||
|
||
const transactions = [...pendingHistory, ...confirmedHistory.transactions] | ||
const txEdges = ledgerTxs.edges.map((edge) => { | ||
const { transactions } = WalletTransactionHistory.fromLedger({ | ||
ledgerTransactions: [edge.node], | ||
nonEndUserWalletIds, | ||
memoSharingConfig, | ||
}) | ||
|
||
return PartialResult.ok({ | ||
slice: transactions, | ||
total: confirmedLedgerTxns.total + pendingHistory.length, | ||
return { | ||
cursor: edge.cursor, | ||
node: transactions[0], | ||
} | ||
}) | ||
|
||
return { ...ledgerTxs, edges: txEdges } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.