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

import asyncio
import aiohttp
import time

bscscan_api_key = '<TRUFFLEHOG_REDACTED_BSCSCAN>'

# Create a semaphore with a limit of 3
semaphore = asyncio.Semaphore(1)

async def get_latest_block_number():
    async with aiohttp.ClientSession() as session:
        url = f'https://api.bscscan.com/api?module=proxy&action=eth_blockNumber&apikey={bscscan_api_key}'
        async with session.get(url) as response:
            data = await response.json()
            return int(data['result'], 16)

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):
                print(f"Error: Cannot find the address")
                return None

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

async def contract_id_ends_with_6040(contract_address):
    async with aiohttp.ClientSession() as session:
        url = f"https://api.bscscan.com/api?module=contract&action=getsourcecode&address={contract_address}&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 False

        if data["result"]:
            contract_id = data["result"][0].get("ContractName", "")
            return contract_id.lower().endswith("6040")

        return False

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:
                contract_address = await get_contract_address(tx['hash'])
                if contract_address:
                    print(f'New contract creation: Contract Address: {contract_address}')
        print("\n")  # Print an empty line between blocks

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

async def main():
    block_start = await get_latest_block_number()  # Start with the latest block number
    block_end = block_start + 10  # Process 10 blocks initially

    while True:
        await display_transactions(block_start, block_end)

        # Update block_start and block_end to check for new blocks every 5 seconds
        block_start = block_end + 1
        block_end = await get_latest_block_number()
        time.sleep(5)

asyncio.run(main())

In the code above, returns all generated tokens, not just those with a method id that ends in 6040. Fix this