Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paraswap/dex-lib",
"version": "4.8.20",
"version": "4.8.21-fluid-dex-state-generation.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": "https://github.com/paraswap/paraswap-dex-lib",
Expand Down
4 changes: 2 additions & 2 deletions src/dex/balancer-v3/balancer-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import { removeCircularStepPairs } from './utils';

const MAX_UINT256 =
'115792089237316195423570985008687907853269984665640564039457584007913129639935';
const POOL_UPDATE_TTL = 1 * 60; // 1min
const RATE_UPDATE_TTL = 1 * 60; // 1min
const POOL_UPDATE_TTL = 2 * 60; // 2min
const RATE_UPDATE_TTL = 2 * 60; // 2min
const HOOK_UPDATE_TTL = 5 * 60; // 5min

type DeepMutable<T> = {
Expand Down
2 changes: 2 additions & 0 deletions src/dex/fluid-dex/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// on-chain we use 1e4 but use extra buffer to avoid reverts
export const MIN_SWAP_LIQUIDITY = 8500n;

export const RESERVE_REFRESH_INTERVAL_MS = 30 * 1000;
4 changes: 2 additions & 2 deletions src/dex/fluid-dex/fluid-dex-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('FluidDex EventPool Mainnet', function () {
const eventsToTest: Record<Address, EventMappings> = {
'0x52aa899454998be5b000ad077a46bbe360f4e497': {
LogOperate: [
21190399, 21190405, 21190420, 21190452, 21190454, 21190465, 21190506,
24619596, 24619595, 24619593, 24619592, 24619591, 24619590, 24619589,
],
},
};
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('FluidDex EventPool Mainnet', function () {

const eventsToTest: Record<Address, EventMappings> = {
'0x91716C4EDA1Fb55e84Bf8b4c7085f84285c19085': {
LogDexDeployed: [21199929],
LogDexDeployed: [24611892],
},
};

Expand Down
27 changes: 5 additions & 22 deletions src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu

resolverContract: Contract;

shouldUpdateState = false;

constructor(
readonly parentName: string,
readonly commonAddresses: CommonAddresses,
Expand All @@ -57,26 +59,16 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
this.handlers['LogOperate'] = this.handleOperate.bind(this);
}

/**
* Handle a trade rate change on the pool.
*/
async handleOperate(
event: any,
state: DeepReadonly<FluidDexLiquidityProxyState>,
log: Readonly<Log>,
): Promise<DeepReadonly<FluidDexLiquidityProxyState> | null> {
return this.generateState(log.blockNumber);
this.shouldUpdateState = true;

return null;
}

/**
* The function is called every time any of the subscribed
* addresses release log. The function accepts the current
* state, updates the state according to the log, and returns
* the updated state.
* @param state - Current state of event subscriber
* @param log - Log released by one of the subscribed addresses
* @returns Updates state of the event subscriber after the log
*/
async processLog(
state: DeepReadonly<FluidDexLiquidityProxyState>,
log: Readonly<Log>,
Expand Down Expand Up @@ -104,15 +96,6 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiqu
return state;
}

/**
* The function generates state using on-chain calls. This
* function is called to regenerate state if the event based
* system fails to fetch events and the local state is no
* more correct.
* @param blockNumber - Blocknumber for which the state should
* should be generated
* @returns state of the event subscriber at blocknumber
*/
async generateState(
blockNumber: number,
): Promise<DeepReadonly<FluidDexLiquidityProxyState>> {
Expand Down
32 changes: 31 additions & 1 deletion src/dex/fluid-dex/fluid-dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { BigNumber } from 'ethers';
import { sqrt } from './utils';
import { FluidDexLiquidityProxy } from './fluid-dex-liquidity-proxy';
import { FluidDexEventPool } from './fluid-dex-pool';
import { MIN_SWAP_LIQUIDITY } from './constants';
import { MIN_SWAP_LIQUIDITY, RESERVE_REFRESH_INTERVAL_MS } from './constants';

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

readonly fluidDexPoolIface: Interface;

private reserveUpdateIntervalTask?: NodeJS.Timeout;

constructor(
readonly network: Network,
readonly dexKey: string,
Expand Down Expand Up @@ -125,6 +127,27 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
);

await this.liquidityProxy.initialize(blockNumber);

if (this.dexHelper.config.isSlave === true) {
this.reserveUpdateIntervalTask = setInterval(
this.updateReserves.bind(this),
RESERVE_REFRESH_INTERVAL_MS,
);
}
}

private async updateReserves(): Promise<void> {
if (!this.liquidityProxy.shouldUpdateState) return;
this.liquidityProxy.shouldUpdateState = false;

try {
const blockNumber = await this.dexHelper.provider.getBlockNumber();

const state = await this.liquidityProxy.generateState(blockNumber);
this.liquidityProxy.setState(state, blockNumber);
} catch (error) {
this.logger.error(`${this.dexKey}: Error updating reserves:`, error);
}
}

getAdapters(side: SwapSide) {
Expand Down Expand Up @@ -1271,4 +1294,11 @@ export class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
private abs(value: bigint): bigint {
return value < 0 ? -value : value;
}

releaseResources(): void {
if (this.reserveUpdateIntervalTask) {
clearInterval(this.reserveUpdateIntervalTask);
this.reserveUpdateIntervalTask = undefined;
}
}
}