Watch pool initialize event (#123)

* Script to init pool.

* Watch pool initialize event.
This commit is contained in:
Ashwin Phatak 2021-07-06 20:05:40 +05:30 committed by GitHub
parent aec9281fb8
commit 194d079d5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 96 additions and 14 deletions

View File

@ -0,0 +1,16 @@
# Uniswap Watcher
Run the server:
```bash
$ yarn server
```
Start watching the factory contract:
Example:
```bash
$ npx ts-node src/cli/watch-contract.ts --configFile environments/local.toml --address 0xfE0034a874c2707c23F91D7409E9036F5e08ac34 --kind factory --startingBlock 100
```

View File

@ -15,6 +15,7 @@ import { Contract, KIND_FACTORY, KIND_POOL } from './entity/Contract';
import { Config } from './config';
import factoryABI from './artifacts/factory.json';
import poolABI from './artifacts/pool.json';
const log = debug('vulcanize:indexer');
@ -33,6 +34,7 @@ export class Indexer {
_getStorageAt: GetStorageAt
_factoryContract: ethers.utils.Interface
_poolContract: ethers.utils.Interface
constructor (config: Config, db: Database, ethClient: EthClient, pubsub: PubSub) {
assert(config);
@ -47,6 +49,7 @@ export class Indexer {
this._getStorageAt = this._ethClient.getStorageAt.bind(this._ethClient);
this._factoryContract = new ethers.utils.Interface(factoryABI);
this._poolContract = new ethers.utils.Interface(poolABI);
}
getEventIterator (): AsyncIterator<any> {
@ -138,12 +141,35 @@ export class Indexer {
let eventName;
let eventProps = {};
if (uniContract.kind === KIND_FACTORY) {
const logDescription = this._factoryContract.parseLog({ data, topics });
const { token0, token1, fee, tickSpacing, pool } = logDescription.args;
switch (uniContract.kind) {
case KIND_FACTORY: {
const logDescription = this._factoryContract.parseLog({ data, topics });
switch (logDescription.name) {
case 'PoolCreated': {
eventName = logDescription.name;
const { token0, token1, fee, tickSpacing, pool } = logDescription.args;
eventProps = { token0, token1, fee, tickSpacing, pool };
eventName = logDescription.name;
eventProps = { token0, token1, fee, tickSpacing, pool };
break;
}
}
break;
}
case KIND_POOL: {
const logDescription = this._poolContract.parseLog({ data, topics });
switch (logDescription.name) {
case 'Initialize': {
eventName = logDescription.name;
const { sqrtPriceX96, tick } = logDescription.args;
eventProps = { sqrtPriceX96: sqrtPriceX96.toString(), tick };
break;
}
}
break;
}
}
let event: DeepPartial<Event> | undefined;

View File

@ -42,14 +42,16 @@ $ yarn deploy:token --network localhost --name Token0 --symbol TK0
* **lint:schema**
Lint schema graphql files.
Lint schema graphql files:
```bash
$ yarn lint:schema schema/frontend.graphql
```
* **deploy:factory**
Deploy Factory contract.
Deploy Factory contract:
```bash
$ yarn deploy:factory
@ -59,18 +61,28 @@ $ yarn deploy:token --network localhost --name Token0 --symbol TK0
* **deploy:token**
Deploy Token contract.
Deploy Token contract:
```bash
$ yarn deploy:token --name TokenName --symbol TKS
```
* **create:pool**
Create pool with factory contract and tokens.
Create pool with factory contract and tokens:
```bash
$ yarn create:pool --factory 0xFactoryAddress --token0 0xToken0Address --token1 0xToken1Address --fee 500
```
* **initialize:pool**
Initialize a pool with price:
```bash
$ yarn initialize:pool --pool 0xPoolAddress --sqrt-price 4295128739
```
## References
* https://github.com/Uniswap/uniswap-v3-core

View File

@ -2,10 +2,11 @@ import 'dotenv/config';
import { HardhatUserConfig } from "hardhat/config";
import "@nomiclabs/hardhat-waffle";
import './tasks/accounts'
import './tasks/deploy-factory'
import './tasks/deploy-token'
import './tasks/create-pool'
import './tasks/accounts';
import './tasks/deploy-factory';
import './tasks/deploy-token';
import './tasks/create-pool';
import './tasks/initialize-pool';
const config: HardhatUserConfig = {
solidity: "0.8.0",

View File

@ -8,7 +8,8 @@
"lint:schema": "graphql-schema-linter",
"deploy:factory": "hardhat deploy-factory",
"deploy:token": "hardhat deploy-token",
"create:pool": "hardhat create-pool"
"create:pool": "hardhat create-pool",
"initialize:pool": "hardhat initialize-pool"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",

View File

@ -0,0 +1,26 @@
import { task, types } from "hardhat/config";
import {
abi as POOL_ABI,
} from '@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json'
import { ContractTransaction } from "ethers";
import '@nomiclabs/hardhat-ethers';
task("initialize-pool", "Initializes a pool")
.addParam('pool', 'Address of pool contract', undefined, types.string)
.addParam('sqrtPrice', 'Initial sqrtPriceX96', undefined, types.int)
.setAction(async (args, hre) => {
const { pool: poolAddress, sqrtPrice } = args
const [signer] = await hre.ethers.getSigners();
const pool = new hre.ethers.Contract(poolAddress, POOL_ABI, signer);
const transaction: ContractTransaction = await pool.initialize(sqrtPrice);
const receipt = await transaction.wait();
if (receipt.events) {
const poolInitializeEvent = receipt.events.find(el => el.event === 'Initialize');
if (poolInitializeEvent && poolInitializeEvent.args) {
const { sqrtPriceX96, tick } = poolInitializeEvent.args;
console.log('Pool initialized:', sqrtPriceX96.toString(), tick);
}
}
});