Published on

subxtpy: Bridging Python and Substrate

Authors
  • avatar
    Name
    Philomath Ingeniare
    Twitter

In the rapidly evolving blockchain landscape, Substrate has emerged as a powerful framework for building custom, scalable blockchains. However, interacting with Substrate-based chains has primarily been the domain of Rust developers. Today, we're excited to introduce subxtpy, a Python library designed to bridge this gap and open up the Substrate ecosystem to Python developers and data scientists.

Empowering Python Developers in the Substrate Ecosystem

subxtpy aims to dramatically simplify the process of interacting with Substrate-based blockchains using Python. This library provides a Pythonic interface to Substrate's functionality, enabling developers to leverage the power of Substrate while working in a familiar and versatile language.

Key Features

  1. Seamless Integration: Interact with Substrate-based blockchains without writing Rust code. subxtpy handles complex low-level operations behind the scenes.

  2. Account Management: Tools for creating and managing accounts, supporting various cryptographic schemes used in the Substrate ecosystem.

  3. Transaction Handling: Simplifies the process of creating, signing, and submitting transactions to the blockchain.

  4. Query Support: Easily query blockchain state, retrieve account balances, and access other on-chain data.

  5. Event Subscription: Subscribe to blockchain events, enabling the development of reactive applications.

  6. Cross-Platform Compatibility: Works across different operating systems for versatile development environments.

Getting Started

Let's illustrate how simple subxtpy makes interacting with a Substrate chain:

import asyncio
from subxtpy import SubxtClient, Keypair

async def main():
    client = await SubxtClient.from_url("ws://127.0.0.1:9944")

    # Read a storage value
    value = await client.storage("System", "Account",
                                 ["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"])
    print(value)

    # Fetch a constant
    constant_value = await client.constant("Balances", "ExistentialDeposit")
    print(constant_value)

    # Call a runtime API
    api_result = await client.runtime_api_call("AccountNonceApi", "account_nonce",
                                               ["e5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a"])
    print(api_result)

    # Sign and submit a transaction
    from_keypair = Keypair.from_secret_key("e5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a")

    remark_payload = ["Hello"]
    transfer_tx_hash = await client.sign_and_submit(from_keypair, "System", "remark", remark_payload)
    print("Remark tx hash:", transfer_tx_hash)

asyncio.run(main())

The Result:

With just a few lines of code, you can connect to a Substrate node, query account information, and submit transactions. subxtpy abstracts away the complexities, allowing you to focus on building your application logic.

Benefits to the Substrate/Polkadot/Kusama Ecosystem

  1. Expanded Developer Base: Opens up Substrate development to the vast Python community, potentially accelerating adoption and innovation.

  2. Simplified Onboarding: Reduces the learning curve for newcomers to the Substrate ecosystem through familiar Python syntax and concepts.

  3. Enhanced Tooling: Enables the creation of Python-based tools and applications for Substrate chains, enriching the ecosystem's toolset.

  4. Improved Interoperability: Facilitates easier integration of Substrate-based blockchains with existing Python applications and services.

  5. Data Analysis and Machine Learning: Leverages Python's strength in data analysis and machine learning for blockchain data applications.

Why Another Python Library for Substrate?

While the Substrate ecosystem already has a Python library called py-substrate-interface, subxtpy brings unique advantages that justify its development:

  1. Direct Mapping to subxt: subxtpy is a one-to-one wrapper of the official subxt Rust library. This means it directly inherits the latest features, optimizations, and compatibility updates from subxt, ensuring it stays current with the rapidly evolving Substrate ecosystem.

  2. Performance: Built using PyO3, subxtpy leverages the speed and efficiency of the underlying Rust implementation. This approach minimizes overhead, potentially offering better performance compared to a pure Python implementation, especially for computationally intensive tasks.

  3. Type Safety: By wrapping the Rust subxt library, subxtpy maintains much of the type safety guarantees provided by Rust. This can lead to more robust code and easier debugging, even when working in Python.

  4. Consistency with Rust Ecosystem: For teams working across both Rust and Python, subxtpy provides a consistent API mirroring the Rust subxt library. This can simplify development and knowledge transfer between Rust and Python codebases.

  5. Leveraging Rust's Substrate Expertise: The Substrate ecosystem is primarily Rust-based. By closely following subxt, subxtpy benefits from the deep Substrate expertise embedded in the Rust library's design and implementation.

  6. Simplified Maintenance: As a wrapper around subxt, subxtpy can more easily stay up-to-date with the latest Substrate changes. When subxt is updated to support new Substrate features, these can be more quickly reflected in subxtpy.

  7. Complementary Approach: Rather than replacing existing solutions, subxtpy aims to complement them, offering developers another option that might better suit certain use cases, especially those requiring close alignment with the Rust ecosystem.

While py-substrate-interface has served the community well, subxtpy's approach of wrapping the official subxt library opens up new possibilities for Python developers in the Substrate ecosystem. It bridges the gap between Rust and Python development, potentially accelerating adoption and enabling more diverse applications in the Polkadot/Kusama ecosystem.

Future Directions

We envision subxtpy as an actively maintained community project. Contributions are encouraged for features such as:

  • Static calls and types based on specific chain metadata generated with decorators for the client class
  • Advanced utility functions for common blockchain operations
  • Integration with popular Python data analysis and visualization libraries
  • Comprehensive documentation and tutorials for various use cases

subxtpy represents a significant step forward in making Substrate development more accessible and versatile. By bridging the gap between Python and Substrate, it opens up new possibilities for blockchain development, data analysis, and application integration.

We invite Python developers, data scientists, and blockchain enthusiasts to explore subxtpy and contribute to its growth. Whether you're building a new blockchain application, analyzing on-chain data, or simply curious about Substrate, subxtpy provides an excellent entry point into this exciting ecosystem.

The future of blockchain development is collaborative and multi-lingual. With subxtpy, we're one step closer to realizing that vision in the Substrate ecosystem.