Traceback (most recent call last):
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 137, in <module>
    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 135, in main
    await display_transactions(block_start, block_end)
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 129, in display_transactions
    await asyncio.gather(*tasks)
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 120, in process_block
    age = format_age(timestamp)
          ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject4\main.py", line 101, in format_age
    age = int(datetime.now().timestamp()) - int(timestamp, 16)
                                            ^^^^^^^^^^^^^^^^^^
TypeError: int() can't convert non-string with explicit base

Process finished with exit code 1


The code that resulted in the error is shown below.

# Оптимизированный!!! Возвращает адреса созданных токенов в указанном интревале блоков (Обрабатывет множество блоков)

import asyncio
import aiohttp
from datetime import datetime
import re

bscscan_api_key = '<TRUFFLEHOG_REDACTED_BSCSCAN>'

# Define ABI request URLs
abi_url = f'https://api.bscscan.com/api?module=contract&action=getabi&apikey={bscscan_api_key}'

# Define the contract method signatures
name_signature = '0x06fdde03'
symbol_signature = '0x95d89b41'

# Create a semaphore with a limit of n
semaphore = asyncio.Semaphore(5)

async def get_external_transactions(block_number):
    async with semaphore:
        async with aiohttp.ClientSession() as session:
            url = f'https://api.bscscan.com/api?module=proxy&action=eth_getBlockByNumber&tag={block_number}&boolean=true&apikey={bscscan_api_key}'

            try:
                async with session.get(url) as response:
                    data = await response.json()
            except Exception as e:
                print(f'Error in API request: {e}')
                return []

            if data['result'] is None or isinstance(data['result'], str):
                print(f"Error: Cannot find the block")
                return []

            return data['result'].get('transactions', [])

async def get_contract_address(tx_hash):
    async with semaphore:
        async with aiohttp.ClientSession() as session:
            url = f'https://api.bscscan.com/api?module=proxy&action=eth_getTransactionReceipt&txhash={tx_hash}&apikey={bscscan_api_key}'

            try:
                async with session.get(url) as response:
                    data = await response.json()
            except Exception as e:
                print(f'Error in API request: {e}')
                return None

            if data['result'] is None or not isinstance(data['result'], dict):
                return None

            return data['result'].get('contractAddress')

async def get_block_timestamp(block_number):
    async with semaphore:
        async with aiohttp.ClientSession() as session:
            url = f'https://api.bscscan.com/api?module=proxy&action=eth_getBlockByNumber&tag={block_number}&boolean=true&apikey={bscscan_api_key}'

            try:
                async with session.get(url) as response:
                    data = await response.json()
            except Exception as e:
                print(f'Error in API request: {e}')
                return None

            if data['result'] is None or isinstance(data['result'], str):
                print(f"Error: Cannot find the block")
                return None

            return data['result'].get('timestamp')

def is_hex(string):
    return re.fullmatch(r"^(0x)?[0-9a-fA-F]*$", string) is not None

async def get_contract_info(contract_address, method_signature):
    async with semaphore:
        async with aiohttp.ClientSession() as session:
            url = f'https://api.bscscan.com/api?module=proxy&action=eth_call&to={contract_address}&data={method_signature}&tag=latest&apikey={bscscan_api_key}'

            try:
                async with session.get(url) as response:
                    data = await response.json()
            except Exception as e:
                print(f'Error in API request: {e}')
                return None

            result = data.get('result')
            if is_hex(result):
                # Normal execution
                result = bytes.fromhex(result[2:]).decode()
            else:
                result = "Error: Invalid hex string"
            return result

def check_method_id(input_data):
    method_id = input_data[:10]
    return method_id[-4:] == '6040'

def format_age(timestamp):
    age = int(datetime.now().timestamp()) - int(timestamp, 16)
    d, rem = divmod(age, 86400)
    h, rem = divmod(rem, 3600)
    m, s = divmod(rem, 60)
    return f"{d}d {h}h {m}m {s}s" if d else f"{h}h {m}m {s}s" if h else f"{m}m {s}s" if m else f"{s}s"
async def display_transactions(block_start, block_end):
    async def process_block(block_number_int):
        block_number = hex(block_number_int)
        transactions = await get_external_transactions(block_number)

        if not transactions:
            print(f'No transactions found in block {block_number_int}')
        else:
            print(f'Transactions in block {block_number_int}:')
            for tx in transactions:
                if tx['to'] is None:
                    if check_method_id(tx['input']):
                        contract_address = await get_contract_address(tx['hash'])
                        timestamp = await get_block_timestamp(block_number)
                        age = format_age(timestamp)
                        token_name = await get_contract_info(contract_address, name_signature)
                        token_symbol = await get_contract_info(contract_address, symbol_signature)
                        print(f'New contract creation: Contract Address: {contract_address}')
                        print(f'Token Name: {token_name}, Token Symbol: {token_symbol}')
                        print(f'Age of the token address: {age}')
            print("\n")  # Print an empty line between blocks

    tasks = [process_block(block_number) for block_number in range(block_start, block_end + 1)]
    await asyncio.gather(*tasks)

async def main():
    block_start = 28466587  # Replace with your desired starting block number
    block_end = 28466640  # Replace with your desired ending block number

    await display_transactions(block_start, block_end)

asyncio.run(main())

Fix it