Ejemplo n.º 1
0
    def filter_events(self, filter_params: Dict, name_to_callback: Dict):
        """ Filter events for given event names

        Params:
            filter_params: arguments for the filter call
            name_to_callback: dict that maps event name to callbacks executed
                if the event is emmited
        """
        for id, (topics, callback) in name_to_callback.items():
            events = get_events(
                web3=self.web3,
                contract_address=self.contract_address,
                topics=topics,
                **filter_params,
            )

            events_abi = filter_by_type(
                "event",
                self.contract_manager.get_contract_abi(self.contract_name))
            topic_to_event_abi = {
                event_abi_to_log_topic(event_abi): event_abi
                for event_abi in events_abi
            }
            for raw_event in events:
                decoded_event = decode_event(
                    codec=self.web3.codec,
                    topic_to_event_abi=topic_to_event_abi,
                    log_entry=raw_event,
                )
                log.debug('Received confirmed event: \n%s', decoded_event)
                callback(decoded_event)
Ejemplo n.º 2
0
def create_event_topic_to_abi_dict() -> Dict[bytes, ABIEvent]:
    contract_names = [
        CONTRACT_TOKEN_NETWORK_REGISTRY,
        CONTRACT_TOKEN_NETWORK,
        CONTRACT_MONITORING_SERVICE,
    ]

    event_abis = {}
    for contract_name in contract_names:
        events = filter_by_type(
            "event", CONTRACT_MANAGER.get_contract_abi(contract_name))

        for event_abi in events:
            event_topic = event_abi_to_log_topic(event_abi)  # type: ignore
            event_abis[event_topic] = event_abi

    return event_abis  # type: ignore
def get_txinfo_by_rpc(receipt_hash):
    cross_info_dic = {}
    withdawcontact = w3.eth.contract(address=address, abi=abi)
    abiArray = [abi for abi in withdawcontact.abi if (abi['type'] == "event" and abi['name'] == 'PayloadReceived')]
    if len(abiArray) > 0:
        topic = event_abi_to_log_topic(abiArray[0])
        recepit = w3.eth.getTransactionReceipt(receipt_hash)
        status = recepit['status']
        if status == 1:
            logs = recepit['logs']
            for log in logs:
                if log['address'] == address and log['topics'][0] == topic:
                    data = HexBytes(log['data'])
                    types = [x['type'] for x in abiArray[0]['inputs']]
                    res = decode_abi(types, data)
                    cross_info_dic.setdefault(str(res[0], encoding="utf8"), []).append(
                        [(str(res[2] / 1e18)), (str(res[1] / 1e18))])

    return cross_info_dic
Ejemplo n.º 4
0
def decode_event(abi: ABI, log_: Dict) -> Dict:
    """ Helper function to unpack event data using a provided ABI

    Args:
        abi: The ABI of the contract, not the ABI of the event
        log_: The raw event data

    Returns:
        The decoded event
    """
    if isinstance(log_["topics"][0], str):
        log_["topics"][0] = decode_hex(log_["topics"][0])
    elif isinstance(log_["topics"][0], int):
        log_["topics"][0] = decode_hex(hex(log_["topics"][0]))
    event_id = log_["topics"][0]
    events = filter_by_type("event", abi)
    topic_to_event_abi = {event_abi_to_log_topic(event_abi): event_abi for event_abi in events}
    event_abi = topic_to_event_abi[event_id]
    return get_event_data(event_abi, log_)
Ejemplo n.º 5
0
def query_blockchain_events(
    web3: Web3,
    contract_manager: ContractManager,
    contract_address: Address,
    contract_name: str,
    topics: List,
    from_block: BlockNumber,
    to_block: BlockNumber,
) -> List[Dict]:
    """Returns events emmitted by a contract for a given event name, within a certain range.

    Args:
        web3: A Web3 instance
        contract_manager: A contract manager
        contract_address: The address of the contract to be filtered, can be `None`
        contract_name: The name of the contract
        topics: The topics to filter for
        from_block: The block to start search events
        to_block: The block to stop searching for events

    Returns:
        All matching events
    """
    events_abi = filter_by_type(
        "event", contract_manager.get_contract_abi(contract_name))
    topic_to_event_abi = {
        event_abi_to_log_topic(event_abi): event_abi
        for event_abi in events_abi
    }

    filter_params = {
        "fromBlock": from_block,
        "toBlock": to_block,
        "address": to_checksum_address(contract_address),
        "topics": topics,
    }

    events = web3.eth.getLogs(filter_params)

    return [
        decode_event(topic_to_event_abi, log_entry) for log_entry in events
    ]
Ejemplo n.º 6
0
def test_event_abi_to_log_topic(event_abi, expected):
    bytes_topic = event_abi_to_log_topic(event_abi)
    hex_topic = encode_hex(bytes_topic)
    assert hex_topic == expected
Ejemplo n.º 7
0
def create_registry_event_topics(contract_manager: ContractManager) -> List:
    new_network_abi = contract_manager.get_event_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY, EVENT_TOKEN_NETWORK_CREATED)
    return [encode_hex(event_abi_to_log_topic(new_network_abi))]
Ejemplo n.º 8
0
def create_registry_event_topics(contract_manager: ContractManager,
                                 contract_name: str, event_name: str) -> List:
    """Returns network ABI"""
    new_network_abi = contract_manager.get_event_abi(contract_name, event_name)
    return [encode_hex(event_abi_to_log_topic(new_network_abi))]
def create_registry_event_topics() -> List:
    new_network_abi = CONTRACT_MANAGER.get_event_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY,
        EVENT_TOKEN_NETWORK_CREATED,
    )
    return [encode_hex(event_abi_to_log_topic(new_network_abi))]
Ejemplo n.º 10
0
def event_signature(event_name):
    abi = event_abi(event_name)
    if abi is None:
        return None
    return web3.toHex(event_abi_to_log_topic(abi))