Handle array and struct type event params in codegen (#150)

* Handle array and struct type event params

* Refactor parse event in watcher indexer code
This commit is contained in:
nikugogoi 2022-08-04 17:09:30 +05:30 committed by GitHub
parent a15305450c
commit 6990eb892a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 97 additions and 796 deletions

View File

@ -8,7 +8,6 @@ import assert from 'assert';
import Handlebars from 'handlebars'; import Handlebars from 'handlebars';
import { Writable } from 'stream'; import { Writable } from 'stream';
import _ from 'lodash'; import _ from 'lodash';
import { utils } from 'ethers';
import { getTsForSol } from './utils/type-mappings'; import { getTsForSol } from './utils/type-mappings';
import { Param } from './utils/types'; import { Param } from './utils/types';
@ -83,31 +82,6 @@ export class Indexer {
this._queries.push(queryObject); this._queries.push(queryObject);
} }
addEvent (name: string, params: Array<utils.ParamType>, contractKind: string): void {
// Check if the event is already added.
if (this._events.some(event => event.name === name && event.kind === contractKind)) {
return;
}
const eventObject = {
name,
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
};
this._events.push(eventObject);
}
addSubgraphEntities (subgraphSchemaDocument: any): void { addSubgraphEntities (subgraphSchemaDocument: any): void {
// Add subgraph entities for creating the relations and entity types maps in the indexer. // Add subgraph entities for creating the relations and entity types maps in the indexer.
const subgraphTypeDefs = subgraphSchemaDocument.definitions; const subgraphTypeDefs = subgraphSchemaDocument.definitions;
@ -180,8 +154,6 @@ export class Indexer {
exportIndexer (outStream: Writable, contracts: any[]): void { exportIndexer (outStream: Writable, contracts: any[]): void {
const template = Handlebars.compile(this._templateString); const template = Handlebars.compile(this._templateString);
const eventNames = this._events.map((event: any) => event.name);
const obj = { const obj = {
contracts, contracts,
queries: this._queries, queries: this._queries,
@ -189,9 +161,7 @@ export class Indexer {
constants: { constants: {
MODE_ETH_CALL, MODE_ETH_CALL,
MODE_STORAGE MODE_STORAGE
}, }
events: this._events,
uniqueEvents: new Set(eventNames)
}; };
const indexer = template(obj); const indexer = template(obj);

View File

@ -4,7 +4,7 @@
import assert from 'assert'; import assert from 'assert';
import { GraphQLSchema, parse, printSchema, print } from 'graphql'; import { GraphQLSchema, parse, printSchema, print } from 'graphql';
import { SchemaComposer } from 'graphql-compose'; import { ObjectTypeComposer, ObjectTypeComposerDefinition, ObjectTypeComposerFieldConfigMapDefinition, SchemaComposer } from 'graphql-compose';
import { Writable } from 'stream'; import { Writable } from 'stream';
import { utils } from 'ethers'; import { utils } from 'ethers';
@ -76,27 +76,7 @@ export class Schema {
return; return;
} }
const typeObject: any = {}; this._createObjectType(name, params);
typeObject.name = name;
typeObject.fields = {};
if (params.length > 0) {
typeObject.fields = params.reduce((acc, curr) => {
const tsCurrType = getTsForSol(curr.type);
assert(tsCurrType, `ts type for sol type ${curr.type} for ${curr.name} not found`);
acc[curr.name] = `${getGqlForTs(tsCurrType)}!`;
return acc;
}, typeObject.fields);
} else {
// Types must define one or more fields.
typeObject.fields = {
dummy: 'String'
};
}
// Create a type composer to add the required type in the schema composer.
this._composer.createObjectTC(typeObject);
this._events.push(name); this._events.push(name);
this._addToEventUnion(name); this._addToEventUnion(name);
@ -459,4 +439,53 @@ export class Schema {
// Add the new fields to the current type. // Add the new fields to the current type.
eventTC.addFields(newFields); eventTC.addFields(newFields);
} }
/**
* Create GraphQL schmea object type.
* @param name
* @param params
*/
_createObjectType (name: string, params: Array<utils.ParamType>): ObjectTypeComposer {
const typeObject: ObjectTypeComposerDefinition<any, any> = { name };
if (params.length > 0) {
typeObject.fields = params.reduce((acc: ObjectTypeComposerFieldConfigMapDefinition<any, any>, curr) => {
acc[curr.name] = this._getObjectTypeField(curr);
return acc;
}, {});
} else {
// Types must define one or more fields.
typeObject.fields = {
dummy: 'String'
};
}
// Create a type composer to add the required type in the schema composer.
return this._composer.createObjectTC(typeObject);
}
/**
* Get type of field in GraphQL schema for object types.
* @param param
*/
_getObjectTypeField (param: utils.ParamType): ObjectTypeComposer | string | any[] {
if (param.indexed && ['string', 'bytes', 'tuple', 'array'].includes(param.baseType)) {
// Check for indexed reference type event params.
param = utils.ParamType.fromObject({ type: 'bytes32', name: param.name });
}
if (param.baseType === 'tuple') {
const typeName = param.name.charAt(0).toUpperCase() + param.name.slice(1);
return this._createObjectType(typeName, param.components);
}
if (param.baseType === 'array') {
return [this._getObjectTypeField(param.arrayChildren)];
}
const tsCurrType = getTsForSol(param.type);
assert(tsCurrType, `ts type for sol type ${param.type} for ${param.name} not found`);
return `${getGqlForTs(tsCurrType)}!`;
}
} }

View File

@ -421,7 +421,6 @@ export class Indexer implements IPLDIndexerInterface {
} }
{{/if}} {{/if}}
async triggerIndexingOnEvent (event: Event): Promise<void> { async triggerIndexingOnEvent (event: Event): Promise<void> {
const resultEvent = this.getResultEvent(event); const resultEvent = this.getResultEvent(event);
@ -450,9 +449,6 @@ export class Indexer implements IPLDIndexerInterface {
} }
parseEventNameAndArgs (kind: string, logObj: any): any { parseEventNameAndArgs (kind: string, logObj: any): any {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
const { topics, data } = logObj; const { topics, data } = logObj;
const contract = this._contractMap.get(kind); const contract = this._contractMap.get(kind);
@ -460,15 +456,7 @@ export class Indexer implements IPLDIndexerInterface {
const logDescription = contract.parseLog({ data, topics }); const logDescription = contract.parseLog({ data, topics });
switch (kind) { const { eventName, eventInfo } = this._baseIndexer.parseEvent(logDescription)
{{#each contracts as | contract |}}
case KIND_{{capitalize contract.contractName}}: {
({ eventName, eventInfo } = this.parse{{contract.contractName}}Event(logDescription));
break;
}
{{/each}}
}
return { return {
eventName, eventName,
@ -477,45 +465,6 @@ export class Indexer implements IPLDIndexerInterface {
}; };
} }
{{#each contracts as | contract |}}
parse{{contract.contractName}}Event (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
{{#each ../events as | event |}}
{{#if (compare contract.contractKind event.kind)}}
case {{capitalize event.name}}_EVENT: {
eventName = logDescription.name;
{{#if event.params}}
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}}
{{~#unless @last}},{{/unless}}
{{/each}}
};
break;
}
{{/if}}
{{/each}}
}
return {
eventName,
eventInfo
};
}
{{/each}}
async getIPLDStatus (): Promise<IpldStatus | undefined> { async getIPLDStatus (): Promise<IpldStatus | undefined> {
return this._db.getIPLDStatus(); return this._db.getIPLDStatus();
} }

View File

@ -138,9 +138,6 @@ export class Visitor {
Object.values(contractInterface.events).forEach(event => { Object.values(contractInterface.events).forEach(event => {
this._schema.addEventType(event.name, event.inputs); this._schema.addEventType(event.name, event.inputs);
assert(this._contract);
this._indexer.addEvent(event.name, event.inputs, this._contract.kind);
}); });
} }

View File

@ -62,32 +62,6 @@ const KIND_EDENNETWORK = 'EdenNetwork';
const KIND_MERKLEDISTRIBUTOR = 'EdenNetworkDistribution'; const KIND_MERKLEDISTRIBUTOR = 'EdenNetworkDistribution';
const KIND_DISTRIBUTORGOVERNANCE = 'EdenNetworkGovernance'; const KIND_DISTRIBUTORGOVERNANCE = 'EdenNetworkGovernance';
const TRANSFER_EVENT = 'Transfer';
const APPROVAL_EVENT = 'Approval';
const AUTHORIZATIONUSED_EVENT = 'AuthorizationUsed';
const ADMINUPDATED_EVENT = 'AdminUpdated';
const TAXRATEUPDATED_EVENT = 'TaxRateUpdated';
const SLOTCLAIMED_EVENT = 'SlotClaimed';
const SLOTDELEGATEUPDATED_EVENT = 'SlotDelegateUpdated';
const STAKE_EVENT = 'Stake';
const UNSTAKE_EVENT = 'Unstake';
const WITHDRAW_EVENT = 'Withdraw';
const APPROVALFORALL_EVENT = 'ApprovalForAll';
const BLOCKPRODUCERADDED_EVENT = 'BlockProducerAdded';
const BLOCKPRODUCERREMOVED_EVENT = 'BlockProducerRemoved';
const BLOCKPRODUCERREWARDCOLLECTORCHANGED_EVENT = 'BlockProducerRewardCollectorChanged';
const REWARDSCHEDULECHANGED_EVENT = 'RewardScheduleChanged';
const CLAIMED_EVENT = 'Claimed';
const SLASHED_EVENT = 'Slashed';
const MERKLEROOTUPDATED_EVENT = 'MerkleRootUpdated';
const ACCOUNTUPDATED_EVENT = 'AccountUpdated';
const PERMANENTURI_EVENT = 'PermanentURI';
const GOVERNANCECHANGED_EVENT = 'GovernanceChanged';
const UPDATETHRESHOLDCHANGED_EVENT = 'UpdateThresholdChanged';
const ROLEADMINCHANGED_EVENT = 'RoleAdminChanged';
const ROLEGRANTED_EVENT = 'RoleGranted';
const ROLEREVOKED_EVENT = 'RoleRevoked';
export type ResultEvent = { export type ResultEvent = {
block: { block: {
cid: string; cid: string;
@ -406,9 +380,6 @@ export class Indexer implements IPLDIndexerInterface {
} }
parseEventNameAndArgs (kind: string, logObj: any): any { parseEventNameAndArgs (kind: string, logObj: any): any {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
const { topics, data } = logObj; const { topics, data } = logObj;
const contract = this._contractMap.get(kind); const contract = this._contractMap.get(kind);
@ -416,23 +387,7 @@ export class Indexer implements IPLDIndexerInterface {
const logDescription = contract.parseLog({ data, topics }); const logDescription = contract.parseLog({ data, topics });
switch (kind) { const { eventName, eventInfo } = this._baseIndexer.parseEvent(logDescription);
case KIND_EDENNETWORK: {
({ eventName, eventInfo } = this.parseEdenNetworkEvent(logDescription));
break;
}
case KIND_MERKLEDISTRIBUTOR: {
({ eventName, eventInfo } = this.parseMerkleDistributorEvent(logDescription));
break;
}
case KIND_DISTRIBUTORGOVERNANCE: {
({ eventName, eventInfo } = this.parseDistributorGovernanceEvent(logDescription));
break;
}
}
return { return {
eventName, eventName,
@ -441,396 +396,6 @@ export class Indexer implements IPLDIndexerInterface {
}; };
} }
parseEdenNetworkEvent (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
case TRANSFER_EVENT: {
eventName = logDescription.name;
const { from, to, value } = logDescription.args;
eventInfo = {
from,
to,
value: BigInt(value.toString())
};
break;
}
case APPROVAL_EVENT: {
eventName = logDescription.name;
const { owner, spender, value } = logDescription.args;
eventInfo = {
owner,
spender,
value: BigInt(value.toString())
};
break;
}
case AUTHORIZATIONUSED_EVENT: {
eventName = logDescription.name;
const { authorizer, nonce } = logDescription.args;
eventInfo = {
authorizer,
nonce
};
break;
}
case ADMINUPDATED_EVENT: {
eventName = logDescription.name;
const { newAdmin, oldAdmin } = logDescription.args;
eventInfo = {
newAdmin,
oldAdmin
};
break;
}
case TAXRATEUPDATED_EVENT: {
eventName = logDescription.name;
const { newNumerator, newDenominator, oldNumerator, oldDenominator } = logDescription.args;
eventInfo = {
newNumerator,
newDenominator,
oldNumerator,
oldDenominator
};
break;
}
case SLOTCLAIMED_EVENT: {
eventName = logDescription.name;
const { slot, owner, delegate, newBidAmount, oldBidAmount, taxNumerator, taxDenominator } = logDescription.args;
eventInfo = {
slot,
owner,
delegate,
newBidAmount: BigInt(newBidAmount.toString()),
oldBidAmount: BigInt(oldBidAmount.toString()),
taxNumerator,
taxDenominator
};
break;
}
case SLOTDELEGATEUPDATED_EVENT: {
eventName = logDescription.name;
const { slot, owner, newDelegate, oldDelegate } = logDescription.args;
eventInfo = {
slot,
owner,
newDelegate,
oldDelegate
};
break;
}
case STAKE_EVENT: {
eventName = logDescription.name;
const { staker, stakeAmount } = logDescription.args;
eventInfo = {
staker,
stakeAmount: BigInt(stakeAmount.toString())
};
break;
}
case UNSTAKE_EVENT: {
eventName = logDescription.name;
const { staker, unstakedAmount } = logDescription.args;
eventInfo = {
staker,
unstakedAmount: BigInt(unstakedAmount.toString())
};
break;
}
case WITHDRAW_EVENT: {
eventName = logDescription.name;
const { withdrawer, withdrawalAmount } = logDescription.args;
eventInfo = {
withdrawer,
withdrawalAmount: BigInt(withdrawalAmount.toString())
};
break;
}
}
return {
eventName,
eventInfo
};
}
parseMerkleDistributorEvent (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
case TRANSFER_EVENT: {
eventName = logDescription.name;
const { from, to, tokenId } = logDescription.args;
eventInfo = {
from,
to,
tokenId: BigInt(tokenId.toString())
};
break;
}
case APPROVAL_EVENT: {
eventName = logDescription.name;
const { owner, approved, tokenId } = logDescription.args;
eventInfo = {
owner,
approved,
tokenId: BigInt(tokenId.toString())
};
break;
}
case APPROVALFORALL_EVENT: {
eventName = logDescription.name;
const { owner, operator, approved } = logDescription.args;
eventInfo = {
owner,
operator,
approved
};
break;
}
case BLOCKPRODUCERADDED_EVENT: {
eventName = logDescription.name;
const { producer } = logDescription.args;
eventInfo = {
producer
};
break;
}
case BLOCKPRODUCERREMOVED_EVENT: {
eventName = logDescription.name;
const { producer } = logDescription.args;
eventInfo = {
producer
};
break;
}
case BLOCKPRODUCERREWARDCOLLECTORCHANGED_EVENT: {
eventName = logDescription.name;
const { producer, collector } = logDescription.args;
eventInfo = {
producer,
collector
};
break;
}
case REWARDSCHEDULECHANGED_EVENT: {
eventName = logDescription.name;
eventInfo = {};
break;
}
case CLAIMED_EVENT: {
eventName = logDescription.name;
const { index, totalEarned, account, claimed } = logDescription.args;
eventInfo = {
index: BigInt(index.toString()),
totalEarned: BigInt(totalEarned.toString()),
account,
claimed: BigInt(claimed.toString())
};
break;
}
case SLASHED_EVENT: {
eventName = logDescription.name;
const { account, slashed } = logDescription.args;
eventInfo = {
account,
slashed: BigInt(slashed.toString())
};
break;
}
case MERKLEROOTUPDATED_EVENT: {
eventName = logDescription.name;
const { merkleRoot, distributionNumber, metadataURI } = logDescription.args;
eventInfo = {
merkleRoot,
distributionNumber: BigInt(distributionNumber.toString()),
metadataURI
};
break;
}
case ACCOUNTUPDATED_EVENT: {
eventName = logDescription.name;
const { account, totalClaimed, totalSlashed } = logDescription.args;
eventInfo = {
account,
totalClaimed: BigInt(totalClaimed.toString()),
totalSlashed: BigInt(totalSlashed.toString())
};
break;
}
case PERMANENTURI_EVENT: {
eventName = logDescription.name;
const { value, id } = logDescription.args;
eventInfo = {
value,
id: BigInt(id.toString())
};
break;
}
case GOVERNANCECHANGED_EVENT: {
eventName = logDescription.name;
const { from, to } = logDescription.args;
eventInfo = {
from,
to
};
break;
}
case UPDATETHRESHOLDCHANGED_EVENT: {
eventName = logDescription.name;
const { updateThreshold } = logDescription.args;
eventInfo = {
updateThreshold: BigInt(updateThreshold.toString())
};
break;
}
case ROLEADMINCHANGED_EVENT: {
eventName = logDescription.name;
const { role, previousAdminRole, newAdminRole } = logDescription.args;
eventInfo = {
role,
previousAdminRole,
newAdminRole
};
break;
}
case ROLEGRANTED_EVENT: {
eventName = logDescription.name;
const { role, account, sender } = logDescription.args;
eventInfo = {
role,
account,
sender
};
break;
}
case ROLEREVOKED_EVENT: {
eventName = logDescription.name;
const { role, account, sender } = logDescription.args;
eventInfo = {
role,
account,
sender
};
break;
}
}
return {
eventName,
eventInfo
};
}
parseDistributorGovernanceEvent (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
case BLOCKPRODUCERADDED_EVENT: {
eventName = logDescription.name;
const { producer } = logDescription.args;
eventInfo = {
producer
};
break;
}
case BLOCKPRODUCERREMOVED_EVENT: {
eventName = logDescription.name;
const { producer } = logDescription.args;
eventInfo = {
producer
};
break;
}
case BLOCKPRODUCERREWARDCOLLECTORCHANGED_EVENT: {
eventName = logDescription.name;
const { producer, collector } = logDescription.args;
eventInfo = {
producer,
collector
};
break;
}
case REWARDSCHEDULECHANGED_EVENT: {
eventName = logDescription.name;
eventInfo = {};
break;
}
case ROLEADMINCHANGED_EVENT: {
eventName = logDescription.name;
const { role, previousAdminRole, newAdminRole } = logDescription.args;
eventInfo = {
role,
previousAdminRole,
newAdminRole
};
break;
}
case ROLEGRANTED_EVENT: {
eventName = logDescription.name;
const { role, account, sender } = logDescription.args;
eventInfo = {
role,
account,
sender
};
break;
}
case ROLEREVOKED_EVENT: {
eventName = logDescription.name;
const { role, account, sender } = logDescription.args;
eventInfo = {
role,
account,
sender
};
break;
}
}
return {
eventName,
eventInfo
};
}
async getIPLDStatus (): Promise<IpldStatus | undefined> { async getIPLDStatus (): Promise<IpldStatus | undefined> {
return this._db.getIPLDStatus(); return this._db.getIPLDStatus();
} }

View File

@ -264,36 +264,10 @@ export class Indexer implements IndexerInterface {
} }
parseEventNameAndArgs (kind: string, logObj: any): any { parseEventNameAndArgs (kind: string, logObj: any): any {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
const { topics, data } = logObj; const { topics, data } = logObj;
const logDescription = this._contract.parseLog({ data, topics }); const logDescription = this._contract.parseLog({ data, topics });
switch (logDescription.name) { const { eventName, eventInfo } = this._baseIndexer.parseEvent(logDescription);
case TRANSFER_EVENT: {
eventName = logDescription.name;
const [from, to, value] = logDescription.args;
eventInfo = {
from,
to,
value: value.toString()
};
break;
}
case APPROVAL_EVENT: {
eventName = logDescription.name;
const [owner, spender, value] = logDescription.args;
eventInfo = {
owner,
spender,
value: value.toString()
};
break;
}
}
return { eventName, eventInfo }; return { eventName, eventInfo };
} }

View File

@ -45,10 +45,6 @@ const log = debug('vulcanize:indexer');
const KIND_ERC721 = 'ERC721'; const KIND_ERC721 = 'ERC721';
const APPROVAL_EVENT = 'Approval';
const APPROVALFORALL_EVENT = 'ApprovalForAll';
const TRANSFER_EVENT = 'Transfer';
export type ResultEvent = { export type ResultEvent = {
block: { block: {
cid: string; cid: string;
@ -789,9 +785,6 @@ export class Indexer implements IPLDIndexerInterface {
} }
parseEventNameAndArgs (kind: string, logObj: any): any { parseEventNameAndArgs (kind: string, logObj: any): any {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
const { topics, data } = logObj; const { topics, data } = logObj;
const contract = this._contractMap.get(kind); const contract = this._contractMap.get(kind);
@ -799,13 +792,7 @@ export class Indexer implements IPLDIndexerInterface {
const logDescription = contract.parseLog({ data, topics }); const logDescription = contract.parseLog({ data, topics });
switch (kind) { const { eventName, eventInfo } = this._baseIndexer.parseEvent(logDescription);
case KIND_ERC721: {
({ eventName, eventInfo } = this.parseERC721Event(logDescription));
break;
}
}
return { return {
eventName, eventName,
@ -814,52 +801,6 @@ export class Indexer implements IPLDIndexerInterface {
}; };
} }
parseERC721Event (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
case APPROVAL_EVENT: {
eventName = logDescription.name;
const [owner, approved, tokenId] = logDescription.args;
eventInfo = {
owner,
approved,
tokenId: BigInt(tokenId.toString())
};
break;
}
case APPROVALFORALL_EVENT: {
eventName = logDescription.name;
const [owner, operator, approved] = logDescription.args;
eventInfo = {
owner,
operator,
approved
};
break;
}
case TRANSFER_EVENT: {
eventName = logDescription.name;
const [from, to, tokenId] = logDescription.args;
eventInfo = {
from,
to,
tokenId: BigInt(tokenId.toString())
};
break;
}
}
return {
eventName,
eventInfo
};
}
async getIPLDStatus (): Promise<IpldStatus | undefined> { async getIPLDStatus (): Promise<IpldStatus | undefined> {
return this._db.getIPLDStatus(); return this._db.getIPLDStatus();
} }

View File

@ -51,10 +51,6 @@ const KIND_EXAMPLE1 = 'Example1';
const KIND_FACTORY = 'Factory'; const KIND_FACTORY = 'Factory';
const KIND_POOL = 'Pool'; const KIND_POOL = 'Pool';
const TEST_EVENT = 'Test';
const POOLCREATED_EVENT = 'PoolCreated';
const INITIALIZE_EVENT = 'Initialize';
export type ResultEvent = { export type ResultEvent = {
block: { block: {
cid: string; cid: string;
@ -416,9 +412,6 @@ export class Indexer implements IPLDIndexerInterface {
} }
parseEventNameAndArgs (kind: string, logObj: any): any { parseEventNameAndArgs (kind: string, logObj: any): any {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
const { topics, data } = logObj; const { topics, data } = logObj;
const contract = this._contractMap.get(kind); const contract = this._contractMap.get(kind);
@ -426,23 +419,7 @@ export class Indexer implements IPLDIndexerInterface {
const logDescription = contract.parseLog({ data, topics }); const logDescription = contract.parseLog({ data, topics });
switch (kind) { const { eventName, eventInfo } = this._baseIndexer.parseEvent(logDescription);
case KIND_EXAMPLE1: {
({ eventName, eventInfo } = this.parseExample1Event(logDescription));
break;
}
case KIND_FACTORY: {
({ eventName, eventInfo } = this.parseFactoryEvent(logDescription));
break;
}
case KIND_POOL: {
({ eventName, eventInfo } = this.parsePoolEvent(logDescription));
break;
}
}
return { return {
eventName, eventName,
@ -451,79 +428,6 @@ export class Indexer implements IPLDIndexerInterface {
}; };
} }
parseExample1Event (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
case TEST_EVENT: {
eventName = logDescription.name;
const { param1, param2, param3 } = logDescription.args;
eventInfo = {
param1,
param2,
param3: BigInt(param3.toString())
};
break;
}
}
return {
eventName,
eventInfo
};
}
parseFactoryEvent (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
case POOLCREATED_EVENT: {
eventName = logDescription.name;
const { token0, token1, fee, tickSpacing, pool } = logDescription.args;
eventInfo = {
token0,
token1,
fee,
tickSpacing,
pool
};
break;
}
}
return {
eventName,
eventInfo
};
}
parsePoolEvent (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
case INITIALIZE_EVENT: {
eventName = logDescription.name;
const { sqrtPriceX96, tick } = logDescription.args;
eventInfo = {
sqrtPriceX96: BigInt(sqrtPriceX96.toString()),
tick
};
break;
}
}
return {
eventName,
eventInfo
};
}
async getIPLDStatus (): Promise<IpldStatus | undefined> { async getIPLDStatus (): Promise<IpldStatus | undefined> {
return this._db.getIPLDStatus(); return this._db.getIPLDStatus();
} }

View File

@ -50,11 +50,6 @@ const log = debug('vulcanize:indexer');
export const KIND_PHISHERREGISTRY = 'PhisherRegistry'; export const KIND_PHISHERREGISTRY = 'PhisherRegistry';
const DELEGATIONTRIGGERED_EVENT = 'DelegationTriggered';
const MEMBERSTATUSUPDATED_EVENT = 'MemberStatusUpdated';
const OWNERSHIPTRANSFERRED_EVENT = 'OwnershipTransferred';
const PHISHERSTATUSUPDATED_EVENT = 'PhisherStatusUpdated';
export type ResultEvent = { export type ResultEvent = {
block: { block: {
cid: string; cid: string;
@ -517,9 +512,6 @@ export class Indexer implements IPLDIndexerInterface {
} }
parseEventNameAndArgs (kind: string, logObj: any): any { parseEventNameAndArgs (kind: string, logObj: any): any {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
const { topics, data } = logObj; const { topics, data } = logObj;
const contract = this._contractMap.get(kind); const contract = this._contractMap.get(kind);
@ -527,13 +519,7 @@ export class Indexer implements IPLDIndexerInterface {
const logDescription = contract.parseLog({ data, topics }); const logDescription = contract.parseLog({ data, topics });
switch (kind) { const { eventName, eventInfo } = this._baseIndexer.parseEvent(logDescription);
case KIND_PHISHERREGISTRY: {
({ eventName, eventInfo } = this.parsePhisherRegistryEvent(logDescription));
break;
}
}
return { return {
eventName, eventName,
@ -542,59 +528,6 @@ export class Indexer implements IPLDIndexerInterface {
}; };
} }
parsePhisherRegistryEvent (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
let eventName = UNKNOWN_EVENT_NAME;
let eventInfo = {};
switch (logDescription.name) {
case DELEGATIONTRIGGERED_EVENT: {
eventName = logDescription.name;
const [principal, agent] = logDescription.args;
eventInfo = {
principal,
agent
};
break;
}
case MEMBERSTATUSUPDATED_EVENT: {
eventName = logDescription.name;
const [entity, isMember] = logDescription.args;
eventInfo = {
entity: entity.hash,
isMember
};
break;
}
case OWNERSHIPTRANSFERRED_EVENT: {
eventName = logDescription.name;
const [previousOwner, newOwner] = logDescription.args;
eventInfo = {
previousOwner,
newOwner
};
break;
}
case PHISHERSTATUSUPDATED_EVENT: {
eventName = logDescription.name;
const [entity, isPhisher] = logDescription.args;
eventInfo = {
entity: entity.hash,
isPhisher
};
break;
}
}
return {
eventName,
eventInfo
};
}
async getIPLDStatus (): Promise<IpldStatus | undefined> { async getIPLDStatus (): Promise<IpldStatus | undefined> {
return this._db.getIPLDStatus(); return this._db.getIPLDStatus();
} }

View File

@ -363,4 +363,43 @@ export class Indexer {
...mappingKeys ...mappingKeys
); );
} }
parseEvent (logDescription: ethers.utils.LogDescription): { eventName: string, eventInfo: any } {
const eventName = logDescription.name;
const eventInfo = logDescription.eventFragment.inputs.reduce((acc: any, input, index) => {
acc[input.name] = this._parseLogArg(input, logDescription.args[index]);
return acc;
}, {});
return {
eventName,
eventInfo
};
}
_parseLogArg (param: ethers.utils.ParamType, arg: ethers.utils.Result): any {
if (ethers.utils.Indexed.isIndexed(arg)) {
// Get hash if indexed reference type.
return arg.hash;
}
if (ethers.BigNumber.isBigNumber(arg)) {
return arg.toBigInt();
}
if (param.baseType === 'array') {
return arg.map(el => this._parseLogArg(param.arrayChildren, el));
}
if (param.baseType === 'tuple') {
return param.components.reduce((acc: any, component) => {
acc[component.name] = this._parseLogArg(component, arg[component.name]);
return acc;
}, {});
}
return arg;
}
} }