Skip to content

Commit

Permalink
Merge pull request #60 from Multi-Agent-io/development
Browse files Browse the repository at this point in the history
1.0.2 `wait_for_inclusion` support for transactions
  • Loading branch information
Vourhey authored May 20, 2022
2 parents dc4780e + ad781cc commit 139276c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
9 changes: 9 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@ One nay also perform custom rpc calls:
service_functions.rpc_request("pubsub_peer", None, result_handler)
There are a lot of dedicated classes for the most frequently used queries, extrinsics and rpc calls. More on that below.


Notice on Below-Listed Classes
++++++++++++++++++++++++++++++

It is worth to mention that any query in these classes may accept ``block_hash`` argument and eny extrinsic may accept
``nonce`` argument. Also, if some method implies query, it name starts with ``get_``.

More that, each time one initialize a class, they may pass ``wait_for_inclusion=False`` argument to avoid waiting for
future transactions to be included in block. It saves time, but one may not know if the transaction was not successful
(e.g. :ref:`DigitalTwin.set_source <Digital Twins>` was submitted by unauthorized account).

Common Functions
++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "robonomics-interface"
version = "1.0.1"
version = "1.0.2"
description = "Robonomics wrapper over https://github.com/polkascan/py-substrate-interface created to facilitate programming with Robonomics"
authors = ["Pavel Tarasov <p040399@outlook.com>"]
license = "Apache-2.0"
Expand Down
7 changes: 5 additions & 2 deletions robonomicsinterface/classes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ class BaseClass:
Base class for different modules to initialize `service_functions` instance for further work.
"""

def __init__(self, account: Account, rws_sub_owner: tp.Optional[str] = None):
def __init__(self, account: Account, wait_for_inclusion: bool = True, rws_sub_owner: tp.Optional[str] = None):
"""
Assign Account dataclass parameters and create an empty interface attribute for a decorator.
:param account: Account dataclass with seed, websocket address and node type_registry.
:param wait_for_inclusion: Whether wait for a transaction to included in block. You will get the hash anyway.
:param rws_sub_owner: Subscription owner address. If passed, all extrinsics will be executed via RWS
subscriptions.
"""
self.account: Account = account
self._service_functions: ServiceFunctions = ServiceFunctions(account, rws_sub_owner=rws_sub_owner)
self._service_functions: ServiceFunctions = ServiceFunctions(
account, wait_for_inclusion=wait_for_inclusion, rws_sub_owner=rws_sub_owner
)
4 changes: 1 addition & 3 deletions robonomicsinterface/classes/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def listen(

return self._service_functions.rpc_request("pubsub_listen", [address], result_handler)

def get_listeners(
self, result_handler: tp.Optional[tp.Callable] = None
) -> ListenersResponse:
def get_listeners(self, result_handler: tp.Optional[tp.Callable] = None) -> ListenersResponse:
"""
Returns a list of node addresses.
Expand Down
12 changes: 9 additions & 3 deletions robonomicsinterface/classes/service_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ class ServiceFunctions:
Class for custom queries, extrinsics and RPC calls to Robonomics parachain network.
"""

def __init__(self, account: Account, rws_sub_owner: tp.Optional[str] = None):
def __init__(self, account: Account, wait_for_inclusion: bool = True, rws_sub_owner: tp.Optional[str] = None):
"""
Assign Account dataclass parameters and create an empty interface attribute for a decorator.
:param account: Account dataclass with ``seed``, ``remote_ws`` and node ``type_registry``.
:param wait_for_inclusion: Whether wait for a transaction to included in block. You will get the hash anyway.
:param rws_sub_owner: Subscription owner address. If passed, all extrinsics will be executed via RWS
subscriptions.
Expand All @@ -31,6 +32,7 @@ def __init__(self, account: Account, rws_sub_owner: tp.Optional[str] = None):
self.type_registry: TypeRegistryTyping = account.type_registry
self.keypair: Keypair = account.keypair
self.interface: tp.Optional[SubstrateInterface] = None
self.wait_for_inclusion: bool = wait_for_inclusion
self.rws_sub_owner: tp.Optional[str] = rws_sub_owner

@check_socket_opened
Expand Down Expand Up @@ -118,9 +120,13 @@ def extrinsic(
)

logger.info("Submitting extrinsic")
receipt: ExtrinsicReceipt = self.interface.submit_extrinsic(extrinsic, wait_for_inclusion=True)
if not receipt.is_success:
receipt: ExtrinsicReceipt = self.interface.submit_extrinsic(
extrinsic, wait_for_inclusion=self.wait_for_inclusion
)

if self.wait_for_inclusion is True and not receipt.is_success:
raise ExtrinsicFailedException()

logger.info(
f"Extrinsic {receipt.extrinsic_hash} for RPC {call_module}:{call_function} submitted and "
f"included in block {receipt.block_hash}"
Expand Down

0 comments on commit 139276c

Please sign in to comment.