Skip to content

Commit

Permalink
solve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
aburkut committed Aug 8, 2023
2 parents d276028 + 1f7c07c commit b6176fc
Show file tree
Hide file tree
Showing 19 changed files with 1,817 additions and 232 deletions.
File renamed without changes.
756 changes: 756 additions & 0 deletions src/abi/algebra/AlgebraPool-v1_9.abi.json

Large diffs are not rendered by default.

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
24 changes: 24 additions & 0 deletions src/dex/algebra/algebra-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,28 @@ describe('Algebra', () => {
)
});
});

describe('CamelotV3', () => {
const dexKey = 'CamelotV3';

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

const tokenAAmount: string = '1000000000';
const tokenBAmount: string = '1000000000';
const nativeTokenAmount = '1000000000000000000';

testForNetwork(
network,
dexKey,
tokenASymbol,
tokenBSymbol,
tokenAAmount,
tokenBAmount,
nativeTokenAmount,
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import {
} from '../../stateful-event-subscriber';
import { IDexHelper } from '../../dex-helper/idex-helper';
import {
PoolState,
PoolStateV1_1,
TickBitMapMappingsWithBigNumber,
TickInfoMappingsWithBigNumber,
} from './types';
import { ethers } from 'ethers';
import { Contract } from 'web3-eth-contract';
import AlgebraABI from '../../abi/algebra/AlgebraPool.abi.json';
import { DecodedStateMultiCallResultWithRelativeBitmaps } from './types';
import AlgebraABI from '../../abi/algebra/AlgebraPool-v1_1.abi.json';
import { DecodedStateMultiCallResultWithRelativeBitmapsV1_1 } from './types';
import {
OUT_OF_RANGE_ERROR_POSTFIX,
TICK_BITMAP_BUFFER,
TICK_BITMAP_TO_USE,
} from '../uniswap-v3/constants';
import { uint256ToBigInt } from '../../lib/decoders';
import { MultiCallParams } from '../../lib/multi-wrapper';
import { decodeStateMultiCallResultWithRelativeBitmaps } from './utils';
import { decodeStateMultiCallResultWithRelativeBitmapsV1_1 } from './utils';
import { AlgebraMath } from './lib/AlgebraMath';
import { TickBitMap } from '../uniswap-v3/contract-math/TickBitMap';
import {
Expand All @@ -34,14 +34,14 @@ import {
import { Constants } from './lib/Constants';
import { Network } from '../../constants';

export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
export class AlgebraEventPoolV1_1 extends StatefulEventSubscriber<PoolStateV1_1> {
handlers: {
[event: string]: (
event: any,
state: DeepReadonly<PoolState>,
state: DeepReadonly<PoolStateV1_1>,
log: Readonly<Log>,
blockHeader: BlockHeader,
) => DeepReadonly<PoolState> | null;
) => DeepReadonly<PoolStateV1_1> | null;
} = {};

logDecoder: (log: Log) => any;
Expand Down Expand Up @@ -99,16 +99,16 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

async initialize(
blockNumber: number,
options?: InitializeStateOptions<PoolState>,
options?: InitializeStateOptions<PoolStateV1_1>,
) {
await super.initialize(blockNumber, options);
}

protected async processBlockLogs(
state: DeepReadonly<PoolState>,
state: DeepReadonly<PoolStateV1_1>,
logs: Readonly<Log>[],
blockHeader: Readonly<BlockHeader>,
): Promise<DeepReadonly<PoolState> | null> {
): Promise<DeepReadonly<PoolStateV1_1> | null> {
const newState = await super.processBlockLogs(state, logs, blockHeader);
if (newState && !newState.isValid) {
return await this.generateState(blockHeader.number);
Expand All @@ -117,17 +117,17 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
}

protected processLog(
state: DeepReadonly<PoolState>,
state: DeepReadonly<PoolStateV1_1>,
log: Readonly<Log>,
blockHeader: Readonly<BlockHeader>,
): DeepReadonly<PoolState> | null {
): DeepReadonly<PoolStateV1_1> | null {
try {
const event = this.logDecoder(log);
if (event.name in this.handlers) {
// Because we have observations in array which is mutable by nature, there is a
// ts compile error: https://stackoverflow.com/questions/53412934/disable-allowing-assigning-readonly-types-to-non-readonly-types
// And there is no good workaround, so turn off the type checker for this line
const _state = _.cloneDeep(state) as PoolState;
const _state = _.cloneDeep(state) as PoolStateV1_1;
try {
const newState = this.handlers[event.name](
event,
Expand Down Expand Up @@ -176,9 +176,11 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

private async _fetchPoolStateSingleStep(
blockNumber: number,
): Promise<[bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmaps]> {
): Promise<
[bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmapsV1_1]
> {
const callData: MultiCallParams<
bigint | DecodedStateMultiCallResultWithRelativeBitmaps
bigint | DecodedStateMultiCallResultWithRelativeBitmapsV1_1
>[] = [
{
target: this.token0,
Expand All @@ -205,13 +207,13 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
this.getBitmapRangeToRequest(),
)
.encodeABI(),
decodeFunction: decodeStateMultiCallResultWithRelativeBitmaps,
decodeFunction: decodeStateMultiCallResultWithRelativeBitmapsV1_1,
},
];

const [resBalance0, resBalance1, resState] =
await this.dexHelper.multiWrapper.tryAggregate<
bigint | DecodedStateMultiCallResultWithRelativeBitmaps
bigint | DecodedStateMultiCallResultWithRelativeBitmapsV1_1
>(
false,
callData,
Expand All @@ -226,16 +228,18 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
resBalance0.returnData,
resBalance1.returnData,
resState.returnData,
] as [bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmaps];
] as [bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmapsV1_1];

return [balance0, balance1, _state];
}

private async _fetchPoolStateMultiStep(
blockNumber: number,
): Promise<[bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmaps]> {
): Promise<
[bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmapsV1_1]
> {
const balancesAndGlobalStateCalldata: MultiCallParams<
bigint | DecodedStateMultiCallResultWithRelativeBitmaps
bigint | DecodedStateMultiCallResultWithRelativeBitmapsV1_1
>[] = [
{
target: this.token0,
Expand All @@ -262,13 +266,13 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
0,
)
.encodeABI(),
decodeFunction: decodeStateMultiCallResultWithRelativeBitmaps,
decodeFunction: decodeStateMultiCallResultWithRelativeBitmapsV1_1,
},
];

const [resBalance0, resBalance1, stateWithoutTicksAndTickBitmap] =
await this.dexHelper.multiWrapper.tryAggregate<
bigint | DecodedStateMultiCallResultWithRelativeBitmaps
bigint | DecodedStateMultiCallResultWithRelativeBitmapsV1_1
>(
false,
balancesAndGlobalStateCalldata,
Expand All @@ -283,7 +287,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
resBalance0.returnData,
resBalance1.returnData,
stateWithoutTicksAndTickBitmap.returnData,
] as [bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmaps];
] as [bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmapsV1_1];

const {
globalState: { tick },
Expand Down Expand Up @@ -317,7 +321,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
}),
);

const _state: DecodedStateMultiCallResultWithRelativeBitmaps = {
const _state: DecodedStateMultiCallResultWithRelativeBitmapsV1_1 = {
..._stateWithoutTicksAndTickBitmap,
tickBitmap: ticksAndBitMaps.flatMap(v => v[0]),
ticks: ticksAndBitMaps.flatMap(v => v[1]),
Expand All @@ -328,7 +332,9 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

async _fetchInitStateMultiStrategies(
blockNumber: number,
): Promise<[bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmaps]> {
): Promise<
[bigint, bigint, DecodedStateMultiCallResultWithRelativeBitmapsV1_1]
> {
try {
return await this._fetchPoolStateSingleStep(blockNumber);
} catch (e) {
Expand All @@ -341,7 +347,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
}
}

async generateState(blockNumber: number): Promise<Readonly<PoolState>> {
async generateState(blockNumber: number): Promise<Readonly<PoolStateV1_1>> {
const [balance0, balance1, _state] =
await this._fetchInitStateMultiStrategies(blockNumber);

Expand All @@ -350,7 +356,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

_reduceTickBitmap(tickBitmap, _state.tickBitmap);
_reduceTicks(ticks, _state.ticks);
const globalState: PoolState['globalState'] = {
const globalState: PoolStateV1_1['globalState'] = {
communityFeeToken0: bigIntify(_state.globalState.communityFeeToken0),
communityFeeToken1: bigIntify(_state.globalState.communityFeeToken1),
fee: bigIntify(_state.globalState.fee),
Expand Down Expand Up @@ -380,7 +386,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

handleSwapEvent(
event: any,
pool: PoolState,
pool: PoolStateV1_1,
log: Log,
blockHeader: BlockHeader,
) {
Expand Down Expand Up @@ -447,7 +453,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {
}
handleMintEvent(
event: any,
pool: PoolState,
pool: PoolStateV1_1,
log: Log,
blockHeader: BlockHeader,
) {
Expand All @@ -473,7 +479,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

handleBurnEvent(
event: any,
pool: PoolState,
pool: PoolStateV1_1,
log: Log,
blockHeader: BlockHeader,
) {
Expand All @@ -496,7 +502,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

handleNewFee(
event: any,
pool: PoolState,
pool: PoolStateV1_1,
log: Log,
blockHeader: BlockHeader,
) {
Expand All @@ -510,7 +516,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

handleCollectEvent(
event: any,
pool: PoolState,
pool: PoolStateV1_1,
log: Log,
blockHeader: BlockHeader,
) {
Expand All @@ -525,7 +531,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

handleFlashEvent(
event: any,
pool: PoolState,
pool: PoolStateV1_1,
log: Log,
blockHeader: BlockHeader,
) {
Expand All @@ -540,7 +546,7 @@ export class AlgebraEventPool extends StatefulEventSubscriber<PoolState> {

handleCommunityFee(
event: any,
pool: PoolState,
pool: PoolStateV1_1,
log: Log,
blockHeader: BlockHeader,
) {
Expand Down
Loading

0 comments on commit b6176fc

Please sign in to comment.