Skip to content

Commit

Permalink
improved log parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz committed Dec 8, 2022
1 parent cfebb16 commit aa86dc0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-lobsters-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/streams': patch
---

The `LogParser` class supports now a case when the parser from the `@ethersproject/abi` package returns an instance of the `Indexed` class.
47 changes: 47 additions & 0 deletions packages/streams/src/mapping/logs-processor/LogParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,51 @@ describe('LogParser', () => {
expect(params['expires'].type).toBe('uint256');
expectBigNumber(params['expires'], '1696950755');
});

it('reads log correctly when internally @ethersproject/abi return an instance of Indexed class', () => {
const log: Log = {
logIndex: '10',
transactionHash: '0x51ece41e15b9bec389ecdba5fc42366e40904ee0a7588a8bd0731f625c5fd0a1',
address: '0xca052c2c5ca7cf81c5d47ca6f1dcd5803d491365',
data: '0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000183633393139666535313833326161656138396637633733360000000000000000',
topic0: '0x5cc8a983927b60e71d90ba4fbf642eb3e461d4eeefca2c1e5789a99d0f1263f1',
topic1: '0x000000000000000000000000a71c821a533fbc68247e729908d485abc14729c1',
topic2: '0xdd59c5286381d1e7091e99aa5732fa35b875b732751f8764ff13f675c48acb25',
topic3: null,
};
const abiItems: AbiItem[] = [
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: 'string',
name: 'id',
type: 'string',
},
{
indexed: true,
internalType: 'address',
name: 'chef',
type: 'address',
},
{
indexed: true,
internalType: 'string',
name: 'rewardNFT',
type: 'string',
},
],
name: 'NewNFTChefContract',
type: 'event',
},
];

const { name, params } = new LogParser(abiItems).read(log);

expect(name).toBe('NewNFTChefContract');
expect(params['id'].value).toBe('63919fe51832aaea89f7c736');
expect(params['chef'].value).toBe('0xA71c821A533FBc68247e729908D485AbC14729c1');
expect(params['rewardNFT'].value).toBe('0xdd59c5286381d1e7091e99aa5732fa35b875b732751f8764ff13f675c48acb25');
});
});
21 changes: 13 additions & 8 deletions packages/streams/src/mapping/logs-processor/LogParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigNumber } from 'ethers';

import { AbiItem, Log } from '@moralisweb3/streams-typings';
import { JsonFragment, Interface } from '@ethersproject/abi';
import { JsonFragment, Interface, Indexed } from '@ethersproject/abi';

export interface ParsedLog {
name: string;
Expand All @@ -26,22 +26,27 @@ export class LogParser {
// Solidity supports max 3 topics. https://docs.soliditylang.org/en/latest/contracts.html#events
const topics = [log.topic0, log.topic1, log.topic2, log.topic3].filter((t) => t !== null) as string[];

const result = this.abiInterface.parseLog({
data: log.data,
topics,
});
// Do not call the `this.abiInterface.parseLog()` method here! The @ethersproject/abi package (5.7.0) has a bug,
// that doesn't return `args` with named keys in a specific case. That problem doesn't occur when we call directly the decodeEventLog() method.

const eventFragment = this.abiInterface.getEvent(topics[0]);
const args = this.abiInterface.decodeEventLog(eventFragment, log.data, topics);

const params: Record<string, LogParam> = {};

for (const input of result.eventFragment.inputs) {
for (const input of eventFragment.inputs) {
let value = args[input.name];
if (value instanceof Indexed) {
value = value.hash;
}
params[input.name] = {
type: input.type,
value: result.args[input.name],
value,
};
}

return {
name: result.name,
name: eventFragment.name,
params,
};
}
Expand Down
1 change: 0 additions & 1 deletion packages/streams/src/methods/bulkLogParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ const webhookDataNoAbi: Partial<IWebhook> = {
describe('logDecoder', () => {
it('should decode logs', () => {
const decodedLogs: any = parseLog(webhookData as IWebhook);
console.log('decodedLogs', decodedLogs);

expect(decodedLogs[0].from).toEqual('0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc');
expect(decodedLogs[0].to).toEqual('0x88ff79eB2Bc5850F27315415da8685282C7610F9');
Expand Down

0 comments on commit aa86dc0

Please sign in to comment.