Skip to content

Commit 3e54dd1

Browse files
committed
fix(subscriber): reduce number of liquidity proxy state generations
1 parent 9ed1597 commit 3e54dd1

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/dex/fluid-dex/fluid-dex-events.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('FluidDex EventPool Mainnet', function () {
4040
const eventsToTest: Record<Address, EventMappings> = {
4141
'0x52aa899454998be5b000ad077a46bbe360f4e497': {
4242
LogOperate: [
43-
21190399, 21190405, 21190420, 21190452, 21190454, 21190465, 21190506,
43+
24619596, 24619595, 24619593, 24619592, 24619591, 24619590, 24619589,
4444
],
4545
},
4646
};
@@ -86,7 +86,7 @@ describe('FluidDex EventPool Mainnet', function () {
8686

8787
const eventsToTest: Record<Address, EventMappings> = {
8888
'0x91716C4EDA1Fb55e84Bf8b4c7085f84285c19085': {
89-
LogDexDeployed: [21199929],
89+
LogDexDeployed: [24611892],
9090
},
9191
};
9292

src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
import { Address } from '../../types';
1616
import { Contract } from 'ethers';
1717

18+
const NON_POOL_THROTTLE_MS = 60 * 1000; // 1 minute
19+
1820
export 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);

src/dex/fluid-dex/fluid-dex.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
124124
}),
125125
);
126126

127+
this.liquidityProxy.setPoolAddresses(this.pools.map(p => p.address));
127128
await this.liquidityProxy.initialize(blockNumber);
128129
}
129130

@@ -135,6 +136,7 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
135136
poolsFromFactory: readonly PoolWithDecimals[],
136137
) {
137138
this.pools = this.generateFluidDexPoolsFromPoolsFactory(poolsFromFactory);
139+
this.liquidityProxy.setPoolAddresses(this.pools.map(p => p.address));
138140
this.logger.info(`${this.dexKey}: pools list was updated ...`);
139141
}
140142

0 commit comments

Comments
 (0)