Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into fix/camelotv3-rever…
Browse files Browse the repository at this point in the history
…t-to-rpc
  • Loading branch information
Verisana committed Aug 16, 2023
2 parents ecf95c6 + bcda72d commit 9913c35
Show file tree
Hide file tree
Showing 29 changed files with 1,014 additions and 112 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.28.0",
"version": "2.28.8",
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": "https://github.com/paraswap/paraswap-dex-lib",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
adapterAddresses: {
AvalancheAdapter01: '0x745Ec73855CeC7249E5fF4c9DD81cc65b4D297a9',
AvalancheAdapter02: '0xDCf4EE5B700e2a5Fec458e06B763A4a3E3004494',
AvalancheBuyAdapter: '0xeBF40A40CA3D4310Bf53048F48e860656e1D7C81',
AvalancheBuyAdapter: '0x7ebbDBB57d2ab59079423cf8337cf8805e225bB1',
},
uniswapV2ExchangeRouterAddress:
'0x53e693c6C7FFC4446c53B205Cf513105Bf140D7b',
Expand Down
20 changes: 20 additions & 0 deletions src/dex/algebra/algebra-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,26 @@ describe('Algebra', () => {
nativeTokenAmount,
);
});

describe('Optimism', () => {
const network = Network.OPTIMISM;
const tokenASymbol: string = 'USDC';
const tokenBSymbol: string = 'USDT';

const tokenAAmount: string = '100000000';
const tokenBAmount: string = '50000';
const nativeTokenAmount = '100000000000000';

testForNetwork(
network,
dexKey,
tokenASymbol,
tokenBSymbol,
tokenAAmount,
tokenBAmount,
nativeTokenAmount,
)
});
});

