From 7d7cf26f81190b0bd0269c225a7b1ff61a6ee3cf Mon Sep 17 00:00:00 2001 From: nikugogoi <95nikass@gmail.com> Date: Mon, 31 May 2021 16:05:31 +0530 Subject: [PATCH] Add method to get event name topics from abi. --- packages/solidity-mapper/src/index.ts | 2 ++ packages/solidity-mapper/src/logs.ts | 26 ++++++++++++++++++++ packages/watcher/src/indexer.ts | 35 +++++++++------------------ 3 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 packages/solidity-mapper/src/logs.ts diff --git a/packages/solidity-mapper/src/index.ts b/packages/solidity-mapper/src/index.ts index 089869e1..61972464 100644 --- a/packages/solidity-mapper/src/index.ts +++ b/packages/solidity-mapper/src/index.ts @@ -1 +1,3 @@ export { getStorageValue, getStorageInfo, StorageLayout, GetStorageAt } from './storage'; + +export { getEventNameTopics } from './logs'; diff --git a/packages/solidity-mapper/src/logs.ts b/packages/solidity-mapper/src/logs.ts new file mode 100644 index 00000000..2e95dc8d --- /dev/null +++ b/packages/solidity-mapper/src/logs.ts @@ -0,0 +1,26 @@ +import { JsonFragment } from "@ethersproject/abi" +import { utils } from "ethers"; + +interface EventNameTopic { + [eventName: string]: string +} + +/** + * Function to get event name topics from abi. + * @param abi + */ +export const getEventNameTopics = (abi: JsonFragment[]): EventNameTopic => { + const eventFragments = abi.filter(({ type }) => type === 'event'); + + return eventFragments.reduce((acc: EventNameTopic, { name, inputs }) => { + if (inputs && name) { + const inputParamsString = inputs.map(({ type }) => type) + .join(','); + + const signature = utils.keccak256(utils.toUtf8Bytes(`${name}(${inputParamsString})`)); + acc[name] = signature; + } + + return acc; + }, {}) +} diff --git a/packages/watcher/src/indexer.ts b/packages/watcher/src/indexer.ts index e72acc38..e2b4fbbb 100644 --- a/packages/watcher/src/indexer.ts +++ b/packages/watcher/src/indexer.ts @@ -1,25 +1,11 @@ import assert from "assert"; import debug from 'debug'; -import { EthClient, getMappingSlot, topictoAddress } from "@vulcanize/ipld-eth-client"; import { Connection } from "typeorm"; +import { invert } from "lodash"; +import { EthClient, getMappingSlot, topictoAddress } from "@vulcanize/ipld-eth-client"; +import { getStorageInfo, getEventNameTopics } from '@vulcanize/solidity-mapper'; -import { getStorageInfo } from '@vulcanize/solidity-mapper'; - -import { storageLayout } from './artifacts/ERC20.json'; - -// Event signatures. -// TODO: Generate from ABI. -const ERC20_EVENT_NAME_TOPICS = { - "Transfer": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", - "Approval": "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925" -}; - -// Topic to GQL event name. -// TODO: Generate from ABI. -const GQL_EVENT_TYPE = { - "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef": "TransferEvent", - "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925": "ApprovalEvent" -}; +import { storageLayout, abi } from './artifacts/ERC20.json'; const log = debug('vulcanize:indexer'); @@ -111,12 +97,15 @@ export class Indexer { const logs = await this._ethClient.getLogs(vars); log(JSON.stringify(logs, null, 2)); + const erc20EventNameTopics = getEventNameTopics(abi); + const gqlEventType = invert(erc20EventNameTopics); + return logs - .filter(e => !name || ERC20_EVENT_NAME_TOPICS[name] === e.topics[0]) + .filter(e => !name || erc20EventNameTopics[name] === e.topics[0]) .map(e => { const [topic0, topic1, topic2] = e.topics; - const eventName = GQL_EVENT_TYPE[topic0]; + const eventName = gqlEventType[topic0]; const address1 = topictoAddress(topic1); const address2 = topictoAddress(topic2); @@ -124,12 +113,12 @@ export class Indexer { switch (eventName) { - case 'TransferEvent': { + case 'Transfer': { eventFields['from'] = address1; eventFields['to'] = address2; break; }; - case 'ApprovalEvent': { + case 'Approval': { eventFields['owner'] = address1; eventFields['spender'] = address2; break; @@ -138,7 +127,7 @@ export class Indexer { return { event: { - __typename: eventName, + __typename: `${eventName}Event`, ...eventFields }, proof: {