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

import asyncio
import aiohttp

bscscan_api_key = 'CXTB4IUT31N836G93ZI3YQBEWBQEGGH5QS'

api_semaphore = asyncio.Semaphore(5)

async def api_call(session, url):
    """Wrapper around aiohttp request to add retries with exponential backoff."""
    retries = 0
    while retries < 5:
        try:
            async with session.get(url) as response:
                data = await response.json()
            if data.get("status") == "1":
                return data
        except Exception as e:
            print(f"Error in API request: {e}")
        retries += 1
        await asyncio.sleep(2 ** retries)  # Exponential backoff
    raise Exception("Failed API request after retries.")


async def get_external_transactions(block_number):
    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}'
        async with api_semaphore:
            data = await api_call(session, url)

        if data['result'] is None or not isinstance(data['result'], dict):
            print(f"Error: Cannot find the block {block_number}")
            return []

        return data['result']['transactions']


async def get_contract_address(tx_hash):
    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}'
        async with api_semaphore:
            data = await api_call(session, url)

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

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


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

        if not data.get('result') or data['result'][:2] == '0x':
            return None

        return bytes.fromhex(data['result'][2:]).decode('utf-8')


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'Tokens created in block {block_number_int}:')
            for tx in transactions:
                if tx['to'] is None:
                    contract_address = await get_contract_address(tx['hash'])
                    if contract_address:
                        contract_name = await get_contract_name(contract_address)
                        if contract_name:
                            print(f'Contract Address: {contract_address} - Contract Name: {contract_name}')
                        else:
                            print(f'Contract Address: {contract_address} - Contract Name not found')
            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 = 28557162  # Replace with your desired starting block number
    block_end = 28557199  # Replace with your desired ending block number

    await display_transactions(block_start, block_end)

asyncio.run(main())

The above code does not display the generated token addresses in the specified range block. The answer is below

C:\Users\AshotxXx\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\AshotxXx\PycharmProjects\pythonProject\main.py 
Traceback (most recent call last):
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject\main.py", line 94, 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\pythonProject\main.py", line 92, in main
    await display_transactions(block_start, block_end)
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject\main.py", line 86, in display_transactions
    await asyncio.gather(*tasks)
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject\main.py", line 68, in process_block
    transactions = await get_external_transactions(block_number)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject\main.py", line 31, in get_external_transactions
    data = await api_call(session, url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AshotxXx\PycharmProjects\pythonProject\main.py", line 24, in api_call
    raise Exception("Failed API request after retries.")
Exception: Failed API request after retries.

Process finished with exit code 1

Fix it