def as_contract_id(cid, template_id=None):
    """
    Convert something that resembles a contract ID to a :class:`ContractId` or
    :class:`RelativeContractRef`.
    """
    from .core import ContractId
    if cid is None:
        raise ValueError('cid is required')
    elif isinstance(cid, str):
        return ContractId(cid, template_id)
    elif isinstance(cid, ContractId):
        return cid
    raise TypeError('Could not serialize an object to a contract ID: {!r}'.
        format(cid))
