mars-v2-frontend/src/utils/iterateContractQuery.ts
Yusuf Seyrek 49e7778b1e
Mp 2836 implement pagination to the batched smart contract queries (#301)
* feat: batch and paginate oracle prices

* fixes and improvements

* feat: complete batching mechanism and its implementation

* fix: getVaultConfigs usage

* fix: linting

* feat: remove hardcoded mars decimals
2023-07-17 12:01:00 +03:00

37 lines
883 B
TypeScript

import { ITEM_LIMIT_PER_QUERY } from 'constants/query'
interface KeyProperties {
denom?: string
addr?: string
}
type Query<T> = ({
limit,
startAfter,
}: {
limit?: number | undefined
startAfter?: string | undefined
}) => Promise<T[]>
export default async function iterateContractQuery<T extends KeyProperties>(
query: Query<T>,
keyProperty: keyof KeyProperties = 'denom',
previousResults?: T[],
): Promise<T[]> {
const lastItem = previousResults && previousResults.at(-1)
const lastItemKey = lastItem && lastItem[keyProperty]
const params = {
limit: ITEM_LIMIT_PER_QUERY,
startAfter: lastItemKey,
}
const results = await query(params)
const accumulated = (previousResults ?? []).concat(results)
if (results.length < ITEM_LIMIT_PER_QUERY) {
return accumulated
}
return await iterateContractQuery(query, keyProperty, accumulated)
}