# G6 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`:

   ```bash
   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:

```python
substrate = SubstrateInterface(
    url="wss://gen6.app:443/node",
    ss58_format=355,
    type_registry_preset='substrate-node-template'
)
```

* **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:

```python
print("## Chain:", substrate.rpc_request("system_chain", []))
print("## Node name:", substrate.rpc_request("system_name", []))
print("## Node version:", substrate.rpc_request("system_version", []))
rv = substrate.rpc_request("state_getRuntimeVersion", [])
print("## Raw runtimeVersion RPC:", rv)
```

#### 3. **Checking Account Balance**

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

```python
result = substrate.query('System', 'Account', ['g6ACqyvx3otpNgA3GmCP6ugJFfM7NEfq92GSdry32R52r3bJJ'])
print(f"## Testing balance check: { result.value['data']['free']}")
```

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:

```python
keypair = Keypair.create_from_uri('//Alice')
```

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.

```python
call = substrate.compose_call(
    call_module='Balances',
    call_function='transfer_keep_alive',
    call_params={
        'dest': recipient_address,
        'value': 10000000000
    }
)

# Wrap in a signed TX
extrinsic = substrate.create_signed_extrinsic(call=call, keypair=keypair)
```

#### 6. **Submitting a Transaction**

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

```python
receipt = substrate.submit_extrinsic(extrinsic, wait_for_inclusion=True)
print("## TX sent!")
print("## TX error:", receipt.error_message)
```

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:

```python
try:
    print("## TX is success:", receipt.is_success)
except:
    print("## TX failed, check balance!")
    exit
print("## TX hash:", receipt.extrinsic_hash)
print("## Block hash:", receipt.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!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.gen6.life/developer-resources/sdk-and-tooling/g6-python3-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
