def all_contract_events(rpc, contract_address, abi, start_block=None, end_block=None): """Find and decode all events for a deployed contract given its `contract_address` and `abi`. Args: rpc (pyethapp.rpc_client.JSONRPCClient): client instance. contract_address (string): hex encoded contract address. abi (list(dict)): the contract's ABI. start_block (int): read event-logs starting from this block number. end_block (int): read event-logs up to this block number. Returns: events (list) """ translator = ContractTranslator(abi) events_raw = all_contract_events_raw( rpc, contract_address, start_block=start_block, end_block=end_block, ) events = list() for event_encoded in events_raw: topics_ids = [ decode_topic(topic) for topic in event_encoded['topics'] ] event_data = data_decoder(event_encoded['data']) event = translator.decode_event(topics_ids, event_data) events.append(event) return events
def all_contract_events(rpc, contract_address, abi, start_block=None, end_block=None): """Find and decode all events for a deployed contract given its `contract_address` and `abi`. Args: rpc (pyethapp.rpc_client.JSONRPCClient): client instance. contract_address (string): hex encoded contract address. abi (list(dict)): the contract's ABI. start_block (int): read event-logs starting from this block number. end_block (int): read event-logs up to this block number. Returns: events (list) """ translator = ContractTranslator(abi) events_raw = all_contract_events_raw( rpc, contract_address, start_block=start_block, end_block=end_block, ) events = list() for event_encoded in events_raw: topics_ids = [decode_topic(topic) for topic in event_encoded['topics']] event_data = data_decoder(event_encoded['data']) event = translator.decode_event(topics_ids, event_data) events.append(event) return events
def test_blockchain(blockchain_backend, private_keys, number_of_nodes, poll_timeout): # pylint: disable=too-many-locals addresses = [privatekey_to_address(priv) for priv in private_keys] privatekey = private_keys[0] address = privatekey_to_address(privatekey) total_asset = 100 jsonrpc_client = JSONRPCClient( privkey=privatekey, print_communication=False, ) patch_send_transaction(jsonrpc_client) humantoken_path = get_contract_path('HumanStandardToken.sol') humantoken_contracts = compile_file(humantoken_path, libraries=dict()) token_proxy = jsonrpc_client.deploy_solidity_contract( address, 'HumanStandardToken', humantoken_contracts, dict(), (total_asset, 'raiden', 2, 'Rd'), timeout=poll_timeout, ) registry_path = get_contract_path('Registry.sol') registry_contracts = compile_file(registry_path) registry_proxy = jsonrpc_client.deploy_solidity_contract( address, 'Registry', registry_contracts, dict(), tuple(), timeout=poll_timeout, ) log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 0 # pylint: disable=no-member assert token_proxy.balanceOf(address) == total_asset transaction_hash = registry_proxy.addAsset.transact( token_proxy.address, gasprice=denoms.wei, ) jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout) assert len(registry_proxy.assetAddresses.call()) == 1 log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 1 channel_manager_address_encoded = registry_proxy.channelManagerByAsset.call( token_proxy.address) channel_manager_address = channel_manager_address_encoded.decode('hex') log = log_list[0] log_topics = [ decode_topic(topic) for topic in log['topics'] # pylint: disable=invalid-sequence-index ] log_data = log['data'] event = registry_proxy.translator.decode_event( log_topics, log_data[2:].decode('hex'), ) assert channel_manager_address == event['channelManagerAddress'].decode( 'hex') assert token_proxy.address == event['assetAddress'].decode('hex') channel_manager_proxy = jsonrpc_client.new_contract_proxy( registry_contracts['ChannelManagerContract']['abi'], channel_manager_address, ) transaction_hash = channel_manager_proxy.newChannel.transact( addresses[1], 10, gasprice=denoms.wei, ) jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout) log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 2
def test_blockchain( blockchain_type, blockchain_backend, # required to start the geth backend blockchain_rpc_ports, private_keys, poll_timeout): # pylint: disable=too-many-locals # this test is for interaction with a blockchain using json-rpc, so it # doesnt make sense to execute it against mock or tester if blockchain_type not in ('geth',): return addresses = [ privatekey_to_address(priv) for priv in private_keys ] privatekey = private_keys[0] address = privatekey_to_address(privatekey) total_token = 100 jsonrpc_client = JSONRPCClient( port=blockchain_rpc_ports[0], privkey=privatekey, print_communication=False, ) patch_send_transaction(jsonrpc_client) patch_send_message(jsonrpc_client) humantoken_path = get_contract_path('HumanStandardToken.sol') humantoken_contracts = compile_file(humantoken_path, libraries=dict()) token_proxy = jsonrpc_client.deploy_solidity_contract( address, 'HumanStandardToken', humantoken_contracts, dict(), (total_token, 'raiden', 2, 'Rd'), contract_path=humantoken_path, gasprice=default_gasprice, timeout=poll_timeout, ) registry_path = get_contract_path('Registry.sol') registry_contracts = compile_file(registry_path) registry_proxy = jsonrpc_client.deploy_solidity_contract( address, 'Registry', registry_contracts, dict(), tuple(), contract_path=registry_path, gasprice=default_gasprice, timeout=poll_timeout, ) log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 0 # pylint: disable=no-member assert token_proxy.balanceOf(address) == total_token transaction_hash = registry_proxy.addToken.transact( token_proxy.address, gasprice=denoms.wei, ) jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout) assert len(registry_proxy.tokenAddresses.call()) == 1 log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 1 channel_manager_address_encoded = registry_proxy.channelManagerByToken.call( token_proxy.address, ) channel_manager_address = channel_manager_address_encoded.decode('hex') log = log_list[0] log_topics = [ decode_topic(topic) for topic in log['topics'] # pylint: disable=invalid-sequence-index ] log_data = log['data'] event = registry_proxy.translator.decode_event( log_topics, log_data[2:].decode('hex'), ) assert channel_manager_address == event['channel_manager_address'].decode('hex') assert token_proxy.address == event['token_address'].decode('hex') channel_manager_proxy = jsonrpc_client.new_contract_proxy( CHANNEL_MANAGER_ABI, channel_manager_address, ) transaction_hash = channel_manager_proxy.newChannel.transact( addresses[1], 10, gasprice=denoms.wei, ) jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout) log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 2
def test_blockchain( blockchain_type, blockchain_backend, # required to start the geth backend blockchain_rpc_ports, private_keys, poll_timeout): # pylint: disable=too-many-locals # this test is for interaction with a blockchain using json-rpc, so it # doesnt make sense to execute it against mock or tester if blockchain_type not in ('geth',): return addresses = [ privatekey_to_address(priv) for priv in private_keys ] privatekey = private_keys[0] address = privatekey_to_address(privatekey) total_asset = 100 jsonrpc_client = JSONRPCClient( port=blockchain_rpc_ports[0], privkey=privatekey, print_communication=False, ) patch_send_transaction(jsonrpc_client) humantoken_path = get_contract_path('HumanStandardToken.sol') humantoken_contracts = compile_file(humantoken_path, libraries=dict()) token_proxy = jsonrpc_client.deploy_solidity_contract( address, 'HumanStandardToken', humantoken_contracts, dict(), (total_asset, 'raiden', 2, 'Rd'), timeout=poll_timeout, ) registry_path = get_contract_path('Registry.sol') registry_contracts = compile_file(registry_path) registry_proxy = jsonrpc_client.deploy_solidity_contract( address, 'Registry', registry_contracts, dict(), tuple(), timeout=poll_timeout, ) log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 0 # pylint: disable=no-member assert token_proxy.balanceOf(address) == total_asset transaction_hash = registry_proxy.addAsset.transact( token_proxy.address, gasprice=denoms.wei, ) jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout) assert len(registry_proxy.assetAddresses.call()) == 1 log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 1 channel_manager_address_encoded = registry_proxy.channelManagerByAsset.call( token_proxy.address, ) channel_manager_address = channel_manager_address_encoded.decode('hex') log = log_list[0] log_topics = [ decode_topic(topic) for topic in log['topics'] # pylint: disable=invalid-sequence-index ] log_data = log['data'] event = registry_proxy.translator.decode_event( log_topics, log_data[2:].decode('hex'), ) assert channel_manager_address == event['channel_manager_address'].decode('hex') assert token_proxy.address == event['asset_address'].decode('hex') channel_manager_proxy = jsonrpc_client.new_contract_proxy( registry_contracts['ChannelManagerContract']['abi'], channel_manager_address, ) transaction_hash = channel_manager_proxy.newChannel.transact( addresses[1], 10, gasprice=denoms.wei, ) jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout) log_list = jsonrpc_client.call( 'eth_getLogs', { 'fromBlock': '0x0', 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 2