Stream data decode

I am testing the Moralis streaming service and it seems wonderful to me, yesterday I tried the firebase ext and everything is perfect.

What I’m trying to do now is listen to the events of a smart contract on my same nodejs server, and for some reason the data is not decoded, I receive it as follows…

{
    confirmed: true,
    chainId: '0x61',
    streamId: 'e141eac1-27e3-42ba-969a-c741fe22268a',
    tag: 'shuffle',
    retries: 0,
    block: {
      number: '27956372',
      hash: '0x6a3355b861bed9054f7787f3014c4958f9aa0362074a731ecb2836b776301077',
      timestamp: '1678556848'
    },
    logs: [
      {
        logIndex: '8',
        transactionHash: '0xd2e361f702863964d89cf8d2bcc1c1f35e22866c04802b38c1dfc633ea361163',
        address: '0x85d7e4ae3412526fc6f78aeb43936543936ccfb8',
        data: '0x0000000000000000000000000000000000000000000000000000000000000005000000000000000000000000ffeecd3004013c08ac95227ba2c22e4331d2e9da00000000000000000000000000000000000000000000000034bc4fdde27c0000',
        topic0: '0x93a86c487b77bf92471eb14e91436a107887e1dfd4830179b6d599d97f3f891b',
        topic1: null,
        topic2: null,
        topic3: null
      }
    ],
    txs: [],
    txsInternal: [],
    erc20Transfers: [],
    erc20Approvals: [],
    nftTokenApprovals: [],
    nftApprovals: { ERC721: [], ERC1155: [] },
    nftTransfers: [],
    nativeBalances: []
}

I have read about it, it is time to decode the data but I see it as an additional step that we do not want to do, is there a chance that Moralis sends the data as it is stored in firebase?

thanks

this is all the data that you received?

you will need the abi to decode that log/event

Yes, they are all the data that I receive in the body, I have the abi, but how is the process?

Isn’t there a way for moralis to send the data already decoded?

there is a function in Moralis SDK to decode a log with an abi, it will use ethers library for that, you can also use ether library directly to decode a log.

try the moralis SDK, everything seems to be going well, it makes me the perfect decode of all the properties emitted in the event…

[
  {
    prop1: BigNumber { _hex: '0x06', _isBigNumber: true },
    addressBuy: '0xf4975efCcE9bAaA1E89A1F8b49011EE1A458F913',
    amount: BigNumber { _hex: '0x0de0b6b3a7640000', _isBigNumber: true },
    prop2: BigNumber { _hex: '0x0a', _isBigNumber: true },
    prop3: BigNumber { _hex: '0xb5', _isBigNumber: true }
  }
]

but there is a problem and that is that the properties below are undefined…

console.log(decodedLogs[0].name);

How do I get the name of the event?

the decode was called according to the documentation…

const decodedLogs = Moralis.Streams.parsedLogs(data)

I think that you have to know what event you expect to match. For example maybe by the name of the fields you could check what was the matched event, if it has prop1, then maybe is event 1.

ok, perfect, with your idea I managed to make a simple function and get the name from the abi.

Thanks for the help. I share the function in case someone needs it or has a notion.

export default function getEventName(abi: any[], events: Record<string, any>[]): string[] {
  const abiEvents = abi.filter((entry) => entry.type === 'event')
  
  return events.map((event) => {
    const keys = Object.keys(event)
    const abiEvent = abiEvents.find((e) => {
      const inspect = e.inputs.map((input) => input.name)

      return inspect.length === keys.length && keys.every((key) => inspect.includes(key))
    })
    
    if (!abiEvent) {
      throw new Error('Event not found')
    }

    return abiEvent.name
  })
}
2 Likes