Skip to content

Commit

Permalink
Changes for sushiswap watchers (#424)
Browse files Browse the repository at this point in the history
* Update imports from watcher packages

* Make cid nullable in uni-info-watcher

* Take contract addresses from env in smoke tests

* Increase wait time for Swap event processing in smoke tests

* Use blockNumber for eth calls in uni-watcher

* Handle pruning of missing null block in FEVM

---------

Co-authored-by: prathamesh0 <[email protected]>
  • Loading branch information
nikugogoi and prathamesh0 authored Aug 9, 2023
1 parent 644229b commit 2b5c9b3
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 79 deletions.
1 change: 1 addition & 0 deletions packages/erc20-watcher/environments/local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
[upstream.ethServer]
gqlApiEndpoint = "http://127.0.0.1:8082/graphql"
rpcProviderEndpoint = "http://127.0.0.1:8081"
rpcClient = true

[upstream.cache]
name = "requests"
Expand Down
22 changes: 14 additions & 8 deletions packages/erc20-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import JSONbig from 'json-bigint';
import { ethers } from 'ethers';

import { IndexerInterface } from '@vulcanize/util';
import { EthClient } from '@cerc-io/ipld-eth-client';
import {
Indexer as BaseIndexer,
ServerConfig,
Expand All @@ -22,7 +21,8 @@ import {
JobQueue,
DatabaseInterface,
Clients,
StateKind
StateKind,
EthClient
} from '@cerc-io/util';
import { StorageLayout, MappingKey } from '@cerc-io/solidity-mapper';

Expand Down Expand Up @@ -139,9 +139,11 @@ export class Indexer implements IndexerInterface {

async totalSupply (blockHash: string, token: string): Promise<ValueResult> {
let result: ValueResult;
const { block: { number: blockNumber } } = await this._ethClient.getBlockByHash(blockHash);

if (this._serverMode === ETH_CALL_MODE) {
const value = await fetchTokenTotalSupply(this._ethProvider, blockHash, token);
// eth_call doesnt support calling method by blockHash https://eth.wiki/json-rpc/API#the-default-block-parameter
const value = await fetchTokenTotalSupply(this._ethProvider, blockNumber, token);

result = { value };
} else {
Expand Down Expand Up @@ -174,7 +176,7 @@ export class Indexer implements IndexerInterface {
const contract = new ethers.Contract(token, this._abi, this._ethProvider);

// eth_call doesnt support calling method by blockHash https://eth.wiki/json-rpc/API#the-default-block-parameter
const value = await contract.balanceOf(owner, { blockTag: blockHash });
const value = await contract.balanceOf(owner, { blockTag: blockNumber });

result = {
value: BigInt(value.toString())
Expand Down Expand Up @@ -209,7 +211,8 @@ export class Indexer implements IndexerInterface {

if (this._serverMode === ETH_CALL_MODE) {
const contract = new ethers.Contract(token, this._abi, this._ethProvider);
const value = await contract.allowance(owner, spender, { blockTag: blockHash });
// eth_call doesnt support calling method by blockHash https://eth.wiki/json-rpc/API#the-default-block-parameter
const value = await contract.allowance(owner, spender, { blockTag: blockNumber });

result = {
value: BigInt(value.toString())
Expand Down Expand Up @@ -243,7 +246,8 @@ export class Indexer implements IndexerInterface {
const { block: { number: blockNumber } } = await this._ethClient.getBlockByHash(blockHash);

if (this._serverMode === ETH_CALL_MODE) {
const value = await fetchTokenName(this._ethProvider, blockHash, token);
// eth_call doesnt support calling method by blockHash https://eth.wiki/json-rpc/API#the-default-block-parameter
const value = await fetchTokenName(this._ethProvider, blockNumber, token);

result = { value };
} else {
Expand Down Expand Up @@ -272,7 +276,8 @@ export class Indexer implements IndexerInterface {
const { block: { number: blockNumber } } = await this._ethClient.getBlockByHash(blockHash);

if (this._serverMode === ETH_CALL_MODE) {
const value = await fetchTokenSymbol(this._ethProvider, blockHash, token);
// eth_call doesnt support calling method by blockHash https://eth.wiki/json-rpc/API#the-default-block-parameter
const value = await fetchTokenSymbol(this._ethProvider, blockNumber, token);

result = { value };
} else {
Expand Down Expand Up @@ -301,7 +306,8 @@ export class Indexer implements IndexerInterface {
const { block: { number: blockNumber } } = await this._ethClient.getBlockByHash(blockHash);

if (this._serverMode === ETH_CALL_MODE) {
const value = await fetchTokenDecimals(this._ethProvider, blockHash, token);
// eth_call doesnt support calling method by blockHash https://eth.wiki/json-rpc/API#the-default-block-parameter
const value = await fetchTokenDecimals(this._ethProvider, blockNumber, token);

result = { value };
} else {
Expand Down
20 changes: 10 additions & 10 deletions packages/erc20-watcher/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import ERC20SymbolBytesABI from '../artifacts/ERC20SymbolBytes.json';
import ERC20NameBytesABI from '../artifacts/ERC20NameBytes.json';
import { StaticTokenDefinition } from './static-token-definition';

export const fetchTokenSymbol = async (ethProvider: providers.BaseProvider, blockHash: string, tokenAddress: string): Promise<string> => {
export const fetchTokenSymbol = async (ethProvider: providers.BaseProvider, blockNumber: number, tokenAddress: string): Promise<string> => {
const contract = new Contract(tokenAddress, abi, ethProvider);
const contractSymbolBytes = new Contract(tokenAddress, ERC20SymbolBytesABI, ethProvider);
let symbolValue = 'unknown';

// Try types string and bytes32 for symbol.
try {
const result = await contract.symbol({ blockTag: blockHash });
const result = await contract.symbol({ blockTag: blockNumber });
symbolValue = result;
} catch (error) {
try {
const result = await contractSymbolBytes.symbol({ blockTag: blockHash });
const result = await contractSymbolBytes.symbol({ blockTag: blockNumber });

// For broken pairs that have no symbol function exposed.
if (!isNullEthValue(utils.hexlify(result))) {
Expand All @@ -41,18 +41,18 @@ export const fetchTokenSymbol = async (ethProvider: providers.BaseProvider, bloc
return symbolValue;
};

export const fetchTokenName = async (ethProvider: providers.BaseProvider, blockHash: string, tokenAddress: string): Promise<string> => {
export const fetchTokenName = async (ethProvider: providers.BaseProvider, blockNumber: number, tokenAddress: string): Promise<string> => {
const contract = new Contract(tokenAddress, abi, ethProvider);
const contractNameBytes = new Contract(tokenAddress, ERC20NameBytesABI, ethProvider);
let nameValue = 'unknown';

// Try types string and bytes32 for name.
try {
const result = await contract.name({ blockTag: blockHash });
const result = await contract.name({ blockTag: blockNumber });
nameValue = result;
} catch (error) {
try {
const result = await contractNameBytes.name({ blockTag: blockHash });
const result = await contractNameBytes.name({ blockTag: blockNumber });

// For broken pairs that have no name function exposed.
if (!isNullEthValue(utils.hexlify(result))) {
Expand All @@ -73,12 +73,12 @@ export const fetchTokenName = async (ethProvider: providers.BaseProvider, blockH
return nameValue;
};

export const fetchTokenTotalSupply = async (ethProvider: providers.BaseProvider, blockHash: string, tokenAddress: string): Promise<bigint> => {
export const fetchTokenTotalSupply = async (ethProvider: providers.BaseProvider, blockNumber: number, tokenAddress: string): Promise<bigint> => {
const contract = new Contract(tokenAddress, abi, ethProvider);
let totalSupplyValue = null;

try {
const result = await contract.totalSupply({ blockTag: blockHash });
const result = await contract.totalSupply({ blockTag: blockNumber });
totalSupplyValue = result.toString();
} catch (error) {
totalSupplyValue = 0;
Expand All @@ -87,14 +87,14 @@ export const fetchTokenTotalSupply = async (ethProvider: providers.BaseProvider,
return BigInt(totalSupplyValue);
};

export const fetchTokenDecimals = async (ethProvider: providers.BaseProvider, blockHash: string, tokenAddress: string): Promise<bigint> => {
export const fetchTokenDecimals = async (ethProvider: providers.BaseProvider, blockNumber: number, tokenAddress: string): Promise<bigint> => {
const contract = new Contract(tokenAddress, abi, ethProvider);

// Try types uint8 for decimals.
let decimalValue = null;

try {
const result = await contract.decimals({ blockTag: blockHash });
const result = await contract.decimals({ blockTag: blockNumber });
decimalValue = result.toString();
} catch (error) {
// Try with the static definition.
Expand Down
1 change: 1 addition & 0 deletions packages/uni-info-watcher/environments/local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
[upstream.ethServer]
gqlApiEndpoint = "http://127.0.0.1:8082/graphql"
rpcProviderEndpoint = "http://127.0.0.1:8081"
rpcClient = true

[upstream.cache]
name = "requests"
Expand Down
1 change: 1 addition & 0 deletions packages/uni-info-watcher/environments/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[upstream.ethServer]
gqlApiEndpoint = "http://127.0.0.1:8082/graphql"
rpcProviderEndpoint = "http://127.0.0.1:8545"
rpcClient = true

[upstream.cache]
name = "requests"
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-info-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"job-runner": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true node --enable-source-maps dist/job-runner.js",
"job-runner:prof": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true node --require pprof --enable-source-maps dist/job-runner.js",
"job-runner:dev": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true ts-node src/job-runner.ts",
"smoke-test": "yarn test:init && mocha src/smoke.test.ts",
"smoke-test": "mocha src/smoke.test.ts",
"fill": "DEBUG=vulcanize:* node --enable-source-maps dist/fill.js",
"fill:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/fill.js",
"fill:dev": "DEBUG=vulcanize:* ts-node src/fill.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-info-watcher/src/entity/BlockProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class BlockProgress implements BlockProgressInterface {
@PrimaryGeneratedColumn()
id!: number;

@Column('varchar')
@Column('varchar', { nullable: true })
cid!: string;

@Column('varchar', { length: 66 })
Expand Down
4 changes: 2 additions & 2 deletions packages/uni-info-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import {
DatabaseInterface,
Clients,
updateSubgraphState,
dumpSubgraphState
dumpSubgraphState,
EthClient
} from '@cerc-io/util';
import { EthClient } from '@cerc-io/ipld-eth-client';
import { StorageLayout, MappingKey } from '@cerc-io/solidity-mapper';

import { findEthPerToken, getEthPriceInUSD, getTrackedAmountUSD, sqrtPriceX96ToTokenPrices, WHITELIST_TOKENS } from './utils/pricing';
Expand Down
31 changes: 20 additions & 11 deletions packages/uni-info-watcher/src/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2021 Vulcanize, Inc.
//

import assert from 'assert';
import { expect } from 'chai';
import { ethers, Contract, Signer, constants, utils } from 'ethers';
import 'mocha';
Expand All @@ -10,15 +11,14 @@ import _ from 'lodash';
import { OrderDirection, wait, getConfig } from '@cerc-io/util';
import { Config } from '@vulcanize/util';
import {
deployTokens,
deployUniswapV3Callee,
TESTERC20_ABI,
createPool,
initializePool,
getMinTick,
getMaxTick,
approveToken,
NFPM_ABI
NFPM_ABI,
TESTUNISWAPV3CALLEE_ABI
} from '@vulcanize/util/test';
import { Client as UniClient, watchEvent } from '@vulcanize/uni-watcher';
import {
Expand All @@ -43,6 +43,7 @@ describe('uni-info-watcher', () => {
let factory: Contract;
let pool: Contract;
let poolCallee: Contract;
let poolCalleeAddress: string;
let token0: Contract;
let token1: Contract;
let token0Address: string;
Expand All @@ -60,6 +61,16 @@ describe('uni-info-watcher', () => {
let deadline: number;

before(async () => {
assert(process.env.ACCOUNT_PRIVATE_KEY, 'ACCOUNT_PRIVATE_KEY not set');
assert(process.env.TOKEN0_ADDRESS, 'TOKEN0_ADDRESS not set');
assert(process.env.TOKEN1_ADDRESS, 'TOKEN1_ADDRESS not set');
assert(process.env.UNISWAP_CALLEE_ADDRESS, 'UNISWAP_CALLEE_ADDRESS not set');

const signerKey = process.env.ACCOUNT_PRIVATE_KEY;
token0Address = process.env.TOKEN0_ADDRESS;
token1Address = process.env.TOKEN1_ADDRESS;
poolCalleeAddress = process.env.UNISWAP_CALLEE_ADDRESS;

config = await getConfig(CONFIG_FILE);

const { upstream, server: { host, port } } = config;
Expand All @@ -78,7 +89,7 @@ describe('uni-info-watcher', () => {
});

const provider = new ethers.providers.JsonRpcProvider(rpcProviderEndpoint);
signer = provider.getSigner();
signer = new ethers.Wallet(signerKey, provider);
recipient = await signer.getAddress();

// Get the factory contract address.
Expand Down Expand Up @@ -108,9 +119,7 @@ describe('uni-info-watcher', () => {
const fee = 500;

before(async () => {
// Deploy 2 tokens.
({ token0Address, token1Address } = await deployTokens(signer));
// Convert the addresses to lowercase.
// Convert token addresses to lowercase.
token0Address = utils.hexlify(token0Address);
token1Address = utils.hexlify(token1Address);

Expand Down Expand Up @@ -244,8 +253,8 @@ describe('uni-info-watcher', () => {
let oldPool: any;

before(async () => {
// Deploy UniswapV3Callee.
poolCallee = await deployUniswapV3Callee(signer);
// Attach to a pre-deployed UniswapV3Callee contract.
poolCallee = new ethers.Contract(poolCalleeAddress, TESTUNISWAPV3CALLEE_ABI, signer);

const tickSpacing = await pool.tickSpacing();
// https://github.com/Uniswap/uniswap-v3-core/blob/main/test/UniswapV3Pool.spec.ts#L196
Expand Down Expand Up @@ -604,8 +613,8 @@ describe('uni-info-watcher', () => {
]);
eventValue = values[1];

// Sleeping for 5 sec for the event to be processed.
await wait(5000);
// Sleeping for 10 sec for the event to be processed.
await wait(10000);
});

it('should update Token entities', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/uni-info-watcher/test/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import assert from 'assert';

import { JobQueue, getConfig, initClients } from '@cerc-io/util';
import { JobQueue, getConfig } from '@cerc-io/util';
import { initClients } from '@cerc-io/cli';
import { Config } from '@vulcanize/util';
import { Client as ERC20Client } from '@vulcanize/erc20-watcher';
import { Client as UniClient } from '@vulcanize/uni-watcher';
Expand Down
1 change: 1 addition & 0 deletions packages/uni-watcher/environments/local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
[upstream.ethServer]
gqlApiEndpoint = "http://127.0.0.1:8082/graphql"
rpcProviderEndpoint = "http://127.0.0.1:8081"
rpcClient = true

[upstream.cache]
name = "requests"
Expand Down
1 change: 1 addition & 0 deletions packages/uni-watcher/environments/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
[upstream.ethServer]
gqlApiEndpoint = "http://127.0.0.1:8082/graphql"
rpcProviderEndpoint = "http://127.0.0.1:8545"
rpcClient = true

[upstream.cache]
name = "requests"
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"job-runner": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true node --enable-source-maps dist/job-runner.js",
"job-runner:prof": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true node --require pprof --enable-source-maps dist/job-runner.js",
"job-runner:dev": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true ts-node src/job-runner.ts",
"smoke-test": "yarn test:init && mocha src/smoke.test.ts",
"smoke-test": "mocha src/smoke.test.ts",
"fill": "DEBUG=vulcanize:* node --enable-source-maps dist/fill.js",
"fill:prof": "DEBUG=vulcanize:* node --require pprof --enable-source-maps dist/fill.js",
"fill:dev": "DEBUG=vulcanize:* ts-node src/fill.ts",
Expand Down
Loading

0 comments on commit 2b5c9b3

Please sign in to comment.