diff --git a/examples/query_map.py b/examples/query_map.py new file mode 100644 index 0000000..1aebf13 --- /dev/null +++ b/examples/query_map.py @@ -0,0 +1,29 @@ +# Python Substrate Interface Library +# +# Copyright 2018-2022 Stichting Polkascan (Polkascan Foundation). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from substrateinterface import SubstrateInterface + +# import logging +# logging.basicConfig(level=logging.DEBUG) + +substrate = SubstrateInterface( + url="ws://127.0.0.1:9944" +) + +result = substrate.query_map("System", "Account", max_results=100) + +for account, account_info in result: + print(f'* {account.value}: {account_info.value}') diff --git a/examples/subscribe_block_headers.py b/examples/subscribe_block_headers.py new file mode 100644 index 0000000..75fb851 --- /dev/null +++ b/examples/subscribe_block_headers.py @@ -0,0 +1,38 @@ +# Python Substrate Interface Library +# +# Copyright 2018-2022 Stichting Polkascan (Polkascan Foundation). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from substrateinterface import SubstrateInterface + +# import logging +# logging.basicConfig(level=logging.DEBUG) + +substrate = SubstrateInterface(url="ws://127.0.0.1:9944") + + +def subscription_handler(obj, update_nr, subscription_id): + print(f"New block #{obj['header']['number']}") + + block = substrate.get_block(block_number=obj['header']['number']) + + for idx, extrinsic in enumerate(block['extrinsics']): + print(f'# {idx}: {extrinsic.value}') + + if update_nr > 2: + return {'message': 'Subscription will cancel when a value is returned', 'updates_processed': update_nr} + + +result = substrate.subscribe_block_headers(subscription_handler) +print(result) diff --git a/examples/subscribe_storage.py b/examples/subscribe_storage.py new file mode 100644 index 0000000..5b9eda9 --- /dev/null +++ b/examples/subscribe_storage.py @@ -0,0 +1,44 @@ +# Python Substrate Interface Library +# +# Copyright 2018-2022 Stichting Polkascan (Polkascan Foundation). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from substrateinterface import SubstrateInterface + +# import logging +# logging.basicConfig(level=logging.DEBUG) + +substrate = SubstrateInterface( + url="ws://127.0.0.1:9944" +) + + +def subscription_handler(account_info_obj, update_nr, subscription_id): + + if update_nr == 0: + print('Initial account data:', account_info_obj.value) + + if update_nr > 0: + # Do something with the update + print('Account data changed:', account_info_obj.value) + + # The execution will block until an arbitrary value is returned, which will be the result of the `query` + if update_nr > 5: + return account_info_obj + + +result = substrate.query("System", "Account", ["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"], + subscription_handler=subscription_handler) + +print(result)