Handle increase liquidity event (#143)

* Implement handler for NFPM IncreaseLiquidity event.

* Get contract values by querying uni-watcher.

Co-authored-by: nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
Ashwin Phatak 2021-07-16 18:34:51 +05:30 committed by GitHub
parent ae13edb99a
commit 3477366458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 2575 additions and 1378 deletions

View File

@ -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));

View File

@ -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;
};

View File

@ -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)));
});
};

View File

@ -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
@ -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);
@ -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);
@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -2,7 +2,7 @@ import assert from 'assert';
import debug from 'debug';
import { Client as UniClient } from '@vulcanize/uni-watcher';
import { Client as ERC20Client } from '@vulcanize/erc20-watcher';
import { BigNumber } from 'ethers';
import { BigNumber, utils } from 'ethers';
import { Database } from './database';
import { findEthPerToken, getEthPriceInUSD, getTrackedAmountUSD, sqrtPriceX96ToTokenPrices, WHITELIST_TOKENS } from './utils/pricing';
@ -11,6 +11,7 @@ import { Token } from './entity/Token';
import { convertTokenToDecimal, loadTransaction, safeDiv } from './utils';
import { loadTick } from './utils/tick';
import Decimal from 'decimal.js';
import { Position } from './entity/Position';
const log = debug('vulcanize:events');
@ -61,6 +62,14 @@ interface SwapEvent {
tick: bigint;
}
interface IncreaseLiquidityEvent {
__typename: 'IncreaseLiquidityEvent';
tokenId: bigint;
liquidity: bigint;
amount0: bigint;
amount1: bigint;
}
interface Block {
number: number;
hash: string;
@ -76,7 +85,7 @@ interface ResultEvent {
block: Block;
tx: Transaction;
contract: string;
event: PoolCreatedEvent | InitializeEvent | MintEvent | BurnEvent | SwapEvent;
event: PoolCreatedEvent | InitializeEvent | MintEvent | BurnEvent | SwapEvent | IncreaseLiquidityEvent;
proof: {
data: string;
}
@ -139,6 +148,11 @@ export class EventWatcher {
this._handleSwap(block, contract, tx, event as SwapEvent);
break;
case 'IncreaseLiquidityEvent':
log('NFPM IncreaseLiquidity event', contract);
this._handleIncreaseLiquidity(block, contract, tx, event as IncreaseLiquidityEvent);
break;
default:
break;
}
@ -299,7 +313,7 @@ export class EventWatcher {
BigInt(mintEvent.tickLower) <= BigInt(pool.tick) &&
BigInt(mintEvent.tickUpper) > BigInt(pool.tick)
) {
pool.liquidity = BigInt(pool.liquidity) + mintEvent.amount;
pool.liquidity = BigInt(pool.liquidity) + BigInt(mintEvent.amount);
}
}
@ -346,14 +360,14 @@ export class EventWatcher {
const lowerTickId = poolAddress + '#' + mintEvent.tickLower.toString();
const upperTickId = poolAddress + '#' + mintEvent.tickUpper.toString();
const lowerTick = await loadTick(this._db, lowerTickId, lowerTickIdx, pool, blockNumber);
const upperTick = await loadTick(this._db, upperTickId, upperTickIdx, pool, blockNumber);
const lowerTick = await loadTick(this._db, lowerTickId, BigInt(lowerTickIdx), pool, blockNumber);
const upperTick = await loadTick(this._db, upperTickId, BigInt(upperTickIdx), pool, blockNumber);
const amount = mintEvent.amount;
lowerTick.liquidityGross = lowerTick.liquidityGross + amount;
lowerTick.liquidityNet = lowerTick.liquidityNet + amount;
upperTick.liquidityGross = upperTick.liquidityGross + amount;
upperTick.liquidityNet = upperTick.liquidityNet + amount;
const amount = BigInt(mintEvent.amount);
lowerTick.liquidityGross = BigInt(lowerTick.liquidityGross) + amount;
lowerTick.liquidityNet = BigInt(lowerTick.liquidityNet) + amount;
upperTick.liquidityGross = BigInt(upperTick.liquidityGross) + amount;
upperTick.liquidityNet = BigInt(upperTick.liquidityNet) + amount;
// TODO: Update Tick's volume, fees, and liquidity provider count.
// Computing these on the tick level requires reimplementing some of the swapping code from v3-core.
@ -395,8 +409,8 @@ export class EventWatcher {
const token0 = pool.token0;
const token1 = pool.token1;
const amount0 = convertTokenToDecimal(burnEvent.amount0, token0.decimals);
const amount1 = convertTokenToDecimal(burnEvent.amount1, token1.decimals);
const amount0 = convertTokenToDecimal(burnEvent.amount0, BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(burnEvent.amount1, BigInt(token1.decimals));
const amountUSD = amount0
.times(token0.derivedETH.times(bundle.ethPriceUSD))
@ -406,20 +420,20 @@ export class EventWatcher {
factory.totalValueLockedETH = factory.totalValueLockedETH.minus(pool.totalValueLockedETH);
// Update globals.
factory.txCount = factory.txCount + BigInt(1);
factory.txCount = BigInt(factory.txCount) + BigInt(1);
// Update token0 data.
token0.txCount = token0.txCount + BigInt(1);
token0.txCount = BigInt(token0.txCount) + BigInt(1);
token0.totalValueLocked = token0.totalValueLocked.minus(amount0);
token0.totalValueLockedUSD = token0.totalValueLocked.times(token0.derivedETH.times(bundle.ethPriceUSD));
// Update token1 data.
token1.txCount = token1.txCount + BigInt(1);
token1.txCount = BigInt(token1.txCount) + BigInt(1);
token1.totalValueLocked = token1.totalValueLocked.minus(amount1);
token1.totalValueLockedUSD = token1.totalValueLocked.times(token1.derivedETH.times(bundle.ethPriceUSD));
// Pool data.
pool.txCount = pool.txCount + BigInt(1);
pool.txCount = BigInt(pool.txCount) + BigInt(1);
// Pools liquidity tracks the currently active liquidity given pools current tick.
// We only want to update it on burn if the position being burnt includes the current tick.
@ -473,11 +487,11 @@ export class EventWatcher {
const upperTickId = poolAddress + '#' + (burnEvent.tickUpper).toString();
const lowerTick = await this._db.loadTick({ id: lowerTickId, blockNumber });
const upperTick = await this._db.loadTick({ id: upperTickId, blockNumber });
const amount = burnEvent.amount;
lowerTick.liquidityGross = lowerTick.liquidityGross - amount;
lowerTick.liquidityNet = lowerTick.liquidityNet - amount;
upperTick.liquidityGross = upperTick.liquidityGross - amount;
upperTick.liquidityNet = upperTick.liquidityNet + amount;
const amount = BigInt(burnEvent.amount);
lowerTick.liquidityGross = BigInt(lowerTick.liquidityGross) - amount;
lowerTick.liquidityNet = BigInt(lowerTick.liquidityNet) - amount;
upperTick.liquidityGross = BigInt(upperTick.liquidityGross) - amount;
upperTick.liquidityNet = BigInt(upperTick.liquidityNet) + amount;
await updateUniswapDayData(this._db, { blockNumber, contractAddress, blockTimestamp });
await updatePoolDayData(this._db, { blockNumber, contractAddress, blockTimestamp });
@ -527,8 +541,8 @@ export class EventWatcher {
assert(token0 && token1, 'Pool tokens not found.');
// Amounts - 0/1 are token deltas. Can be positive or negative.
const amount0 = convertTokenToDecimal(swapEvent.amount0, token0.decimals);
const amount1 = convertTokenToDecimal(swapEvent.amount1, token1.decimals);
const amount0 = convertTokenToDecimal(swapEvent.amount0, BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(swapEvent.amount1, BigInt(token1.decimals));
// Need absolute amounts for volume.
let amount0Abs = amount0;
@ -557,7 +571,7 @@ export class EventWatcher {
const feesUSD = amountTotalUSDTracked.times(pool.feeTier.toString()).div(new Decimal('1000000'));
// Global updates.
factory.txCount = factory.txCount + BigInt(1);
factory.txCount = BigInt(factory.txCount) + BigInt(1);
factory.totalVolumeETH = factory.totalVolumeETH.plus(amountTotalETHTracked);
factory.totalVolumeUSD = factory.totalVolumeUSD.plus(amountTotalUSDTracked);
factory.untrackedVolumeUSD = factory.untrackedVolumeUSD.plus(amountTotalUSDUntracked);
@ -574,7 +588,7 @@ export class EventWatcher {
pool.volumeUSD = pool.volumeUSD.plus(amountTotalUSDTracked);
pool.untrackedVolumeUSD = pool.untrackedVolumeUSD.plus(amountTotalUSDUntracked);
pool.feesUSD = pool.feesUSD.plus(feesUSD);
pool.txCount = pool.txCount + BigInt(1);
pool.txCount = BigInt(pool.txCount) + BigInt(1);
// Update the pool with the new active liquidity, price, and tick.
pool.liquidity = swapEvent.liquidity;
@ -589,7 +603,7 @@ export class EventWatcher {
token0.volumeUSD = token0.volumeUSD.plus(amountTotalUSDTracked);
token0.untrackedVolumeUSD = token0.untrackedVolumeUSD.plus(amountTotalUSDUntracked);
token0.feesUSD = token0.feesUSD.plus(feesUSD);
token0.txCount = token0.txCount + BigInt(1);
token0.txCount = BigInt(token0.txCount) + BigInt(1);
// Update token1 data.
token1.volume = token1.volume.plus(amount1Abs);
@ -597,7 +611,7 @@ export class EventWatcher {
token1.volumeUSD = token1.volumeUSD.plus(amountTotalUSDTracked);
token1.untrackedVolumeUSD = token1.untrackedVolumeUSD.plus(amountTotalUSDUntracked);
token1.feesUSD = token1.feesUSD.plus(feesUSD);
token1.txCount = token1.txCount + BigInt(1);
token1.txCount = BigInt(token1.txCount) + BigInt(1);
// Updated pool rates.
const prices = sqrtPriceX96ToTokenPrices(pool.sqrtPrice, token0 as Token, token1 as Token);
@ -707,4 +721,117 @@ export class EventWatcher {
// Skipping update of inner vars of current or crossed ticks as they are not queried.
}
async _handleIncreaseLiquidity (block: Block, contractAddress: string, tx: Transaction, event: IncreaseLiquidityEvent): Promise<void> {
const { number: blockNumber } = block;
const position = await this._getPosition(block, contractAddress, tx, BigInt(event.tokenId));
// position was not able to be fetched.
if (position === null) {
return;
}
// Temp fix.
if (utils.getAddress(position.pool.id) === utils.getAddress('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248')) {
return;
}
const token0 = position.token0;
const token1 = position.token1;
const amount0 = convertTokenToDecimal(BigInt(event.amount0), BigInt(token0.decimals));
const amount1 = convertTokenToDecimal(BigInt(event.amount1), BigInt(token1.decimals));
position.liquidity = BigInt(position.liquidity) + BigInt(event.liquidity);
position.depositedToken0 = position.depositedToken0.plus(amount0);
position.depositedToken1 = position.depositedToken1.plus(amount1);
await this._updateFeeVars(position, block, contractAddress, BigInt(event.tokenId));
await this._db.savePosition(position, blockNumber);
await this._savePositionSnapshot(position, block, tx);
}
async _getPosition (block: Block, contractAddress: string, tx: Transaction, tokenId: bigint): Promise<Position | null> {
const { number: blockNumber, hash: blockHash, timestamp: blockTimestamp } = block;
const { hash: txHash } = tx;
let position = await this._db.getPosition({ id: tokenId.toString(), blockNumber });
if (!position) {
const nfpmPosition = await this._uniClient.getPosition(blockHash, tokenId);
// The contract call reverts in situations where the position is minted and deleted in the same block.
// From my investigation this happens in calls from BancorSwap.
// (e.g. 0xf7867fa19aa65298fadb8d4f72d0daed5e836f3ba01f0b9b9631cdc6c36bed40)
if (nfpmPosition) {
const { token0: token0Address, token1: token1Address, fee } = await this._uniClient.poolIdToPoolKey(blockHash, nfpmPosition.poolId);
const { pool: poolAddress } = await this._uniClient.getPool(blockHash, token0Address, token1Address, fee);
const transaction = await loadTransaction(this._db, { txHash, blockNumber, blockTimestamp });
const pool = await this._db.getPool({ id: poolAddress, blockNumber });
const [token0, token1] = await Promise.all([
this._db.getToken({ id: token0Address, blockNumber }),
this._db.getToken({ id: token0Address, blockNumber })
]);
const [tickLower, tickUpper] = await Promise.all([
this._db.getTick({ id: poolAddress.concat('#').concat(nfpmPosition.tickLower.toString()), blockNumber }),
this._db.getTick({ id: poolAddress.concat('#').concat(nfpmPosition.tickUpper.toString()), blockNumber })
]);
position = await this._db.loadPosition({
id: tokenId.toString(),
blockNumber,
pool,
token0,
token1,
tickLower,
tickUpper,
transaction,
feeGrowthInside0LastX128: BigInt(nfpmPosition.feeGrowthInside0LastX128.toString()),
feeGrowthInside1LastX128: BigInt(nfpmPosition.feeGrowthInside1LastX128.toString())
});
}
}
return position || null;
}
async _updateFeeVars (position: Position, block: Block, contractAddress: string, tokenId: bigint): Promise<Position> {
const nfpmPosition = await this._uniClient.getPosition(block.hash, tokenId);
if (nfpmPosition) {
position.feeGrowthInside0LastX128 = BigInt(nfpmPosition.feeGrowthInside0LastX128.toString());
position.feeGrowthInside1LastX128 = BigInt(nfpmPosition.feeGrowthInside1LastX128.toString());
}
return position;
}
async _savePositionSnapshot (position: Position, block: Block, tx: Transaction): Promise<void> {
const transaction = await loadTransaction(this._db, { txHash: tx.hash, blockNumber: block.number, blockTimestamp: block.timestamp });
await this._db.loadPositionSnapshot({
id: position.id.concat('#').concat(block.number.toString()),
blockNumber: block.number,
owner: position.owner,
pool: position.pool,
position: position,
timestamp: block.timestamp,
liquidity: position.liquidity,
depositedToken0: position.depositedToken0,
depositedToken1: position.depositedToken1,
withdrawnToken0: position.withdrawnToken0,
withdrawnToken1: position.withdrawnToken1,
collectedFeesToken0: position.collectedFeesToken0,
collectedFeesToken1: position.collectedFeesToken1,
transaction,
feeGrowthInside0LastX128: position.feeGrowthInside0LastX128,
feeGrowthInside1LastX128: position.feeGrowthInside1LastX128
});
}
}

View File

@ -0,0 +1,3 @@
import { utils } from 'ethers';
export const ADDRESS_ZERO = utils.getAddress('0x0000000000000000000000000000000000000000');

View File

@ -1,198 +1,366 @@
[
{
"anonymous": false,
"inputs": [
{
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint24",
"name": "fee",
"type": "uint24"
},
{
"indexed": true,
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
}
],
"name": "FeeAmountEnabled",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "oldOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnerChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "token0",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "token1",
"type": "address"
},
{
"indexed": true,
"internalType": "uint24",
"name": "fee",
"type": "uint24"
},
{
"indexed": false,
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
},
{
"indexed": false,
"internalType": "address",
"name": "pool",
"type": "address"
}
],
"name": "PoolCreated",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenA",
"type": "address"
},
{
"internalType": "address",
"name": "tokenB",
"type": "address"
},
{
"internalType": "uint24",
"name": "fee",
"type": "uint24"
}
],
"name": "createPool",
"outputs": [
{
"internalType": "address",
"name": "pool",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint24",
"name": "fee",
"type": "uint24"
},
{
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
}
],
"name": "enableFeeAmount",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint24",
"name": "",
"type": "uint24"
}
],
"name": "feeAmountTickSpacing",
"outputs": [
{
"internalType": "int24",
"name": "",
"type": "int24"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
},
{
"internalType": "address",
"name": "",
"type": "address"
},
{
"internalType": "uint24",
"name": "",
"type": "uint24"
}
],
"name": "getPool",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "parameters",
"outputs": [
{
"internalType": "address",
"name": "factory",
"type": "address"
},
{
"internalType": "address",
"name": "token0",
"type": "address"
},
{
"internalType": "address",
"name": "token1",
"type": "address"
},
{
"internalType": "uint24",
"name": "fee",
"type": "uint24"
},
{
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_owner",
"type": "address"
}
],
"name": "setOwner",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
"storageLayout": {
"storage": [
{
"indexed": true,
"internalType": "uint24",
"name": "fee",
"type": "uint24"
"astId": 2840,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "parameters",
"offset": 0,
"slot": "0",
"type": "t_struct(Parameters)2836_storage"
},
{
"indexed": true,
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
}
],
"name": "FeeAmountEnabled",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "oldOwner",
"type": "address"
"astId": 56,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "owner",
"offset": 0,
"slot": "3",
"type": "t_address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnerChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "token0",
"type": "address"
"astId": 62,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "feeAmountTickSpacing",
"offset": 0,
"slot": "4",
"type": "t_mapping(t_uint24,t_int24)"
},
{
"indexed": true,
"internalType": "address",
"name": "token1",
"type": "address"
"astId": 72,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "getPool",
"offset": 0,
"slot": "5",
"type": "t_mapping(t_address,t_mapping(t_address,t_mapping(t_uint24,t_address)))"
}
],
"types": {
"t_address": {
"encoding": "inplace",
"label": "address",
"numberOfBytes": "20"
},
{
"indexed": true,
"internalType": "uint24",
"name": "fee",
"type": "uint24"
"t_int24": {
"encoding": "inplace",
"label": "int24",
"numberOfBytes": "3"
},
{
"indexed": false,
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
"t_mapping(t_address,t_mapping(t_address,t_mapping(t_uint24,t_address)))": {
"encoding": "mapping",
"key": "t_address",
"label": "mapping(address => mapping(address => mapping(uint24 => address)))",
"numberOfBytes": "32",
"value": "t_mapping(t_address,t_mapping(t_uint24,t_address))"
},
{
"indexed": false,
"internalType": "address",
"name": "pool",
"type": "address"
}
],
"name": "PoolCreated",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenA",
"type": "address"
"t_mapping(t_address,t_mapping(t_uint24,t_address))": {
"encoding": "mapping",
"key": "t_address",
"label": "mapping(address => mapping(uint24 => address))",
"numberOfBytes": "32",
"value": "t_mapping(t_uint24,t_address)"
},
{
"internalType": "address",
"name": "tokenB",
"type": "address"
"t_mapping(t_uint24,t_address)": {
"encoding": "mapping",
"key": "t_uint24",
"label": "mapping(uint24 => address)",
"numberOfBytes": "32",
"value": "t_address"
},
{
"internalType": "uint24",
"name": "fee",
"type": "uint24"
}
],
"name": "createPool",
"outputs": [
{
"internalType": "address",
"name": "pool",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint24",
"name": "fee",
"type": "uint24"
"t_mapping(t_uint24,t_int24)": {
"encoding": "mapping",
"key": "t_uint24",
"label": "mapping(uint24 => int24)",
"numberOfBytes": "32",
"value": "t_int24"
},
{
"internalType": "int24",
"name": "tickSpacing",
"type": "int24"
}
],
"name": "enableFeeAmount",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint24",
"name": "fee",
"type": "uint24"
}
],
"name": "feeAmountTickSpacing",
"outputs": [
{
"internalType": "int24",
"name": "",
"type": "int24"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenA",
"type": "address"
"t_struct(Parameters)2836_storage": {
"encoding": "inplace",
"label": "struct UniswapV3PoolDeployer.Parameters",
"members": [
{
"astId": 2827,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "factory",
"offset": 0,
"slot": "0",
"type": "t_address"
},
{
"astId": 2829,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "token0",
"offset": 0,
"slot": "1",
"type": "t_address"
},
{
"astId": 2831,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "token1",
"offset": 0,
"slot": "2",
"type": "t_address"
},
{
"astId": 2833,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "fee",
"offset": 20,
"slot": "2",
"type": "t_uint24"
},
{
"astId": 2835,
"contract": "contracts/UniswapV3Factory.sol:UniswapV3Factory",
"label": "tickSpacing",
"offset": 23,
"slot": "2",
"type": "t_int24"
}
],
"numberOfBytes": "96"
},
{
"internalType": "address",
"name": "tokenB",
"type": "address"
},
{
"internalType": "uint24",
"name": "fee",
"type": "uint24"
"t_uint24": {
"encoding": "inplace",
"label": "uint24",
"numberOfBytes": "3"
}
],
"name": "getPool",
"outputs": [
{
"internalType": "address",
"name": "pool",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_owner",
"type": "address"
}
],
"name": "setOwner",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
}
]
}

View File

@ -1,6 +1,8 @@
import { gql } from '@apollo/client/core';
import { GraphQLClient, GraphQLConfig } from '@vulcanize/ipld-eth-client';
import { queryGetPool, queryPoolIdToPoolKey, queryPosition } from './queries';
export class Client {
_config: GraphQLConfig;
_client: GraphQLClient;
@ -72,6 +74,13 @@ export class Client {
liquidity
tick
}
... on IncreaseLiquidityEvent {
tokenId
liquidity
amount0
amount1
}
}
}
}
@ -81,4 +90,42 @@ export class Client {
}
);
}
async getPosition (blockHash: string, tokenId: bigint): Promise<any> {
const { position } = await this._client.query(
gql(queryPosition),
{
blockHash,
tokenId: tokenId.toString()
}
);
return position;
}
async poolIdToPoolKey (blockHash: string, poolId: bigint): Promise<any> {
const { poolIdToPoolKey } = await this._client.query(
gql(queryPoolIdToPoolKey),
{
blockHash,
poolId: poolId.toString()
}
);
return poolIdToPoolKey;
}
async getPool (blockHash: string, token0: string, token1: string, fee: bigint): Promise<any> {
const { getPool } = await this._client.query(
gql(queryGetPool),
{
blockHash,
token0,
token1,
fee: fee.toString()
}
);
return getPool;
}
}

View File

@ -122,6 +122,14 @@ export class Database {
.getOne();
}
async getLatestContract (kind: string): Promise<Contract | undefined> {
return this._conn.getRepository(Contract)
.createQueryBuilder('contract')
.where('kind = :kind', { kind })
.orderBy('id', 'DESC')
.getOne();
}
async saveContract (address: string, kind: string, startingBlock: number): Promise<void> {
await this._conn.transaction(async (tx) => {
const repo = tx.getRepository(Contract);

View File

@ -2,9 +2,10 @@ import debug from 'debug';
import { DeepPartial } from 'typeorm';
import JSONbig from 'json-bigint';
import { ethers } from 'ethers';
import assert from 'assert';
import { EthClient } from '@vulcanize/ipld-eth-client';
import { GetStorageAt } from '@vulcanize/solidity-mapper';
import { GetStorageAt, getStorageValue, StorageLayout } from '@vulcanize/solidity-mapper';
import { Config } from '@vulcanize/util';
import { Database } from './database';
@ -12,9 +13,9 @@ import { Event, UNKNOWN_EVENT_NAME } from './entity/Event';
import { BlockProgress } from './entity/BlockProgress';
import { Contract, KIND_FACTORY, KIND_POOL, KIND_NFPM } from './entity/Contract';
import factoryABI from './artifacts/factory.json';
import { abi as factoryABI, storageLayout as factoryStorageLayout } from './artifacts/factory.json';
import { abi as nfpmABI, storageLayout as nfpmStorageLayout } from './artifacts/NonfungiblePositionManager.json';
import poolABI from './artifacts/pool.json';
import nfpmABI from './artifacts/NonfungiblePositionManager.json';
// TODO: Move to config.
const MAX_EVENTS_BLOCK_RANGE = 1000;
@ -33,6 +34,13 @@ type ResultEvent = {
proof: string;
};
interface ValueResult {
value: any;
proof: {
data: string;
}
}
export class Indexer {
_config: Config;
_db: Database
@ -374,4 +382,49 @@ export class Indexer {
return this._db.getEventsInRange(fromBlockNumber, toBlockNumber);
}
async position (blockHash: string, tokenId: string) {
const nfpmContract = await this._db.getLatestContract('nfpm');
assert(nfpmContract, 'No NFPM contract watched.');
const { value, proof } = await this._getStorageValue(nfpmStorageLayout, blockHash, nfpmContract.address, '_positions', BigInt(tokenId));
return {
...value,
proof
};
}
async poolIdToPoolKey (blockHash: string, poolId: string) {
const nfpmContract = await this._db.getLatestContract('nfpm');
assert(nfpmContract, 'No NFPM contract watched.');
const { value, proof } = await this._getStorageValue(nfpmStorageLayout, blockHash, nfpmContract.address, '_poolIdToPoolKey', BigInt(poolId));
return {
...value,
proof
};
}
async getPool (blockHash: string, token0: string, token1: string, fee: string) {
const factoryContract = await this._db.getLatestContract('factory');
assert(factoryContract, 'No Factory contract watched.');
const { value, proof } = await this._getStorageValue(factoryStorageLayout, blockHash, factoryContract.address, 'getPool', token0, token1, BigInt(fee));
return {
pool: value,
proof
};
}
// TODO: Move into base/class or framework package.
async _getStorageValue (storageLayout: StorageLayout, blockHash: string, token: string, variable: string, ...mappingKeys: any[]): Promise<ValueResult> {
return getStorageValue(
storageLayout,
this._getStorageAt,
blockHash,
token,
variable,
...mappingKeys
);
}
}

View File

@ -12,3 +12,49 @@ query getEvents($blockHash: String!, $token: String!) {
}
}
`;
export const queryPosition = gql`
query getPosition($blockHash: String!, $tokenId: String!) {
position(blockHash: $blockHash, tokenId: $tokenId) {
nonce
operator
poolId
tickLower
tickUpper
liquidity
feeGrowthInside0LastX128
feeGrowthInside1LastX128
tokensOwed0
tokensOwed1
proof {
data
}
}
}
`;
export const queryPoolIdToPoolKey = gql`
query poolIdToPoolKey($blockHash: String!, $poolId: String!) {
poolIdToPoolKey(blockHash: $blockHash, poolId: $poolId) {
token0
token1
fee
proof {
data
}
}
}
`;
export const queryGetPool = gql`
query getPool($blockHash: String!, $token0: String!, $token1: String!, $fee: String!) {
getPool(blockHash: $blockHash, token0: $token0, token1: $token1, fee: $fee) {
pool
proof {
data
}
}
}
`;

View File

@ -72,6 +72,21 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch
const events = await indexer.getEventsInRange(fromBlockNumber, toBlockNumber);
return events.map(event => indexer.getResultEvent(event));
},
position: (_: any, { blockHash, tokenId }: { blockHash: string, tokenId: string }) => {
log('position', blockHash, tokenId);
return indexer.position(blockHash, tokenId);
},
poolIdToPoolKey: (_: any, { blockHash, poolId }: { blockHash: string, poolId: string }) => {
log('poolIdToPoolKey', blockHash, poolId);
return indexer.poolIdToPoolKey(blockHash, poolId);
},
getPool: (_: any, { blockHash, token0, token1, fee }: { blockHash: string, token0: string, token1: string, fee: string }) => {
log('getPool', blockHash, token0, token1, fee);
return indexer.getPool(blockHash, token0, token1, fee);
}
}
};

View File

@ -202,12 +202,12 @@ type Query {
position(
blockHash: String!
tokenId: BigInt!
tokenId: String!
): ResultPosition
poolIdToPoolKey(
blockHash: String!
poolId: BigInt!
poolId: String!
): ResultPoolKey
# Factory
@ -216,7 +216,7 @@ type Query {
blockHash: String!
token0: String!
token1: String!
fee: BigInt!
fee: String!
): ResultGetPool
# Pool