Register the secret on-chain if the payer channel is already closed and
    the mediator learned the secret off-chain.

    Balance proofs are not exchanged for closed channels, so there is no reason
    to wait for the unsafe region to register secret.

    Note:

        If the secret is learned before the channel is closed, then the channel
        will register the secrets in bulk, not the transfer.

def events_for_onchain_secretreveal_if_closed(
        channelmap: ChannelMap,
        transfers_pair: List[MediationPairState],
        secret: Secret,
        secrethash: SecretHash,
        block_hash: BlockHash,
) -> List[Event]:
    """ Register the secret on-chain if the payer channel is already closed and
    the mediator learned the secret off-chain.

    Balance proofs are not exchanged for closed channels, so there is no reason
    to wait for the unsafe region to register secret.

    Note:

        If the secret is learned before the channel is closed, then the channel
        will register the secrets in bulk, not the transfer.
    """
    events: List[Event] = list()

    all_payer_channels = []
    for pair in transfers_pair:
        channel_state = get_payer_channel(channelmap, pair)
        if channel_state:
            all_payer_channels.append(channel_state)
    transaction_sent = has_secret_registration_started(
        all_payer_channels,
        transfers_pair,
        secrethash,
    )

    # Just like the case for entering the danger zone, this will only consider
    # the transfers which have a pair.

    for pending_pair in get_pending_transfer_pairs(transfers_pair):
        payer_channel = get_payer_channel(channelmap, pending_pair)
        # Don't register the secret on-chain if the channel is open or settled
        if payer_channel and channel.get_status(payer_channel) == CHANNEL_STATE_CLOSED:
            pending_pair.payer_state = 'payer_waiting_secret_reveal'

            if not transaction_sent:
                partner_state = payer_channel.partner_state

                lock = channel.get_lock(partner_state, secrethash)

                # The mediator task lives as long as there are any pending
                # locks, it may be the case that some of the transfer_pairs got
                # resolved off-chain, but others didn't. For this reason we
                # must check if the lock is still part of the channel
                if lock:
                    reveal_events = secret_registry.events_for_onchain_secretreveal(
                        channel_state=payer_channel,
                        secret=secret,
                        expiration=lock.expiration,
                        block_hash=block_hash,
                    )
                    events.extend(reveal_events)
                    transaction_sent = True

    return events