DEV Community

Max Daunarovich for Flow Blockchain

Posted on

Build on Flow | Learn FCL - 8. How to Query Transaction by Id

Overview

In this short guide, you will learn how to:

  • query transaction data by the transaction id
  • process and analyze acquired data

Short, but straight to the point! 😀

Why?

After a transaction is submitted to the chain, you will get the corresponding id, which anyone can use to query the transaction data of that specific transaction. For example, if you are transferring some tokens to another account, you can also provide the transaction ID as a proof that you have completed the task on your side and now recipient can check status updates, using that transaction.

How?

FCL provides a function [tx](https://docs.onflow.org/fcl/reference/api/#tx) that lets you subscribe to tranaction status updates (via polling), but subsequentially also returns finalized result if the status of transaction is sealed . tx function will return an object and one of the fields is .onceSealed() method. When called this method will return a Promise, which will resolve if the transaction with specified id have status equal to 5 - sealed.

Sealed again? 🤔

Similar to blocks, transactions also have different statuses:

  • 1 - pending - transaction awaits for finalization
  • 2 - finalized - transaction awaits execution
  • 3- executed - transaction awaits sealing
  • 4 - sealed - transaction is sealed and could be viewed as complete and irreversable
  • 5 - expired - transaction expired

You’ve read that right, transaction can expire! When transaction is created, it’s “anchored” to the latest available block and need to be sealed within 600 blocks range (or around 10 minutes of human perceived time 👨‍🏫)

Now let’s try it on practice! 💪

Step 1 - Installation

Add "@onflow/fcl": "1.0.0" as your dependency

Step 2 - Setup

Just like the last time we will import necessary methods and setup FCL:

// Import methods from FCL
import { tx, config } from "@onflow/fcl";

// Specify the API endpoint - this time we will use Mainnet
const api = "https://rest-mainnet.onflow.org";

// Configure FCL to use mainnet as the access node
config().put("accessNode.api", api);
Enter fullscreen mode Exit fullscreen mode

Finally

As always we will wrap the code in the IIFE block for automatic execution:

// We will use IIFE to execute our code right away
(async () => {
  console.clear();

    // This is just a random transaction id we've got from Flowscan live feed
    const id = "2297668a3f35d6c6b4a18bb9c5ea8d3e60ce7e4a4e0fe31ad5a9c623d002b9d7";

  const txResult = await tx(id).onceSealed();
  console.log({ txResult });
}
Enter fullscreen mode Exit fullscreen mode

After the execution you console should have this output:

txResult: {
    blockId: "f21eb65a65cd8f2f26a340265c24497ee2ca130862e4c7e9d6f1e6d6680a2fe6",
    status: 4,
    statusString: "SEALED",
    statusCode: 0,
    errorMessage: "",
    events: Array(7),
}
Enter fullscreen mode Exit fullscreen mode

Each event will be in form of an object:

{
    type: "A.2d4c3caffbeab845.FLOAT.FLOATMinted",
    transactionId: "2297668a3f35d6c6b4a18bb9c5ea8d3e60ce7e4a4e0fe31ad5a9c623d002b9d7",
    transactionIndex: 4,
    eventIndex: 0,
    data: Object,
}
Enter fullscreen mode Exit fullscreen mode

If you take a look at the name of event type of different events, you can clearly see a pattern:

  • A - stands for Account
  • 2d4c3caffbeab845 - this is the address where contract holding event declaration is deployed
  • FLOAT - name of the contract
  • FLOATMinted - name of the event

We can create a link and explore this contract on Flow View Source. Line 53 have the definition of FLOATMinted event.

FCL “Quick Reference” on Docs Site have more information about both transaction and events:

Other Ways to Explore Transaction

In similar way to account, as we covered last time, we can explore transaction data on FlowScan and Flow View Source

Until next time 👋

Resources

Other resources you might find useful:

Top comments (0)