Skip to main content

Python SDK for Kuru's Central Limit Orderbook (CLOB)

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Kuru Python SDK

A Python SDK for interacting with the Kuru protocol, enabling market makers to manage orders, interact with margin accounts, and query exchange data.

Create a venv

python -m venv kuru-venv

Installation

To install the Kuru SDK, you can use pip:

pip install kuru-sdk

Getting Started

This SDK provides tools to interact with the Kuru order book and margin accounts primarily through Web3 and WebSocket connections for real-time order execution. It also offers a basic REST API client for querying data.

Prerequisites

  • Python 3.8+
  • A Monad RPC URL (e.g., from Infura, Alchemy, or a local node)
  • A private key for the wallet interacting with the Kuru contracts.

Configuration

The SDK often requires environment variables for configuration. Create a .env file in your project root:

RPC_URL=YOUR_ETHEREUM_RPC_URL
PK=YOUR_WALLET_PRIVATE_KEY
# Optional: WebSocket URL if different from default
WEBSOCKET_URL=wss://ws.testnet.kuru.io

Basic Usage: Placing Orders

Here's a simplified example of connecting to the WebSocket and placing a batch of orders using the ClientOrderExecutor:

import asyncio
import os
from web3 import Web3
from dotenv import load_dotenv
from kuru_sdk import ClientOrderExecutor
from kuru_sdk.types import OrderRequest

load_dotenv()

# Network and contract configuration (replace with actual addresses)
NETWORK_RPC = os.getenv("RPC_URL")
PRIVATE_KEY = os.getenv("PK")
WEBSOCKET_URL = os.getenv("WEBSOCKET_URL", "wss://ws.testnet.kuru.io")
ORDERBOOK_ADDRESS = '0x05e6f736b5dedd60693fa806ce353156a1b73cf3' # Example address

async def run():
    client = ClientOrderExecutor(
        web3=Web3(Web3.HTTPProvider(NETWORK_RPC)),
        contract_address=ORDERBOOK_ADDRESS,
        private_key=PRIVATE_KEY,
        websocket_url=WEBSOCKET_URL
    )

    try:
        print("Connecting client...")
        await client.connect()
        print("Client connected.")

        # Define orders
        orders_to_place = [
            OrderRequest(
                market_address=ORDERBOOK_ADDRESS,
                order_type='limit',
                side='buy',
                price=0.0000002,
                size=10000,
                cloid="my_buy_order_1" # Unique client order ID
            ),
            OrderRequest(
                market_address=ORDERBOOK_ADDRESS,
                order_type='limit',
                side='sell',
                price=0.0000005,
                size=5000,
                cloid="my_sell_order_1"
            ),
        ]

        # Place batch orders
        tx_hash_place = await client.batch_orders(orders_to_place)
        print(f"Batch place order transaction hash: {tx_hash_place}")

        await asyncio.sleep(5) # Allow time for processing

        # Define cancellations
        orders_to_cancel = [
             OrderRequest(
                market_address=ORDERBOOK_ADDRESS,
                order_type='cancel',
                cancel_cloids=["my_buy_order_1"] # Reference by client order ID
            )
        ]

        # Cancel orders
        tx_hash_cancel = await client.batch_orders(orders_to_cancel)
        print(f"Batch cancel order transaction hash: {tx_hash_cancel}")

        # Keep running or add other logic here
        print("Orders managed. Add further logic or close.")

    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        print("Disconnecting client...")
        await client.disconnect()
        print("Client disconnected.")

if __name__ == "__main__":
    try:
        asyncio.run(run())
    except KeyboardInterrupt:
        print("\nExiting gracefully...")

Key Features

  • ClientOrderExecutor: Manages Orders with client orders for real-time order placement, cancellation, and updates. Uses Web3 for signing and sending transactions.
  • Orderbook: Interacts directly with the Orderbook contract via Web3 calls (primarily for read operations or direct transactions if not using the WebSocket client).
  • MarginAccount: Interacts with the MarginAccount contract via Web3 calls.
  • KuruAPI: A simple client for querying REST API endpoints (e.g., fetching user orders, trades).
  • types: Defines data structures like OrderRequest for standardized interactions.
  • websocket_handler: Core WebSocket communication logic used by ClientOrderExecutor.

Examples

The examples/ directory contains more detailed scripts demonstrating various functionalities:

  • deposit.py: Shows how to deposit funds into the margin account.
  • place_order.py: Demonstrates placing and cancelling orders via WebSocket, including signal handling for graceful shutdown.
  • simple_market_maker.py: A more complex example implementing a basic market-making strategy.
  • view_orderbook.py: Example of querying order book data.
  • ws_place_order.py: Another example focusing on WebSocket order placement.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kuru_sdk-0.2.8.tar.gz (31.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kuru_sdk-0.2.8-py3-none-any.whl (36.3 kB view details)

Uploaded Python 3

File details

Details for the file kuru_sdk-0.2.8.tar.gz.

File metadata

  • Download URL: kuru_sdk-0.2.8.tar.gz
  • Upload date:
  • Size: 31.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for kuru_sdk-0.2.8.tar.gz
Algorithm Hash digest
SHA256 8ac45eed7acab4fd31d4c37f0ac76b3edaece93d61068d63016204ead6b78920
MD5 78d2c4825432b63c021fbcbe4ad11f75
BLAKE2b-256 4e9ca9903bb87f868c4c71f4e384280c4ab95c9a3aa2020929b5c19f0703a051

See more details on using hashes here.

File details

Details for the file kuru_sdk-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: kuru_sdk-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 36.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for kuru_sdk-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 aee7f98ee6c093a49640b9cb9735b08f3855a0d70c922e65a37975eb913fd23c
MD5 48650d64526a50345f0f5f33b7e0024f
BLAKE2b-256 f8f610a62c13e7afa9fc9378363fe4a4ad8b3aa25076492e1515296618af8306

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page