Build on Flow | Learn FCL - 8. How to Query Transaction by Id
Max Daunarovich
Posted on June 15, 2022
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);
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 });
}
After the execution you console should have this output:
txResult: {
blockId: "f21eb65a65cd8f2f26a340265c24497ee2ca130862e4c7e9d6f1e6d6680a2fe6",
status: 4,
statusString: "SEALED",
statusCode: 0,
errorMessage: "",
events: Array(7),
}
Each event will be in form of an object:
{
type: "A.2d4c3caffbeab845.FLOAT.FLOATMinted",
transactionId: "2297668a3f35d6c6b4a18bb9c5ea8d3e60ce7e4a4e0fe31ad5a9c623d002b9d7",
transactionIndex: 4,
eventIndex: 0,
data: Object,
}
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:
- transaction result - https://docs.onflow.org/fcl/reference/api/#returns-after-decoding-7
- events data - https://docs.onflow.org/fcl/reference/api/#event-object
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
- Flow View Source - https://flow-view-source.com/mainnet/tx/2297668a3f35d6c6b4a18bb9c5ea8d3e60ce7e4a4e0fe31ad5a9c623d002b9d7 - is a community project by OG of FCL - James Hunter - https://github.com/orodio
- FlowScan -https://flowscan.org/transaction/2297668a3f35d6c6b4a18bb9c5ea8d3e60ce7e4a4e0fe31ad5a9c623d002b9d7 - contrary to account information, transaction on Flowscan have the same data as Flow View Source
Until next time 👋
Resources
- Full Source Code - https://codesandbox.io/s/dev-to-fcl-08-fetch-transaction-by-id-44kccc?file=/src/index.js
-
Docs -
tx
function - https://docs.onflow.org/fcl/reference/api/#tx -
Docs -
transaction status
response - https://docs.onflow.org/fcl/reference/api/#returns-after-decoding-7 -
Docs -
event
data - https://docs.onflow.org/fcl/reference/api/#event-object
Other resources you might find useful:
- Flow Docs Site - https://docs.onflow.org/ - More detailed information about Flow blockchain and how to interact with it
- Flow Portal - https://flow.com/ - your entry point to Flow
- FCL JS - https://github.com/onflow/fcl-js - Source code and ability to contribute to the FCL JS library
- Cadence - https://docs.onflow.org/cadence/ - Introduction to Cadence
- Codesandbox - https://codesandbox.io - An amazing in-browser IDE enabling quick prototyping
Posted on June 15, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
August 29, 2022
June 22, 2022