Skip to content

Commit

Permalink
Add cursor based pagination for asset transactions (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophialittlejohn authored Sep 6, 2024
1 parent 699bebd commit 958faf9
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3027,12 +3027,63 @@ export function getPoolsModule(inst: Centrifuge) {
function getAssetTransactions(args: [poolId: string, from?: Date, to?: Date]) {
const [poolId, from, to] = args

const $query = inst.getSubqueryObservable<{
assetTransactions: { nodes: SubqueryAssetTransaction[] }
return of({ assetTransactions: [], endCursor: null, hasNextPage: true }).pipe(
expand(({ assetTransactions, endCursor, hasNextPage }) => {
if (!hasNextPage) return EMPTY
return getAssetTransactionsWithCursor(poolId, endCursor, from, to).pipe(
map((response) => {
if (response?.assetTransactions) {
const { endCursor, hasNextPage } = response.assetTransactions.pageInfo

return {
endCursor,
hasNextPage,
assetTransactions: [...assetTransactions, ...response.assetTransactions.nodes],
}
}
return {}
})
)
}),
takeLast(1),
switchMap(({ assetTransactions }) => combineLatest([of(assetTransactions), getPoolCurrency([poolId])])),
map(([transactions, currency]) => {
return (
transactions?.map((tx) => ({
...tx,
amount: tx.amount ? new CurrencyBalance(tx.amount, currency.decimals) : undefined,
principalAmount: tx.principalAmount
? new CurrencyBalance(tx.principalAmount, currency.decimals)
: undefined,
interestAmount: tx.interestAmount ? new CurrencyBalance(tx.interestAmount, currency.decimals) : undefined,
realizedProfitFifo: tx.realizedProfitFifo
? new CurrencyBalance(tx.realizedProfitFifo, currency.decimals)
: undefined,
sumRealizedProfitFifo: tx.asset.sumRealizedProfitFifo
? new CurrencyBalance(tx.asset.sumRealizedProfitFifo, currency.decimals)
: undefined,
unrealizedProfitAtMarketPrice: tx.asset.unrealizedProfitAtMarketPrice
? new CurrencyBalance(tx.asset.unrealizedProfitAtMarketPrice, currency.decimals)
: undefined,
timestamp: new Date(`${tx.timestamp}+00:00`),
})) || ([] satisfies AssetTransaction[])
)
})
)
}

function getAssetTransactionsWithCursor(poolId: string, endCursor: string | null, from?: Date, to?: Date) {
return inst.getSubqueryObservable<{
assetTransactions: {
nodes: SubqueryAssetTransaction[]
pageInfo: { hasNextPage: boolean; endCursor: string }
}
}>(
`query($poolId: String!, $from: Datetime!, $to: Datetime!) {
`query($poolId: String!, $from: Datetime!, $to: Datetime!, $after: Cursor) {
assetTransactions(
first: 100,
orderBy: TIMESTAMP_ASC,
after: $after,
filter: {
poolId: { equalTo: $poolId },
timestamp: { greaterThan: $from, lessThan: $to },
Expand Down Expand Up @@ -3069,38 +3120,20 @@ export function getPoolsModule(inst: Centrifuge) {
type
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
`,
}`,
{
poolId,
from: from ? from.toISOString() : getDateYearsFromNow(-10).toISOString(),
to: to ? to.toISOString() : new Date().toISOString(),
after: endCursor,
},
false
)

return $query.pipe(
switchMap(() => combineLatest([$query, getPoolCurrency([poolId])])),
map(([data, currency]) => {
return data!.assetTransactions.nodes.map((tx) => ({
...tx,
amount: tx.amount ? new CurrencyBalance(tx.amount, currency.decimals) : undefined,
principalAmount: tx.principalAmount ? new CurrencyBalance(tx.principalAmount, currency.decimals) : undefined,
interestAmount: tx.interestAmount ? new CurrencyBalance(tx.interestAmount, currency.decimals) : undefined,
realizedProfitFifo: tx.realizedProfitFifo
? new CurrencyBalance(tx.realizedProfitFifo, currency.decimals)
: undefined,
sumRealizedProfitFifo: tx.asset.sumRealizedProfitFifo
? new CurrencyBalance(tx.asset.sumRealizedProfitFifo, currency.decimals)
: undefined,
unrealizedProfitAtMarketPrice: tx.asset.unrealizedProfitAtMarketPrice
? new CurrencyBalance(tx.asset.unrealizedProfitAtMarketPrice, currency.decimals)
: undefined,
timestamp: new Date(`${tx.timestamp}+00:00`),
})) satisfies AssetTransaction[]
})
)
}

function getFeeTransactions(args: [poolId: string, from?: Date, to?: Date]) {
Expand Down

0 comments on commit 958faf9

Please sign in to comment.