This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
forked from polkascan/py-substrate-interface
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.py
87 lines (65 loc) · 2.51 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from substrateinterface import SubstrateInterface, Keypair, SubstrateRequestException
from substrateinterface.utils.ss58 import ss58_encode
substrate = SubstrateInterface(
url="wss://kusama-rpc.polkadot.io/",
address_type=2,
type_registry_preset='kusama'
)
# 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
))
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']))
# Storage call examples
print("\n\nCurrent Account info: {} DOT".format(
substrate.get_runtime_state(
module='System',
storage_function='Account',
params=['EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk']
).get('result')
))
print("Balance @ {}: {} DOT".format(
block_hash,
substrate.get_runtime_state(
module='Balances',
storage_function='FreeBalance',
params=['EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk'],
block_hash=block_hash
).get('result')
))
# Create, sign and submit extrinsic example
mnemonic = Keypair.generate_mnemonic()
keypair = Keypair.create_from_mnemonic(mnemonic, 2)
print("Created address: {}".format(keypair.ss58_address))
call = substrate.compose_call(
call_module='Balances',
call_function='transfer',
call_params={
'dest': 'EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk',
'value': 2 * 10**3
}
)
extrinsic = substrate.create_signed_extrinsic(call=call, keypair=keypair)
try:
# result = substrate.send_extrinsic(extrinsic)
result = substrate.submit_extrinsic(extrinsic, wait_for_inclusion=True)
print('Extrinsic "{}" included in block "{}"'.format(
result['extrinsic_hash'], result.get('block_hash')
))
except SubstrateRequestException as e:
print("Failed to send: {}".format(e))