Asyncio library for interacting with substrate. Mostly API-compatible with py-substrate-interface
Archived. The repository is read-only: no more fixes or releases will come.
pip install async-substrate-interfaceAsync Substrate Interface
This project provides an asynchronous interface for interacting with Substrate-based blockchains. Its API is based on the py-substrate-interface project, but is not fully compatible.
Additionally, this project uses cyscale instead of py-scale-codec for faster SCALE decoding. Since v2.0, because cyscale and py-substrate-interface share the same namespace, we require that only cyscale is installed to be able to use this. If you run into a runtime error stating that both cannot be installed at the same time, simply remove them both, and reinstall cyscale:
pip uninstall cyscale scalecodec -y
pip install cyscale
Installation
To install the package, use the following command:
pip install async-substrate-interface
Usage
Here are examples of how to use the sync and async interfaces:
from async_substrate_interface import SubstrateInterface
def main():
substrate = SubstrateInterface(
url="wss://rpc.polkadot.io"
)
with substrate:
result = substrate.query(
module='System',
storage_function='Account',
params=['5CZs3T15Ky4jch1sUpSFwkUbYEnsCfe1WCY51fH3SPV6NFnf']
)
print(result)
main()
import asyncio
from async_substrate_interface import AsyncSubstrateInterface
async def main():
substrate = AsyncSubstrateInterface(
url="wss://rpc.polkadot.io"
)
async with substrate:
result = await substrate.query(
module='System',
storage_function='Account',
params=['5CZs3T15Ky4jch1sUpSFwkUbYEnsCfe1WCY51fH3SPV6NFnf']
)
print(result)
asyncio.run(main())
Caching
There are a few different cache types used in this library to improve the performance overall. The one with which
you are probably familiar is the typical functools.lru_cache used in sync_substrate.SubstrateInterface.
By default, it uses a max cache size of 512 for smaller returns, and 16 for larger ones. These cache sizes are
user-configurable using the respective env vars, SUBSTRATE_CACHE_METHOD_SIZE and SUBSTRATE_RUNTIME_CACHE_SIZE.