Skip to main content

Library for interfacing with a Substrate node

Reason this release was yanked:

Update package to substrate-interface

Project description

Python Substrate Interface

Python Substrate Interface Library

Description

This library specializes in interfacing with a Substrate node, providing additional convenience methods to deal with SCALE encoding/decoding (the default output and input format of the Substrate JSONRPC), metadata parsing, type registry management and versioning of types.

Documentation

https://polkascan.github.io/py-substrate-interface/

Examples

Simple example, initialize interface and get head block hash of Kusama chain:

Initialization

substrate = SubstrateInterface(
    url="wss://kusama-rpc.polkadot.io/",
    address_type=2,
    type_registry_preset='kusama'
)

substrate.get_chain_head() 

Note on support for wss, this is still quite limited at the moment as connections are not reused yet. Until support is improved it is prefered to use http endpoints (e.g. http://127.0.0.1:9933)

Get extrinsics for a certain block

# Set block_hash to None for chaintip
block_hash = "0x588930468212316d8a75ede0bec0bc949451c164e2cea07ccfc425f497b077b7"

# Retrieve extrinsics in block
result = substrate.get_runtime_block(block_hash=block_hash)

for extrinsic in result['block']['extrinsics']:

    if 'account_id' in extrinsic:
        signed_by_address = ss58_encode(address=extrinsic['account_id'], address_type=2)
    else:
        signed_by_address = None

    print('\nModule: {}\nCall: {}\nSigned by: {}'.format(
        extrinsic['call_module'],
        extrinsic['call_function'],
        signed_by_address
    ))

    # Loop through params
    for param in extrinsic['params']:

        if param['type'] == 'Address':
            param['value'] = ss58_encode(address=param['value'], address_type=2)

        if param['type'] == 'Compact<Balance>':
            param['value'] = '{} DOT'.format(param['value'] / 10**12)

        print("Param '{}': {}".format(param['name'], param['value']))

Make a storage call

The modules and storage functions are provided in the metadata (see substrate.get_metadata_storage_functions()), parameters will be automatically converted to SCALE-bytes (also including decoding of SS58 addresses).

print("\n\nCurrent balance: {} DOT".format(
    substrate.get_runtime_state(
        module='Balances',
        storage_function='FreeBalance',
        params=['EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk']
    ).get('result') / 10**12
))

Or get a historic balance at a certain block hash:

print("Balance @ {}: {} DOT".format(
    block_hash, 
    substrate.get_runtime_state(
        module='Balances',
        storage_function='FreeBalance',
        params=['EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk'],
        block_hash=block_hash
    ).get('result') / 10**12
))

Compose call

Py-substrate-interface will also let you compose calls you can use as an unsigned extrinsic or as a proposal:

payload = substrate.compose_call(
    call_module='Balances',
    call_function='transfer',
    call_params={
        'dest': 'EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk',
        'value': 1000000000000
    }
)

Py-substrate-interface makes it also possible to easily interprete changed types and historic runtimes. As an example we create an (not very useful) historic call of a module that has been removed later on: retrieval of historic metadata and apply the correct version of types in the type registry is all done automatically. Because parsing of metadata and type registry is quite heavy, the result will be cached per runtime id. In the future there could be support for caching backends like Redis to make this cache more persistent.

Create an unsigned extrinsic of a module that was removed by providing block hash:

payload = substrate.compose_call(
    call_module='Nicks',
    call_function='clear_name',
    call_params={},
    block_hash="0x918107632d7994d50f3661db3af353d2aa378f696e47a393bab573f63f7d6c3a"
)

License

https://github.com/polkascan/py-substrate-interface/blob/master/LICENSE

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

substrateinterface-0.9.0.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

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

substrateinterface-0.9.0-py3-none-any.whl (55.8 kB view details)

Uploaded Python 3

File details

Details for the file substrateinterface-0.9.0.tar.gz.

File metadata

  • Download URL: substrateinterface-0.9.0.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for substrateinterface-0.9.0.tar.gz
Algorithm Hash digest
SHA256 91ef2320e1c0960148a5992c08d6d29bff43e832bb6661c70dc3a92edf8096b6
MD5 eb54967fd6fcbd2aa9199c1d65af64ef
BLAKE2b-256 665d81b52e9a925133b819e214d55436d0b701b22bbe0c7b9930e7f8b26ba172

See more details on using hashes here.

File details

Details for the file substrateinterface-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: substrateinterface-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 55.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.5

File hashes

Hashes for substrateinterface-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35468145dfa88bab21393e8320b116327ba64fe2183a4b53312f0cbbd332e72e
MD5 2053d8072ce74d94c127d36942b7e1b4
BLAKE2b-256 4b6f1edcb2f3dd005cc6d995567ea2e08ace2a95c78034e2420b52ea23125e5f

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