1- import re
21from binascii import unhexlify
32from typing import Optional
43from crypto .enums .constants import Constants
4+ from crypto .enums .contract_abi_type import ContractAbiType
55from crypto .transactions .types .abstract_transaction import AbstractTransaction
66from crypto .transactions .types .transfer import Transfer
77from crypto .transactions .types .evm_call import EvmCall
8+ from crypto .transactions .types .username_registration import UsernameRegistration
9+ from crypto .transactions .types .username_resignation import UsernameResignation
810from crypto .transactions .types .vote import Vote
911from crypto .transactions .types .unvote import Unvote
1012from crypto .transactions .types .validator_registration import ValidatorRegistration
1315from crypto .enums .abi_function import AbiFunction
1416from crypto .utils .abi_decoder import AbiDecoder
1517from crypto .utils .rlp_decoder import RlpDecoder
18+ from crypto .utils .transaction_utils import TransactionUtils
1619
1720class Deserializer :
1821 SIGNATURE_SIZE = 64
@@ -59,30 +62,40 @@ def guess_transaction_from_data(self, data: dict) -> AbstractTransaction:
5962 if data ['value' ] != '0' :
6063 return Transfer (data )
6164
62- payload_data = self .decode_payload (data )
65+ consensus_payload_data = self .decode_payload (data )
66+ if consensus_payload_data is not None :
67+ function_name = consensus_payload_data .get ('functionName' )
68+ if function_name == AbiFunction .VOTE .value :
69+ return Vote (data )
6370
64- if payload_data is None :
65- return EvmCall (data )
71+ if function_name == AbiFunction . UNVOTE . value :
72+ return Unvote (data )
6673
67- function_name = payload_data .get ('functionName' )
68- if function_name == AbiFunction .VOTE .value :
69- return Vote (data )
70- elif function_name == AbiFunction .UNVOTE .value :
71- return Unvote (data )
72- elif function_name == AbiFunction .VALIDATOR_REGISTRATION .value :
73- return ValidatorRegistration (data )
74- elif function_name == AbiFunction .VALIDATOR_RESIGNATION .value :
75- return ValidatorResignation (data )
76- else :
77- return EvmCall (data )
74+ if function_name == AbiFunction .VALIDATOR_REGISTRATION .value :
75+ return ValidatorRegistration (data )
7876
79- def decode_payload (self , data : dict ) -> Optional [dict ]:
77+ if function_name == AbiFunction .VALIDATOR_RESIGNATION .value :
78+ return ValidatorResignation (data )
79+
80+ username_payload_data = self .decode_payload (data , ContractAbiType .USERNAMES )
81+ if username_payload_data is not None :
82+ function_name = username_payload_data .get ('functionName' )
83+ if function_name == AbiFunction .USERNAME_REGISTRATION .value :
84+ return UsernameRegistration (data )
85+
86+ if function_name == AbiFunction .USERNAME_RESIGNATION .value :
87+ return UsernameResignation (data )
88+
89+ return EvmCall (data )
90+
91+ @staticmethod
92+ def decode_payload (data : dict , abi_type : ContractAbiType = ContractAbiType .CONSENSUS ) -> Optional [dict ]:
8093 payload = data .get ('data' , '' )
8194
8295 if payload == '' :
8396 return None
8497
85- decoder = AbiDecoder ()
98+ decoder = AbiDecoder (abi_type )
8699 try :
87100 return decoder .decode_function_data (payload )
88101 except Exception as e :
@@ -100,7 +113,7 @@ def parse_big_number(value: str) -> str:
100113
101114 @staticmethod
102115 def parse_hex (value : str ) -> str :
103- return re . sub ( r'^0x' , '' , value )
116+ return TransactionUtils . parse_hex_from_str ( value )
104117
105118 @staticmethod
106119 def parse_address (value : str ) -> Optional [str ]:
0 commit comments