Skip to content

Commit

Permalink
Merge pull request #523 from paraswap/feat/BACK-1274
Browse files Browse the repository at this point in the history
BACK-1274: Cache invalidation on pool creation (bug with local state)
  • Loading branch information
JackieJoo authored Oct 10, 2023
2 parents 3a41515 + ebc55da commit 2a43853
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 33 deletions.
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": "2.37.4",
"version": "2.37.5",
"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/algebra/algebra-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export class AlgebraFactory extends StatefulEventSubscriber<FactoryState> {
}

async handleNewPool(event: LogDescription) {
const token0 = event.args.token0;
const token1 = event.args.token1;
const token0 = event.args.token0.toLowerCase();
const token1 = event.args.token1.toLowerCase();

await this.onPoolCreated({ token0, token1 });
}
Expand Down
2 changes: 1 addition & 1 deletion src/dex/algebra/algebra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class Algebra extends SimpleExchange implements IDex<AlgebraData> {
}) => {
const logPrefix = '[Algebra.onPoolCreatedDeleteFromNonExistingSet]';
const [_token0, _token1] = this._sortTokens(token0, token1);
const poolKey = `${token0}_${token1}`.toLowerCase();
const poolKey = `${_token0}_${_token1}`;

// consider doing it only from master pool for less calls to distant cache

Expand Down
4 changes: 2 additions & 2 deletions src/dex/pancakeswap-v3/pancakeswap-v3-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export class PancakeswapV3Factory extends StatefulEventSubscriber<FactoryState>
}