describe('CamelotV3', () => {
Expand Down
80 changes: 73 additions & 7 deletions src/dex/algebra/algebra-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,101 @@ import { AlgebraConfig } from './config';
import { Network } from '../../constants';
import { DummyDexHelper } from '../../dex-helper/index';
import { testEventSubscriber } from '../../../tests/utils-events';
import { PoolState } from './types';
import { PoolStateV1_1, PoolState_v1_9 } from './types';
import { Interface } from '@ethersproject/abi';
import ERC20ABI from '../../abi/erc20.json';
import StateMulticallABI from '../../abi/algebra/AlgebraStateMulticall.abi.json';
import { AbiItem } from 'web3-utils';
import { AlgebraEventPool } from './algebra-pool';
import { AlgebraEventPoolV1_1 } from './algebra-pool-v1_1';
import { AlgebraEventPoolV1_9 } from './algebra-pool-v1_9';

jest.setTimeout(300 * 1000);
const dexKey = 'QuickSwapV3';
const network = Network.POLYGON;
const config = AlgebraConfig[dexKey][network];

async function fetchPoolStateFromContract(
algebraPool: AlgebraEventPool,
algebraPool: AlgebraEventPoolV1_1 | AlgebraEventPoolV1_9,
blockNumber: number,
poolAddress: string,
): Promise<PoolState> {
): Promise<PoolStateV1_1 | PoolState_v1_9> {
const message = `Algebra: ${poolAddress} blockNumber ${blockNumber}`;
console.log(`Fetching state ${message}`);
// Be careful to not request state prior to contract deployment
// Otherwise need to use manual state sourcing from multicall
// We had that mechanism, but removed it with this commit
// You can restore it, but better just to find block after state multicall
// deployment
const state = algebraPool.generateState(blockNumber);
const state = await algebraPool.generateState(blockNumber);
console.log(`Done ${message}`);
return state;
}

// To make this test to pass, you need to increase till 1500: TICK_BITMAP_BUFFER=1500
describe('CamelotV3 Event Edge Case', function () {
const poolAddress = '0xaB72b23F347d41e8E993176ecb7CF3b842FBAC8C';
const token0 = '0x6bb7a17acc227fd1f6781d1eedeae01b42047ee0';
const token1 = '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8';
const dexKey = 'CamelotV3';
const network = Network.ARBITRUM;
const config = AlgebraConfig[dexKey][network];

const blockNumbers: { [eventName: string]: number[] } = {
// topic0 - 0x0c396cd989a39f4459b5fa1aed6a9a8dcdbc45908acfd67e028cd568da98982c
['Burn']: [119409575],
// topic0 - 0xbdbdb71d7860376ba52b25a5028beea23581364a40522f6bcfb86bb1f2dca633
['Collect']: [119409575],
};

describe('AlgebraEventPool', function () {
Object.keys(blockNumbers).forEach((event: string) => {
blockNumbers[event].forEach((blockNumber: number) => {
it(`${event}:${blockNumber} - should return correct state`, async function () {
const dexHelper = new DummyDexHelper(network);
// await dexHelper.init();

const logger = dexHelper.getLogger(dexKey);

const algebraPool = new AlgebraEventPoolV1_9(
dexHelper,
dexKey,
new dexHelper.web3Provider.eth.Contract(
StateMulticallABI as AbiItem[],
config.algebraStateMulticall,
),
new Interface(ERC20ABI),
config.factory,
token0,
token1,
logger,
undefined,
config.initHash,
config.deployer,
);

// It is done in generateState. But here have to make it manually
algebraPool.poolAddress = poolAddress.toLowerCase();
algebraPool.addressesSubscribed[0] = poolAddress;

await testEventSubscriber(
algebraPool as any,
algebraPool.addressesSubscribed,
(_blockNumber: number) =>
fetchPoolStateFromContract(
algebraPool,
_blockNumber,
poolAddress,
),
blockNumber,
`${dexKey}_${poolAddress}`,
dexHelper.provider,
);
});
});
});
});
});

describe('Algebra Event', function () {
const poolAddress = '0x5b41eedcfc8e0ae47493d4945aa1ae4fe05430ff';
const token0 = '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270';
Expand Down Expand Up @@ -79,7 +145,7 @@ describe('Algebra Event', function () {

const logger = dexHelper.getLogger(dexKey);

const algebraPool = new AlgebraEventPool(
const algebraPool = new AlgebraEventPoolV1_1(
dexHelper,
dexKey,
new dexHelper.web3Provider.eth.Contract(
Expand All @@ -101,7 +167,7 @@ describe('Algebra Event', function () {
algebraPool.addressesSubscribed[0] = poolAddress;

await testEventSubscriber(
algebraPool,
algebraPool as any,
algebraPool.addressesSubscribed,
(_blockNumber: number) =>
fetchPoolStateFromContract(
Expand Down
5 changes: 3 additions & 2 deletions src/dex/algebra/algebra-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ describe('Algebra', function () {

const tokens = Tokens[network];

const srcTokenSymbol = 'USDC';
const destTokenSymbol = 'WMATIC';
const srcTokenSymbol = 'WMATIC';
const destTokenSymbol = 'DAI';
// const destTokenSymbol = 'USDC';

const amountsForSell = [
0n,
Expand Down
5 changes: 3 additions & 2 deletions src/dex/algebra/algebra-pool-v1_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import { uint256ToBigInt } from '../../lib/decoders';
import { MultiCallParams } from '../../lib/multi-wrapper';
import { decodeStateMultiCallResultWithRelativeBitmapsV1_1 } from './utils';
import { AlgebraMath } from './lib/AlgebraMath';
import { TickBitMap } from '../uniswap-v3/contract-math/TickBitMap';
import {
_reduceTickBitmap,
_reduceTicks,
} from '../uniswap-v3/contract-math/utils';
import { Constants } from './lib/Constants';
import { Network } from '../../constants';
import { TickTable } from './lib/TickTable';

export class AlgebraEventPoolV1_1 extends StatefulEventSubscriber<PoolStateV1_1> {
handlers: {
Expand Down Expand Up @@ -364,7 +364,7 @@ export class AlgebraEventPoolV1_1 extends StatefulEventSubscriber<PoolStateV1_1>
tick: bigIntify(_state.globalState.tick),
};
const currentTick = globalState.tick;
const startTickBitmap = TickBitMap.position(
const startTickBitmap = TickTable.position(
BigInt(currentTick) / Constants.TICK_SPACING,
)[0];

Expand All @@ -381,6 +381,7 @@ export class AlgebraEventPoolV1_1 extends StatefulEventSubscriber<PoolStateV1_1>
isValid: true,
balance0,
balance1,
areTicksCompressed: true,
};
}

Expand Down
11 changes: 4 additions & 7 deletions src/dex/algebra/algebra-pool-v1_9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import { uint256ToBigInt } from '../../lib/decoders';
import { MultiCallParams } from '../../lib/multi-wrapper';
import { decodeStateMultiCallResultWithRelativeBitmapsV1_9 } from './utils';
import { AlgebraMath } from './lib/AlgebraMath';
import { TickBitMap } from '../uniswap-v3/contract-math/TickBitMap';
import {
_reduceTickBitmap,
_reduceTicks,
} from '../uniswap-v3/contract-math/utils';
import { Constants } from './lib/Constants';
import { Network } from '../../constants';
import { TickTable } from './lib/TickTable';

export class AlgebraEventPoolV1_9 extends StatefulEventSubscriber<PoolState_v1_9> {
handlers: {
Expand Down Expand Up @@ -293,9 +293,7 @@ export class AlgebraEventPoolV1_9 extends StatefulEventSubscriber<PoolState_v1_9
const {
globalState: { tick },
} = _stateWithoutTicksAndTickBitmap;
const currentBitmapIndex = int16(
(BigInt(tick) / Constants.TICK_SPACING) >> 8n,
);
const currentBitmapIndex = int16(BigInt(tick) >> 8n);

const buffer = this.getBitmapRangeToRequest();
const startBitMapIndex = currentBitmapIndex - buffer;
Expand Down Expand Up @@ -366,9 +364,7 @@ export class AlgebraEventPoolV1_9 extends StatefulEventSubscriber<PoolState_v1_9
tick: bigIntify(_state.globalState.tick),
};
const currentTick = globalState.tick;
const startTickBitmap = TickBitMap.position(
BigInt(currentTick) / Constants.TICK_SPACING,
)[0];
const startTickBitmap = TickTable.position(BigInt(currentTick))[0];

return {
pool: _state.pool,
Expand All @@ -383,6 +379,7 @@ export class AlgebraEventPoolV1_9 extends StatefulEventSubscriber<PoolState_v1_9
isValid: true,
balance0,
balance1,
areTicksCompressed: false,
};
}

Expand Down
8 changes: 6 additions & 2 deletions src/dex/algebra/algebra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
import { getBigIntPow, getDexKeysWithNetwork, interpolate } from '../../utils';
import { IDex } from '../../dex/idex';
import { IDexHelper } from '../../dex-helper/idex-helper';
import { AlgebraData, DexParams, PoolStateV1_1, PoolState_v1_9 } from './types';
import { AlgebraData, DexParams, IAlgebraPoolState } from './types';
import {
SimpleExchange,
getLocalDeadlineAsFriendlyPlaceholder,
Expand Down Expand Up @@ -57,7 +57,6 @@ const MAX_STALE_STATE_BLOCK_AGE = {
};

type IAlgebraEventPool = AlgebraEventPoolV1_1 | AlgebraEventPoolV1_9;
type IAlgebraPoolState = PoolStateV1_1 | PoolState_v1_9;

export class Algebra extends SimpleExchange implements IDex<AlgebraData> {
readonly isFeeOnTransferSupported: boolean = false;
Expand Down Expand Up @@ -507,6 +506,11 @@ export class Algebra extends SimpleExchange implements IDex<AlgebraData> {
const zeroForOne = token0 === _srcAddress ? true : false;

if (state.liquidity <= 0n) {
if (state.liquidity < 0) {
this.logger.error(
`${this.dexKey}-${this.network}: ${pool.poolAddress} pool has negative liquidity: ${state.liquidity}. Find with key: ${pool.mapKey}`,
);
}
this.logger.trace(`pool have 0 liquidity`);
return null;
}
Expand Down
21 changes: 20 additions & 1 deletion src/dex/algebra/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ export const AlgebraConfig: DexConfigMap<DexParams> = {
deployer: '0x24e85f5f94c6017d2d87b434394e87df4e4d56e3',
version: 'v1.1',
},
[Network.OPTIMISM]: {
factory: '0x0C8f7b0cb986b31c67D994fb5c224592A03A4AfD',
router: '0xEDB4E3E3bB11255fF14C2762C6A6A28F1D3A36f2',
quoter: '0xf4211E7709D2294Cd10799E41623006dFB0D66aF',
initHash:
'0xbce37a54eab2fcd71913a0d40723e04238970e7fc1159bfd58ad5b79531697e7',
chunksCount: 10,
initRetryFrequency: 10,
algebraStateMulticall: '0x30F6B9b6485ff0B67E881f5ac80D3F1c70A4B23d',
subgraphURL:
'https://api.thegraph.com/subgraphs/name/iliaazhel/zyberswap-info-optimism-pp',
uniswapMulticall: '0x1F98415757620B543A52E61c46B32eB19261F984',
deployer: '0xc0d4323426c709e8d04b5b130e7f059523464a91',
version: 'v1.1',
},
},
CamelotV3: {
[Network.ARBITRUM]: {
Expand All @@ -61,7 +76,7 @@ export const AlgebraConfig: DexConfigMap<DexParams> = {
'0x6c1bebd370ba84753516bc1393c0d0a6c645856da55f5393ac8ab3d6dbc861d3',
chunksCount: 10,
initRetryFrequency: 10,
algebraStateMulticall: '0x541FeaEcB21a4cb0fBFCF90C5bae47BaDF747edE',
algebraStateMulticall: '0x2cB568442a102dF518b3D37CBD0d2884523C940B',
subgraphURL:
'https://api.thegraph.com/subgraphs/name/camelotlabs/camelot-amm-v3',
uniswapMulticall: '0x1F98415757620B543A52E61c46B32eB19261F984',
Expand All @@ -85,4 +100,8 @@ export const Adapters: Record<number, AdapterMappings> = {
[SwapSide.SELL]: [{ name: 'ArbitrumAdapter01', index: 3 }],
[SwapSide.BUY]: [{ name: 'ArbitrumBuyAdapter', index: 2 }],
},
[Network.OPTIMISM]: {
[SwapSide.SELL]: [{ name: 'OptimismAdapter01', index: 3 }],
[SwapSide.BUY]: [{ name: 'OptimismBuyAdapter', index: 2 }],
},
};
Loading

0 comments on commit 9913c35

Please sign in to comment.