How to decode events to readable string with ceramic-one?

Hello,

I am following the following link where I am able to get the event data, but looks like I would need to decode it in order to read it properly.

How could I decode it to read it? Is there any method I could use?

I saw that @ceramic-sdk/events have some methods, but did not see anything relevant at simple sight. Maybe eventToString, but not sure which codec is used as parameter.

Thanks in advance!

I figured out.

In order to decode events it is needed to use “codeco” library and follow this method explained in ceramic-one consume documentation and also dagCbor package.

The following code works for me, but the Decoder I am using as SampleDecoder, it could be anything, and it would work and return all the events. Am I doing something wrong here?

Now in order to filter the events I am mainly filtering the decoded information (content), but I guess that I should be able to filter events beforehand using the SampleDecoder and SamplePayload, because I am also getting other events with different structure as I registered interests for different models.

Thanks in advance!

This is the code I used as sample:

const ceramic = new CeramicClient({ url: "URL_TO_YOUR_CERAMIC_NODE" });
const { events } = await ceramic.getEventsFeed();

await new Promise(resolve => setTimeout(resolve, 8000));

console.log(`Found ${events.length} events\n`);

// Schema of the data of your 
type SamplePayload = {
  cid: string;
  timestamp: number;
  comment?: string;
};

const SampleDecoder: Decoder<unknown, SamplePayload> = strict({
  cid: string,
  timestamp: number,
  comment: optional(string),
});

for (const event of events) {
  try {
    const decoded = await ceramic.getEventType(SampleDecoder, event.id);
    const content = dagCbor.decode(decoded.linkedBlock);

    console.log(`✅ Event ID: ${event.id}`);
    console.log(`Decoded Payload:`, content);
    console.log('-------------------------');
  } catch (error) {
    console.warn(`❌ Failed to decode event ${event.id}:`, error.message || error);
  }
}
1 Like

Oh, cool. Apologies, I missed this message.

Glad you figured it out!! :smile:

Just minor thing @mohsin, how would you filter the events?

For example, my use case is a model with accountRelation list, and I have different instances then.

I would like to filter the events per instance, instead of getting all the events from all the different instances.

Is there a way to do so?

I found the following code in the C1 node api (code below), but for sepValue I need to parse the Uint8Array to a string, what base should it be? Base36, base58…

"/experimental/events/{sep}/{sepValue}":
    options:
      summary: cors
      parameters:
        - name: sep
          in: path
          description: Name of the field in the Events header that holds the separator value e.g. 'model'
          schema:
            type: string
          required: true
        - name: sepValue
          in: path
          description: The value of the field in the Events header indicated by the separator key e.g. multibase encoded model ID
          schema:
            type: string
          required: true
      responses:
        "200":
          description: cors
    get:
      summary: Get events matching the interest stored on the node
      parameters:
        - name: sep
          in: path
          description: Name of the field in the Events header that holds the separator value e.g. 'model'
          schema:
            type: string
          required: true
        - name: sepValue
          in: path
          description: The value of the field in the Events header indicated by the separator key e.g. multibase encoded model ID
          schema:
            type: string
          required: true
        - name: controller
          in: query
          description: the controller to filter (DID string)
          required: false
          schema:
            type: string
        - name: streamId
          in: query
          description: the stream to filter (multibase encoded stream ID)
          required: false
          schema:
            type: string
        - name: offset
          in: query
          description: token that designates the point to resume from, that is find keys added after this point
          schema:
            type: integer
          required: false
        - name: limit
          in: query
          description: the maximum number of events to return, default is 10000.
          required: false
          schema:
            type: integer
      responses:
        "200":
          description: success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/EventsGet"
        "400":
          description: bad request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/BadRequestResponse"
        "500":
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"

Thanks a lot!