DKG Python SDK (dkg.py)
Python library for interacting with the DKG
If you are looking to build applications leveraging Knowledge Assets on the OriginTrail Decentralized Knowledge Graph (DKG), the dkg.py library is the best place to start!
The DKG SDK is used together with an OriginTrail gateway node to build applications that interface with the OriginTrail DKG (the node is a dependency). Therefore, you either need to run a gateway node on your local environment or a hosted OT-node, in order to use the SDK.
Prerequisites
python ≥ 3.11
poetry ≥ 1.8.5
Installation
The library can be used in any Python application.
Run the command to install dkg.py library using pip:
pip x:
or poetry:
Then import DKG, BlockchainProvider, and NodeHTTPProvider classes inside your project:
🏂 Quickstart
In this package, there are both synchronous and asynchronous versions of the DKG client.
The synchronous client is designed for applications where blocking calls are acceptable. It operates sequentially, making it simpler to integrate into existing codebases that do not use asynchronous programming.
The asynchronous client is built for non-blocking operations, making it ideal for scenarios where multiple tasks need to run concurrently. It is generally faster than the synchronous client.
Synchronous DKG client
To use the Synchronous DKG library, you need to connect to a running local or remote OT-node.
Asynchronous DKG client
The asynchronous DKG client leverages Python's asyncio
library for managing asynchronous operations. Below is an example of how to set up and use the asynchronous DKG client:
Make sure to create an .env file and add the PRIVATE_KEY variable to it so that the blockchain provider can pick it up.
Blockchain networks
The system supports multiple blockchain networks, which can be configured using the BlockchainIds
constants. You can select the desired blockchain by specifying the corresponding constant. The available options are:
DKG mainnet options:
Base: base:8453
Gnosis: gnosis:100
Neuroweb: otp:2043
DKG testnet options:
Base: base:84532
Gnosis: gnosis:10200
Neuroweb: otp:20430
DKG devnet options:
Base: base:84532
Gnosis: gnosis:10200
Neuroweb: otp:2160
Local options:
Hardhat1: hardhat1:31337
Hardhat2: hardhat2:31337
Create a knowledge collection
In this example, let’s create an example knowledge collection representing a city. The content contains both public and private assertions. Public assertions will be exposed publicly (replicated to other nodes), while private ones won't (stay on the node you published to only). If you have access to the particular node that has the data, when you search for it using get or query, you will see both public and private assertions.
When you create the knowledge collection, the above JSON-LD object will be converted into an assertion. When an assertion with public data is prepared, we can create a Knowledge Asset on the DKG. epochs_number
specifies how many epochs the asset should be kept for (an epoch is equal to three months).
To use the synchronous version, just remove the await (this applies for any function call you see in the rest of this document)
The complete response of the method will look like:
If you want to create multiple different assets, you can increase your allowance. Then, each time you initiate a publish, the step of calling the blockchain to increase your allowance will be skipped, resulting in a faster publishing time.
After you've finished publishing data to the blockchain, you can decrease your allowance to revoke the authorization given to the contract to spend your tokens. If you want to revoke all remaining authorization, it's a good practice to pass the same value that you used for increasing your allowance.
Read Knowledge Asset data from the DKG
To read Knowledge Asset data from the DKG, we utilize the get protocol operation.
In this example, we will get the latest state of the Knowledge Asset we published previously:
The response of the get operation will be the assertion graph:
Querying Knowledge Asset data with SPARQL
Querying the DKG is done by using the SPARQL query language, which is very similar to SQL applied to graph data.
(If you have SQL experience, SPARQL should be relatively easy to get started with. More information can be found here).
Let’s write a simple query to select all subjects and objects in the graph that have the Model property of Schema.org context:
The returned response will contain an array of n-quads:
As the OriginTrail node leverages a fully fledged graph database (a triple store supporting RDF), you can run arbitrary SPARQL queries on it.
More on types of interaction with the DKG SDK
We can divide operations done by SDK into 3 types:
Node API request
Smart contract call (non-state-changing interaction)
Smart contract transaction (state-changing interaction)
Non-state-changing interactions with smart contracts are free and can be described as contract-getters. They don’t require transactions on the blockchain. This means they do not incur transaction fees.
Smart contract transactions are state-changing operations. This means they change the state of the smart contract memory, which requires some blockchain-native gas tokens (such as ETH, NEURO, etc.).
In order to perform state-changing operations, you need to use a wallet funded with gas tokens.
You can use default keys from the example below for hardhat blockchain:
The default keys above should not be used anywhere except in a local environment for development.
Last updated
Was this helpful?