Skip to content

Commit

Permalink
Handle increase liquidity event (#143)
Browse files Browse the repository at this point in the history
* Implement handler for NFPM IncreaseLiquidity event.

* Get contract values by querying uni-watcher.

Co-authored-by: nabarun <[email protected]>
  • Loading branch information
ashwinphatak and nikugogoi authored Jul 16, 2021
1 parent ae13edb commit 3477366
Show file tree
Hide file tree
Showing 16 changed files with 2,573 additions and 1,376 deletions.
2 changes: 1 addition & 1 deletion packages/solidity-mapper/src/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('Get value from storage', () => {
const { storageLayout } = contracts.TestAddress;
const { value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, testAddress.address, 'address1');
expect(value).to.be.a('string');
expect(String(value).toLowerCase()).to.equal(address1Value);
expect(value).to.equal(address1Value);

if (isIpldGql) {
assertProofData(blockHash, testAddress.address, JSON.parse(proofData));
Expand Down
4 changes: 4 additions & 0 deletions packages/solidity-mapper/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export const getValueByType = (storageValue: string, typeLabel: string): bigint
return utils.toUtf8String(storageValue);
}

if (typeLabel.startsWith('address')) {
return utils.getAddress(storageValue);
}

return storageValue;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/solidity-mapper/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const getStorageAt: GetStorageAt = async ({ blockHash, contract, slot })
*/
export const generateDummyAddresses = (length: number): Array<string> => {
return Array.from({ length }, () => {
return ethers.utils.hexlify(ethers.utils.randomBytes(20));
return ethers.utils.getAddress(ethers.utils.hexlify(ethers.utils.randomBytes(20)));
});
};

Expand Down
94 changes: 94 additions & 0 deletions packages/uni-info-watcher/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { TokenDayData } from './entity/TokenDayData';
import { TokenHourData } from './entity/TokenHourData';
import { Burn } from './entity/Burn';
import { Swap } from './entity/Swap';
import { Position } from './entity/Position';
import { PositionSnapshot } from './entity/PositionSnapshot';

export class Database {
_config: ConnectionOptions
Expand Down Expand Up @@ -80,6 +82,44 @@ export class Database {
return repo.findOne(findOptions);
}

async getPosition ({ id, blockNumber }: DeepPartial<Position>): Promise<Position | undefined> {
const repo = this._conn.getRepository(Position);
const whereOptions: FindConditions<Position> = { id };

if (blockNumber) {
whereOptions.blockNumber = LessThanOrEqual(blockNumber);
}

const findOptions: FindOneOptions<Position> = {
where: whereOptions,
relations: ['pool', 'token0', 'token1', 'tickLower', 'tickUpper', 'transaction'],
order: {
blockNumber: 'DESC'
}
};

return repo.findOne(findOptions);
}

async getTick ({ id, blockNumber }: DeepPartial<Tick>): Promise<Tick | undefined> {
const repo = this._conn.getRepository(Tick);
const whereOptions: FindConditions<Tick> = { id };

if (blockNumber) {
whereOptions.blockNumber = LessThanOrEqual(blockNumber);
}

const findOptions: FindOneOptions<Tick> = {
where: whereOptions,
relations: ['pool'],
order: {
blockNumber: 'DESC'
}
};

return repo.findOne(findOptions);
}

async getFactories ({ blockNumber }: DeepPartial<Factory>, queryOptions: { [key: string]: any }): Promise<Array<Factory>> {
const repo = this._conn.getRepository(Factory);

Expand Down Expand Up @@ -438,6 +478,52 @@ export class Database {
});
}

async loadPosition ({ id, blockNumber, ...values }: DeepPartial<Position>): Promise<Position> {
return this._conn.transaction(async (tx) => {
const repo = tx.getRepository(Position);

let selectQueryBuilder = repo.createQueryBuilder('position')
.where('id = :id', { id });

if (blockNumber) {
selectQueryBuilder = selectQueryBuilder.andWhere('block_number <= :blockNumber', { blockNumber });
}

let entity = await selectQueryBuilder.orderBy('block_number', 'DESC')
.getOne();

if (!entity) {
entity = repo.create({ blockNumber, id, ...values });
entity = await repo.save(entity);
}

return entity;
});
}

async loadPositionSnapshot ({ id, blockNumber, ...values }: DeepPartial<PositionSnapshot>): Promise<PositionSnapshot> {
return this._conn.transaction(async (tx) => {
const repo = tx.getRepository(PositionSnapshot);

let selectQueryBuilder = repo.createQueryBuilder('positionSnapshot')
.where('id = :id', { id });

if (blockNumber) {
selectQueryBuilder = selectQueryBuilder.andWhere('block_number <= :blockNumber', { blockNumber });
}

let entity = await selectQueryBuilder.orderBy('block_number', 'DESC')
.getOne();

if (!entity) {
entity = repo.create({ blockNumber, id, ...values });
entity = await repo.save(entity);
}

return entity;
});
}

async saveFactory (factory: Factory, blockNumber: number): Promise<Factory> {
return this._conn.transaction(async (tx) => {
const repo = tx.getRepository(Factory);
Expand Down Expand Up @@ -526,6 +612,14 @@ export class Database {
});
}

async savePosition (position: Position, blockNumber: number): Promise<Position> {
return this._conn.transaction(async (tx) => {
const repo = tx.getRepository(Position);
position.blockNumber = blockNumber;
return repo.save(position);
});
}

// Returns true if events have already been synced for the (block, token) combination.
async didSyncEvents ({ blockHash, token }: { blockHash: string, token: string }): Promise<boolean> {
const numRows = await this._conn.getRepository(EventSyncProgress)
Expand Down
66 changes: 66 additions & 0 deletions packages/uni-info-watcher/src/entity/Position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Entity, PrimaryColumn, Column, ManyToOne } from 'typeorm';
import Decimal from 'decimal.js';
import { decimalTransformer } from '@vulcanize/util';

import { Pool } from './Pool';
import { Token } from './Token';
import { Tick } from './Tick';
import { Transaction } from './Transaction';
import { ADDRESS_ZERO } from '../utils/constants';

@Entity()
export class Position {
@PrimaryColumn('varchar')
id!: string;

@PrimaryColumn('integer')
blockNumber!: number;

@Column('bigint')
feeGrowthInside0LastX128!: bigint

@Column('bigint')
feeGrowthInside1LastX128!: bigint

@Column('bigint', { default: BigInt(0) })
liquidity!: bigint

@Column('numeric', { default: 0, transformer: decimalTransformer })
depositedToken0!: Decimal

@Column('numeric', { default: 0, transformer: decimalTransformer })
depositedToken1!: Decimal

@Column('varchar', { length: 42, default: ADDRESS_ZERO })
owner!: string

@Column('numeric', { default: 0, transformer: decimalTransformer })
withdrawnToken0!: Decimal

@Column('numeric', { default: 0, transformer: decimalTransformer })
withdrawnToken1!: Decimal

@Column('numeric', { default: 0, transformer: decimalTransformer })
collectedFeesToken0!: Decimal

@Column('numeric', { default: 0, transformer: decimalTransformer })
collectedFeesToken1!: Decimal

@ManyToOne(() => Pool)
pool!: Pool

@ManyToOne(() => Token)
token0!: Token

@ManyToOne(() => Token)
token1!: Token

@ManyToOne(() => Tick)
tickLower!: Tick

@ManyToOne(() => Tick)
tickUpper!: Tick

@ManyToOne(() => Transaction)
transaction!: Transaction
}
59 changes: 59 additions & 0 deletions packages/uni-info-watcher/src/entity/PositionSnapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Entity, PrimaryColumn, Column, ManyToOne } from 'typeorm';
import Decimal from 'decimal.js';
import { decimalTransformer } from '@vulcanize/util';

import { Pool } from './Pool';
import { Transaction } from './Transaction';
import { ADDRESS_ZERO } from '../utils/constants';
import { Position } from './Position';

@Entity()
export class PositionSnapshot {
@PrimaryColumn('varchar')
id!: string;

@PrimaryColumn('integer')
blockNumber!: number;

@Column('bigint')
timestamp!: BigInt;

@Column('bigint')
feeGrowthInside0LastX128!: bigint

@Column('bigint')
feeGrowthInside1LastX128!: bigint

@Column('bigint', { default: BigInt(0) })
liquidity!: bigint

@Column('numeric', { default: 0, transformer: decimalTransformer })
depositedToken0!: Decimal

@Column('numeric', { default: 0, transformer: decimalTransformer })
depositedToken1!: Decimal

@Column('varchar', { length: 42, default: ADDRESS_ZERO })
owner!: string

@Column('numeric', { default: 0, transformer: decimalTransformer })
withdrawnToken0!: Decimal

@Column('numeric', { default: 0, transformer: decimalTransformer })
withdrawnToken1!: Decimal

@Column('numeric', { default: 0, transformer: decimalTransformer })
collectedFeesToken0!: Decimal

@Column('numeric', { default: 0, transformer: decimalTransformer })
collectedFeesToken1!: Decimal

@ManyToOne(() => Pool)
pool!: Pool

@ManyToOne(() => Position)
position!: Position

@ManyToOne(() => Transaction)
transaction!: Transaction
}
Loading

0 comments on commit 3477366

Please sign in to comment.