#### This project is forked from https://github.com/polkascan/py-substrate-interface
# Python Polymath Substrate Interface
[![Latest Version](https://img.shields.io/pypi/v/polymath-substrate-interface.svg)](https://pypi.org/project/polymath-substrate-interface/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/polymath-substrate-interface.svg)](https://pypi.org/project/polymath-substrate-interface/)
Python Polymath Substrate Interface Library
## Description
This library specializes in interfacing with a Polymesh 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/
## Installation
```bash
pip install polymath-substrate-interface
```
### Initialization
The following examples show how to initialize for Polymesh chain:
#### Substrate Node Template
Compatible with https://github.com/substrate-developer-hub/substrate-node-template
```python
substrate = SubstrateInterface(
url="http://127.0.0.1:9933",
address_type=42,
type_registry_preset='substrate-node-template'
)
```
If custom types are introduced in the Substrate chain, the following example will add compatibility by creating a custom type
registry JSON file and including this during initialization:
```json
{
"runtime_id": 2,
"types": {
"MyCustomInt": "u32",
"MyStruct": {
"type": "struct",
"type_mapping": [
["account", "AccountId"],
["message", "Vec<u8>"]
]
}
},
"versioning": [
]
}
```
```python
custom_type_registry = load_type_registry_file("my-custom-types.json")
substrate = SubstrateInterface(
url="http://127.0.0.1:9933",
address_type=42,
type_registry_preset='substrate-node-template',
type_registry=custom_type_registry
)
```
## Keeping type registry presets up to date
When on-chain runtime upgrades occur, types used in call- or storage functions can be added or modified. Therefor it is
important to keep the type registry presets up to date. At the moment the type registry for Polymesh is being actively maintained for this library and an check and update procedure can be triggered with:
```python
substrate.update_type_registry_presets()
```
## Examples
### Get extrinsics for a certain block
```python
# 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).
```python
balance_info = substrate.get_runtime_state(
module='System',
storage_function='Account',
params=['5E9oDs9PjpsBbxXxRE9uMaZZhnBAV38n2ouLB28oecBDdeQo']
).get('result')
if balance_info:
print("\n\nCurrent free balance: {} KSM".format(
balance_info.get('data').get('free', 0) / 10**12
))
```
Or get a historic balance at a certain block hash:
```python
balance_info = substrate.get_runtime_state(
module='System',
storage_function='Account',
params=['5E9oDs9PjpsBbxXxRE9uMaZZhnBAV38n2ouLB28oecBDdeQo'],
block_hash=block_hash
).get('result')
if balance_info:
print("\n\nFree balance @ {}: {} KSM".format(
block_hash,
balance_info.get('data').get('free', 0) / 10**12
))
```
Or get all the key pairs of a map:
```python
# Get all the stash and controller bondings.
all_bonded_stash_ctrls = substrate.iterate_map(
module='Staking',
storage_function='Bonded',
block_hash=block_hash
)
```
### Create and send signed extrinsics
The following code snippet illustrates how to create a call, wrap it in an signed extrinsic and send it to the network:
```python
from substrateinterface import SubstrateInterface, SubstrateRequestException, Keypair
substrate = SubstrateInterface(
url="ws://127.0.0.1:9944",
address_type=42,
type_registry_preset='kusama'
)
keypair = Keypair.create_from_mnemonic('episode together nose spoon dose oil faculty zoo ankle evoke admit walnut')
call = substrate.compose_call(
call_module='Balances',
call_function='transfer',
call_params={
'dest': '5E9oDs9PjpsBbxXxRE9uMaZZhnBAV38n2ouLB28oecBDdeQo',
'value': 1 * 10**12
}
)
extrinsic = substrate.create_signed_extrinsic(call=call, keypair=keypair)
try:
result = substrate.submit_extrinsic(extrinsic, wait_for_inclusion=True)
print("Extrinsic '{}' sent and included in block '{}'".format(result['extrinsic_hash'], result['block_hash']))
except SubstrateRequestException as e:
print("Failed to send: {}".format(e))
```
### Create mortal extrinsics
By default `immortal` extrinsics are created, which means they have an indefinite lifetime for being included in a
block. However it is recommended to use specify an expiry window, so you know after a certain amount of time if the
extrinsic is not included in a block, it will be invalidated.
```python
extrinsic = substrate.create_signed_extrinsic(call=call, keypair=keypair, era={'period': 64})
```
The `period` specifies the number of blocks the extrinsic is valid counted from current head.
### Keypair creation and signing
```python
mnemonic = Keypair.generate_mnemonic()
keypair = Keypair.create_from_mnemonic(mnemonic)
signature = keypair.sign("Test123")
if keypair.verify("Test123", signature):
print('Verified')
```
By default a keypair is using SR25519 cryptography, alternatively ED25519 can be explictly specified:
```python
keypair = Keypair.create_from_mnemonic(mnemonic, crypto_type=KeypairType.ED25519)
```
### Creating keypairs with soft and hard key derivation paths
```python
mnemonic = Keypair.generate_mnemonic()
keypair = Keypair.create_from_uri(mnemonic + '//hard/soft')
```
By omitting the mnemonic the default development mnemonic is used:
```python
keypair = Keypair.create_from_uri('//Alice')
```
### Getting estimate of network fees for extrinsic in advance
```python
keypair = Keypair(ss58_address="EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk")
call = self.kusama_substrate.compose_call(
call_module='Balances',
call_function='transfer',
call_params={
'dest': 'EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk',
'value': 2 * 10 ** 3
}
)
payment_info = self.kusama_substrate.get_payment_info(call=call, keypair=keypair)
```
### Offline signing of extrinsics
This example generates a signature payload which can be signed on another (offline) machine and later on sent to the
network with the generated signature.
Generate signature payload on online machine:
```python
substrate = SubstrateInterface(
url="http://127.0.0.1:9933",
address_type=42,
type_registry_preset='substrate-node-template',
)
call = substrate.compose_call(
call_module='Balances',
call_function='transfer',
call_params={
'dest': '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXC
没有合适的资源?快使用搜索试试~ 我知道了~
PyPI 官网下载 | polymath-substrate-interface-3.1.1.tar.gz
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 63 浏览量
2022-01-14
21:28:25
上传
评论
收藏 120KB GZ 举报
温馨提示
资源来自pypi官网。 资源全名:polymath-substrate-interface-3.1.1.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
polymath-substrate-interface-3.1.1.tar.gz (25个子文件)
polymath-substrate-interface-3.1.1
setup.py 10KB
LICENSE 11KB
setup.cfg 38B
polymath_substrate_interface.egg-info
top_level.txt 24B
SOURCES.txt 699B
PKG-INFO 13KB
dependency_links.txt 1B
requires.txt 266B
README.md 10KB
PKG-INFO 13KB
substrateinterface
__init__.py 68KB
utils
ss58.py 5KB
__init__.py 649B
hasher.py 3KB
constants.py 901B
key.py 2KB
exceptions.py 804B
test
test_helper_functions.py 5KB
__init__.py 647B
test_runtime_state.py 7KB
test_create_extrinsics.py 7KB
settings.py 858B
test_type_registry.py 2KB
test_keypair.py 9KB
fixtures.py 502KB
共 25 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功