# Buggy code
def transfer_tasks_view(
    transfer_tasks: Dict[SecretHash, TransferTask],
    token_address: TokenAddress = None,
    channel_id: ChannelID = None,
) -> List[Dict[str, Any]]:
    view = list()

    for secrethash, transfer_task in transfer_tasks.items():
        transfer, role = get_transfer_from_task(secrethash, transfer_task)

        if transfer is None:
            continue
        if token_address is not None:
            if transfer.token != token_address:
                continue
            elif channel_id is not None:
                if transfer.balance_proof.channel_identifier != channel_id:
                    continue

        view.append(flatten_transfer(transfer, role))

    return view


# Traceback (KeyError)
2019-12-11 10:18:23.082312 [error    ] UnlockFailed!                  [raiden.raiden_event_handler] node=0x65A148DE5d57279EB9FdFe8c11cF9A43E599a537 reason=route was canceled secrethash=0xa045514824f5fd112d2f42a67e26ec965dce904a2edd90dfdda77fc882339275
2019-12-11 10:18:23.083932 [error    ] Error processing request       [raiden.api.rest] errors=Payment couldn't be completed because: there is no route available status_code=<HTTPStatus.CONFLICT: 409>
2019-12-11 10:18:23.084729 [info     ] 127.0.0.1 - - [2019-12-11 11:18:23] "POST /api/v1/payments/0x31aA9D3E2bd38d22CA3Ae9be7aae1D518fe46043/0x1111111111111111111111111111111111111111 HTTP/1.1" 409 288 1.289927 [raiden.api.rest.pywsgi]
2019-12-11 10:18:24.453252 [info     ] 127.0.0.1 - - [2019-12-11 11:18:24] "GET /api/v1/channels HTTP/1.1" 200 2721 0.011393 [raiden.api.rest.pywsgi]
2019-12-11 10:18:27.349499 [critical ] Unhandled exception when processing endpoint request [raiden.api.rest] node=0x65A148DE5d57279EB9FdFe8c11cF9A43E599a537
Traceback (most recent call last):
File "/home/manuel/.virtualenvs/raiden-nightly/lib/python3.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "/home/manuel/.virtualenvs/raiden-nightly/lib/python3.7/site-packages/flask/app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/manuel/.virtualenvs/raiden-nightly/lib/python3.7/site-packages/flask_restful/__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "/home/manuel/.virtualenvs/raiden-nightly/lib/python3.7/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/home/manuel/.virtualenvs/raiden-nightly/lib/python3.7/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/manuel/Projects/raiden/raiden/raiden/api/v1/resources.py", line 298, in get
return self.rest_api.get_pending_transfers()
File "/home/manuel/Projects/raiden/raiden/raiden/api/rest.py", line 1403, in get_pending_transfers
token_address=token_address, partner_address=partner_address
File "/home/manuel/Projects/raiden/raiden/raiden/api/python.py", line 1285, in get_pending_transfers
return transfer_tasks_view(transfer_tasks, token_address, channel_id)
File "/home/manuel/Projects/raiden/raiden/raiden/api/python.py", line 154, in transfer_tasks_view
transfer, role = get_transfer_from_task(secrethash, transfer_task)
File "/home/manuel/Projects/raiden/raiden/raiden/api/python.py", line 131, in get_transfer_from_task
transfer = transfer_task.manager_state.initiator_transfers[secrethash].transfer
KeyError: b'Hv\x1e\x1a\xdb\xccZ-\\k\x93\x05$\x0e\xad\tX\x1a\x96|\x15\x96^^\xb1#G\xb9\xfc\xf0\xe0f'
2019-12-11 10:18:28.355096 [info     ] 127.0.0.1 - - [2019-12-11 11:18:28] "GET /api/v1/pending_transfers HTTP/1.1" 500 161 1.007112 [raiden.api.rest.pywsgi]
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 716, in gevent._greenlet.Greenlet.run
File "/home/manuel/Projects/raiden/raiden/raiden/api/rest.py", line 441, in _run
self.wsgiserver.serve_forever()
File "/home/manuel/.virtualenvs/raiden-nightly/lib/python3.7/site-packages/gevent/baseserver.py", line 364, in serve_forever
self._stop_event.wait()
File "src/gevent/event.py", line 240, in gevent._event.Event.wait
File "src/gevent/event.py", line 140, in gevent._event._AbstractLinkable._wait
File "src/gevent/event.py", line 117, in gevent._event._AbstractLinkable._wait_core
File "src/gevent/event.py", line 119, in gevent._event._AbstractLinkable._wait_core
File "src/gevent/_greenlet_primitives.py", line 59, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 59, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 63, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
KeyError: b'Hv\x1e\x1a\xdb\xccZ-\\k\x93\x05$\x0e\xad\tX\x1a\x96|\x15\x96^^\xb1#G\xb9\xfc\xf0\xe0f'
2019-12-11T10:18:28Z <Greenlet "APIServer|Greenlet-5" at 0x7f8d493edb90: <bound method APIServer._run of <raiden.api.rest.APIServer object at 0x7f8d4a7cdd10>>> failed with KeyError

# Fixed code
def transfer_tasks_view(
    transfer_tasks: Dict[SecretHash, TransferTask],
    token_address: TokenAddress = None,
    channel_id: ChannelID = None,
) -> List[Dict[str, Any]]:
    view = list()

    for secrethash, transfer_task in transfer_tasks.items():
        transfer = get_transfer_from_task(secrethash, transfer_task)

        if transfer is None:
            continue
        if token_address is not None:
            if transfer.token != token_address:
                continue
            elif channel_id is not None:
                if transfer.balance_proof.channel_identifier != channel_id:
                    continue

        role = views.role_from_transfer_task(transfer_task)
        view.append(flatten_transfer(transfer, role))

    return view