def clear_if_finalized(
        iteration: TransitionResult,
) -> TransitionResult[InitiatorPaymentState]:
    """ Clear the initiator payment task if all transfers have been finalized
    or expired. """
    state = cast(InitiatorPaymentState, iteration.new_state)

    if state is None:
        return iteration

    if len(state.initiator_transfers) == 0:
        return TransitionResult(None, iteration.events)

    return iteration
Clear the initiator payment task if all transfers have been finalized
    or expired.