@@ -15,6 +15,8 @@ import {
1515import { Address } from '../../types' ;
1616import { Contract } from 'ethers' ;
1717
18+ const NON_POOL_THROTTLE_MS = 60 * 1000 ; // 1 minute
19+
1820export class FluidDexLiquidityProxy extends StatefulEventSubscriber < FluidDexLiquidityProxyState > {
1921 handlers : {
2022 [ event : string ] : (
@@ -34,6 +36,10 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
3436
3537 resolverContract : Contract ;
3638
39+ private poolAddresses : Set < string > = new Set ( ) ;
40+ private lastPoolEventBlockNumber : number = 0 ;
41+ private lastNonPoolEventTimestamp : number = 0 ;
42+
3743 constructor (
3844 readonly parentName : string ,
3945 readonly commonAddresses : CommonAddresses ,
@@ -57,9 +63,10 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
5763 this . handlers [ 'LogOperate' ] = this . handleOperate . bind ( this ) ;
5864 }
5965
60- /**
61- * Handle a trade rate change on the pool.
62- */
66+ setPoolAddresses ( addresses : string [ ] ) {
67+ this . poolAddresses = new Set ( addresses . map ( a => a . toLowerCase ( ) ) ) ;
68+ }
69+
6370 async handleOperate (
6471 event : any ,
6572 state : DeepReadonly < FluidDexLiquidityProxyState > ,
@@ -82,6 +89,28 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
8289 log : Readonly < Log > ,
8390 ) : Promise < DeepReadonly < FluidDexLiquidityProxyState > | null > {
8491 try {
92+ if ( log . topics . length >= 2 ) {
93+ const userAddress = ( '0x' + log . topics [ 1 ] . slice ( 26 ) ) . toLowerCase ( ) ;
94+
95+ if ( this . poolAddresses . has ( userAddress ) ) {
96+ // Check if state was already updated for this block
97+ if ( log . blockNumber === this . lastPoolEventBlockNumber ) {
98+ return null ;
99+ }
100+
101+ this . lastPoolEventBlockNumber = log . blockNumber ;
102+ } else {
103+ const now = Date . now ( ) ;
104+
105+ // Throttle non-pool events to avoid generating state too often
106+ if ( now - this . lastNonPoolEventTimestamp < NON_POOL_THROTTLE_MS ) {
107+ return null ;
108+ }
109+
110+ this . lastNonPoolEventTimestamp = now ;
111+ }
112+ }
113+
85114 const event = this . logDecoder ( log ) ;
86115 if ( event . name in this . handlers ) {
87116 return await this . handlers [ event . name ] ( event , state , log ) ;
0 commit comments