How to read contract logs

Given a transaction id from the past, how to get its logs and decode them? Is it necessary to provide the contract abi for that? Please give an example, it’s not clear from the docs

1 Like

The ABI is essential because it defines the structure and types of the logs, enabling proper decoding.
Here’s an example of how you can achieve this using TS SDK:

// Import necessary modules
import { Contract, TransactionResponse } from 'fuels-ts';
import abi from './path-to-your-abi.json';

// Your transaction ID
const transactionId = '0x...'; // Replace with your actual transaction ID

// Retrieve the transaction response from the transaction ID
const transactionResponse = await TransactionResponse.create(transactionId, provider);

// Decode the logs using the ABI
const logs = transactionResponse.decodeLogs(abi);

// Access and process the logs
logs.forEach(log => {
  console.log(log);
});

lmk if this is what you’re looking for

1 Like

If this is a task you want to continually do to perhaps monitor certain acivity, perform analytics, or hydrate a front-end, you may want to consider using an Indexer that is designed for this exact use case.

Envio lets you do this with a simple no-code quickstart that you can evolve to suit your own needs :+1:

1 Like