# Buggy code
def transfer_async(
    self,
    registry_address: TokenNetworkRegistryAddress,
    token_address: TokenAddress,
    amount: PaymentAmount,
    target: TargetAddress,
    identifier: PaymentID = None,
    secret: Secret = None,
    secrethash: SecretHash = None,
    lock_timeout: BlockTimeout = None,
) -> "PaymentStatus":
    current_state = views.state_from_raiden(self.raiden)
    token_network_registry_address = self.raiden.default_registry.address

    if not isinstance(amount, int):  # pragma: no unittest
        raise InvalidAmount("Amount not a number")

    if amount <= 0:
        raise InvalidAmount("Amount negative")

    if amount > UINT256_MAX:
        raise InvalidAmount("Amount too large")

    if not is_binary_address(token_address):
        raise InvalidBinaryAddress("token address is not valid.")

    if token_address not in views.get_token_identifiers(
        current_state, registry_address
    ):
        raise UnknownTokenAddress("Token address is not known.")

    if not is_binary_address(target):
        raise InvalidBinaryAddress("target address is not valid.")

    valid_tokens = views.get_token_identifiers(
        views.state_from_raiden(self.raiden), registry_address
    )
    if token_address not in valid_tokens:
        raise UnknownTokenAddress("Token address is not known.")

    if secret is not None and not isinstance(secret, T_Secret):
        raise InvalidSecret("secret is not valid.")

    if secrethash is not None and not isinstance(secrethash, T_SecretHash):
        raise InvalidSecretHash("secrethash is not valid.")

    if identifier is None:
        identifier = create_default_identifier()

    if identifier <= 0:
        raise InvalidPaymentIdentifier("Payment identifier cannot be 0 or negative")

    if identifier > UINT64_MAX:
        raise InvalidPaymentIdentifier("Payment identifier is too large")

    log.debug(
        "Initiating transfer",
        initiator=to_checksum_address(self.raiden.address),
        target=to_checksum_address(target),
        token=to_checksum_address(token_address),
        amount=amount,
        identifier=identifier,
    )

    token_network_address = views.get_token_network_address_by_token_address(
        chain_state=current_state,
        token_network_registry_address=token_network_registry_address,
        token_address=token_address,
    )

    if token_network_address is None:
        raise UnknownTokenAddress(
            f"Token {to_checksum_address(token_address)} is not registered "
            f"with the network {to_checksum_address(registry_address)}."
        )

    payment_status = self.raiden.mediated_transfer_async(
        token_network_address=token_network_address,
        amount=amount,
        target=target,
        identifier=identifier,
        secret=secret,
        secrethash=secrethash,
        lock_timeout=lock_timeout,
    )
    return payment_status


# Traceback (ValueError)
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._greenlet.Greenlet.run
File "site-packages/raiden/network/transport/matrix/transport.py", line 629, in _health_check_worker
File "site-packages/raiden/network/transport/matrix/transport.py", line 606, in immediate_health_check_for
File "site-packages/raiden/network/transport/matrix/transport.py", line 577, in whitelist
File "site-packages/raiden/network/transport/matrix/transport.py", line 1194, in _maybe_create_room_for_address
File "site-packages/raiden/network/transport/matrix/utils.py", line 876, in my_place_or_yours
ValueError: Addresses to compare must differ
2020-02-19T14:55:13Z <Greenlet "Greenlet<fn:_health_check_worker>" at 0x7f1f5e02a050: <bound method MatrixTransport._health_check_worker of <MatrixTransport node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9 id:585de50e-f064-4a93-b3f4-df2bf40f8078>>> failed with ValueError

2020-02-19 14:55:13.088967 [error    ] Runnable subtask died!         [raiden.utils.runnable] exc=ValueError('Addresses to compare must differ') running=True subtask=<Greenlet "Greenlet<fn:_health_check_worker>" at 0x7f1f5e02a050: <bound method MatrixTransport._health_check_worker of <MatrixTransport node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9 id:585de50e-f064-4a93-b3f4-df2bf40f8078>>> this=<MatrixTransport node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9 id:585de50e-f064-4a93-b3f4-df2bf40f8078>
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._greenlet.Greenlet.run
File "site-packages/raiden/network/transport/matrix/transport.py", line 496, in _run
File "site-packages/raiden/network/transport/matrix/transport.py", line 718, in _broadcast_worker
File "src/gevent/event.py", line 133, in gevent._event.Event.wait
File "src/gevent/_abstract_linkable.py", line 208, in gevent.__abstract_linkable.AbstractLinkable._wait
File "src/gevent/_abstract_linkable.py", line 178, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 196, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 182, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 65, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
ValueError: Addresses to compare must differ
2020-02-19T14:55:13Z <Greenlet "MatrixTransport._run node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9" at 0x7f1f5f9a3a70: _run> failed with ValueError

