txpipe / python-dbsync-starter-kit

A set of python scripts that query the DB-Sync instance running on Demeter's cluster to generate different data analysis outputs from the ledger history.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python-dbsync Starter-kit

This Starter-Kit is composed of a set of python scripts that query the DB-sync instance running on Demeter.run cluster to generate different data analysis outputs from the ledger history.

We have implemented as an example some of the queries available in input-output-hk /interesting-queries.md. This kit should be easy to extend with additional functionality.

Dev Environment

For executing the scripts in this starter kit you'll need access to a running DB-sync instance in sync with a Node running on the network of your preference.

In case you don't want to install the required components yourself, you can use Demeter.run platform to create a cloud environment with access to common Cardano infrastructure. The following command will open this repo in a private, web-based VSCode IDE with access to a running DB-Sync instance in the preview network.

Code in Cardano Workspace

What is Included

We have included 6 python scripts with an specific use-case in each one of them:

Name Description
query-current-valid-pools get the latest pool registration for every pool that is still valid together with its hash
query-latest-slot-no Queries the latest available slot number
query-total-supply This just queries the UTxO set for unspent transaction outputs. It does not include staking rewards that have have not yet been withdrawn. Before being withdrawn rewards exist in ledger state and not on-chain
tx-fee-tx-hash Transaction fee for specified transaction hash in the preview network
tx-in-tx-hash Transaction inputs for specified transaction hash
tx-out-tx-hash Transaction outputs for specified transaction hash

Running the scripts

Each script executes a simple query to DB-Sync and outputs the result in the terminal. The database connection is established in the file connection.py.

We are going to use a library called psycopg2 for interfacing with the postgres SQL instance of DB-Sync.

The first step would be to install the dependencies. For doing that open a new terminal in the Development workspace and execute the following command:

pip install psycopg2-binary

As you might have noticed, we installed the psycopg2-binary package, which is the binary version of Psycopg2. What this means is that this version of the library comes with its own version of C libraries, namely liboq and libssl. For Psycopg2 beginners and most users, this version is perfectly fine

Now we are going to use the DB-Sync feature of Demeter.run for establishing a connection.

All the required inputs for establishing a connection are environment values available in the Workspace. We take advantage of this in connection.py where we establish the database connection.

conn = psycopg2.connect(database="cardanodbsync",
                        host=os.environ['DBSYNC_POSTGRESQL_HOSTNAME'],
                        user=os.environ["DBSYNC_POSTGRESQL_USER"],
                        password=os.environ["DBSYNC_POSTGRESQL_PASSWORD"],
                        port=os.environ["DBSYNC_POSTGRESQL_PORT"])

For running a script you can run it from the terminal with the python3 command and the name of the script:

python3 query-current-valid-pools.py

Feel free to navigate the DB-Sync schema inside of the Demeter.run features list for extending this kit functionality with additional queries.

schema

About

A set of python scripts that query the DB-Sync instance running on Demeter's cluster to generate different data analysis outputs from the ledger history.

License:MIT License


Languages

Language:Python 100.0%