Chain-Data Query
Learn how to connect to Filecoin RPC nodes and query Filecoin chain state and data.
Connecting to Filecoin networks via public RPC nodes
To query chain state and data on any Filecoin network, it is necessary to connect to public node providers. However, it's important to note that most public node providers offer limited access, typically allowing read-only JSON RPC calls and MPoolPush
to send signed messages to the Filecoin networks.
To explore further details about the available public RPC providers supporting Filecoin mainnet and Calibration testnet, you can refer to the following page.
Ingredients
Let's use Glif nodes as an example to demonstrate how to connect to a public Filecoin RPC node provider. Additionally, we will utilize ethers.js
to establish the connection with the RPC nodes.
Instructions
We will use ethers.js
to establish a connection with the public Filecoin node provided by Glif. The following code demonstrates connecting to the Filecoin Calibration testnet as an example.
The expected output:
Listen to smart contract events
Since the Filecoin Virtual Machine (FVM) is EVM-compatible, we can use ethers.js
to listen to smart contract events for specific contract actions on the Filecoin network. For instance, we can monitor ERC20 token transfer
events or client contract DealProposalCreate
events.
Ingredients
We will also use ethers.js
to connect to the public Glif node to listen to the smart contract events.
Instructions
Let's consider the wFIL contract, an ERC-20 token on Filecoin, as an example for listening to its transfer event. To demonstrate how to listen to smart contract events using ethers, we will use the deployed wFIL token address on the Filecoin calibration network and a simplified ABI object for the transfer event. Typically, you would have the wFIL smart contract's Application Binary Interface (ABI) defined in an abi.json
file.
The code to listen to transfer
events for the wFIL token.
Once a wFIL token transfer is executed on the blockchain, the following code snippet will capture the corresponding events and print out the event details.
Filter smart contract events
We can also use filters to retrieve specific smart contract transactions from the Filecoin network. Filters enable us to define criteria or conditions to search for event logs that match specific requirements. By setting up a filter, we can monitor and retrieve event logs related to our interests or specific smart contracts.
Ingredients
We will also use ethers.js
to connect to the public Glif node to filter the smart contract events by providing conditions.
Instructions
Here's an example of how you can connect to a Glif node on the calibration network, create a filter to list all wFIL token transfers from your address, and execute the filter to look back 2000 blocks to find the matched transaction list:
The expected transaction will be similar as follows. With this information, you can develop custom logic to efficiently track and process specific events or blocks on your FEVM smart contracts.
Last updated