@pytest.mark.timeout(180)
@pytest.mark.parametrize('privatekey_seed', ['blockchain:{}'])
@pytest.mark.parametrize('number_of_nodes', [3])
@pytest.mark.parametrize('channels_per_node', [0])
@pytest.mark.parametrize('number_of_assets', [0])
def test_new_netting_contract(raiden_network, asset_amount, settle_timeout):
    (app0, app1, app2) = raiden_network
    peer0_address = app0.raiden.address
    peer1_address = app1.raiden.address
    peer2_address = app2.raiden.address
    blockchain_service0 = app0.raiden.chain
    asset_address = blockchain_service0.deploy_and_register_asset(contract_name='HumanStandardToken', contract_file='HumanStandardToken.sol', constructor_parameters=(asset_amount, 'raiden', 2, 'Rd'))
    asset0 = blockchain_service0.asset(asset_address)
    for transfer_to in raiden_network[1:]:
        asset0.transfer(privatekey_to_address(transfer_to.raiden.privkey), (asset_amount // len(raiden_network)))
    manager0 = blockchain_service0.manager_by_asset(asset_address)
    assert (manager0.channels_addresses() == [])
    assert (manager0.channels_by_participant(peer0_address) == [])
    assert (manager0.channels_by_participant(peer1_address) == [])
    assert (manager0.channels_by_participant(peer2_address) == [])
    netting_address_01 = manager0.new_netting_channel(peer0_address, peer1_address, settle_timeout)
    netting_channel_01 = blockchain_service0.netting_channel(netting_address_01)
    assert (netting_channel_01.isopen() is False)
    assert (netting_channel_01.partner(peer0_address) == peer1_address)
    assert (netting_channel_01.partner(peer1_address) == peer0_address)
    channel_list = manager0.channels_addresses()
    assert (sorted(channel_list[0]) == sorted([peer0_address, peer1_address]))
    assert (manager0.channels_by_participant(peer0_address) == [netting_address_01])
    assert (manager0.channels_by_participant(peer1_address) == [netting_address_01])
    assert (manager0.channels_by_participant(peer2_address) == [])
    netting_address_02 = manager0.new_netting_channel(peer0_address, peer2_address, settle_timeout)
    netting_channel_02 = blockchain_service0.netting_channel(netting_address_02)
    assert (netting_channel_02.isopen() is False)
    assert (netting_channel_02.partner(peer0_address) == peer2_address)
    assert (netting_channel_02.partner(peer2_address) == peer0_address)
    channel_list = manager0.channels_addresses()
    expected_channels = [sorted([peer0_address, peer1_address]), sorted([peer0_address, peer2_address])]
    for channel in channel_list:
        assert (sorted(channel) in expected_channels)
    result0 = sorted(manager0.channels_by_participant(peer0_address))
    result1 = sorted([netting_address_01, netting_address_02])
    assert (result0 == result1)
    assert (manager0.channels_by_participant(peer1_address) == [netting_address_01])
    assert (manager0.channels_by_participant(peer2_address) == [netting_address_02])
    netting_channel_01.deposit(peer0_address, 100)
    assert (netting_channel_01.isopen() is False)
    assert (netting_channel_02.isopen() is False)
    assert (netting_channel_01.detail(peer0_address)['our_balance'] == 0)
    assert (netting_channel_01.detail(peer1_address)['our_balance'] == 0)
    app0.raiden.chain.asset(asset_address).approve(netting_address_01, 100)
    netting_channel_01.deposit(peer0_address, 100)
    assert (netting_channel_01.isopen() is True)
    assert (netting_channel_02.isopen() is False)
    assert (netting_channel_01.detail(peer0_address)['our_balance'] == 100)
    assert (netting_channel_01.detail(peer1_address)['our_balance'] == 0)
    app0.raiden.chain.asset(asset_address).approve(netting_address_02, 70)
    netting_channel_02.deposit(peer0_address, 70)
    assert (netting_channel_01.isopen() is True)
    assert (netting_channel_02.isopen() is True)
    assert (netting_channel_02.detail(peer0_address)['our_balance'] == 70)
    assert (netting_channel_02.detail(peer2_address)['our_balance'] == 0)
    app2.raiden.chain.asset(asset_address).approve(netting_address_02, 130)
    app2.raiden.chain.netting_channel(netting_address_02).deposit(peer2_address, 130)
    assert (netting_channel_01.isopen() is True)
    assert (netting_channel_02.isopen() is True)
    assert (netting_channel_02.detail(peer0_address)['our_balance'] == 70)
    assert (netting_channel_02.detail(peer2_address)['our_balance'] == 130)
@pytest.mark.timeout(180)
@pytest.mark.parametrize('privatekey_seed', ['blockchain:{}'])
@pytest.mark.parametrize('number_of_nodes', [3])
@pytest.mark.parametrize('channels_per_node', [0])
@pytest.mark.parametrize('number_of_assets', [0])
def test_new_netting_contract(raiden_network, asset_amount, settle_timeout):
    (app0, app1, app2) = raiden_network
    peer0_address = app0.raiden.address
    peer1_address = app1.raiden.address
    peer2_address = app2.raiden.address
    blockchain_service0 = app0.raiden.chain
    asset_address = blockchain_service0.deploy_and_register_asset(contract_name='HumanStandardToken', contract_file='HumanStandardToken.sol', constructor_parameters=(asset_amount, 'raiden', 2, 'Rd'))
    asset0 = blockchain_service0.asset(asset_address)
    for transfer_to in raiden_network[1:]:
        asset0.transfer(privatekey_to_address(transfer_to.raiden.privkey), (asset_amount // len(raiden_network)))
    manager0 = blockchain_service0.manager_by_asset(asset_address)
    assert (manager0.channels_addresses() == [])
    assert (manager0.channels_by_participant(peer0_address) == [])
    assert (manager0.channels_by_participant(peer1_address) == [])
    assert (manager0.channels_by_participant(peer2_address) == [])
    netting_address_01 = manager0.new_netting_channel(peer0_address, peer1_address, settle_timeout)
    netting_channel_01 = blockchain_service0.netting_channel(netting_address_01)
    assert (netting_channel_01.isopen() is False)
    assert (netting_channel_01.partner(peer0_address) == peer1_address)
    assert (netting_channel_01.partner(peer1_address) == peer0_address)
    channel_list = manager0.channels_addresses()
    assert (sorted(channel_list[0]) == sorted([peer0_address, peer1_address]))
    assert (manager0.channels_by_participant(peer0_address) == [netting_address_01])
    assert (manager0.channels_by_participant(peer1_address) == [netting_address_01])
    assert (manager0.channels_by_participant(peer2_address) == [])
    netting_address_02 = manager0.new_netting_channel(peer0_address, peer2_address, settle_timeout)
    netting_channel_02 = blockchain_service0.netting_channel(netting_address_02)
    assert (netting_channel_02.isopen() is False)
    assert (netting_channel_02.partner(peer0_address) == peer2_address)
    assert (netting_channel_02.partner(peer2_address) == peer0_address)
    channel_list = manager0.channels_addresses()
    expected_channels = [sorted([peer0_address, peer1_address]), sorted([peer0_address, peer2_address])]
    for channel in channel_list:
        assert (sorted(channel) in expected_channels)
    result0 = sorted(manager0.channels_by_participant(peer0_address))
    result1 = sorted([netting_address_01, netting_address_02])
    assert (result0 == result1)
    assert (manager0.channels_by_participant(peer1_address) == [netting_address_01])
    assert (manager0.channels_by_participant(peer2_address) == [netting_address_02])
    netting_channel_01.deposit(peer0_address, 100)
    assert (netting_channel_01.isopen() is False)
    assert (netting_channel_02.isopen() is False)
    assert (netting_channel_01.detail(peer0_address)['our_balance'] == 0)
    assert (netting_channel_01.detail(peer1_address)['our_balance'] == 0)
    app0.raiden.chain.asset(asset_address).approve(netting_address_01, 100)
    netting_channel_01.deposit(peer0_address, 100)
    assert (netting_channel_01.isopen() is True)
    assert (netting_channel_02.isopen() is False)
    assert (netting_channel_01.detail(peer0_address)['our_balance'] == 100)
    assert (netting_channel_01.detail(peer1_address)['our_balance'] == 0)
    app0.raiden.chain.asset(asset_address).approve(netting_address_02, 70)
    netting_channel_02.deposit(peer0_address, 70)
    assert (netting_channel_01.isopen() is True)
    assert (netting_channel_02.isopen() is True)
    assert (netting_channel_02.detail(peer0_address)['our_balance'] == 70)
    assert (netting_channel_02.detail(peer2_address)['our_balance'] == 0)
    app2.raiden.chain.asset(asset_address).approve(netting_address_02, 130)
    app2.raiden.chain.netting_channel(netting_address_02).deposit(peer2_address, 130)
    assert (netting_channel_01.isopen() is True)
    assert (netting_channel_02.isopen() is True)
    assert (netting_channel_02.detail(peer0_address)['our_balance'] == 70)
    assert (netting_channel_02.detail(peer2_address)['our_balance'] == 130)