def test_query_partners_by_token( api_test_server, api_test_context, api_raiden_service): # let's create 2 new channels for the same token first_partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' second_partner_address = '0x29fa6cf0cce24582a9b20db94be4b6e017896038' token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8' settle_timeout = 1650 channel_data_obj = { 'partner_address': first_partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout } request = grequests.put( 'http://localhost:5001/api/1/channels', json=channel_data_obj ) response = request.send().response assert_proper_response(response) response = decode_response(response) first_channel_address = response['channel_address'] channel_data_obj['partner_address'] = second_partner_address request = grequests.put( 'http://localhost:5001/api/1/channels', json=channel_data_obj ) response = request.send().response assert_proper_response(response) response = decode_response(response) second_channel_address = response['channel_address'] # and a channel for another token channel_data_obj['partner_address'] = '0xb07937AbA15304FBBB0Bf6454a9377a76E3dD39E' channel_data_obj['token_address'] = '0x70faa28A6B8d6829a4b1E649d26eC9a2a39ba413' request = grequests.put( 'http://localhost:5001/api/1/channels', json=channel_data_obj ) response = request.send().response assert_proper_response(response) # and now let's query our partners per token for the first token request = grequests.get( 'http://localhost:5001/api/1/tokens/0xea674fdde714fd979de3edf0f56aa9716b898ec8/partners', ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = [ { 'partner_address': first_partner_address, 'channel': '/api/1/channels/{}'.format(first_channel_address) }, { 'partner_address': second_partner_address, 'channel': '/api/1/channels/{}'.format(second_channel_address) } ] assert all(r in response for r in expected_response)
def test_api_query_channels(api_test_server, api_test_context, api_raiden_service): request = grequests.get('http://localhost:5001/api/1/channels') response = request.send().response assert_proper_response(response) assert decode_response(response) == api_test_context.expect_channels() api_test_context.make_channel_and_add() response = request.send().response assert_proper_response(response) assert decode_response(response) == api_test_context.expect_channels()
def test_api_query_channels(api_backend, api_test_context, api_raiden_service): request = grequests.get(api_url_for(api_backend, 'channelsresource')) response = request.send().response assert_proper_response(response) assert decode_response(response) == api_test_context.expect_channels() api_test_context.make_channel_and_add() response = request.send().response assert_proper_response(response) assert decode_response(response) == api_test_context.expect_channels()
def test_api_transfers( api_backend, api_test_context, api_raiden_service): amount = 200 identifier = 42 token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8' target_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' transfer = { 'initiator_address': address_encoder(api_raiden_service.address), 'target_address': target_address, 'token_address': token_address, 'amount': amount, 'identifier': identifier } request = grequests.post( api_url_for( api_backend, 'transfertotargetresource', token_address=token_address, target_address=target_address ), json={'amount': amount, 'identifier': identifier} ) response = request.send().response assert_proper_response(response) response = decode_response(response) assert response == transfer
def test_api_tokens( api_backend, api_test_context, api_raiden_service): # let's create 2 new channels for 2 different tokens partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8' settle_timeout = 1650 channel_data_obj = { 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout } request = grequests.put( api_url_for(api_backend, 'channelsresource'), json=channel_data_obj ) response = request.send().response assert_proper_response(response) partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' token_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' settle_timeout = 1650 channel_data_obj = { 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout } request = grequests.put( api_url_for(api_backend, 'channelsresource'), json=channel_data_obj ) response = request.send().response assert_proper_response(response) # and now let's get the token list request = grequests.get( api_url_for(api_backend, 'tokensresource'), ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = [ {'address': '0x61c808d82a3ac53231750dadc13c777b59310bd9'}, {'address': '0xea674fdde714fd979de3edf0f56aa9716b898ec8'}, ] assert all(r in response for r in expected_response)
def test_api_tokens(api_test_server, api_test_context, api_raiden_service, reveal_timeout): # let's create 2 new channels for 2 different tokens partner_address = "0x61c808d82a3ac53231750dadc13c777b59310bd9" token_address = "0xea674fdde714fd979de3edf0f56aa9716b898ec8" settle_timeout = 1650 channel_data_obj = { "partner_address": partner_address, "token_address": token_address, "settle_timeout": settle_timeout } request = grequests.put('http://localhost:5001/api/1/channels', json=channel_data_obj) response = request.send().response assert response and response.status_code == httplib.OK partner_address = "0x61c808d82a3ac53231750dadc13c777b59310bd9" token_address = "0x61c808d82a3ac53231750dadc13c777b59310bd9" settle_timeout = 1650 channel_data_obj = { "partner_address": partner_address, "token_address": token_address, "settle_timeout": settle_timeout } request = grequests.put('http://localhost:5001/api/1/channels', json=channel_data_obj) response = request.send().response assert response and response.status_code == httplib.OK # and now let's get the token list request = grequests.get('http://localhost:5001/api/1/tokens', ) response = request.send().response assert response and response.status_code == httplib.OK response = decode_response(response) expected_response = [ { "address": "0x61c808d82a3ac53231750dadc13c777b59310bd9" }, { "address": "0xea674fdde714fd979de3edf0f56aa9716b898ec8" }, ] assert all(r in response for r in expected_response)
def test_api_transfers( api_test_server, api_test_context, api_raiden_service): amount = 200 identifier = 42 transfer = { 'initiator_address': address_encoder(api_raiden_service.address), 'target_address': '0x61c808d82a3ac53231750dadc13c777b59310bd9', 'token_address': '0xea674fdde714fd979de3edf0f56aa9716b898ec8', 'amount': amount, 'identifier': identifier } request = grequests.post( 'http://localhost:5001/api/1/transfers/0xea674fdde714fd979de3edf0f56aa9716b898ec8/0x61c808d82a3ac53231750dadc13c777b59310bd9', json={'amount': amount, 'identifier': identifier} ) response = request.send().response assert_proper_response(response) response = decode_response(response) assert response == transfer
def test_api_open_and_deposit_channel( api_backend, api_test_context, api_raiden_service): # let's create a new channel first_partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8' settle_timeout = 1650 channel_data_obj = { 'partner_address': first_partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout } request = grequests.put( api_url_for(api_backend, 'channelsresource'), json=channel_data_obj ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = channel_data_obj expected_response['balance'] = 0 expected_response['state'] = CHANNEL_STATE_OPENED # can't know the channel address beforehand but make sure we get one assert 'channel_address' in response first_channel_address = response['channel_address'] expected_response['channel_address'] = response['channel_address'] assert response == expected_response # now let's open a channel and make a deposit too second_partner_address = '0x29fa6cf0cce24582a9b20db94be4b6e017896038' balance = 100 channel_data_obj = { 'partner_address': second_partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout, 'balance': balance } request = grequests.put( api_url_for(api_backend, 'channelsresource'), json=channel_data_obj ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = channel_data_obj expected_response['balance'] = balance expected_response['state'] = CHANNEL_STATE_OPENED # can't know the channel address beforehand but make sure we get one assert 'channel_address' in response expected_response['channel_address'] = response['channel_address'] second_channel_address = response['channel_address'] assert response == expected_response # let's deposit on the first channel request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=first_channel_address ), json={'balance': balance} ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = { 'channel_address': first_channel_address, 'partner_address': first_partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout, 'state': CHANNEL_STATE_OPENED, 'balance': balance } assert response == expected_response # finally let's try querying for the second channel request = grequests.get( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=second_channel_address ) ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = { 'channel_address': second_channel_address, 'partner_address': second_partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout, 'state': CHANNEL_STATE_OPENED, 'balance': balance } assert response == expected_response
def test_api_channel_state_change_errors( api_backend, api_test_context, api_raiden_service): # let's create a new channel partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8' settle_timeout = 1650 channel_data_obj = { 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout } request = grequests.put( api_url_for(api_backend, 'channelsresource'), json=channel_data_obj ) response = request.send().response assert_proper_response(response) response = decode_response(response) channel_address = response['channel_address'] # let's try to settle the channel (we are bad!) request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'state': CHANNEL_STATE_SETTLED} ) response = request.send().response assert response is not None and response.status_code == httplib.CONFLICT # let's try to set a random state request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'state': 'inlimbo'} ) response = request.send().response assert response is not None and response.status_code == httplib.BAD_REQUEST # let's try to set both new state and balance request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'state': CHANNEL_STATE_CLOSED, 'balance': 200} ) response = request.send().response assert response is not None and response.status_code == httplib.CONFLICT # let's try to path with no arguments request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), ) response = request.send().response assert response is not None and response.status_code == httplib.BAD_REQUEST # ok now let's close and settle for real request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'state': CHANNEL_STATE_CLOSED} ) response = request.send().response assert_proper_response(response) request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'state': CHANNEL_STATE_SETTLED} ) response = request.send().response assert_proper_response(response) # let's try to deposit to a settled channel request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'balance': 500} ) response = request.send().response assert response is not None and response.status_code == httplib.CONFLICT # and now let's try to settle again request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'state': CHANNEL_STATE_SETTLED} ) response = request.send().response assert response is not None and response.status_code == httplib.CONFLICT
def test_api_open_close_and_settle_channel( api_backend, api_test_context, api_raiden_service): # let's create a new channel partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8' settle_timeout = 1650 channel_data_obj = { 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout } request = grequests.put( api_url_for(api_backend, 'channelsresource'), json=channel_data_obj ) response = request.send().response balance = 0 assert_proper_response(response) response = decode_response(response) expected_response = channel_data_obj expected_response['balance'] = balance expected_response['state'] = CHANNEL_STATE_OPENED # can't know the channel address beforehand but make sure we get one assert 'channel_address' in response channel_address = response['channel_address'] expected_response['channel_address'] = response['channel_address'] assert response == expected_response # let's the close the channel request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'state': CHANNEL_STATE_CLOSED} ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = { 'channel_address': channel_address, 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout, 'state': CHANNEL_STATE_CLOSED, 'balance': balance } assert response == expected_response # let's settle the channel request = grequests.patch( api_url_for( api_backend, 'channelsresourcebychanneladdress', channel_address=channel_address ), json={'state': CHANNEL_STATE_SETTLED} ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = { 'channel_address': channel_address, 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout, 'state': CHANNEL_STATE_SETTLED, 'balance': balance } assert response == expected_response
def test_connect_and_leave_token_network( api_backend, api_test_context, api_raiden_service): # first check we don't have any open channels request = grequests.get( api_url_for(api_backend, 'channelsresource') ) response = request.send().response assert_proper_response(response) channels = decode_response(response) assert len(channels) == 0 assert decode_response(response) == api_test_context.expect_channels() funds = 100 initial_channel_target = DEFAULT_INITIAL_CHANNEL_TARGET joinable_funds_target = DEFAULT_JOINABLE_FUNDS_TARGET token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8' connect_data_obj = { 'funds': funds, } request = grequests.put( api_url_for(api_backend, 'connectionsresource', token_address=token_address), json=connect_data_obj, ) response = request.send().response assert_proper_response(response) # check that channels got created request = grequests.get( api_url_for(api_backend, 'channelsresource') ) response = request.send().response assert_proper_response(response) channels = decode_response(response) # There should be three channels according to the default initial_channel_target assert len(channels) == DEFAULT_INITIAL_CHANNEL_TARGET assert decode_response(response) == api_test_context.expect_channels() expected_balance = int((funds * joinable_funds_target) / initial_channel_target) assert channels[0]['balance'] == expected_balance assert channels[1]['balance'] == expected_balance assert channels[2]['balance'] == expected_balance assert channels[0]['state'] == CHANNEL_STATE_OPENED assert channels[1]['state'] == CHANNEL_STATE_OPENED assert channels[2]['state'] == CHANNEL_STATE_OPENED # Let's leave the token network request = grequests.delete( api_url_for(api_backend, 'connectionsresource', token_address=token_address), ) response = request.send().response assert_proper_response(response) # check that all channels were settled after calling `leave` request = grequests.get( api_url_for(api_backend, 'channelsresource') ) response = request.send().response assert_proper_response(response) channels = decode_response(response) assert channels[0]['state'] == CHANNEL_STATE_SETTLED assert channels[1]['state'] == CHANNEL_STATE_SETTLED assert channels[2]['state'] == CHANNEL_STATE_SETTLED
def test_api_open_close_and_settle_channel( api_test_server, api_test_context, api_raiden_service): # let's create a new channel partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9' token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8' settle_timeout = 1650 channel_data_obj = { 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout } request = grequests.put( 'http://localhost:5001/api/1/channels', json=channel_data_obj ) response = request.send().response balance = 0 assert_proper_response(response) response = decode_response(response) expected_response = channel_data_obj expected_response['balance'] = balance expected_response['state'] = 'open' # can't know the channel address beforehand but make sure we get one assert 'channel_address' in response channel_address = response['channel_address'] expected_response['channel_address'] = response['channel_address'] assert response == expected_response # let's the close the channel request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={'state': 'closed'} ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = { 'channel_address': channel_address, 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout, 'state': 'closed', 'balance': balance } assert response == expected_response # let's settle the channel request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={'state': 'settled'} ) response = request.send().response assert_proper_response(response) response = decode_response(response) expected_response = { 'channel_address': channel_address, 'partner_address': partner_address, 'token_address': token_address, 'settle_timeout': settle_timeout, 'state': 'settled', 'balance': balance } assert response == expected_response
def test_api_open_and_deposit_channel(api_test_server, api_test_context, api_raiden_service, reveal_timeout): # let's create a new channel first_partner_address = "0x61c808d82a3ac53231750dadc13c777b59310bd9" token_address = "0xea674fdde714fd979de3edf0f56aa9716b898ec8" settle_timeout = 1650 channel_data_obj = { "partner_address": first_partner_address, "token_address": token_address, "settle_timeout": settle_timeout } request = grequests.put('http://localhost:5001/api/1/channels', json=channel_data_obj) response = request.send().response assert response and response.status_code == httplib.OK response = decode_response(response) expected_response = channel_data_obj expected_response['reveal_timeout'] = reveal_timeout expected_response['balance'] = 0 expected_response['state'] = 'open' # can't know the channel address beforehand but make sure we get one assert 'channel_address' in response first_channel_address = response['channel_address'] expected_response['channel_address'] = response['channel_address'] assert response == expected_response # now let's open a channel and make a deposit too second_partner_address = '0x29fa6cf0cce24582a9b20db94be4b6e017896038' balance = 100 channel_data_obj = { "partner_address": second_partner_address, "token_address": token_address, "settle_timeout": settle_timeout, "balance": balance } request = grequests.put('http://localhost:5001/api/1/channels', json=channel_data_obj) response = request.send().response assert response and response.status_code == httplib.OK response = decode_response(response) expected_response = channel_data_obj expected_response['reveal_timeout'] = reveal_timeout expected_response['balance'] = balance expected_response['state'] = 'open' # can't know the channel address beforehand but make sure we get one assert 'channel_address' in response expected_response['channel_address'] = response['channel_address'] second_channel_address = response['channel_address'] assert response == expected_response # let's deposit on the first channel request = grequests.patch('http://localhost:5001/api/1/channels/{}'.format( first_channel_address), json={'balance': balance}) response = request.send().response assert response and response.status_code == httplib.OK response = decode_response(response) expected_response = { "channel_address": first_channel_address, "partner_address": first_partner_address, "token_address": token_address, "settle_timeout": settle_timeout, "reveal_timeout": reveal_timeout, "state": 'open', "balance": balance } assert response == expected_response # finall let's try querying for the second channel request = grequests.get('http://localhost:5001/api/1/channels/{}'.format( second_channel_address)) response = request.send().response assert response and response.status_code == httplib.OK response = decode_response(response) expected_response = { "channel_address": second_channel_address, "partner_address": second_partner_address, "token_address": token_address, "settle_timeout": settle_timeout, "reveal_timeout": reveal_timeout, "state": 'open', "balance": balance } assert response == expected_response
def test_api_channel_state_change_errors(api_test_server, api_test_context, api_raiden_service, reveal_timeout): # let's create a new channel partner_address = "0x61c808d82a3ac53231750dadc13c777b59310bd9" token_address = "0xea674fdde714fd979de3edf0f56aa9716b898ec8" settle_timeout = 1650 channel_data_obj = { "partner_address": partner_address, "token_address": token_address, "settle_timeout": settle_timeout } request = grequests.put('http://localhost:5001/api/1/channels', json=channel_data_obj) response = request.send().response assert response and response.status_code == httplib.OK response = decode_response(response) channel_address = response['channel_address'] # let's try to settle the channel (we are bad!) request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={'state': 'settled'}) response = request.send().response assert response is not None and response.status_code == httplib.CONFLICT # let's try to set a random state request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={'state': 'inlimbo'}) response = request.send().response assert response is not None and response.status_code == httplib.BAD_REQUEST # let's try to set both new state and balance request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={ 'state': 'closed', 'balance': 200 }) response = request.send().response assert response is not None and response.status_code == httplib.CONFLICT # let's try to path with no arguments request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), ) response = request.send().response assert response is not None and response.status_code == httplib.BAD_REQUEST # ok now let's close and settle for real request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={'state': 'closed'}) response = request.send().response assert response and response.status_code == httplib.OK request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={'state': 'settled'}) response = request.send().response assert response and response.status_code == httplib.OK # let's try to deposit to a settled channel request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={'balance': 500}) response = request.send().response assert response is not None and response.status_code == httplib.CONFLICT # and now let's try to settle again request = grequests.patch( 'http://localhost:5001/api/1/channels/{}'.format(channel_address), json={'state': 'settled'}) response = request.send().response assert response is not None and response.status_code == httplib.CONFLICT