async handleNewPool(event: LogDescription) {
const token0 = event.args.token0;
const token1 = event.args.token1;
const token0 = event.args.token0.toLowerCase();
const token1 = event.args.token1.toLowerCase();
const fee = event.args.fee;

await this.onPoolCreated({ token0, token1, fee });
Expand Down
2 changes: 1 addition & 1 deletion src/dex/pancakeswap-v3/pancakeswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class PancakeswapV3
}) => {
const logPrefix = '[PancakeV3.onPoolCreatedDeleteFromNonExistingSet]';
const [_token0, _token1] = this._sortTokens(token0, token1);
const poolKey = `${token0}_${token1}_${fee}`.toLowerCase();
const poolKey = `${_token0}_${_token1}_${fee}`;

// consider doing it only from master pool for less calls to distant cache

Expand Down
21 changes: 13 additions & 8 deletions src/dex/uniswap-v3/forks/ramses-v2/ramses-v2-pool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { UniswapV3EventPool } from '../../uniswap-v3-pool';
import { DecodedStateMultiCallResultWithRelativeBitmaps, DecodeStateMultiCallFunc, PoolState } from '../../types';
import {
DecodedStateMultiCallResultWithRelativeBitmaps,
DecodeStateMultiCallFunc,
PoolState,
} from '../../types';
import { assert } from 'ts-essentials';
import { _reduceTickBitmap, _reduceTicks } from '../../contract-math/utils';
import { bigIntify } from '../../../../utils';
Expand All @@ -12,7 +16,6 @@ import { Contract } from 'web3-eth-contract';
import { Address, Logger } from '../../../../types';

export class RamsesV2EventPool extends UniswapV3EventPool {

public readonly poolIface = new Interface(RamsesV2PoolABI);

constructor(
Expand Down Expand Up @@ -49,10 +52,7 @@ export class RamsesV2EventPool extends UniswapV3EventPool {
this.handlers['FeeAdjustment'] = this.handleFeeAdjustmentEvent.bind(this);
}

handleFeeAdjustmentEvent(
event: any,
pool: PoolState,
): PoolState {
handleFeeAdjustmentEvent(event: any, pool: PoolState): PoolState {
const newFee = bigIntify(event.args.newFee);

pool.fee = newFee;
Expand All @@ -77,7 +77,7 @@ export class RamsesV2EventPool extends UniswapV3EventPool {
const [resBalance0, resBalance1, resState, resCurrentFee] =
await this.dexHelper.multiWrapper.tryAggregate<
bigint | DecodedStateMultiCallResultWithRelativeBitmaps
>(
>(
false,
calldataWithFee,
blockNumber,
Expand All @@ -92,7 +92,12 @@ export class RamsesV2EventPool extends UniswapV3EventPool {
resBalance1.returnData,
resState.returnData,
resCurrentFee.returnData,
] as [bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmaps, bigint];
] as [
bigint,
bigint,
DecodedStateMultiCallResultWithRelativeBitmaps,
bigint,
];

const tickBitmap = {};
const ticks = {};
Expand Down
4 changes: 2 additions & 2 deletions src/dex/uniswap-v3/uniswap-v3-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export class UniswapV3Factory extends StatefulEventSubscriber<FactoryState> {
}

async handleNewPool(event: LogDescription) {
const token0 = event.args.token0;
const token1 = event.args.token1;
const token0 = event.args.token0.toLowerCase();
const token1 = event.args.token1.toLowerCase();
const fee = event.args.fee;

await this.onPoolCreated({ token0, token1, fee });
Expand Down
45 changes: 29 additions & 16 deletions src/dex/uniswap-v3/uniswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import {
} from '../../types';
import { CACHE_PREFIX, Network, SwapSide } from '../../constants';
import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
import { getBigIntPow, getDexKeysWithNetwork, interpolate, isTruthy, uuidToBytes16, } from '../../utils';
import {
getBigIntPow,
getDexKeysWithNetwork,
interpolate,
isTruthy,
uuidToBytes16,
} from '../../utils';
import { IDex } from '../../dex/idex';
import { IDexHelper } from '../../dex-helper/idex-helper';
import {
Expand All @@ -29,7 +35,10 @@ import {
UniswapV3Param,
UniswapV3SimpleSwapParams,
} from './types';
import { getLocalDeadlineAsFriendlyPlaceholder, SimpleExchange, } from '../simple-exchange';
import {
getLocalDeadlineAsFriendlyPlaceholder,
SimpleExchange,
} from '../simple-exchange';
import { Adapters, PoolsToPreload, UniswapV3Config } from './config';
import { UniswapV3EventPool } from './uniswap-v3-pool';
import UniswapV3RouterABI from '../../abi/uniswap-v3/UniswapV3Router.abi.json';
Expand All @@ -49,7 +58,11 @@ import { uniswapV3Math } from './contract-math/uniswap-v3-math';
import { Contract } from 'web3-eth-contract';
import { AbiItem } from 'web3-utils';
import { BalanceRequest, getBalances } from '../../lib/tokens/balancer-fetcher';
import { AssetType, DEFAULT_ID_ERC20, DEFAULT_ID_ERC20_AS_STRING, } from '../../lib/tokens/types';
import {
AssetType,
DEFAULT_ID_ERC20,
DEFAULT_ID_ERC20_AS_STRING,
} from '../../lib/tokens/types';
import { OptimalSwapExchange } from '@paraswap/core';
import { OnPoolCreatedCallback, UniswapV3Factory } from './uniswap-v3-factory';

Expand Down Expand Up @@ -196,7 +209,7 @@ export class UniswapV3
}) => {
const logPrefix = '[UniswapV3.onPoolCreatedDeleteFromNonExistingSet]';
const [_token0, _token1] = this._sortTokens(token0, token1);
const poolKey = `${token0}_${token1}_${fee}`.toLowerCase();
const poolKey = `${_token0}_${_token1}_${fee}`;

// consider doing it only from master pool for less calls to distant cache

Expand Down Expand Up @@ -281,18 +294,18 @@ export class UniswapV3
pool =
pool ||
new poolImplementation(
this.dexHelper,
this.dexKey,
this.stateMultiContract,
this.config.decodeStateMultiCallResultWithRelativeBitmaps,
this.erc20Interface,
this.config.factory,
fee,
token0,
token1,
this.logger,
this.cacheStateKey,
this.config.initHash,
this.dexHelper,
this.dexKey,
this.stateMultiContract,
this.config.decodeStateMultiCallResultWithRelativeBitmaps,
this.erc20Interface,
this.config.factory,
fee,
token0,
token1,
this.logger,
this.cacheStateKey,
this.config.initHash,
);

try {
Expand Down

0 comments on commit 2a43853

Please sign in to comment.