Skip to content

Conversation

@JunichiSugiura
Copy link
Contributor

Incrementally fetch events by 10 blocks when table pagination hits the last 3 pages.

@vercel
Copy link

vercel bot commented Mar 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
explorer-sepolia ✅ Ready (Inspect) Visit Preview Apr 11, 2025 1:40pm
explorer-starknet ✅ Ready (Inspect) Visit Preview Apr 11, 2025 1:40pm

initialData: {
pages: [],
},
enabled: !!contractAddress && !latestBlockQuery.isLoading && isLocalNode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enabled: !!contractAddress && !latestBlockQuery.isLoading && isLocalNode
enabled: contractAddress && !latestBlockQuery.isLoading && isLocalNode

Comment on lines 276 to 153
const blockOffset = 10;
const fetchOffset = 5;
const eventsQuery = useInfiniteQuery({
queryKey: ["contract", contractAddress, "events"],
queryFn: async ({ pageParam }) => {
const events = [];
let continuationToken = null;
while (continuationToken === null || !!continuationToken) {
const res = await RPC_PROVIDER.getEvents({
address: contractAddress!,
chunk_size: 100,
from_block: pageParam.fromBlock,
to_block: pageParam.toBlock,
continuation_token: continuationToken ?? undefined
});
continuationToken = res.continuation_token;
events.push(...res.events);
}

return events
},
getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
return {
fromBlock: { block_number: lastPageParam.fromBlock.block_number - blockOffset },
toBlock: { block_number: lastPageParam.toBlock.block_number - blockOffset }
};
},
initialPageParam: {
fromBlock: { block_number: latestBlockQuery.data ? latestBlockQuery.data.block_number - blockOffset : undefined } as Block,
toBlock: { block_number: latestBlockQuery.data ? latestBlockQuery.data.block_number : undefined } as Block,
},
initialData: {
pages: [],
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be simplified to:

  1. Limit the chunk size to pageSize (ie the number of events we want to display per page)
  2. Set from_block == 0 and to_block == 'pending'
    • This avoid having to manually adjust the block offset
  3. Store the continuation_token as a state
  4. Return the continuation_token from getNextPageParam

Copy link
Contributor Author

@JunichiSugiura JunichiSugiura Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set from_block == 0 and to_block == 'pending'

Does it allows us to order by newest entries?

I tried from_block === "latest" and to_block === "0" but didn't work.

If it works, step 3 is unnecessary since it can be passed as a page param.

Copy link
Member

@kariy kariy Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it allows us to order by newest entries?

Yup, technically it should be ordered like so.

I tried from_block === "latest" and to_block === "0" but didn't work.

The to_block should be a higher block than from_block, so it should be the other way around. And in this case, let's set the to_block to 'pending'

Copy link
Contributor Author

@JunichiSugiura JunichiSugiura Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, i still don't understand how to get the latest txs in desc without having block_number as a page params if to_block has to be higher than from_block?
(i.e. what's the query for fetching the latest N txs from block 0 to "pending"?)

Also, eventsQuery.fetchNextPage would be called from a transactions table as events are dependencies of fetching all txs related to the contract without indexer.

Copy link
Member

@kariy kariy Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah my bad sorry, I misunderstood your statement. I was assuming that you're fetching the events from oldest to newest (the default ordering).

(i.e. what's the query for fetching the latest N txs from block 0 to "pending"?)

Yea, this can't be done directly in the query. In which case, you're approach is valid then - to fetch from latest block to latest block - offset. But the chunk size can still be limited to the page size.


Assuming?? In this day and age?? What a blasphemy.

Copy link
Contributor Author

@JunichiSugiura JunichiSugiura Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think limiting chunk size to page size would matter since all events between pageParam.fromBlock - pageParam.toBlock has to be fetched in order to sort in desc anyways. I can change the fixed chunk_size smaller tho in case current 100 is too big per request (or smaller block offset).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, make sense. Then, I think for now the chunk size and block offset value don't matter much considering this is only for local dev.

@JunichiSugiura
Copy link
Contributor Author

JunichiSugiura commented Apr 11, 2025

The problem with this approach is that if there's no event in the latest blocks, it'll spam node with get_events RPC calls till it'll find enough events in earlier blocks or genesis. The other problem is to find a proper event index within tx as get_events call is scope to the contract (not aware of how many events in the same tx in total).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants