This feature is in technical preview - feel free to try it, but there may be edge cases we haven’t caught yet. Feel free to contact support if you need any help!

Mirror pipeline transforms can be used to decode raw contracts during processing using a combination of the _gs_fetch_abi and _gs_evm_decode functions

Overview

To get decoded contract data on EVM chains in a Mirror pipeline, there are three options:

  1. Decode data with a subgraph, then use a subgraph entity source.
  2. Use the decoded_logs and decoded_traces direct indexing datasets. These are pre-decoded datasets, with coverage for common contracts, events, and functions.
  3. Use the raw_logs dataset and decode inside a pipeline transform.

Decoding with transforms

Mirror provides custom functions for decoding.

This is full example definition which will define a pipeline that decodes friendtech events from raw logs.

sources:
  - type: dataset
    referenceName: base.raw_logs
    version: 1.0.0
transforms:
  - referenceName: friendtech_decoded
    type: sql
    primaryKey: id
    # Fetch the ABI from basescan, then use it to decode from the friendtech address.
    sql: >
      select 
        `id`,
        _gs_evm_decode(
            _gs_fetch_abi('https://api.basescan.org/api?module=contract&action=getabi&address=0xcf205808ed36593aa40a44f10c7f7c2f67d4a4d4', 'etherscan'), 
            `topics`, 
            `data`
        ) as `decoded`, 
        block_number, 
        transaction_hash 
      from base.raw_logs
      where address='0xcf205808ed36593aa40a44f10c7f7c2f67d4a4d4'
  - referenceName: friendtech_clean
    primaryKey: id
    type: sql
    # Clean up the previous transform, unnest the values from the `decoded` object.
    sql: >
      select 
        `id`, 
        decoded.event_params as `event_params`, 
        decoded.event_signature as `event_signature`,
        block_number,
        transaction_hash
      from friendtech_decoded 
      where decoded is not null
sinks:
  - referenceName: friendtech_events
    secretName: EXAMPLE_SECRET
    type: postgres
    sourceStreamName: friendtech_clean
    schema: decoded_events
    table: friendtech

_gs_evm_decode

_gs_evm_decode can decode data given a json string representing the ABI string.

It will automatically use the matching event in the ABI, but partial ABIs that only contain the target event will also work.

select _gs_evm_decode(abi, topics, data) as result
from ethereum.raw_log
where address = '0x...'

Using a WHERE clause on address or another topic is highly recommended to reduce the amount of resources needed by the pipeline

The function will output a nested ROW type with event_param::TEXT[] and event_signature::TEXT. If you’re planning on using a sink that doesn’t support nested ROWs, you may want to do a further transformation to unnest the result.

If above transformation was named decoded_logs:

select result.event_params as event_params, result.event_signature as event_signature
from decoded_logs

Parameters

abi
string

A string representing a json array of events and functions

topics
string

A string containing the topics, comma separated.

data
string

A string containing the payload of the event Example:

This function is stateless, and will scale very well across different pipeline sizes.

_gs_fetch_abi

We provide a convenient function for fetching ABIs, as often they are too big to copy and paste into the yaml definition.

There is an option for fetching from etherscan-like sites, like bscscan, https://optimistic.etherscan.io/, or basescan.

It’s highly recommended to include your own etherscan API key if you are using this function with a large pipeline. The amount of workers may result in the API limits being surpassed.

select
    # Call the EVM decoding function
    _gs_evm_decode(
        # This fetches the ABI from basescan, a `etherscan` compatible site.
        _gs_fetch_abi('https://api.basescan.org/api?module=contract&action=getabi&address=0xcf205808ed36593aa40a44f10c7f7c2f67d4a4d4', 'etherscan'),
        `topics`,
        `data`
    ) as `decoded`,
from base.raw_logs

Parameters

url
string

The URL from where the ABI will be fetched

type
string

The type of url - be either etherscan for etherscan-compatible APIs, or raw for json array ABIs.