Skip to content

Commit

Permalink
solve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
aburkut committed Dec 9, 2024
2 parents 15f9767 + 27b3526 commit fcdb964
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/dex/pancakeswap-v3/pancakeswap-v3-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,36 @@ export class PancakeSwapV3EventPool extends StatefulEventSubscriber<PoolState> {
return TICK_BITMAP_TO_USE + TICK_BITMAP_BUFFER;
}

async getOrGenerateState(
blockNumber: number,
): Promise<DeepReadonly<PoolState> | null> {
const state = this.getState(blockNumber);
if (state) {
return state;
}

this.logger.error(
`PancakeV3: No state found for ${this.name} ${this.addressesSubscribed[0]}, generating new one`,
);
try {
const newState = await this.generateState(blockNumber);

if (!newState) {
this.logger.error(
`PancakeV3: Could not generate state for ${this.name} ${this.addressesSubscribed[0]}`,
);
return null;
}
this.setState(newState, blockNumber);
return newState;
} catch (error) {
this.logger.error(
`PancakeV3: Failed to generate state for ${this.name} ${this.addressesSubscribed[0]}`,
);
return null;
}
}

async generateState(blockNumber: number): Promise<Readonly<PoolState>> {
const callData = this._getStateRequestCallData();

Expand Down
10 changes: 10 additions & 0 deletions src/dex/pancakeswap-v3/pancakeswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,10 @@ export class PancakeswapV3

if (selectedPools.length === 0) return null;

await Promise.all(
selectedPools.map(pool => pool.getOrGenerateState(blockNumber)),
);

const poolsToUse = selectedPools.reduce(
(acc, pool) => {
let state = pool.getState(blockNumber);
Expand All @@ -632,6 +636,12 @@ export class PancakeswapV3
},
);

poolsToUse.poolWithoutState.forEach(pool => {
this.logger.warn(
`PancakeV3: Pool ${pool.name} on ${this.dexKey} has no state. Fallback to rpc`,
);
});

const rpcResultsPromise = this.getPricingFromRpc(
_srcToken,
_destToken,
Expand Down
50 changes: 50 additions & 0 deletions src/dex/uniswap-v3/uniswap-v3-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,36 @@ export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
return TICK_BITMAP_TO_USE + TICK_BITMAP_BUFFER;
}

async getOrGenerateState(
blockNumber: number,
): Promise<DeepReadonly<PoolState> | null> {
const state = this.getState(blockNumber);
if (state) {
return state;
}

this.logger.error(
`UniV3: No state found for ${this.name} ${this.addressesSubscribed[0]}, generating new one`,
);
try {
const newState = await this.generateState(blockNumber);

if (!newState) {
this.logger.error(
`UniV3: Could not generate state for ${this.name} ${this.addressesSubscribed[0]}`,
);
return null;
}
this.setState(newState, blockNumber);
return newState;
} catch (error) {
this.logger.error(
`UniV3: Failed to generate state for ${this.name} ${this.addressesSubscribed[0]}`,
);
return null;
}
}

async generateState(blockNumber: number): Promise<Readonly<PoolState>> {
const callData = this._getStateRequestCallData();

Expand Down Expand Up @@ -524,4 +554,24 @@ export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
this.poolInitCodeHash,
);
}

// todo: remove
// nullify the state for specific pool for testing purposes
async update(
logs: Readonly<Log>[],
blockHeaders: Readonly<{ [p: number]: Readonly<BlockHeader> }>,
) {
// USDT - WETH Mainnet UniswapV3 Pool
if (this.poolAddress === '0x11b815efb8f581194ae79006d24e0d814b7697f6') {
const blockNumbers = logs.map(log => log.blockNumber);
this.logger.info(
`Invalidating state for pool ${
this.poolAddress
} at blocks ${blockNumbers.join(', ')}`,
);
this.invalidate();
} else {
await super.update(logs, blockHeaders);
}
}
}
11 changes: 11 additions & 0 deletions src/dex/uniswap-v3/uniswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,13 +715,18 @@ export class UniswapV3

if (selectedPools.length === 0) return null;

await Promise.all(
selectedPools.map(pool => pool.getOrGenerateState(blockNumber)),
);

const poolsToUse = selectedPools.reduce(
(acc, pool) => {
let state = pool.getState(blockNumber);
if (state === null) {
this.logger.trace(
`${this.dexKey}: State === null. Fallback to rpc ${pool.name}`,
);
// as we generate state (if nullified) in previous Promise.all, here should only be pools with failed initialization
acc.poolWithoutState.push(pool);
} else {
acc.poolWithState.push(pool);
Expand All @@ -734,6 +739,12 @@ export class UniswapV3
},
);

poolsToUse.poolWithoutState.forEach(pool => {
this.logger.warn(
`UniV3: Pool ${pool.name} on ${this.dexKey} has no state. Fallback to rpc`,
);
});

const states = poolsToUse.poolWithState.map(
p => p.getState(blockNumber)!,
);
Expand Down

0 comments on commit fcdb964

Please sign in to comment.