def get_event_id(self, event_name): """ Not really generic, as it maps event names to events of specific contracts, but it is good enough for what we want to accomplish. """ event = get_event(self.get_abi(self.event_to_contract[event_name]), event_name) return event_id(*get_eventname_types(event))
def encode_topic(topic): name, types = _process_topic(topic) for arg in types: try: process_type(arg) except AssertionError: raise ValueError("Invalid argument type") return hex(event_id(name, types)), "{}({})".format(name, ",".join(types))
def test_event(): event_abi = [{ 'name': 'Test', 'anonymous': False, 'inputs': [ { 'indexed': False, 'name': 'a', 'type': 'int256' }, { 'indexed': False, 'name': 'b', 'type': 'int256' }, ], 'type': 'event', }] contract_abi = ContractTranslator(event_abi) normalized_name = normalize_name('Test') encode_types = ['int256', 'int256'] id_ = event_id(normalized_name, encode_types) topics = [id_] data = encode_abi(encode_types, [1, 2]) result = contract_abi.decode_event(topics, data) assert result['_event_type'] == b'Test' assert result['a'] == 1 assert result['b'] == 2
def event_id(cls): return abi.event_id(cls.__name__, cls.arg_types())
) registry_compiled = _solidity.compile_contract( get_contract_path('Registry.sol'), 'Registry', combined='abi', ) # pylint: enable=invalid-name HUMAN_TOKEN_ABI = human_token_compiled['abi'] CHANNEL_MANAGER_ABI = channel_manager_compiled['abi'] NETTING_CHANNEL_ABI = netting_channel_compiled['abi'] REGISTRY_ABI = registry_compiled['abi'] ASSETADDED_EVENT = get_event(REGISTRY_ABI, 'AssetAdded') ASSETADDED_EVENTID = event_id(*get_eventname_types(ASSETADDED_EVENT)) CHANNELNEW_EVENT = get_event(CHANNEL_MANAGER_ABI, 'ChannelNew') CHANNELNEW_EVENTID = event_id(*get_eventname_types(CHANNELNEW_EVENT)) CHANNELNEWBALANCE_EVENT = get_event(NETTING_CHANNEL_ABI, 'ChannelNewBalance') CHANNELNEWBALANCE_EVENTID = event_id( *get_eventname_types(CHANNELNEWBALANCE_EVENT)) CHANNELCLOSED_EVENT = get_event(NETTING_CHANNEL_ABI, 'ChannelClosed') CHANNELCLOSED_EVENTID = event_id(*get_eventname_types(CHANNELCLOSED_EVENT)) CHANNELSECRETREVEALED_EVENT = get_event(NETTING_CHANNEL_ABI, 'ChannelSecretRevealed') CHANNELSECRETREVEALED_EVENTID = event_id( *get_eventname_types(CHANNELSECRETREVEALED_EVENT))
def event_id(event_name, event_params): return abi.event_id(event_name, event_params)
def test_logfilters_topics(test_app): sample_compiled = _solidity.compile_code( sample_sol_code, combined='bin,abi', ) filepath = None contract_data = _solidity.solidity_get_contract_data( sample_compiled, filepath, 'SampleContract') theabi = contract_data['abi'] theevm = contract_data['bin_hex'] sender_address = test_app.services.accounts.unlocked_accounts[0].address sender = address_encoder(sender_address) event1 = get_event(theabi, 'Event1') event2 = get_event(theabi, 'Event2') event3 = get_event(theabi, 'Event3') event1_id = event_id(*get_eventname_types(event1)) event2_id = event_id(*get_eventname_types(event2)) event3_id = event_id(*get_eventname_types(event3)) test_app.mine_next_block() # start with a fresh block n0 = test_app.services.chain.chain.head.number assert n0 == 1 contract_creation = { 'from': sender, 'data': '0x' + theevm, 'gas': quantity_encoder(1000000) } tx_hash = test_app.client.call('eth_sendTransaction', contract_creation) test_app.mine_next_block() receipt = test_app.client.call('eth_getTransactionReceipt', tx_hash) contract_address = receipt['contractAddress'] sample_contract = ContractProxy(sender_address, theabi, contract_address, test_app.client.call, test_app.client.send_transaction) topic1 = hex(event1_id).rstrip("L") topic2 = hex(event2_id).rstrip("L") topic3 = hex(event3_id).rstrip("L") topica, topicb, topicc = \ '0x0000000000000000000000000000000000000000000000000000000000000001',\ '0x0000000000000000000000000000000000000000000000000000000000000064',\ '0x00000000000000000000000000000000000000000000000000000000000003e8' topic_filter_1 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic1] }) topic_filter_2 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic2] }) topic_filter_3 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic3] }) topic_filter_4 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic1, topica] }) topic_filter_5 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic2, topica, topicb] }) topic_filter_6 = test_app.client.call( 'eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic3, topica, topicb, topicc] }) topic_filter_7 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topica, topicb, topicc] }) topic_filter_8 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic3, topica, topicb] }) topic_filter_9 = test_app.client.call( 'eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topicc, topicb, topica, topic3] }) topic_filter_10 = test_app.client.call( 'eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topicb, topicc, topica, topic3] }) topic_filter_11 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic2, topica] }) topic_filter_12 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic3, topica] }) topic_filter_13 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topica, topicb] }) topic_filter_14 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic2, [topica, topicb]] }) topic_filter_15 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [[topic1, topic2], topica] }) topic_filter_16 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [[topic1, topic2, topic3]] }) topic_filter_17 = test_app.client.call( 'eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [[topic1, topic2, topic3, topica, topicb, topicc]] }) topic_filter_18 = test_app.client.call( 'eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic2, topica, topicb, [topic2, topica, topicb]] }) topic_filter_19 = test_app.client.call('eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [topic1, topica, topicb] }) topic_filter_20 = test_app.client.call( 'eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [[topic1, topic2], [topica, topicb], [topica, topicb]] }) topic_filter_21 = test_app.client.call( 'eth_newFilter', { 'fromBlock': 0, 'toBlock': 'pending', 'topics': [[topic2, topic3], [topica, topicb], [topica, topicb]] }) thecode = test_app.client.call('eth_getCode', address_encoder(sample_contract.address)) assert len(thecode) > 2 sample_contract.trigger1(1) test_app.mine_next_block() sample_contract.trigger2(100) test_app.mine_next_block() sample_contract.trigger3(1000) test_app.mine_next_block() tl1 = test_app.client.call('eth_getFilterChanges', topic_filter_1) assert len(tl1) == 1 tl2 = test_app.client.call('eth_getFilterChanges', topic_filter_2) assert len(tl2) == 1 tl3 = test_app.client.call('eth_getFilterChanges', topic_filter_3) assert len(tl3) == 1 tl4 = test_app.client.call('eth_getFilterChanges', topic_filter_4) assert len(tl4) == 1 tl5 = test_app.client.call('eth_getFilterChanges', topic_filter_5) assert len(tl5) == 1 tl6 = test_app.client.call('eth_getFilterChanges', topic_filter_6) assert len(tl6) == 1 tl7 = test_app.client.call('eth_getFilterChanges', topic_filter_7) assert len(tl7) == 0 tl8 = test_app.client.call('eth_getFilterChanges', topic_filter_8) assert len(tl8) == 1 tl9 = test_app.client.call('eth_getFilterChanges', topic_filter_9) assert len(tl9) == 0 tl10 = test_app.client.call('eth_getFilterChanges', topic_filter_10) assert len(tl10) == 0 tl11 = test_app.client.call('eth_getFilterChanges', topic_filter_11) assert len(tl11) == 1 tl12 = test_app.client.call('eth_getFilterChanges', topic_filter_12) assert len(tl12) == 1 tl13 = test_app.client.call('eth_getFilterChanges', topic_filter_13) assert len(tl13) == 0 tl14 = test_app.client.call('eth_getFilterChanges', topic_filter_14) assert len(tl14) == 1 tl15 = test_app.client.call('eth_getFilterChanges', topic_filter_15) assert len(tl15) == 2 tl16 = test_app.client.call('eth_getFilterChanges', topic_filter_16) assert len(tl16) == 3 tl17 = test_app.client.call('eth_getFilterChanges', topic_filter_17) assert len(tl17) == 3 tl18 = test_app.client.call('eth_getFilterChanges', topic_filter_18) assert len(tl18) == 0 tl19 = test_app.client.call('eth_getFilterChanges', topic_filter_19) assert len(tl19) == 0 tl20 = test_app.client.call('eth_getFilterChanges', topic_filter_20) assert len(tl20) == 1 tl21 = test_app.client.call('eth_getFilterChanges', topic_filter_21) assert len(tl21) == 2
registry_compiled = _solidity.compile_contract( get_contract_path('Registry.sol'), 'Registry', combined='abi', ) # pylint: enable=invalid-name HUMAN_TOKEN_ABI = human_token_compiled['abi'] CHANNEL_MANAGER_ABI = channel_manager_compiled['abi'] NETTING_CHANNEL_ABI = netting_channel_compiled['abi'] REGISTRY_ABI = registry_compiled['abi'] ENDPOINT_REGISTRY_ABI = endpoint_registry_compiled['abi'] ASSETADDED_EVENT = get_event(REGISTRY_ABI, 'AssetAdded') ASSETADDED_EVENTID = event_id(*get_eventname_types(ASSETADDED_EVENT)) CHANNELNEW_EVENT = get_event(CHANNEL_MANAGER_ABI, 'ChannelNew') CHANNELNEW_EVENTID = event_id(*get_eventname_types(CHANNELNEW_EVENT)) CHANNELNEWBALANCE_EVENT = get_event(NETTING_CHANNEL_ABI, 'ChannelNewBalance') CHANNELNEWBALANCE_EVENTID = event_id(*get_eventname_types(CHANNELNEWBALANCE_EVENT)) CHANNELCLOSED_EVENT = get_event(NETTING_CHANNEL_ABI, 'ChannelClosed') CHANNELCLOSED_EVENTID = event_id(*get_eventname_types(CHANNELCLOSED_EVENT)) CHANNELSECRETREVEALED_EVENT = get_event(NETTING_CHANNEL_ABI, 'ChannelSecretRevealed') CHANNELSECRETREVEALED_EVENTID = event_id(*get_eventname_types(CHANNELSECRETREVEALED_EVENT)) CHANNELSETTLED_EVENT = get_event(NETTING_CHANNEL_ABI, 'ChannelSettled') CHANNELSETTLED_EVENTID = event_id(*get_eventname_types(CHANNELSETTLED_EVENT))
def __init__(self, contract_interface): if isinstance(contract_interface, str): contract_interface = json.dumps(contract_interface) self.fallback_data = None self.constructor_data = None self.function_data = {} self.event_data = {} for description in contract_interface: entry_type = description.get('type', 'function') encode_types = [] signature = [] # If it's a function/constructor/event if entry_type != 'fallback' and 'inputs' in description: encode_types = [] signature = [] for element in description.get('inputs', []): encode_type = process_abi_type(element) encode_types.append(encode_type) signature.append((encode_type, element['name'])) if entry_type == 'function': normalized_name = normalize_name(description['name']) prefix = method_id(normalized_name, encode_types) decode_types = [] for element in description.get('outputs', []): decode_type = process_abi_type(element) decode_types.append(decode_type) # 1st is 0.6.0 way, 2nd is old is_constant = description.get('stateMutability', '') == 'view' \ or description.get('constant', False) self.function_data[normalized_name] = { 'prefix': prefix, 'encode_types': encode_types, 'decode_types': decode_types, 'is_constant': is_constant, 'signature': signature, 'payable': description.get('payable', False), } elif entry_type == 'event': normalized_name = normalize_name(description['name']) indexed = [ element['indexed'] for element in description['inputs'] ] names = [element['name'] for element in description['inputs']] # event_id == topics[0] self.event_data[event_id(normalized_name, encode_types)] = { 'types': encode_types, 'name': normalized_name, 'names': names, 'indexed': indexed, 'anonymous': description.get('anonymous', False), } elif entry_type == 'constructor': if self.constructor_data is not None: raise ValueError('Only one constructor is supported.') self.constructor_data = { 'encode_types': encode_types, 'signature': signature, } elif entry_type == 'fallback': if self.fallback_data is not None: raise ValueError( 'Only one fallback function is supported.') self.fallback_data = {'payable': description['payable']} else: raise ValueError('Unknown type {}'.format(description['type']))
def computeEventId(hook): assert hook.abi['type'] == 'event' return hex( abi.event_id(hook.abi['name'], [x['type'] for x in hook.abi['inputs']]))[:-1]