@@ -15,20 +15,52 @@ import {
1515 formatSetSwapperEvent ,
1616 formatTransferEvent
1717} from './formatting'
18- import { prizeHook , prizePool , prizeVault , twabRewards } from '$lib/config'
18+ import { eventQuerySettings , prizeHook , prizePool , prizeVault , twabRewards } from '$lib/config'
1919import { validateClientNetwork } from './providers'
2020import { get } from 'svelte/store'
2121import type { ClaimedPrizeEvent , FlashEvent , PromotionCreatedEvent , RewardsClaimedEvent , SetSwapperEvent , TransferEvent } from '$lib/types'
22- import type { Address } from 'viem'
23-
24- export const getTransferEvents = async (
25- address : Address ,
26- tokenAddress : Address ,
27- options ?: { filter ?: 'from' | 'to' ; fromBlock ?: bigint }
22+ import type { AbiEvent , Address , GetLogsParameters , GetLogsReturnType } from 'viem'
23+
24+ const getPaginatedEvents = async < Event extends AbiEvent > (
25+ params : GetLogsParameters < Event , undefined , true , bigint , bigint | 'latest' > & {
26+ fromBlock : bigint
27+ toBlock : bigint | 'latest'
28+ args ?: any
29+ }
2830) => {
2931 const publicClient = get ( clients ) . public
3032 validateClientNetwork ( publicClient )
3133
34+ const events : GetLogsReturnType < Event , undefined , true , bigint , bigint , Event [ 'name' ] > = [ ]
35+
36+ const maxBlock = params . toBlock === 'latest' ? await publicClient . getBlockNumber ( ) : params . toBlock
37+
38+ let fromBlock = params . fromBlock
39+ let toBlock = params . fromBlock + eventQuerySettings . maxPageSizeInBlocks - 1n
40+
41+ if ( toBlock > maxBlock ) {
42+ toBlock = maxBlock
43+ }
44+
45+ while ( toBlock <= maxBlock ) {
46+ const newEventsPage = await publicClient . getLogs < Event , undefined , true , bigint , bigint > ( { ...params , fromBlock, toBlock } )
47+ events . push ( ...newEventsPage )
48+
49+ fromBlock = toBlock + 1n
50+
51+ if ( toBlock !== maxBlock && toBlock + eventQuerySettings . maxPageSizeInBlocks > maxBlock ) {
52+ toBlock = maxBlock
53+ } else {
54+ toBlock += eventQuerySettings . maxPageSizeInBlocks
55+ }
56+
57+ await new Promise ( ( resolve ) => setTimeout ( resolve , eventQuerySettings . paginationDelay ) )
58+ }
59+
60+ return events
61+ }
62+
63+ const getTransferEvents = async ( address : Address , tokenAddress : Address , options ?: { filter ?: 'from' | 'to' ; fromBlock ?: bigint } ) => {
3264 const transferEvent = {
3365 type : 'event' ,
3466 name : 'Transfer' ,
@@ -42,7 +74,7 @@ export const getTransferEvents = async (
4274 const fromTransferEvents =
4375 options ?. filter === 'to'
4476 ? [ ]
45- : await publicClient . getLogs ( {
77+ : await getPaginatedEvents ( {
4678 address : tokenAddress ,
4779 event : transferEvent ,
4880 args : { from : address } ,
@@ -54,7 +86,7 @@ export const getTransferEvents = async (
5486 const toTransferEvents =
5587 options ?. filter === 'from'
5688 ? [ ]
57- : await publicClient . getLogs ( {
89+ : await getPaginatedEvents ( {
5890 address : tokenAddress ,
5991 event : transferEvent ,
6092 args : { to : address } ,
@@ -66,13 +98,10 @@ export const getTransferEvents = async (
6698 return [ ...fromTransferEvents , ...toTransferEvents ]
6799}
68100
69- export const getFlashEvents = async ( beneficiary : Address , swapperAddresses : Address [ ] , options ?: { fromBlock ?: bigint } ) => {
101+ const getFlashEvents = async ( beneficiary : Address , swapperAddresses : Address [ ] , options ?: { fromBlock ?: bigint } ) => {
70102 if ( ! swapperAddresses . length ) return [ ]
71103
72- const publicClient = get ( clients ) . public
73- validateClientNetwork ( publicClient )
74-
75- const flashEvents = await publicClient . getLogs ( {
104+ const flashEvents = await getPaginatedEvents ( {
76105 address : swapperAddresses ,
77106 event : {
78107 anonymous : false ,
@@ -114,11 +143,8 @@ export const getFlashEvents = async (beneficiary: Address, swapperAddresses: Add
114143 return flashEvents
115144}
116145
117- export const getSetSwapperEvents = async ( userAddress : Address , options ?: { fromBlock ?: bigint } ) => {
118- const publicClient = get ( clients ) . public
119- validateClientNetwork ( publicClient )
120-
121- const setSwapperEvents = await publicClient . getLogs ( {
146+ const getSetSwapperEvents = async ( userAddress : Address , options ?: { fromBlock ?: bigint } ) => {
147+ const setSwapperEvents = await getPaginatedEvents ( {
122148 address : prizeHook . address ,
123149 event : {
124150 anonymous : false ,
@@ -139,11 +165,8 @@ export const getSetSwapperEvents = async (userAddress: Address, options?: { from
139165 return setSwapperEvents
140166}
141167
142- export const getPromotionCreatedEvents = async ( options ?: { fromBlock ?: bigint } ) => {
143- const publicClient = get ( clients ) . public
144- validateClientNetwork ( publicClient )
145-
146- const promotionCreatedEvents = await publicClient . getLogs ( {
168+ const getPromotionCreatedEvents = async ( options ?: { fromBlock ?: bigint } ) => {
169+ const promotionCreatedEvents = await getPaginatedEvents ( {
147170 address : twabRewards . address ,
148171 event : {
149172 anonymous : false ,
@@ -171,11 +194,8 @@ export const getPromotionCreatedEvents = async (options?: { fromBlock?: bigint }
171194 return promotionCreatedEvents
172195}
173196
174- export const getRewardsClaimedEvents = async ( userAddress : Address , options ?: { fromBlock ?: bigint } ) => {
175- const publicClient = get ( clients ) . public
176- validateClientNetwork ( publicClient )
177-
178- const rewardsClaimedEvents = await publicClient . getLogs ( {
197+ const getRewardsClaimedEvents = async ( userAddress : Address , options ?: { fromBlock ?: bigint } ) => {
198+ const rewardsClaimedEvents = await getPaginatedEvents ( {
179199 address : twabRewards . address ,
180200 event : {
181201 anonymous : false ,
@@ -199,11 +219,8 @@ export const getRewardsClaimedEvents = async (userAddress: Address, options?: {
199219 return rewardsClaimedEvents
200220}
201221
202- export const getClaimedPrizeEvents = async ( userAddress : Address , options ?: { fromBlock ?: bigint } ) => {
203- const publicClient = get ( clients ) . public
204- validateClientNetwork ( publicClient )
205-
206- const claimedPrizeEvents = await publicClient . getLogs ( {
222+ const getClaimedPrizeEvents = async ( userAddress : Address , options ?: { fromBlock ?: bigint } ) => {
223+ const claimedPrizeEvents = await getPaginatedEvents ( {
207224 address : prizePool . address ,
208225 event : {
209226 anonymous : false ,
0 commit comments