From ac6434601a95715b8a74e69c28b299f0968b792d Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Mon, 12 Feb 2024 11:01:33 +0200 Subject: [PATCH] fixes after review --- .../transaction_outcome_parsers/resources.py | 4 - ..._management_transactions_outcome_parser.py | 166 +++++++++--------- ...gement_transactions_outcome_parser_test.py | 24 +-- 3 files changed, 89 insertions(+), 105 deletions(-) diff --git a/multiversx_sdk/core/transaction_outcome_parsers/resources.py b/multiversx_sdk/core/transaction_outcome_parsers/resources.py index 929eeb31..599444a6 100644 --- a/multiversx_sdk/core/transaction_outcome_parsers/resources.py +++ b/multiversx_sdk/core/transaction_outcome_parsers/resources.py @@ -23,14 +23,10 @@ def __init__(self, class SmartContractResult: def __init__(self, - hash: str = "", - timestamp: int = 0, sender: str = "", receiver: str = "", data: str = "", logs: TransactionLogs = TransactionLogs()) -> None: - self.hash = hash - self.timestamp = timestamp self.sender = sender self.receiver = receiver self.data = data diff --git a/multiversx_sdk/core/transaction_outcome_parsers/token_management_transactions_outcome_parser.py b/multiversx_sdk/core/transaction_outcome_parsers/token_management_transactions_outcome_parser.py index 70f3c95c..78e4ae87 100644 --- a/multiversx_sdk/core/transaction_outcome_parsers/token_management_transactions_outcome_parser.py +++ b/multiversx_sdk/core/transaction_outcome_parsers/token_management_transactions_outcome_parser.py @@ -20,44 +20,44 @@ def __init__(self) -> None: pass def parse_issue_fungible(self, transaction_outcome: TransactionOutcome) -> IssueFungibleOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "issue") - identifier = self.extract_token_identifier(event) + event = self._find_single_event_by_identifier(transaction_outcome, "issue") + identifier = self._extract_token_identifier(event) return IssueFungibleOutcome(identifier) def parse_issue_non_fungible(self, transaction_outcome: TransactionOutcome) -> IssueNonFungibleOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "issueNonFungible") - identifier = self.extract_token_identifier(event) + event = self._find_single_event_by_identifier(transaction_outcome, "issueNonFungible") + identifier = self._extract_token_identifier(event) return IssueNonFungibleOutcome(identifier) def parse_issue_semi_fungible(self, transaction_outcome: TransactionOutcome) -> IssueSemiFungibleOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "issueSemiFungible") - identifier = self.extract_token_identifier(event) + event = self._find_single_event_by_identifier(transaction_outcome, "issueSemiFungible") + identifier = self._extract_token_identifier(event) return IssueSemiFungibleOutcome(identifier) def parse_register_meta_esdt(self, transaction_outcome: TransactionOutcome) -> RegisterMetaEsdtOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "registerMetaESDT") - identifier = self.extract_token_identifier(event) + event = self._find_single_event_by_identifier(transaction_outcome, "registerMetaESDT") + identifier = self._extract_token_identifier(event) return RegisterMetaEsdtOutcome(identifier) def parse_register_and_set_all_roles(self, transaction_outcome: TransactionOutcome) -> RegisterAndSetAllRolesOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - register_event = self.find_single_event_by_identifier(transaction_outcome, "registerAndSetAllRoles") - token_identifier = self.extract_token_identifier(register_event) + register_event = self._find_single_event_by_identifier(transaction_outcome, "registerAndSetAllRoles") + token_identifier = self._extract_token_identifier(register_event) - set_role_event = self.find_single_event_by_identifier(transaction_outcome, "ESDTSetRole") + set_role_event = self._find_single_event_by_identifier(transaction_outcome, "ESDTSetRole") encoded_roles = set_role_event.topics[3:] roles: List[str] = [] @@ -68,17 +68,17 @@ def parse_register_and_set_all_roles(self, transaction_outcome: TransactionOutco return RegisterAndSetAllRolesOutcome(token_identifier, roles) def parse_set_burn_role_globally(self, transaction_outcome: TransactionOutcome) -> None: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) def parse_unset_burn_role_globally(self, transaction_outcome: TransactionOutcome) -> None: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) def parse_set_special_role(self, transaction_outcome: TransactionOutcome) -> SetSpecialRoleOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTSetRole") + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTSetRole") user_address = event.address - token_identifier = self.extract_token_identifier(event) + token_identifier = self._extract_token_identifier(event) encoded_roles = event.topics[3:] roles: List[str] = [] @@ -90,117 +90,117 @@ def parse_set_special_role(self, transaction_outcome: TransactionOutcome) -> Set return SetSpecialRoleOutcome(user_address, token_identifier, roles) def parse_nft_create(self, transaction_outcome: TransactionOutcome) -> NFTCreateOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTNFTCreate") - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) - amount = self.extract_amount(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTNFTCreate") + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) + amount = self._extract_amount(event) return NFTCreateOutcome(token_identifier, nonce, amount) def parse_local_mint(self, transaction_outcome: TransactionOutcome) -> MintOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTLocalMint") + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTLocalMint") user_address = event.address - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) - minted_supply = self.extract_amount(event) + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) + minted_supply = self._extract_amount(event) return MintOutcome(user_address, token_identifier, nonce, minted_supply) def parse_local_burn(self, transaction_outcome: TransactionOutcome) -> BurnOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTLocalBurn") + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTLocalBurn") user_address = event.address - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) - burnt_supply = self.extract_amount(event) + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) + burnt_supply = self._extract_amount(event) return BurnOutcome(user_address, token_identifier, nonce, burnt_supply) def parse_pause(self, transaction_outcome: TransactionOutcome) -> PauseOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTPause") - identifier = self.extract_token_identifier(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTPause") + identifier = self._extract_token_identifier(event) return PauseOutcome(identifier) def parse_unpause(self, transaction_outcome: TransactionOutcome) -> UnPauseOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTUnPause") - identifier = self.extract_token_identifier(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTUnPause") + identifier = self._extract_token_identifier(event) return UnPauseOutcome(identifier) def parse_freeze(self, transaction_outcome: TransactionOutcome) -> FreezeOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTFreeze") - user_address = self.extract_address(event) - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) - balance = self.extract_amount(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTFreeze") + user_address = self._extract_address(event) + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) + balance = self._extract_amount(event) return FreezeOutcome(user_address, token_identifier, nonce, balance) def parse_unfreeze(self, transaction_outcome: TransactionOutcome) -> UnFreezeOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTUnFreeze") - user_address = self.extract_address(event) - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) - balance = self.extract_amount(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTUnFreeze") + user_address = self._extract_address(event) + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) + balance = self._extract_amount(event) return UnFreezeOutcome(user_address, token_identifier, nonce, balance) def parse_wipe(self, transaction_outcome: TransactionOutcome) -> WipeOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTWipe") - user_address = self.extract_address(event) - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) - balance = self.extract_amount(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTWipe") + user_address = self._extract_address(event) + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) + balance = self._extract_amount(event) return WipeOutcome(user_address, token_identifier, nonce, balance) def parse_update_attributes(self, transaction_outcome: TransactionOutcome) -> UpdateAttributesOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTNFTUpdateAttributes") - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTNFTUpdateAttributes") + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) attributes = base64.b64decode(event.topics[3]) if event.topics[3] else b"" return UpdateAttributesOutcome(token_identifier, nonce, attributes) def parse_add_quantity(self, transaction_outcome: TransactionOutcome) -> AddQuantityOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTNFTAddQuantity") - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) - added_quantity = self.extract_amount(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTNFTAddQuantity") + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) + added_quantity = self._extract_amount(event) return AddQuantityOutcome(token_identifier, nonce, added_quantity) def parse_burn_quantity(self, transaction_outcome: TransactionOutcome) -> BurnQuantityOutcome: - self.ensure_no_error(transaction_outcome.transaction_logs.events) + self._ensure_no_error(transaction_outcome.transaction_logs.events) - event = self.find_single_event_by_identifier(transaction_outcome, "ESDTNFTBurn") - token_identifier = self.extract_token_identifier(event) - nonce = self.extract_nonce(event) - added_quantity = self.extract_amount(event) + event = self._find_single_event_by_identifier(transaction_outcome, "ESDTNFTBurn") + token_identifier = self._extract_token_identifier(event) + nonce = self._extract_nonce(event) + burnt_quantity = self._extract_amount(event) - return BurnQuantityOutcome(token_identifier, nonce, added_quantity) + return BurnQuantityOutcome(token_identifier, nonce, burnt_quantity) - def ensure_no_error(self, transaction_events: List[TransactionEvent]) -> None: + def _ensure_no_error(self, transaction_events: List[TransactionEvent]) -> None: for event in transaction_events: if event.identifier == "signalError": hex_data = base64.b64decode(event.data or "").hex() @@ -211,8 +211,8 @@ def ensure_no_error(self, transaction_events: List[TransactionEvent]) -> None: raise ParseTransactionOutcomeError(f"encountered signalError: {message} ({bytes.fromhex(data[1:]).decode()})") - def find_single_event_by_identifier(self, transaction_outcome: TransactionOutcome, identifier: str) -> TransactionEvent: - events = self.gather_all_events(transaction_outcome) + def _find_single_event_by_identifier(self, transaction_outcome: TransactionOutcome, identifier: str) -> TransactionEvent: + events = self._gather_all_events(transaction_outcome) events_with_matching_id = [event for event in events if event.identifier == identifier] if len(events_with_matching_id) == 0: @@ -223,7 +223,7 @@ def find_single_event_by_identifier(self, transaction_outcome: TransactionOutcom return events_with_matching_id[0] - def gather_all_events(self, transaction_outcome: TransactionOutcome) -> List[TransactionEvent]: + def _gather_all_events(self, transaction_outcome: TransactionOutcome) -> List[TransactionEvent]: all_events = [*transaction_outcome.transaction_logs.events] for result in transaction_outcome.transaction_results: @@ -231,28 +231,28 @@ def gather_all_events(self, transaction_outcome: TransactionOutcome) -> List[Tra return all_events - def extract_token_identifier(self, event: TransactionEvent) -> str: + def _extract_token_identifier(self, event: TransactionEvent) -> str: if not event.topics[0]: return "" ticker = base64.b64decode(event.topics[0]) return ticker.decode() - def extract_nonce(self, event: TransactionEvent) -> int: + def _extract_nonce(self, event: TransactionEvent) -> int: if not event.topics[1]: return 0 nonce = base64.b64decode(event.topics[1]) return decode_unsigned_number(nonce) - def extract_amount(self, event: TransactionEvent) -> int: + def _extract_amount(self, event: TransactionEvent) -> int: if not event.topics[2]: return 0 amount = base64.b64decode(event.topics[2]) return decode_unsigned_number(amount) - def extract_address(self, event: TransactionEvent) -> str: + def _extract_address(self, event: TransactionEvent) -> str: if not event.topics[3]: return "" diff --git a/multiversx_sdk/core/transaction_outcome_parsers/token_management_transactions_outcome_parser_test.py b/multiversx_sdk/core/transaction_outcome_parsers/token_management_transactions_outcome_parser_test.py index 63042eab..51117091 100644 --- a/multiversx_sdk/core/transaction_outcome_parsers/token_management_transactions_outcome_parser_test.py +++ b/multiversx_sdk/core/transaction_outcome_parsers/token_management_transactions_outcome_parser_test.py @@ -21,17 +21,13 @@ def test_ensure_error(self): data="QDc1NzM2NTcyMjA2NTcyNzI2Zjcy" ) - with pytest.raises(ParseTransactionOutcomeError, match=re.escape("encountered signalError: ticker name is not valid (user error)")): - self.parser.ensure_no_error([event]) + sc_result = SmartContractResult() + logs = TransactionLogs("", [event]) - event = TransactionEvent( - address="erd1qqqqqqqqqqqqqpgq50wpucem6hvn0g8mwa670fznqz4n38h9d8ss564tlz", - identifier="writeLog", - topics=["ATlHLv9ohncamC8wg9pdQh8kwpGB5jiIIo3IHKYNaeE=", - "QHRvbyBtdWNoIGdhcyBwcm92aWRlZCBmb3IgcHJvY2Vzc2luZzogZ2FzIHByb3ZpZGVkID0gOTc4MzIwMDAsIGdhcyB1c2VkID0gNTg5MTc1"], - data="QDc1NzM2NTcyMjA2NTcyNzI2Zjcy" - ) - self.parser.ensure_no_error([event]) + tx_outcome = TransactionOutcome([sc_result], logs) + + with pytest.raises(ParseTransactionOutcomeError, match=re.escape("encountered signalError: ticker name is not valid (user error)")): + self.parser.parse_issue_fungible(tx_outcome) def test_parse_issue_fungible(self): identifier = "ZZZ-9ee87d" @@ -170,8 +166,6 @@ def test_parse_register_and_set_all_roles(self): ) result_log = TransactionLogs("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", [result_event]) sc_result = SmartContractResult( - hash="777a55e938a76ae57c832e235ee6360c03f4d80e7fee10ed5e71a9ba293d1ea2", - timestamp=1706171168, sender="erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u", receiver="erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", data="RVNEVFNldFJvbGVANGM0ZDQxNGYyZDY0Mzk2NjM4MzkzMkA0NTUzNDQ1NDUyNmY2YzY1NGM2ZjYzNjE2YzRkNjk2ZTc0QDQ1NTM0NDU0NTI2ZjZjNjU0YzZmNjM2MTZjNDI3NTcyNmU=", @@ -340,8 +334,6 @@ def test_parse_freeze(self): ) tx_log = TransactionLogs("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", [event]) sc_result = SmartContractResult( - hash="db39f9a792f56641aff6196d542d6aff437a6cb8b39c78c6b8b48b5a7830d714", - timestamp=1706177672, sender="erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u", receiver="erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", data="RVNEVEZyZWV6ZUA0MTQxNDEyZDMyMzk2MzM0NjMzOQ==", @@ -374,8 +366,6 @@ def test_parse_unfreeze(self): ) tx_log = TransactionLogs("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", [event]) sc_result = SmartContractResult( - hash="db39f9a792f56641aff6196d542d6aff437a6cb8b39c78c6b8b48b5a7830d714", - timestamp=1706177672, sender="erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u", receiver="erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", data="RVNEVEZyZWV6ZUA0MTQxNDEyZDMyMzk2MzM0NjMzOQ==", @@ -408,8 +398,6 @@ def test_parse_wipe(self): ) tx_log = TransactionLogs("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", [event]) sc_result = SmartContractResult( - hash="6e5baf006ee871d856360ae7ac4e4e3b3ad756db532b218601f236461583c511", - timestamp=1706177672, sender="erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u", receiver="erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", data="RVNEVEZyZWV6ZUA0MTQxNDEyZDMyMzk2MzM0NjMzOQ==",