Skip to content

Commit eac17bd

Browse files
committed
feat(FluidDex): add liquidity proxy state refresh interval
1 parent c556b9c commit eac17bd

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

src/dex/fluid-dex/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
// on-chain we use 1e4 but use extra buffer to avoid reverts
22
export const MIN_SWAP_LIQUIDITY = 8500n;
3+
4+
export const RESERVE_REFRESH_INTERVAL_MS = 30 * 1000;

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

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

18-
const NON_POOL_THROTTLE_MS = 60 * 1000; // 1 minute
19-
2018
export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiquidityProxyState> {
2119
handlers: {
2220
[event: string]: (
@@ -36,9 +34,7 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
3634

3735
resolverContract: Contract;
3836

39-
private poolAddresses: Set<string> = new Set();
40-
private lastPoolEventBlockNumber: number = 0;
41-
private lastNonPoolEventTimestamp: number = 0;
37+
shouldUpdateState = false;
4238

4339
constructor(
4440
readonly parentName: string,
@@ -63,35 +59,14 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
6359
this.handlers['LogOperate'] = this.handleOperate.bind(this);
6460
}
6561

66-
setPoolAddresses(addresses: string[]) {
67-
this.poolAddresses = new Set(addresses.map(a => a.toLowerCase()));
68-
}
69-
7062
async handleOperate(
7163
event: any,
7264
state: DeepReadonly<FluidDexLiquidityProxyState>,
7365
log: Readonly<Log>,
7466
): Promise<DeepReadonly<FluidDexLiquidityProxyState> | null> {
75-
if (log.topics.length < 2) {
76-
return null;
77-
}
78-
79-
const userAddress = ('0x' + log.topics[1].slice(26)).toLowerCase();
67+
this.shouldUpdateState = true;
8068

81-
if (this.poolAddresses.has(userAddress)) {
82-
if (log.blockNumber === this.lastPoolEventBlockNumber) {
83-
return null;
84-
}
85-
this.lastPoolEventBlockNumber = log.blockNumber;
86-
} else {
87-
const now = Date.now();
88-
if (now - this.lastNonPoolEventTimestamp < NON_POOL_THROTTLE_MS) {
89-
return null;
90-
}
91-
this.lastNonPoolEventTimestamp = now;
92-
}
93-
94-
return this.generateState(log.blockNumber);
69+
return null;
9570
}
9671

9772
async processLog(
@@ -121,15 +96,6 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
12196
return state;
12297
}
12398

124-
/**
125-
* The function generates state using on-chain calls. This
126-
* function is called to regenerate state if the event based
127-
* system fails to fetch events and the local state is no
128-
* more correct.
129-
* @param blockNumber - Blocknumber for which the state should
130-
* should be generated
131-
* @returns state of the event subscriber at blocknumber
132-
*/
13399
async generateState(
134100
blockNumber: number,
135101
): Promise<DeepReadonly<FluidDexLiquidityProxyState>> {

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { BigNumber } from 'ethers';
3232
import { sqrt } from './utils';
3333
import { FluidDexLiquidityProxy } from './fluid-dex-liquidity-proxy';
3434
import { FluidDexEventPool } from './fluid-dex-pool';
35-
import { MIN_SWAP_LIQUIDITY } from './constants';
35+
import { MIN_SWAP_LIQUIDITY, RESERVE_REFRESH_INTERVAL_MS } from './constants';
3636

3737
export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
3838
readonly hasConstantPriceLargeAmounts = false;
@@ -53,6 +53,8 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
5353

5454
readonly fluidDexPoolIface: Interface;
5555

56+
private reserveUpdateIntervalTask?: NodeJS.Timeout;
57+
5658
constructor(
5759
readonly network: Network,
5860
readonly dexKey: string,
@@ -124,8 +126,28 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
124126
}),
125127
);
126128

127-
this.liquidityProxy.setPoolAddresses(this.pools.map(p => p.address));
128129
await this.liquidityProxy.initialize(blockNumber);
130+
131+
if (this.dexHelper.config.isSlave === true) {
132+
this.reserveUpdateIntervalTask = setInterval(
133+
this.updateReserves.bind(this),
134+
RESERVE_REFRESH_INTERVAL_MS,
135+
);
136+
}
137+
}
138+
139+
private async updateReserves(): Promise<void> {
140+
if (!this.liquidityProxy.shouldUpdateState) return;
141+
this.liquidityProxy.shouldUpdateState = false;
142+
143+
try {
144+
const blockNumber = await this.dexHelper.provider.getBlockNumber();
145+
146+
const state = await this.liquidityProxy.generateState(blockNumber);
147+
this.liquidityProxy.setState(state, blockNumber);
148+
} catch (error) {
149+
this.logger.error(`${this.dexKey}: Error updating reserves:`, error);
150+
}
129151
}
130152

131153
getAdapters(side: SwapSide) {
@@ -136,7 +158,6 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
136158
poolsFromFactory: readonly PoolWithDecimals[],
137159
) {
138160
this.pools = this.generateFluidDexPoolsFromPoolsFactory(poolsFromFactory);
139-
this.liquidityProxy.setPoolAddresses(this.pools.map(p => p.address));
140161
this.logger.info(`${this.dexKey}: pools list was updated ...`);
141162
}
142163

@@ -1273,4 +1294,11 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
12731294
private abs(value: bigint): bigint {
12741295
return value < 0 ? -value : value;
12751296
}
1297+
1298+
releaseResources(): void {
1299+
if (this.reserveUpdateIntervalTask) {
1300+
clearInterval(this.reserveUpdateIntervalTask);
1301+
this.reserveUpdateIntervalTask = undefined;
1302+
}
1303+
}
12761304
}

0 commit comments

Comments
 (0)