@@ -705,10 +705,10 @@ export class ProtocolContracts<
705
705
* @param {(logs: GetFilterLogsReturnType<TAbi>) => void } onLogs Callback to execute when logs are received.
706
706
* @param {bigint } [fromBlock] The block number from which to start listening for events.
707
707
* @param {number } [pollInterval=1000] The interval in milliseconds at which to poll for new events.
708
- * @returns {Promise< () => void> } A promise that resolves to an unsubscribe function.
708
+ * @returns {() => void } A promise that resolves to an unsubscribe function.
709
709
* @private
710
710
*/
711
- private async subscribeToEvents <
711
+ private subscribeToEvents <
712
712
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
713
713
const TAbi extends Abi | readonly unknown [ ] = Abi ,
714
714
TEventName extends string | undefined = undefined ,
@@ -717,38 +717,47 @@ export class ProtocolContracts<
717
717
address : Address ,
718
718
eventName : InferEventName < TAbi , TEventName > ,
719
719
// eslint-disable-next-line @typescript-eslint/no-explicit-any
720
- onLogs : ( logs : any ) => void ,
721
- fromBlock ? : bigint ,
720
+ onLogs : ( logs : any , maxBlockNumber : bigint ) => void ,
721
+ fromBlock : bigint = 0n ,
722
722
pollInterval : number = 1000 ,
723
- ) : Promise < ( ) => void > {
724
- let blockNumber = await this . publicClient . getBlockNumber ( ) ;
725
- let isUnsubscribed = false ;
723
+ ) : ( ) => void {
726
724
let timeoutId : NodeJS . Timeout ;
727
-
728
- // Adjust starting block number if fromBlock is provided and valid
729
- if ( fromBlock && fromBlock < blockNumber ) {
730
- blockNumber = fromBlock ;
731
- }
725
+ let isUnsubscribed = false ;
732
726
733
727
// Function to fetch and process logs
734
728
const getLogs = async ( ) => {
735
729
if ( isUnsubscribed ) return ;
736
730
731
+ const blockNumber = await this . publicClient . getBlockNumber ( ) ;
732
+
733
+ // Adjust starting block number if fromBlock is provided
734
+ if ( fromBlock > blockNumber ) {
735
+ fromBlock = blockNumber ;
736
+ }
737
+
737
738
const filter = await this . publicClient . createContractEventFilter ( {
738
739
abi,
739
740
address,
740
- fromBlock : blockNumber ,
741
+ fromBlock,
741
742
strict : true ,
742
743
eventName : eventName ,
743
744
} as unknown as FilterOptions ) ;
744
745
745
746
const logs = await this . publicClient . getFilterLogs ( { filter } ) ;
746
747
747
748
if ( logs . length > 0 ) {
748
- onLogs ( logs ) ;
749
- // Update the block number to the next after the last log's block
750
- const bn = logs [ logs . length - 1 ] . blockNumber ;
751
- blockNumber = ( bn !== null ? bn : BigInt ( 0 ) ) + BigInt ( 1 ) ;
749
+ const maxBlockNumber = logs . reduce (
750
+ ( max , log ) =>
751
+ log . blockNumber !== null && log . blockNumber > max
752
+ ? log . blockNumber
753
+ : max ,
754
+ 0n ,
755
+ ) ;
756
+
757
+ if ( maxBlockNumber > fromBlock ) {
758
+ fromBlock = maxBlockNumber + 1n ;
759
+ onLogs ( logs , maxBlockNumber ) ;
760
+ }
752
761
}
753
762
754
763
if ( ! isUnsubscribed ) {
@@ -776,14 +785,14 @@ export class ProtocolContracts<
776
785
* @param {(logs: GetFilterLogsReturnType<typeof marketABI>) => void } onLogs Callback for when logs are received.
777
786
* @param {bigint } [fromBlock] Starting block number for listening for events.
778
787
* @param {number } [pollInterval=1000] Polling interval in milliseconds.
779
- * @returns {Promise< () => void> } Unsubscribe function.
788
+ * @returns {() => void } Unsubscribe function.
780
789
*/
781
- async subscribeMarket < TEventName extends string | undefined = undefined > (
790
+ subscribeMarket < TEventName extends string | undefined = undefined > (
782
791
eventName : InferEventName < typeof marketABI , TEventName > ,
783
792
onLogs : ( logs : GetFilterLogsReturnType < typeof marketABI > ) => void ,
784
793
fromBlock ?: bigint ,
785
794
pollInterval : number = 1000 ,
786
- ) : Promise < ( ) => void > {
795
+ ) : ( ) => void {
787
796
return this . subscribeToEvents (
788
797
marketABI ,
789
798
this . contracts [ 'market' ] . address ,
@@ -802,14 +811,14 @@ export class ProtocolContracts<
802
811
* @param {(logs: GetFilterLogsReturnType<typeof entitiesRegistryABI>) => void } onLogs Callback for when logs are received.
803
812
* @param {bigint } [fromBlock] Starting block number for listening for events.
804
813
* @param {number } [pollInterval=1000] Polling interval in milliseconds.
805
- * @returns {Promise< () => void> } Unsubscribe function.
814
+ * @returns {() => void } Unsubscribe function.
806
815
*/
807
- async subscribeEntities < TEventName extends string | undefined = undefined > (
816
+ subscribeEntities < TEventName extends string | undefined = undefined > (
808
817
eventName : InferEventName < typeof entitiesRegistryABI , TEventName > ,
809
818
onLogs : ( logs : GetFilterLogsReturnType < typeof entitiesRegistryABI > ) => void ,
810
819
fromBlock ?: bigint ,
811
820
pollInterval : number = 1000 ,
812
- ) : Promise < ( ) => void > {
821
+ ) : ( ) => void {
813
822
return this . subscribeToEvents (
814
823
entitiesRegistryABI ,
815
824
this . contracts [ 'entities' ] . address ,
@@ -828,14 +837,14 @@ export class ProtocolContracts<
828
837
* @param {(logs: GetFilterLogsReturnType<typeof configABI>) => void } onLogs Callback for when logs are received.
829
838
* @param {bigint } [fromBlock] Starting block number for listening for events.
830
839
* @param {number } [pollInterval=1000] Polling interval in milliseconds.
831
- * @returns {Promise< () => void> } Unsubscribe function.
840
+ * @returns {() => void } Unsubscribe function.
832
841
*/
833
- async subscribeConfig < TEventName extends string | undefined = undefined > (
842
+ subscribeConfig < TEventName extends string | undefined = undefined > (
834
843
eventName : InferEventName < typeof configABI , TEventName > ,
835
844
onLogs : ( logs : GetFilterLogsReturnType < typeof configABI > ) => void ,
836
845
fromBlock ?: bigint ,
837
846
pollInterval : number = 1000 ,
838
- ) : Promise < ( ) => void > {
847
+ ) : ( ) => void {
839
848
return this . subscribeToEvents (
840
849
configABI ,
841
850
this . contracts [ 'config' ] . address ,
0 commit comments