Path: blob/main/docs/versioned_docs/version-v0.26/07-packages/cosmostxcollector.md
1007 views
---
---
cosmostxcollector
The package implements support for collecting transactions and events from Cosmos blockchains into a data backend and it also adds support for querying the collected data.
Transaction and event data collecting
Transactions and events can be collected using the cosmostxcollector.Collector
type. This type uses a cosmosclient.Client
instance to fetch the data from each block and a data backend adapter to save the data.
Data backend adapters
Data backend adapters are used to query and save the collected data into different types of data backends and must implement the cosmostxcollector.adapter.Adapter
interface.
An adapter for PostgreSQL is already implemented in cosmostxcollector.adapter.postgres.Adapter
. This is the one used in the examples.
Example: Data collection
The data collection example assumes that there is a PostgreSQL database running in the local environment containing an empty database named "cosmos".
The required database tables will be created automatically by the collector the first time it is run.
When the application is run it will fetch all the transactions and events starting from one of the recent blocks until the current block height and populate the database:
Queries
Collected data can be queried through the data backend adapters using event queries or cursor-based queries.
Queries support sorting, paging and filtering by using different options during creation. The cursor-based ones also support the selection of specific fields or properties and also passing arguments in cases where the query is a function.
By default no sorting, filtering nor paging is applied to the queries.
Event queries
The event queries return events and their attributes as []cosmostxcollector.query.Event
.
Example: Query events
The example reads transfer events from Cosmos' bank module and paginates the results.
Cursor-based queries
This type of queries is meant to be used in contexts where the Event queries are not useful.
Cursor-based queries can query a single "entity" which can be a table, view or function in relational databases or a collection or function in non relational data backends.
The result of these types of queries is a cursor that implements the cosmostxcollector.query.Cursor
interface.