mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-02-08 11:02:52 +00:00
Add method to get event name topics from abi.
This commit is contained in:
parent
13ec526ebd
commit
7d7cf26f81
@ -1 +1,3 @@
|
||||
export { getStorageValue, getStorageInfo, StorageLayout, GetStorageAt } from './storage';
|
||||
|
||||
export { getEventNameTopics } from './logs';
|
||||
|
26
packages/solidity-mapper/src/logs.ts
Normal file
26
packages/solidity-mapper/src/logs.ts
Normal file
@ -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;
|
||||
}, {})
|
||||
}
|
@ -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: {
|
||||
|
Loading…
Reference in New Issue
Block a user