-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 12 commits
3960a3e
c1461b2
af71228
f4c6b66
341aca5
7044659
edc7f3f
2019385
0b4aaba
e0b99bf
1a2ae53
ef831d9
eff977c
07a8fff
358e0bb
9a81fd3
bae49e0
0501acf
8e0771f
200d60b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||
... | ||
|
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's receive There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, now passing |
||
config = TransactionsFactoryConfig(self.chain_id) | ||
self.factory = AccountTransactionsFactory(config) |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed it.