@@ -407,6 +407,91 @@ func (rc *NodeClient) AccountTransactions(account AccountAddress, start *uint64,
407
407
})
408
408
}
409
409
410
+ func (rc * NodeClient ) EventsByHandle (
411
+ account AccountAddress ,
412
+ eventHandle string ,
413
+ fieldName string ,
414
+ start * uint64 ,
415
+ limit * uint64 ,
416
+ ) (data []* api.Event , err error ) {
417
+ basePath := fmt .Sprintf ("accounts/%s/events/%s/%s" ,
418
+ account .String (),
419
+ eventHandle ,
420
+ fieldName )
421
+
422
+ baseUrl := rc .baseUrl .JoinPath (basePath )
423
+
424
+ const eventsPageSize = 100
425
+ var effectiveLimit uint64
426
+ if limit == nil {
427
+ effectiveLimit = eventsPageSize
428
+ } else {
429
+ effectiveLimit = * limit
430
+ }
431
+
432
+ var effectiveStart uint64
433
+ if start == nil {
434
+ effectiveStart = 0
435
+ } else {
436
+ effectiveStart = * start
437
+ }
438
+
439
+ if effectiveLimit <= eventsPageSize {
440
+ params := url.Values {}
441
+ params .Set ("start" , strconv .FormatUint (effectiveStart , 10 ))
442
+ params .Set ("limit" , strconv .FormatUint (effectiveLimit , 10 ))
443
+
444
+ requestUrl := * baseUrl
445
+ requestUrl .RawQuery = params .Encode ()
446
+
447
+ data , err = Get [[]* api.Event ](rc , requestUrl .String ())
448
+ if err != nil {
449
+ return nil , fmt .Errorf ("get events api err: %w" , err )
450
+ }
451
+ return data , nil
452
+ }
453
+
454
+ pages := (effectiveLimit + eventsPageSize - 1 ) / eventsPageSize
455
+ channels := make ([]chan ConcResponse [[]* api.Event ], pages )
456
+
457
+ for i := uint64 (0 ); i < pages ; i ++ {
458
+ channels [i ] = make (chan ConcResponse [[]* api.Event ], 1 )
459
+ pageStart := effectiveStart + (i * eventsPageSize )
460
+ pageLimit := min (eventsPageSize , effectiveLimit - (i * eventsPageSize ))
461
+
462
+ go fetch (func () ([]* api.Event , error ) {
463
+ params := url.Values {}
464
+ params .Set ("start" , strconv .FormatUint (pageStart , 10 ))
465
+ params .Set ("limit" , strconv .FormatUint (pageLimit , 10 ))
466
+
467
+ requestUrl := * baseUrl
468
+ requestUrl .RawQuery = params .Encode ()
469
+
470
+ events , err := Get [[]* api.Event ](rc , requestUrl .String ())
471
+ if err != nil {
472
+ return nil , fmt .Errorf ("get events api err: %w" , err )
473
+ }
474
+ return events , nil
475
+ }, channels [i ])
476
+ }
477
+
478
+ events := make ([]* api.Event , 0 , effectiveLimit )
479
+ for i , ch := range channels {
480
+ response := <- ch
481
+ if response .Err != nil {
482
+ return nil , response .Err
483
+ }
484
+ events = append (events , response .Result ... )
485
+ close (channels [i ])
486
+ }
487
+
488
+ sort .Slice (events , func (i , j int ) bool {
489
+ return events [i ].SequenceNumber < events [j ].SequenceNumber
490
+ })
491
+
492
+ return events , nil
493
+ }
494
+
410
495
// handleTransactions is a helper function for fetching transactions
411
496
//
412
497
// It will fetch the transactions from the node in a single request if possible, otherwise it will fetch them concurrently.
0 commit comments