Skip to content

Commit

Permalink
Merge pull request #62 from Multi-Agent-io/development
Browse files Browse the repository at this point in the history
1.0.4 multiple events subscription, crypto_type definition.
  • Loading branch information
Vourhey authored May 30, 2022
2 parents 67ab9a6 + 3d6f9eb commit bea0e5a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
10 changes: 9 additions & 1 deletion docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ By default, you will only be able to fetch Chainstate info from
`Robonomics Kusama parachain <https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama.rpc.robonomics.network%2F#/explorer>`_
and use :ref:`PubSub <PubSub>` and :ref:`ReqRes <ReqRes API>` patterns.

You can specify another ``remote_ws`` (e.g. local), ``seed`` to sign extrinsics and custom ``type_registry``.
You can specify another ``remote_ws`` (e.g. local), ``seed`` to sign extrinsics, custom ``type_registry`` and ``crypto_type``.

.. code-block:: python
Expand Down Expand Up @@ -262,6 +262,14 @@ events are ``NewRecord``, ``NewLaunch``, ``Transfer``, ``TopicChanged`` and ``Ne
subscriber = Subscriber(account, SubEvent.NewRecord, subscription_handler=callback)
One may also pass a list of addresses or one address as a parameter to filter trigger situations.
There is a way to subscribe to multiple events by using side package ``aenum``.

.. code-block:: python
from aenum import extend_enum
extend_enum(SubEvent, "MultiEvent", f"{SubEvent.NewRecord.value, SubEvent.NewLaunch.value}")
subscriber = Subscriber(acc, SubEvent.MultiEvent, subscription_handler=callback)
IO
++
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.3"
version = "1.0.4"
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
6 changes: 4 additions & 2 deletions robonomicsinterface/classes/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dataclasses import dataclass
from logging import getLogger
from substrateinterface import Keypair
from substrateinterface import Keypair, KeypairType

from ..constants import REMOTE_WS, TYPE_REGISTRY
from ..exceptions import NoPrivateKeyException
Expand All @@ -24,6 +24,7 @@ def __init__(
seed: tp.Optional[str] = None,
remote_ws: tp.Optional[str] = None,
type_registry: tp.Optional[TypeRegistryTyping] = None,
crypto_type: int = KeypairType.SR25519,
) -> None:
"""
Save node connection parameters and create a keypair to sign transactions and define address if seed was passed
Expand All @@ -33,12 +34,13 @@ def __init__(
:param remote_ws: Node url. Default node address is "wss://kusama.rpc.robonomics.network". Another address may
be specified (e.g. "ws://127.0.0.1:9944" for local node).
:param type_registry: Types used in the chain. Defaults are the most frequently used in Robonomics.
:param crypto_type: Use KeypairType.SR25519 or KeypairType.ED25519 cryptography for generating the Keypair.
"""
self.remote_ws: tp.Optional[str] = remote_ws or REMOTE_WS
self.type_registry: tp.Optional[TypeRegistryTyping] = type_registry or TYPE_REGISTRY
if seed:
self.keypair: tp.Optional[Keypair] = create_keypair(seed)
self.keypair: tp.Optional[Keypair] = create_keypair(seed, crypto_type)
self._address: str = self.keypair.ss58_address
else:
self.keypair: tp.Optional[Keypair] = None
Expand Down
2 changes: 1 addition & 1 deletion robonomicsinterface/classes/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _event_callback(self, index_obj: tp.Any, update_nr: int, subscription_id: in

chain_events: list = self._custom_functions.chainstate_query("System", "Events")
for events in chain_events:
if events["event_id"] == self._event.value:
if events["event_id"] in self._event.value:
if self._target_address is None:
self._callback(events["event"]["attributes"]) # All events
elif (
Expand Down
10 changes: 5 additions & 5 deletions robonomicsinterface/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
import logging
import typing as tp

import substrateinterface as substrate

from base58 import b58decode, b58encode
from scalecodec.base import RuntimeConfiguration, ScaleBytes, ScaleType
from substrateinterface import Keypair, KeypairType

logger = logging.getLogger(__name__)


def create_keypair(seed: str) -> substrate.Keypair:
def create_keypair(seed: str, crypto_type: int = KeypairType.SR25519) -> Keypair:
"""
Create a keypair for further use.
:param seed: Account seed (mnemonic or raw) as a key to sign transactions.
:param crypto_type: Use KeypairType.SR25519 or KeypairType.ED25519 cryptography for generating the Keypair.
:return: A Keypair instance used by substrate to sign transactions.
"""

if seed.startswith("0x"):
return substrate.Keypair.create_from_seed(seed_hex=hex(int(seed, 16)), ss58_format=32)
return Keypair.create_from_seed(seed_hex=hex(int(seed, 16)), ss58_format=32, crypto_type=crypto_type)
else:
return substrate.Keypair.create_from_mnemonic(seed, ss58_format=32)
return Keypair.create_from_mnemonic(seed, ss58_format=32, crypto_type=crypto_type)


def dt_encode_topic(topic: str) -> str:
Expand Down

0 comments on commit bea0e5a

Please sign in to comment.