2020-02-19 14:55:13.259801 [error    ] Runnable subtask died!         [raiden.utils.runnable] exc=ValueError('Addresses to compare must differ') running=True subtask=<Greenlet "MatrixTransport._run node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9" at 0x7f1f5f9a3a70: _run> this=<RaidenService node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9>
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._greenlet.Greenlet.run
File "site-packages/raiden/raiden_service.py", line 375, in _run
File "src/gevent/event.py", line 133, in gevent._event.Event.wait
File "src/gevent/_abstract_linkable.py", line 208, in gevent.__abstract_linkable.AbstractLinkable._wait
File "src/gevent/_abstract_linkable.py", line 178, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 196, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 182, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 65, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
ValueError: Addresses to compare must differ
2020-02-19T14:55:13Z <Greenlet "RaidenService._run node:0x812f85143bdF6d28228FC580Ef649AA95ba6Fcd9" at 0x7f1f5f625b90: _run> failed with ValueError

FATAL: An unexpected exception occured. A traceback has been written to /tmp/raiden-exception-2020-02-19T14-5542cjz658.txt
Addresses to compare must differ
ReturnCode.FATAL

# Fixed code
def transfer_async(
    self,
    registry_address: TokenNetworkRegistryAddress,
    token_address: TokenAddress,
    amount: PaymentAmount,
    target: TargetAddress,
    identifier: PaymentID = None,
    secret: Secret = None,
    secrethash: SecretHash = None,
    lock_timeout: BlockTimeout = None,
) -> "PaymentStatus":
    current_state = views.state_from_raiden(self.raiden)
    token_network_registry_address = self.raiden.default_registry.address

    if not isinstance(amount, int):  # pragma: no unittest
        raise InvalidAmount("Amount not a number")

    if Address(target) == self.address:
        raise SamePeerAddress("Address must be different than ours")

    if amount <= 0:
        raise InvalidAmount("Amount negative")

    if amount > UINT256_MAX:
        raise InvalidAmount("Amount too large")

    if not is_binary_address(token_address):
        raise InvalidBinaryAddress("token address is not valid.")

    if token_address not in views.get_token_identifiers(
        current_state, registry_address
    ):
        raise UnknownTokenAddress("Token address is not known.")

    if not is_binary_address(target):
        raise InvalidBinaryAddress("target address is not valid.")

    valid_tokens = views.get_token_identifiers(
        views.state_from_raiden(self.raiden), registry_address
    )
    if token_address not in valid_tokens:
        raise UnknownTokenAddress("Token address is not known.")

    if secret is not None and not isinstance(secret, T_Secret):
        raise InvalidSecret("secret is not valid.")

    if secrethash is not None and not isinstance(secrethash, T_SecretHash):
        raise InvalidSecretHash("secrethash is not valid.")

    if identifier is None:
        identifier = create_default_identifier()

    if identifier <= 0:
        raise InvalidPaymentIdentifier("Payment identifier cannot be 0 or negative")

    if identifier > UINT64_MAX:
        raise InvalidPaymentIdentifier("Payment identifier is too large")

    log.debug(
        "Initiating transfer",
        initiator=to_checksum_address(self.raiden.address),
        target=to_checksum_address(target),
        token=to_checksum_address(token_address),
        amount=amount,
        identifier=identifier,
    )

    token_network_address = views.get_token_network_address_by_token_address(
        chain_state=current_state,
        token_network_registry_address=token_network_registry_address,
        token_address=token_address,
    )

    if token_network_address is None:
        raise UnknownTokenAddress(
            f"Token {to_checksum_address(token_address)} is not registered "
            f"with the network {to_checksum_address(registry_address)}."
        )

    payment_status = self.raiden.mediated_transfer_async(
        token_network_address=token_network_address,
        amount=amount,
        target=target,
        identifier=identifier,
        secret=secret,
        secrethash=secrethash,
        lock_timeout=lock_timeout,
    )
    return payment_status