Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix running erc20-watcher as active kind watcher #116

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/erc20-watcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,33 @@ createdb erc20-watcher
```

Update `environments/local.toml` with database connection settings for both the databases.
```toml
[database]
type = "postgres"
host = "localhost"
port = 5432
database = "erc20-watcher"
username = "postgres"
password = "postgres"

Update the `upstream` config in `environments/local.toml` and provide the `ipld-eth-server` GQL API and the `indexer-db` postgraphile endpoints.
[jobQueue]
dbConnectionString = "postgres://postgres:postgres@localhost/erc20-watcher-job-queue"
```

Update the `upstream` config in `environments/local.toml`. Provide the `ipld-eth-server` GQL and RPC API and the `indexer-db` postgraphile endpoints.
```toml
[upstream]
[upstream.ethServer]
gqlApiEndpoint = "http://127.0.0.1:8082/graphql"
gqlPostgraphileEndpoint = "http://127.0.0.1:5000/graphql"
rpcProviderEndpoint = "http://127.0.0.1:8081"
```

Ensure that watcher is of active kind. Update the kind in `server` config to active.
```toml
[server]
kind = "active"
```

## Run

Expand Down Expand Up @@ -70,6 +95,11 @@ $ yarn job-runner -f environments/local.toml

GQL console: http://localhost:3001/graphql

Deploy an ERC20 token:
```bash
$ yarn token:deploy
```

Start watching a token:

```bash
Expand Down
8 changes: 8 additions & 0 deletions packages/erc20-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class Indexer implements IndexerInterface {
this._contract = new ethers.utils.Interface(this._abi);
}

async init (): Promise<void> {
await this._baseIndexer.fetchContracts();
}

getResultEvent (event: Event): EventResult {
const eventFields = JSON.parse(event.eventInfo);

Expand Down Expand Up @@ -303,6 +307,10 @@ export class Indexer implements IndexerInterface {
return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock);
}

cacheContract (contract: Contract): void {
return this._baseIndexer.cacheContract(contract);
}

async saveEventEntity (dbEvent: Event): Promise<Event> {
return this._baseIndexer.saveEventEntity(dbEvent);
}
Expand Down
1 change: 1 addition & 0 deletions packages/erc20-watcher/src/job-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const main = async (): Promise<any> => {
await jobQueue.start();

const indexer = new Indexer(db, ethClient, postgraphileClient, ethProvider, jobQueue, config.server.mode);
await indexer.init();

const jobRunner = new JobRunner(jobQueueConfig, indexer, jobQueue);
await jobRunner.start();
Expand Down
1 change: 1 addition & 0 deletions packages/erc20-watcher/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const main = async (): Promise<any> => {
const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs });

const indexer = new Indexer(db, ethClient, postgraphileClient, ethProvider, jobQueue, mode);
await indexer.init();

const eventWatcher = new EventWatcher(config.upstream, ethClient, postgraphileClient, indexer, pubsub, jobQueue);

Expand Down
14 changes: 8 additions & 6 deletions packages/util/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ export class Indexer {
}

async getEventsByFilter (blockHash: string, contract?: string, name?: string): Promise<Array<EventInterface>> {
if (contract) {
const watchedContract = await this.isWatchedContract(contract);
if (!watchedContract) {
throw new Error('Not a watched contract');
}
}
// TODO: Uncomment after implementing hot reload of watched contracts in server process.
// This doesn't affect functionality as we already have a filter condition on the contract in the query.
// if (contract) {
// const watchedContract = await this.isWatchedContract(contract);
// if (!watchedContract) {
// throw new Error('Not a watched contract');
// }
// }

const where: Where = {
eventName: [{
Expand Down