Skip to main content

kwil.py

PyPI - Version PyPI - Python Version

Borrows heavily from web3.py



What is Kwil?

Kwil is a blockchain-based database platform, while keep being blockchain agnostic on Account. To use Kwil.py, you need to do the following:

  1. Create a Ethereum account
  2. Fund the account with some required TOKEN of your kwil provider
  3. Create database schema, through Kwil Kuneiform IDE. From there, you can get compiled schema bytes.
  4. Deploy the database schema to the blockchain.
  5. Create/Update data through predefined Actions.

Installation

pip install kwil

Supported python version:

  • 3.9
  • 3.10

Configuration

Providers

Providers are used to interact with the blockchain. They are responsible for sending and receiving RPC requests. There are following builtin providers:

  • GRPCProvider - connects to a remote gRPC based server

Private Key

Kwil now support Ethereum private key to sign transaction. This key is also associated with an Ethereum account which needed to be funded to use Kwil.

API

There are examples that shows how to use these API.

helpers

  • Kwil.generate_dbi(owner_addr, dataset_name)
  • Kwil.load_wallet(private_key)

high level API

  • kwil.deploy_database(payload) - deploys a new database, payload is compiled bytes of schema
  • kwil.get_schema(db_identifier) - returns the database schema
  • kwil.list_databases(OPTIONAL[owner_address]) - returns the list of databases under current account
  • kwil.drop_database(dataset_name) - drops the database under current account
  • kwil.execute_action(db_identifier, action_name, inputs) - executes the action on the database
  • kwil.query(db_identifier, query) - executes query(ad-hoc SQL) on the database

low level API (Kwil.kwild API)

Fetching data

  • Kwil.kwild.ping()
  • Kwil.kwild.get_config() - returns the configuration of the node
  • Kwil.kwild.get_schema(DBIdentifier) - returns the dataset schema info
  • Kwil.kwild.get_account(Adddress) - returns the account info(nonce, balance, etc)
  • Kwil.kwild.estimate_price(TxParams) - returns the estimated price for the transaction
  • Kwil.kwild.query(DBIdentifier, Query) - returns the query(ad-hoc SQL) result
  • Kwil.kwild.list_databases() - returns the list of databases under current account

Making transactions

  • Kwil.kwild.broadcast(TxParams) - broadcasts the transaction to the network

Usage

There are more examples in examples folder.

Connect to Kwil node

Kwil.py will connect to a Kwil node through a provider. Currently, only GRPC provider is supported.

>>> from kwil import Kwil
>>> kwil = Kwil(Kwil.GRPCProvider("localhost:50051"),
                Kwil.load_wallet("YOUR_ETH_PRIVATE_KEY"))
>>> kwil.is_connected()
True

Deploy Database

Assume we have our schema defined and compiled(./test_db.json). We can deploy the schema to the blockchain.

First, create a provider using Kwil.GRPCProvider.

Then, create a Kwil instance, can call deploy_database to deploy the schema to the blockchain.

Finally, call list_database to list all the databases under current account to verify it's there.

import logging

from kwil import Kwil


# assume that account has enough fund
# here we use examples/test_db.kf as our dataset schema, we'll use examples/test_db.json(compiled schema)

# create client
client = Kwil(Kwil.GRPCProvider("localhost:50051"),
              Kwil.load_wallet("YOUR_ETH_PRIVATE_KEY"))

# create dataset
with open("./test_db.json", "r") as f:
    schema_json = f.read()
    try:
        client.deploy_database(schema_json.encode("utf-8"))
    except Exception as e:
        logging.exception(e)

# list dataset
dbs = client.list_databases()
print("datasets after create: ", dbs)

Working with Actions

# execute an action
db_name = "testdb"  # The name of the database, from the schema file
db_id = Kwil.generate_dbi(client.wallet.address, db_name)

action = "create_user"
tx_receipt = client.execute_action(db_id,
                                   action,
                                   # `create_user` is defined in examples/test_db.kf
                                   [{"$id": 1, "$username": "aha", "$age": 18}])
result = tx_receipt["result"]
print("create user result: ", result)

# execute a pre-defined query through action
action = "list_users"
tx_receipt = client.execute_action(db_id, action, [])
result = tx_receipt["result"]
print("list user result:", result)

Let's extend the example above. We can call execute_action to execute an action on the database. In our schema(examples/test_db.kf), we have

action create_user($id, $username, $age) public {
    INSERT INTO users (id, username, age, wallet)
    VALUES ($id, $username, $age, @caller);
}

create_user is a public action, so we can call it without any permission.

To call this action, we need to provide the dataset identifier, action name and inputs.

tx_receipt = client.execute_action(db_id,
                                   action,
                                   [{"$id": 1, "$username": "aha", "$age": 18}])

Here, we only create one user, the inputs is just a dict of the input parameters with values.

License

MIT

Metadata

Release files for kwil 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kwil 0.1.0
File Size Uploaded
kwil-0.1.0.tar.gz 23.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kwil 0.1.0
File Interpreter ABI Platform
kwil-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 59.4 kB

Release files / kwil-0.1.0.tar.gz

Download URL kwil-0.1.0.tar.gz
Size 23.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f8708fd92b0f77c07ea1a93d5630bf46390452882bd5161938691bfff097b9a6
BLAKE2b-256 checksum
How to use checksums
b061d2790441d99277e70aeb9ef6f575a1347d54fee87fb2bf9f64db94374e18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.1

Release files / kwil-0.1.0-py3-none-any.whl

Download URL kwil-0.1.0-py3-none-any.whl
Size 35.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa58d4a68aaa1bae0981cf06f5c4390a5aed0327ca124c1e21dc99aa3b175801
BLAKE2b-256 checksum
How to use checksums
cd47220f01334c31422eebdf846645c90e61c55bdfb3e7379277d197b129a7e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page