getEvents method which allows you to query events from a smart contract. However, the data retention window for these events is 7 days at most. If you need access to a longer-lived record of these events you’ll want to “ingest” the events as they are published, maintaining your own record or database as events are ingested.
There are many strategies you can use to ingest and keep the events published by a smart contract. Among the simplest might be using a community-developed tool such as Mercury which will take all the infrastructure work off your plate for a low subscription fee. Or, indexers can provide ingested event data for affordable prices.
Another approach we’ll explore here is using a cron job to query Stellar RPC periodically and store the relevant events in a locally stored SQLite database. We are going to use an Object Relational Mapper (ORM), allowing us to write database query directly in Python or JavaScript.
Setup
- Python
In a virtual environment, install the Python dependencies:
Setup the Database Client
- Python
- JavaScript
To access the database, we will use SQLAlchemy, which is a frequently used Python library to query database.We are going to ingest events in a table named We will use an in-memory-only SQLite database for this guide, but thanks to the use of an ORM, we could be using any other supported database. We would simply need to change the connection string.
StellarEvent. In SQLAlchemy, this translates into a class, also called a database model:Using a database model is very convenient as it allows us to control the database schema programmatically. If we need to change the schema, by adding a new columns for instance, then using an ORM allows us to use very powerful migration tools.
Query Events from Stellar RPC
First, we’ll need to query the events from Stellar RPC. This simple example makes an RPC request using thegetEvents method, filtering for all transfer events that are emitted by the native XLM contract.
We are making some assumptions here. We’ll assume that your contract sees enough activity, and that you are querying for events frequently enough that you aren’t in danger of needing to figure out the oldest ledger Stellar RPC is aware of. The approach we’re taking is to find the largest (most recent) ledger sequence number in the database and query for events starting there. Your use-case may require some logic to determine what the latest ledger is, and what the oldest ledger available is, etc.
- Python
- JavaScript
If we start from scratch, there is no known ledger so we can try to ingest roughly the last 7 days assuming a ledger closes every 6s.Later on, we will be able to start from the latest ingested ledger by making a query to our DB.Let’s get events from Stellar RPC!
Store Events in the Database
Now, we’ll check if theevents object contains any new events we should store, and we do exactly that. We’re storing the event’s topics and values as base64-encoded strings here, but you could decode the necessary topics and values into the appropriate data types for your use-case.
- Python
- JavaScript
Run the Script with Cron
A cron entry is an excellent way to automate this script to gather and ingest events every so often. You could configure this script to run as (in)frequently as you want or need. This example would run the script every 24 hours at 1:14 pm:- Python
- JavaScript
- Python
- JavaScript