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

Implemented token management transactions parser #8

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 0 additions & 4 deletions multiversx_sdk/core/transaction_outcome_parsers/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ def __init__(self,
sender: str = "",
receiver: str = "",
data: str = "",
original_tx_hash: str = "",
miniblock_hash: str = "",
logs: TransactionLogs = TransactionLogs()) -> None:
self.hash = hash
self.timestamp = timestamp
self.sender = sender
self.receiver = receiver
self.data = data
self.original_tx_hash = original_tx_hash
self.miniblock_hash = miniblock_hash
self.logs = logs


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,25 +232,29 @@ def gather_all_events(self, transaction_outcome: TransactionOutcome) -> List[Tra
return all_events

def extract_token_identifier(self, event: TransactionEvent) -> str:
if event.topics[0]:
hex_ticker = base64.b64decode(event.topics[0]).hex()
return bytes.fromhex(hex_ticker).decode()
return ""
if not event.topics[0]:
return ""

ticker = base64.b64decode(event.topics[0])
return ticker.decode()

def extract_nonce(self, event: TransactionEvent) -> int:
if event.topics[1]:
nonce = base64.b64decode(event.topics[1])
return decode_unsigned_number(nonce)
return 0
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:
if event.topics[2]:
amount = base64.b64decode(event.topics[2])
return decode_unsigned_number(amount)
return 0
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:
if event.topics[3]:
hex_address = base64.b64decode(event.topics[3]).hex()
return Address.new_from_hex(hex_address, DEFAULT_HRP).to_bech32()
return ""
if not event.topics[3]:
return ""

address = base64.b64decode(event.topics[3])
return Address(address, DEFAULT_HRP).to_bech32()
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import base64
import re

import pytest

from multiversx_sdk.core.address import Address
from multiversx_sdk.core.codec import decode_unsigned_number
from multiversx_sdk.core.errors import ParseTransactionOutcomeError
from multiversx_sdk.core.transaction_outcome_parsers.resources import (
SmartContractResult, TransactionEvent, TransactionLogs, TransactionOutcome)
from multiversx_sdk.core.transaction_outcome_parsers.token_management_transactions_outcome_parser import \
TokenManagementTransactionsOutcomeParser
from multiversx_sdk.network_providers.constants import DEFAULT_ADDRESS_HRP


class TestTokenManagementTransactionsOutcomeParser:
Expand Down Expand Up @@ -49,7 +53,7 @@ def test_parse_issue_fungible(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_issue_fungible(tx_results_and_logs)
assert outcome.identifier == "ZZZ-9ee87d"
assert outcome.identifier == base64.b64decode(event.topics[0]).decode()
Copy link
Contributor

Choose a reason for hiding this comment

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

i think it's more visible to have the identifier in plain form and then as encoded, something like this

        identifier = "ZZZ-9ee87d"
        identifier_base64 = base64.b64encode(identifier.encode()).decode()

        event = TransactionEvent(
            address="erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2",
            identifier="issue",
            topics=[
                identifier_base64,
                "U0VDT05E",
                "Wlpa",
                "RnVuZ2libGVFU0RU",
                "Ag=="
            ]
        )
        empty_result = SmartContractResult()
        tx_log = TransactionLogs("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", [event])
        tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

        outcome = self.parser.parse_issue_fungible(tx_results_and_logs)
        assert outcome.identifier == identifier

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

replaced the identifier as described above


def test_parse_issue_non_fungible(self):
first_event = TransactionEvent(
Expand Down Expand Up @@ -88,7 +92,7 @@ def test_parse_issue_non_fungible(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_issue_non_fungible(tx_results_and_logs)
assert outcome.identifier == "NFT-f01d1e"
assert outcome.identifier == base64.b64decode(third_event.topics[0]).decode()

def test_parse_issue_semi_fungible(self):
event = TransactionEvent(
Expand All @@ -106,7 +110,7 @@ def test_parse_issue_semi_fungible(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_issue_semi_fungible(tx_results_and_logs)
assert outcome.identifier == "SEMIFNG-2c6d9f"
assert outcome.identifier == base64.b64decode(event.topics[0]).decode()

def test_parse_register_meta_esdt(self):
event = TransactionEvent(
Expand All @@ -124,7 +128,7 @@ def test_parse_register_meta_esdt(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_register_meta_esdt(tx_results_and_logs)
assert outcome.identifier == "METATEST-e05d11"
assert outcome.identifier == base64.b64decode(event.topics[0]).decode()

def test_parse_register_and_set_all_roles(self):
event = TransactionEvent(
Expand Down Expand Up @@ -158,16 +162,14 @@ def test_parse_register_and_set_all_roles(self):
sender="erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u",
receiver="erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2",
data="RVNEVFNldFJvbGVANGM0ZDQxNGYyZDY0Mzk2NjM4MzkzMkA0NTUzNDQ1NDUyNmY2YzY1NGM2ZjYzNjE2YzRkNjk2ZTc0QDQ1NTM0NDU0NTI2ZjZjNjU0YzZmNjM2MTZjNDI3NTcyNmU=",
original_tx_hash="24b8bb9782c07092e8ce75b819690de58839f650a1287e27bbcc652c6d310664",
miniblock_hash="042835c44b06cf0cbf0d26e903873a71f1418598dbbf8dde5f5e1616498ffa03",
logs=result_log
)

tx_results_and_logs = TransactionOutcome([sc_result], tx_log)
outcome = self.parser.parse_register_and_set_all_roles(tx_results_and_logs)

assert outcome.token_identifier == "LMAO-d9f892"
assert outcome.roles == ["ESDTRoleLocalMint", "ESDTRoleLocalBurn"]
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.roles == [base64.b64decode(result_event.topics[3]).decode(), base64.b64decode(result_event.topics[4]).decode()]

def test_parse_set_special_role(self):
event = TransactionEvent(
Expand All @@ -188,8 +190,12 @@ def test_parse_set_special_role(self):

outcome = self.parser.parse_set_special_role(tx_results_and_logs)
assert outcome.user_address == "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"
assert outcome.token_identifier == "METATEST-e05d11"
assert outcome.roles == ["ESDTRoleNFTCreate", "ESDTRoleNFTAddQuantity", "ESDTRoleNFTBurn"]
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.roles == [
base64.b64decode(event.topics[3]).decode(),
base64.b64decode(event.topics[4]).decode(),
base64.b64decode(event.topics[5]).decode()
]

def test_parse_nft_create(self):
event = TransactionEvent(
Expand All @@ -207,9 +213,9 @@ def test_parse_nft_create(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_nft_create(tx_results_and_logs)
assert outcome.token_identifier == "NFT-f01d1e"
assert outcome.nonce == 1
assert outcome.initial_quantity == 1
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == decode_unsigned_number(base64.b64decode(event.topics[1]))
assert outcome.initial_quantity == decode_unsigned_number(base64.b64decode(event.topics[2]))

def test_parse_local_mint(self):
event = TransactionEvent(
Expand All @@ -226,10 +232,10 @@ def test_parse_local_mint(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_local_mint(tx_results_and_logs)
assert outcome.user_address == "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"
assert outcome.token_identifier == "AAA-29c4c9"
assert outcome.user_address == event.address
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == 0
assert outcome.minted_supply == 100000
assert outcome.minted_supply == decode_unsigned_number(base64.b64decode(event.topics[2]))

def test_parse_local_burn(self):
event = TransactionEvent(
Expand All @@ -246,10 +252,10 @@ def test_parse_local_burn(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_local_burn(tx_results_and_logs)
assert outcome.user_address == "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"
assert outcome.token_identifier == "AAA-29c4c9"
assert outcome.user_address == event.address
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == 0
assert outcome.burnt_supply == 100000
assert outcome.burnt_supply == decode_unsigned_number(base64.b64decode(event.topics[2]))

def test_parse_pause(self):
event = TransactionEvent(
Expand All @@ -264,7 +270,7 @@ def test_parse_pause(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_pause(tx_results_and_logs)
assert outcome.identifier == "AAA-29c4c9"
assert outcome.identifier == base64.b64decode(event.topics[0]).decode()

def test_parse_unpause(self):
event = TransactionEvent(
Expand All @@ -279,7 +285,7 @@ def test_parse_unpause(self):
tx_results_and_logs = TransactionOutcome([empty_result], tx_log)

outcome = self.parser.parse_unpause(tx_results_and_logs)
assert outcome.identifier == "AAA-29c4c9"
assert outcome.identifier == base64.b64decode(event.topics[0]).decode()

def test_parse_freeze(self):
event = TransactionEvent(
Expand All @@ -299,17 +305,15 @@ def test_parse_freeze(self):
sender="erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u",
receiver="erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
data="RVNEVEZyZWV6ZUA0MTQxNDEyZDMyMzk2MzM0NjMzOQ==",
original_tx_hash="1594205eff82126a8c34da4db46fcf6a7838b953317f6c1186b6a514be91895d",
miniblock_hash="a6a783d73d61d93041aa04382107f92a704fcfcf42567bdd7a045c7cd5c97c4a",
logs=tx_log
)
tx_results_and_logs = TransactionOutcome([sc_result], TransactionLogs())

outcome = self.parser.parse_freeze(tx_results_and_logs)
assert outcome.user_address == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"
assert outcome.token_identifier == "AAA-29c4c9"
assert outcome.user_address == Address(base64.b64decode(event.topics[3]), DEFAULT_ADDRESS_HRP).to_bech32()
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == 0
assert outcome.balance == 10000000
assert outcome.balance == decode_unsigned_number(base64.b64decode(event.topics[2]))

def test_parse_unfreeze(self):
event = TransactionEvent(
Expand All @@ -329,17 +333,15 @@ def test_parse_unfreeze(self):
sender="erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u",
receiver="erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
data="RVNEVEZyZWV6ZUA0MTQxNDEyZDMyMzk2MzM0NjMzOQ==",
original_tx_hash="1594205eff82126a8c34da4db46fcf6a7838b953317f6c1186b6a514be91895d",
miniblock_hash="a6a783d73d61d93041aa04382107f92a704fcfcf42567bdd7a045c7cd5c97c4a",
logs=tx_log
)
tx_results_and_logs = TransactionOutcome([sc_result], TransactionLogs())

outcome = self.parser.parse_unfreeze(tx_results_and_logs)
assert outcome.user_address == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"
assert outcome.token_identifier == "AAA-29c4c9"
assert outcome.user_address == Address(base64.b64decode(event.topics[3]), DEFAULT_ADDRESS_HRP).to_bech32()
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == 0
assert outcome.balance == 10000000
assert outcome.balance == decode_unsigned_number(base64.b64decode(event.topics[2]))

def test_parse_wipe(self):
event = TransactionEvent(
Expand All @@ -359,17 +361,15 @@ def test_parse_wipe(self):
sender="erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u",
receiver="erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
data="RVNEVEZyZWV6ZUA0MTQxNDEyZDMyMzk2MzM0NjMzOQ==",
original_tx_hash="1594205eff82126a8c34da4db46fcf6a7838b953317f6c1186b6a514be91895d",
miniblock_hash="a6a783d73d61d93041aa04382107f92a704fcfcf42567bdd7a045c7cd5c97c4a",
logs=tx_log
)
tx_results_and_logs = TransactionOutcome([sc_result], TransactionLogs())

outcome = self.parser.parse_wipe(tx_results_and_logs)
assert outcome.user_address == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"
assert outcome.token_identifier == "AAA-29c4c9"
assert outcome.user_address == Address(base64.b64decode(event.topics[3]), DEFAULT_ADDRESS_HRP).to_bech32()
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == 0
assert outcome.balance == 10000000
assert outcome.balance == decode_unsigned_number(base64.b64decode(event.topics[2]))

def test_parse_update_attributes(self):
event = TransactionEvent(
Expand All @@ -387,8 +387,9 @@ def test_parse_update_attributes(self):
tx_results_and_logs = TransactionOutcome([tx_result], tx_log)

outcome = self.parser.parse_update_attributes(tx_results_and_logs)
assert outcome.token_identifier == "NFT-f01d1e"
assert outcome.nonce == 1
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == decode_unsigned_number(base64.b64decode(event.topics[1]))
assert outcome.attributes == base64.b64decode(event.topics[3])
assert outcome.attributes.decode() == "metadata:ipfsCID/test.json;tags:tag1,tag2"

def test_parse_add_quantity(self):
Expand All @@ -406,9 +407,9 @@ def test_parse_add_quantity(self):
tx_results_and_logs = TransactionOutcome([tx_result], tx_log)

outcome = self.parser.parse_add_quantity(tx_results_and_logs)
assert outcome.token_identifier == "SEMIFNG-2c6d9f"
assert outcome.nonce == 1
assert outcome.added_quantity == 10
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == decode_unsigned_number(base64.b64decode(event.topics[1]))
assert outcome.added_quantity == decode_unsigned_number(base64.b64decode(event.topics[2]))

def test_parse_burn_quantity(self):
event = TransactionEvent(
Expand All @@ -425,6 +426,6 @@ def test_parse_burn_quantity(self):
tx_results_and_logs = TransactionOutcome([tx_result], tx_log)

outcome = self.parser.parse_burn_quantity(tx_results_and_logs)
assert outcome.token_identifier == "SEMIFNG-2c6d9f"
assert outcome.nonce == 1
assert outcome.burnt_quantity == 16
assert outcome.token_identifier == base64.b64decode(event.topics[0]).decode()
assert outcome.nonce == decode_unsigned_number(base64.b64decode(event.topics[1]))
assert outcome.burnt_quantity == decode_unsigned_number(base64.b64decode(event.topics[2]))
Loading