How to Use the Python Substrate Interface

Apr 28, 2024 | Blockchain

The Python Substrate Interface is a powerful library designed for easy interaction with Substrate nodes. Whether you’re querying storage or composing extrinsics, this library simplifies the process, making it accessible to a variety of users. In this article, we’ll guide you through the setup, usage, and even some troubleshooting tips.

Getting Started with Installation

To begin, you need to install the library. Here’s how you can do it:

bash
pip install substrate-interface

Initializing the Substrate Interface

Once installed, you’ll want to set up the Substrate connection. This is done by initializing the SubstrateInterface class. It’s like opening a bridge to communicate with your Substrate node.

python
substrate = SubstrateInterface(url='ws://127.0.0.1:9944')

With this line, you connect to your local Substrate node. The library will automatically query the node and determine properties like the ss58_format.

Quick Usage Examples

Let’s dive into some practical applications. Think of the Substrate Interface as a postman that not only delivers messages but also tracks the balance of accounts, composes transactions, and sends them off!

1. Querying Balance Information

To check the balance of an account, you can run the following:

python
result = substrate.query('System', 'Account', ['F4xQKRUagnSGjFqafyhajLs94e7Vvzvr8ebwYJceKpr8R7T'])
print(result.value['data']['free'])

In this example, the balance is extracted from the account, allowing you to see how much free balance is available.

2. Creating a Balance Transfer Extrinsic

To send a balance transfer, utilize the compose_call method:

python
call = substrate.compose_call(
    call_module='Balances',
    call_function='transfer',
    call_params={
        'dest': '5E9oDs9PjpsBbxXxRE9uMaZZhnBAV38n2ouLB28oecBDdeQo',
        'value': 1 * 10**12
    }
)
keypair = Keypair.create_from_uri('Alice')
extrinsic = substrate.create_signed_extrinsic(call=call, keypair=keypair)
receipt = substrate.submit_extrinsic(extrinsic, wait_for_inclusion=True)
print(f'Extrinsic {receipt.extrinsic_hash} sent and included in block {receipt.block_hash}')

In this analogy, think of “Alice” as the sender, crafting a letter (the extrinsic) to deliver funds to another address.

Troubleshooting Tips

If you encounter any issues while using the Substrate Interface, here are some ideas to help you troubleshoot:

  • Ensure that your Substrate node is correctly running and is accessible at the specified URL.
  • If you receive errors regarding the ss58_format, check if you are using the correct formats for your runtime version.
  • For any coding or integration questions, visit Substrate StackExchange or explore Github Discussions.
  • If you’re still stuck, don’t hesitate to reach out on our matrix chat group: Polkascan Technical.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With the Python Substrate Interface, you have the tools to interact seamlessly with a Substrate node. The possibilities range from querying account information to managing transactions with ease.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox