@@ -113,6 +113,9 @@ export default class EventDbRepository implements EventRepository {
113113 }
114114
115115 async getEventsWithQualifiedName ( params : GetEventsParams ) {
116+ const HEIGHT_BATCH_SIZE = 200 ;
117+ const localOperator = ( paramsLength : number ) => ( paramsLength > 2 ? `\nAND` : 'WHERE' ) ;
118+
116119 const {
117120 qualifiedEventName,
118121 blockHash,
@@ -138,73 +141,121 @@ export default class EventDbRepository implements EventRepository {
138141 const name = splitted . pop ( ) ?? '' ;
139142 const module = splitted . join ( '.' ) ;
140143
141- const queryParams : ( string | number ) [ ] = [ limit ] ;
144+ const queryParams : ( string | number ) [ ] = [ limit , module , name ] ;
145+ const blockQueryParams : ( string | number ) [ ] = [ ] ;
142146 let conditions = '' ;
147+ let eventConditions = '' ;
143148
144- queryParams . push ( module ) ;
145- conditions += `WHERE e.module = $${ queryParams . length } ` ;
146- queryParams . push ( name ) ;
147- conditions += `\nAND e.name = $${ queryParams . length } ` ;
148-
149- if ( blockHash ) {
150- queryParams . push ( blockHash ) ;
151- conditions += `\nAND b.hash = $${ queryParams . length } ` ;
149+ if ( before ) {
150+ queryParams . push ( before ) ;
151+ eventConditions += `\nAND e.id > $${ queryParams . length } ` ;
152152 }
153153
154- if ( chainId ) {
155- queryParams . push ( chainId ) ;
156- conditions += `\nAND b."chainId" = $${ queryParams . length } ` ;
154+ if ( after ) {
155+ queryParams . push ( after ) ;
156+ eventConditions += `\nAND e.id < $${ queryParams . length } ` ;
157157 }
158158
159- if ( minHeight ) {
160- queryParams . push ( minHeight ) ;
161- conditions += `\nAND b."height" >= $${ queryParams . length } ` ;
159+ if ( requestKey ) {
160+ blockQueryParams . push ( requestKey ) ;
161+ const op = localOperator ( blockQueryParams . length ) ;
162+ conditions += `${ op } t."requestkey" = $${ blockQueryParams . length + queryParams . length } ` ;
162163 }
163164
164- if ( maxHeight ) {
165- queryParams . push ( maxHeight ) ;
166- conditions += `\nAND b."height" <= $${ queryParams . length } ` ;
165+ if ( blockHash ) {
166+ blockQueryParams . push ( blockHash ) ;
167+ const op = localOperator ( blockQueryParams . length ) ;
168+ conditions += `${ op } b.hash = $${ blockQueryParams . length + queryParams . length } ` ;
167169 }
168170
169- if ( minimumDepth ) {
170- queryParams . push ( minimumDepth ) ;
171- conditions += `\nAND b."height" <= $${ queryParams . length } ` ;
171+ if ( chainId ) {
172+ blockQueryParams . push ( chainId ) ;
173+ const op = localOperator ( blockQueryParams . length ) ;
174+ conditions += `${ op } b."chainId" = $${ blockQueryParams . length + queryParams . length } ` ;
172175 }
173176
174- if ( requestKey ) {
175- queryParams . push ( requestKey ) ;
176- conditions += `\nAND t."requestkey" = $${ queryParams . length } ` ;
177+ let fromHeight = 0 ;
178+ let toHeight = 0 ;
179+ if ( minHeight && maxHeight ) {
180+ fromHeight = minHeight ;
181+ toHeight = maxHeight - minHeight > 100 ? minHeight + HEIGHT_BATCH_SIZE : toHeight ;
182+ } else if ( minHeight ) {
183+ fromHeight = minHeight ;
184+ toHeight = minHeight + HEIGHT_BATCH_SIZE ;
185+ } else if ( maxHeight ) {
186+ fromHeight = maxHeight - HEIGHT_BATCH_SIZE ;
187+ toHeight = maxHeight ;
177188 }
178189
179- if ( before ) {
180- queryParams . push ( before ) ;
181- conditions += `\nAND e.id > $${ queryParams . length } ` ;
190+ if ( fromHeight && toHeight ) {
191+ queryParams . push ( fromHeight ) ;
192+ let op = localOperator ( blockQueryParams . length ) ;
193+ conditions += `${ op } b."height" >= $${ blockQueryParams . length + queryParams . length } ` ;
194+ queryParams . push ( toHeight ) ;
195+ conditions += `\nAND b."height" <= $${ blockQueryParams . length + queryParams . length } ` ;
182196 }
183197
184- if ( after ) {
185- queryParams . push ( after ) ;
186- conditions += `\nAND e.id < $${ queryParams . length } ` ;
198+ if ( minimumDepth ) {
199+ queryParams . push ( minimumDepth ) ;
200+ const op = localOperator ( blockQueryParams . length ) ;
201+ conditions += `${ op } b."height" <= $${ blockQueryParams . length + queryParams . length } ` ;
187202 }
188203
189- const query = `
190- select e.id as id,
191- t.requestkey as "requestKey",
192- b."chainId" as "chainId",
193- b.height as height,
194- e."orderIndex" as "orderIndex",
195- e.module as "moduleName",
196- e.name as name,
197- e.params as parameters,
198- b.hash as "blockHash"
199- from "Events" e
200- join "Transactions" t on e."transactionId" = t.id
201- join "Blocks" b on b.id = t."blockId"
202- ${ conditions }
203- ORDER BY id ${ order }
204- LIMIT $1
205- ` ;
204+ let query = '' ;
205+ if ( fromHeight || toHeight || blockHash || chainId ) {
206+ query = `
207+ WITH block_filtered AS (
208+ select *
209+ from "Blocks" b
210+ ${ conditions }
211+ )
212+ SELECT
213+ e.id as id,
214+ e.requestkey as "requestKey",
215+ e."chainId" as "chainId",
216+ b.height as height,
217+ e."orderIndex" as "orderIndex",
218+ e.module as "moduleName",
219+ e.name as name,
220+ e.params as parameters,
221+ b.hash as "blockHash"
222+ FROM block_filtered b
223+ join "Transactions" t ON t."blockId" = b.id
224+ join "Events" e ON e."transactionId" = t.id
225+ WHERE e.module = $2
226+ AND e.name = $3
227+ ${ eventConditions }
228+ LIMIT $1
229+ ` ;
230+ } else {
231+ query = `
232+ WITH event_filtered AS (
233+ select *
234+ from "Events" e
235+ WHERE e.module = $2
236+ AND e.name = $3
237+ ${ eventConditions }
238+ ORDER BY e.module, e.name ${ order }
239+ )
240+ SELECT
241+ e.id as id,
242+ e.requestkey as "requestKey",
243+ e."chainId" as "chainId",
244+ b.height as height,
245+ e."orderIndex" as "orderIndex",
246+ e.module as "moduleName",
247+ e.name as name,
248+ e.params as parameters,
249+ b.hash as "blockHash"
250+ FROM event_filtered e
251+ join "Transactions" t ON t.id = e."transactionId"
252+ join "Blocks" b ON b.id = t."blockId"
253+ ${ conditions }
254+ LIMIT $1
255+ ` ;
256+ }
206257
207- const { rows } = await rootPgPool . query ( query , queryParams ) ;
258+ const { rows } = await rootPgPool . query ( query , [ ... queryParams , ... blockQueryParams ] ) ;
208259
209260 const edges = rows . map ( row => ( {
210261 cursor : row . id . toString ( ) ,
0 commit comments