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:
pipinstalldkg
pip x:
pipxinstalldkg
or poetry:
poetryadddkg==8.0.1
Then import DKG, BlockchainProvider, and NodeHTTPProvider classes inside your project:
from dkg import DKGfrom dkg.providers import BlockchainProvider, NodeHTTPProvider
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.
from dkg.providers import BlockchainProvider, NodeHTTPProvider
node_provider = NodeHTTPProvider(endpoint_uri="http://localhost:8900", api_version="v1")
blockchain_provider = BlockchainProvider(
Environments.DEVELOPMENT.value, # or TESTNET, MAINNET
BlockchainIds.HARDHAT_1.value,
)
dkg = DKG(node_provider, blockchain_provider)
print(dkg.node.info)
# if successfully connected, this should print the dictionary with node version
# { "version": "8.X.X" }
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:
import asyncio
from dkg.providers import AsyncBlockchainProvider, AsyncNodeHTTPProvider
from dkg import AsyncDKG
async def main():
node_provider = AsyncNodeHTTPProvider(
endpoint_uri="http://localhost:8900",
api_version="v1",
)
# make sure that you have PRIVATE_KEY in .env so the blockchain provider can load it
blockchain_provider = AsyncBlockchainProvider(
Environments.DEVELOPMENT.value,
BlockchainIds.HARDHAT_1.value,
)
dkg = AsyncDKG(
node_provider,
blockchain_provider,
config={"max_number_of_retries": 300, "frequency": 2},
)
if __name__ == "__main__":
asyncio.run(main())
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).
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.
dkg.asset.decrease_allowance(1569429592284014000)
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:
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 costs some amount of 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: