Traceback (most recent call last): File…
- User
Traceback (most recent call last): File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 127, in asyncio.run(main()) File "C:\Users\AshotxXx\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Users\AshotxXx\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\AshotxXx\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 125, in main await display_transactions(block_start, block_end) File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 119, in display_transactions await asyncio.gather(*tasks) File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 111, in process_block token_name = await get_contract_info(contract_address, name_signature) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 89, in get_contract_info result = bytes.fromhex(result[2:]).decode() ^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: non-hexadecimal number found in fromhex() arg at position 0
Process finished with exit code 1
The code that resulted in the error is shown below.
Traceback (most recent call last): File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 127, in asyncio.run(main()) File "C:\Users\AshotxXx\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Users\AshotxXx\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\AshotxXx\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 125, in main await display_transactions(block_start, block_end) File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 119, in display_transactions await asyncio.gather(*tasks) File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 111, in process_block token_name = await get_contract_info(contract_address, name_signature) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 89, in get_contract_info result = bytes.fromhex(result[2:]).decode() ^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: non-hexadecimal number found in fromhex() arg at position 0
Process finished with exit code 1
- Assistant
The error is caused by trying to convert a string to bytes using the
fromhex()function, which expects a hexadecimal string. The error message "non-hexadecimal number found in fromhex() arg at position 0" indicates that there is an invalid character or value at position 0.To fix this error, check the input string
resultbefore calling thefromhex()function and make sure it's a valid hexadecimal string. You can add a check using a regular expression to verify if theresultstring contains only valid hexadecimal characters:-
Import the
remodule at the top of your file:import re -
Add a condition to check if the
resultstring is a valid hexadecimal string:def is_hex(string): return re.fullmatch(r"^(0x)?[0-9a-fA-F]*$", string) is not None # In your get_contract_info function def get_contract_info(address: str, signature: str) -> str: ... if is_hex(result): # Normal execution result = bytes.fromhex(result[2:]).decode() else: result = "Error: Invalid hex string" return result
This should handle the cases where the input string is not a valid hexadecimal value and prevent the
ValueErrorfrom being raised. However, this is just a patch, and you should investigate why the invalid hex string is being returned in the first place. You might need to check if the API you are calling is returning the expected data or if there are any issues with thecontract_addressandname_signaturevalues being passed to theget_contract_infofunction. -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.