From 26d998d3a71b5a93d71286f273fd55ab04d043a2 Mon Sep 17 00:00:00 2001 From: nabarun Date: Wed, 29 Jun 2022 12:03:24 +0530 Subject: [PATCH] Update codegen to store hash for indexed reference type event args --- packages/codegen/src/indexer.ts | 22 +++++++++++-------- packages/codegen/src/schema.ts | 3 ++- .../src/templates/indexer-template.handlebars | 4 +++- packages/codegen/src/visitor.ts | 8 ++----- packages/moby-mask-watcher/demo.md | 6 ++--- packages/moby-mask-watcher/src/indexer.ts | 2 -- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/codegen/src/indexer.ts b/packages/codegen/src/indexer.ts index 21ac41e3..ceb7bc1c 100644 --- a/packages/codegen/src/indexer.ts +++ b/packages/codegen/src/indexer.ts @@ -8,6 +8,7 @@ import assert from 'assert'; import Handlebars from 'handlebars'; import { Writable } from 'stream'; import _ from 'lodash'; +import { utils } from 'ethers'; import { getTsForSol } from './utils/type-mappings'; import { Param } from './utils/types'; @@ -82,7 +83,7 @@ export class Indexer { this._queries.push(queryObject); } - addEvent (name: string, params: Array, contractKind: string): void { + addEvent (name: string, params: Array, contractKind: string): void { // Check if the event is already added. if (this._events.some(event => event.name === name && event.kind === contractKind)) { return; @@ -90,17 +91,20 @@ export class Indexer { const eventObject = { name, - params: _.cloneDeep(params), + params: params.map((param) => { + const tsParamType = getTsForSol(param.type); + assert(tsParamType); + const isReferenceType = param.type === 'string' || param.type === 'bytes' || param.baseType === 'tuple' || param.baseType === 'array'; + + return { + ...param, + type: tsParamType, + isIndexedReferenceType: param.indexed && isReferenceType + }; + }), kind: contractKind }; - eventObject.params = eventObject.params.map((param) => { - const tsParamType = getTsForSol(param.type); - assert(tsParamType); - param.type = tsParamType; - return param; - }); - this._events.push(eventObject); } diff --git a/packages/codegen/src/schema.ts b/packages/codegen/src/schema.ts index 4a957287..abc7b4ce 100644 --- a/packages/codegen/src/schema.ts +++ b/packages/codegen/src/schema.ts @@ -6,6 +6,7 @@ import assert from 'assert'; import { GraphQLSchema, parse, printSchema, print } from 'graphql'; import { SchemaComposer } from 'graphql-compose'; import { Writable } from 'stream'; +import { utils } from 'ethers'; import { getTsForSol, getGqlForTs } from './utils/type-mappings'; import { Param } from './utils/types'; @@ -65,7 +66,7 @@ export class Schema { * @param name Event name. * @param params Event parameters. */ - addEventType (name: string, params: Array): void { + addEventType (name: string, params: Array): void { name = `${name}Event`; // Check if the type is already added. diff --git a/packages/codegen/src/templates/indexer-template.handlebars b/packages/codegen/src/templates/indexer-template.handlebars index 64737e70..02bc3d53 100644 --- a/packages/codegen/src/templates/indexer-template.handlebars +++ b/packages/codegen/src/templates/indexer-template.handlebars @@ -465,12 +465,14 @@ export class Indexer implements IPLDIndexerInterface { case {{capitalize event.name}}_EVENT: { eventName = logDescription.name; {{#if event.params}} - const [ {{#each event.params~}} {{this.name}} {{~#unless @last}}, {{/unless}} {{~/each}} ] = logDescription.args; + const [{{#each event.params~}} {{this.name}} {{~#unless @last}}, {{/unless}} {{~/each}}] = logDescription.args; {{/if}} eventInfo = { {{#each event.params}} {{#if (compare this.type 'bigint')}} {{this.name}}: BigInt({{this.name}}.toString()) + {{~else if this.isIndexedReferenceType}} + {{this.name}}: {{this.name}}.hash {{~else}} {{this.name}} {{~/if}} diff --git a/packages/codegen/src/visitor.ts b/packages/codegen/src/visitor.ts index 8e58750b..6d5dc8cb 100644 --- a/packages/codegen/src/visitor.ts +++ b/packages/codegen/src/visitor.ts @@ -133,14 +133,10 @@ export class Visitor { const contractInterface = new utils.Interface(abi); Object.values(contractInterface.events).forEach(event => { - const params = event.inputs.map(input => { - return { name: input.name, type: input.type }; - }); - - this._schema.addEventType(event.name, params); + this._schema.addEventType(event.name, event.inputs); assert(this._contract); - this._indexer.addEvent(event.name, params, this._contract.kind); + this._indexer.addEvent(event.name, event.inputs, this._contract.kind); }); } diff --git a/packages/moby-mask-watcher/demo.md b/packages/moby-mask-watcher/demo.md index 61f2fc12..2d026d75 100644 --- a/packages/moby-mask-watcher/demo.md +++ b/packages/moby-mask-watcher/demo.md @@ -109,7 +109,7 @@ ```bash # In MobyMask repo. - git checkout ng-use-watcher + git checkout use-laconic-watcher-as-hosted-index ``` * Run yarn to install the packages @@ -204,7 +204,7 @@ isPhisher( blockHash: "LATEST_BLOCK_HASH" contractAddress: "MOBY_ADDRESS", - key0: "phisherName" + key0: "TWT:phishername" ) { value proof { @@ -215,7 +215,7 @@ isMember( blockHash: "LATEST_BLOCK_HASH" contractAddress: "MOBY_ADDRESS", - key0: "memberName" + key0: "TWT:membername" ) { value proof { diff --git a/packages/moby-mask-watcher/src/indexer.ts b/packages/moby-mask-watcher/src/indexer.ts index 8b75b1fb..c1be9225 100644 --- a/packages/moby-mask-watcher/src/indexer.ts +++ b/packages/moby-mask-watcher/src/indexer.ts @@ -589,7 +589,6 @@ export class Indexer implements IPLDIndexerInterface { eventName = logDescription.name; const [entity, isMember] = logDescription.args; eventInfo = { - // Indexed reference type arg entity: entity.hash, isMember }; @@ -610,7 +609,6 @@ export class Indexer implements IPLDIndexerInterface { eventName = logDescription.name; const [entity, isPhisher] = logDescription.args; eventInfo = { - // Indexed reference type arg entity: entity.hash, isPhisher };