from typing import Any
from typing import Optional
from typing import Type


def load_contract_transaction_interface_from_module(contract_mod: Any,
                                                    contract_name: Optional[str] = None) -> Type:
    """
    Load contract class from transaction interface module

    :param contract_mod: loaded transaction interface module
    :param contract_name: contract name, only required if file contains multiple contracts
    :return: Contract class
    """

    contracts = {}
    for name, cls in contract_mod.__dict__.items():
        if isinstance(cls, type) and 'ContractSimulator' in [b.__name__ for b in cls.__bases__]:
            contracts[cls.__name__] = cls

    if contract_name is None:
        if len(contracts) != 1:
            raise ValueError('If file contains multiple contracts, contract name must be specified')
        return next(iter(contracts.values()))
    else:
        return contracts[contract_name]