Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facade implementation #88

Merged
merged 20 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions examples/Cookbook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1542,7 +1542,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1559,7 +1559,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 50,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1589,7 +1589,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 51,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1641,7 +1641,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"['output', 'border', 'position', 'book', 'series', 'crunch', 'matrix', 'seven', 'indicate', 'broom', 'olive', 'echo', 'trap', 'situate', 'exercise', 'inform', 'give', 'liberty', 'kidney', 'twelve', 'husband', 'object', 'unlock', 'spike']\n"
"['rich', 'solution', 'blue', 'ice', 'pass', 'clinic', 'beyond', 'chicken', 'people', 'field', 'release', 'planet', 'arrive', 'tornado', 'trip', 'math', 'circle', 'valley', 'describe', 'second', 'cool', 'range', 'devote', 'case']\n"
]
}
],
Expand Down Expand Up @@ -1697,8 +1697,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Secret key: b87ff17ae7c0a22ee6b0c60af14fc03988b8a42697043fc5c49566ffed0c0f83\n",
"Public key: 8d5e518432f4e158496bf5fb3e7a095d6b734bdece09cbd5f2eadca129e0aa98\n"
"Secret key: 2892696376fef33c5f2c76754e2f701073d524588c7a9cd6681bd6d32bea6650\n",
"Public key: f481465e5a676ddc0d73157470e61703abf779496fbc90dce2e97a382bc2871c\n"
]
}
],
Expand Down Expand Up @@ -2161,8 +2161,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Nonce: 1549\n",
"Balance: 2808163193196014407\n"
"Nonce: 1853\n",
"Balance: 3364970609086014407\n"
]
}
],
Expand Down Expand Up @@ -2226,7 +2226,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Transaction hash: abcc49a04ec5657c5a2eee73ec807407589d9c5153de1ca98b4583df7c73745d\n"
"Transaction hash: 999da2b84011ffd6aedb966c8262804bb5280064a77f543f841e4eae96af8013\n"
]
}
],
Expand Down Expand Up @@ -2266,7 +2266,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Transactions hashes: (3, {'0': 'abcc49a04ec5657c5a2eee73ec807407589d9c5153de1ca98b4583df7c73745d', '1': 'd42827b66e592abcef9ba27eba3817a4b990a57f669c4c800ba2443170b4503f', '2': '492c921667da454d7fdb09bd7a33bed6cfc58bccaa0ac0e0b97d639c428f97aa'})\n"
"Transactions hashes: (3, {'0': '999da2b84011ffd6aedb966c8262804bb5280064a77f543f841e4eae96af8013', '1': '0cf69d20c599334047fb1ec9e392086a5b00a42b688dbd9728d7e979a2555622', '2': 'b2e2c5fbb4fbf9be8ec539af4cb719c5b478067eb6772fa0a47a031ef4150b5b'})\n"
]
}
],
Expand Down
101 changes: 101 additions & 0 deletions multiversx_sdk/core/controllers/account_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from typing import Dict, Protocol, Union

from multiversx_sdk.core.interfaces import IAddress
from multiversx_sdk.core.transaction import Transaction
from multiversx_sdk.core.transaction_computer import TransactionComputer
from multiversx_sdk.core.transactions_factories.account_transactions_factory import \
AccountTransactionsFactory
from multiversx_sdk.core.transactions_factories.transactions_factory_config import \
TransactionsFactoryConfig


class INetworkConfig(Protocol):
chain_id: str


class INetworkProvider(Protocol):
def get_network_config(self) -> INetworkConfig:
...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this context, I think we can move these interfaces (defined a few times) directly in core/controllers/interfaces.py.

Later edit: by receiving the chain_id directly in controllers (see below), these won't be needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it.



class IAccount(Protocol):
address: IAddress

def sign(self, data: bytes) -> bytes:
...


class AccountController:
def __init__(self, network_provider: INetworkProvider) -> None:
self.chain_id: Union[str, None] = None
self.factory: Union[AccountTransactionsFactory, None] = None
self.provider = network_provider
self.tx_computer = TransactionComputer()

def create_transaction_for_saving_key_value(self,
sender: IAccount,
nonce: int,
key_value_pairs: Dict[bytes, bytes]) -> Transaction:
self._ensure_factory_is_initialized()

transaction = self.factory.create_transaction_for_saving_key_value( # type: ignore
sender=sender.address,
key_value_pairs=key_value_pairs
)

transaction.nonce = nonce
transaction.signature = sender.sign(self.tx_computer.compute_bytes_for_signing(transaction))

return transaction

def create_transaction_for_setting_guardian(self,
sender: IAccount,
nonce: int,
guardian_address: IAddress,
service_id: str) -> Transaction:
self._ensure_factory_is_initialized()

transaction = self.factory.create_transaction_for_setting_guardian( # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is type: ignore needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed anymore

sender=sender.address,
guardian_address=guardian_address,
service_id=service_id
)

transaction.nonce = nonce
transaction.signature = sender.sign(self.tx_computer.compute_bytes_for_signing(transaction))

return transaction

def create_transaction_for_guarding_account(self,
sender: IAccount,
nonce: int) -> Transaction:
self._ensure_factory_is_initialized()

transaction = self.factory.create_transaction_for_guarding_account( # type: ignore
sender=sender.address
)

transaction.nonce = nonce
transaction.signature = sender.sign(self.tx_computer.compute_bytes_for_signing(transaction))

return transaction

def create_transaction_for_unguarding_account(self,
sender: IAccount,
nonce: int) -> Transaction:
self._ensure_factory_is_initialized()

transaction = self.factory.create_transaction_for_unguarding_account( # type: ignore
sender=sender.address
)

transaction.nonce = nonce
transaction.signature = sender.sign(self.tx_computer.compute_bytes_for_signing(transaction))

return transaction

def _ensure_factory_is_initialized(self):
if self.factory is None:
self.chain_id = self.provider.get_network_config().chain_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's receive chain_id directly in constructor, maybe? Here, and in other controllers. Then, I think we won't need the network provider in the controller anymore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, now passing chain_id in the constructor of the controllers.

config = TransactionsFactoryConfig(self.chain_id)
self.factory = AccountTransactionsFactory(config)
Loading
Loading