Python3 SDK

Overview

The G6 SDK for Python allows you to interact with the G6 type blockchains easily. This SDK provides functions to query network details, check account balances, and send transactions (such as balance transfers).

In this guide, we walk you through connecting to the Gen6 public chain, querying network information, checking balances, and performing a balance transfer using Python.

Prerequisites

To begin using the SDK, you need:

  1. Python 3.x installed on your system.

  2. The substrate-interface Python package, which can be installed using pip:

    pip3 install substrate-interface

Setting Up the G6 Python SDK

We leverages the substrate-interface library to interact with G6 based blockchains.

Here is a full example demonstrating how to connect to the Gen6 public chain, check account balances, and send balance transfers: https://g.g6.network/g6-networks-release/gen6-snippets/-/blob/main/gen6_python3_connect_and_send_balance.py

SDK Functions and Usage

1. Connecting to a Gen6 Public Node

To connect to a Gen6 public chain WSS node, use the SubstrateInterface class:

  • url: WebSocket URL for connecting to the Gen6 node.

  • ss58_format: The SS58 format for addresses on the Gen6 network. For Gen6, the SS58 format is 355.

  • type_registry_preset: A preset to define the structure of the blockchain (e.g., substrate-node-template).

2. Querying the Network

You can query the connected Gen6 node for important details, such as the chain name, node version, and runtime version:

3. Checking Account Balance

To check an account's balance, use the System.Account query:

This query will return the free balance of the specified account.

4. Creating a Keypair

You can create a keypair from a mnemonic, seed, or URI. In this example, we create a keypair from the predefined //Alice URI:

Make sure to use a secure method for generating and storing your private keys in a real-world application.

5. Sending a Balance Transfer

To send a balance transfer, you can use the Balances pallet, specifically the transfer_keep_alive function. This function ensures that the sender's account remains alive after the transfer.

6. Submitting a Transaction

After creating the extrinsic (transaction), submit it to the blockchain:

The submit_extrinsic method sends the transaction and waits for inclusion in the block.

7. Transaction Status

Check if the transaction was successful and print the transaction and block hash:

Contibute

Join our community and DM any core member or write to Gen6 support Discord, then we can add you a dev contributor role!

Last updated

Was this helpful?