示例#1
0
文件: event.py 项目: tbrent/brownie
def _get_topics(abi: List) -> Dict:
    topic_map = eth_event.get_topic_map(abi)

    new_topics = _topics.copy()
    new_topics.update(topic_map)
    if new_topics != _topics:
        _topics.update(new_topics)
        with __get_path().open("w") as fp:
            json.dump(new_topics, fp, sort_keys=True, indent=2)

    return {v["name"]: k for k, v in topic_map.items()}
def decode_log(tx, contract, event_name):
    """
    Get transaction logs for specific event name
    :param tx: transaction object
    :param contract: Contract object
    :param event_name: name of the event
    :return: EventDict with the events parsed
    """
    topic_map = eth_event.get_topic_map(contract.abi)
    logs = eth_event.decode_logs(tx.logs, topic_map, True)
    events = list(filter(lambda tx_: tx_['name'] == event_name, logs))
    return EventDict([format_event(i) for i in events])[event_name]
def parse_swap(log):
    addr_pair = log['address']
    [(t0_balance, t0_address), (t1_balance, t1_address),
     amount_lp] = web3_utils.get_lp_value(uni_wrapper, addr_pair)
    t0_info = web3_utils.get_token_info(uni_wrapper, t0_address)
    t0_info = TokenInfo(addr=t0_address,
                        name=t0_info['name'],
                        symbol=t0_info['symbol'],
                        decimals=t0_info['decimals'])
    t1_info = web3_utils.get_token_info(uni_wrapper, t1_address)
    t1_info = TokenInfo(addr=t1_address,
                        name=t1_info['name'],
                        symbol=t1_info['symbol'],
                        decimals=t1_info['decimals'])
    topic_map = eth_event.get_topic_map(abi_pair)
    swap = eth_event.decode_logs([log], topic_map)
    # pprint(swap)
    (amount0In, amount1Out, amount1In, amount0Out, to,
     sender) = (None, None, None, None, None, None)
    for value in swap[0]['data']:
        # pprint(value)
        if value['name'] == 'sender':
            sender = value['value']
        elif value['name'] == 'to':
            to = value['value']
        elif value['name'] == 'amount0In':
            amount0In = int(value['value'])
        elif value['name'] == 'amount0Out':
            amount0Out = int(value['value'])
        elif value['name'] == 'amount1In':
            amount1In = int(value['value'])
        elif value['name'] == 'amount1Out':
            amount1Out = int(value['value'])
    if None in [amount0In, amount1Out, amount1In, amount0Out, to, sender]:
        return None
    if amount0In > 0:
        return Swap((t1_info, amount1Out), (t0_info, amount0In),
                    sender=to,
                    to=sender)
    else:
        return Swap((t0_info, amount0Out), (t1_info, amount1In),
                    sender=sender,
                    to=to)
示例#4
0
def _get_topics(abi: List) -> Dict:
    topic_map = eth_event.get_topic_map(abi)

    updated_topics = _topics.copy()

    for key, value in topic_map.items():
        if key not in updated_topics:
            # new event topic
            updated_topics[key] = value
        elif value == updated_topics[key]:
            # existing event topic, nothing has changed
            continue
        elif not next((i for i in updated_topics[key]["inputs"] if i["indexed"]), False):
            # existing topic, but the old abi has no indexed events - keep the new one
            updated_topics[key] = value

    if updated_topics != _topics:
        _topics.update(updated_topics)
        with __get_path().open("w") as fp:
            json.dump(updated_topics, fp, sort_keys=True, indent=2)

    return {v["name"]: k for k, v in topic_map.items()}
示例#5
0
def _add_deployment_topics(address: str, abi: List) -> None:
    _deployment_topics[address] = eth_event.get_topic_map(abi)
示例#6
0
def test_event_abi(abi):
    result = get_topic_map(abi)
    assert len(result) + 2 == len(abi)
示例#7
0
def test_insufficient_log_topics():
    topic_map = get_topic_map([abi_two_indexes])
    with pytest.raises(EventError, match="more topics than expected"):
        decode_log(log_one_index, topic_map)
示例#8
0
def test_too_many_log_topics():
    topic_map = get_topic_map([abi_no_indexes])
    with pytest.raises(EventError, match="does not contain enough topics"):
        decode_log(log_two_indexes, topic_map)
示例#9
0
def test_no_topics_in_log():
    topic_map = get_topic_map([abi_two_indexes])
    decode_log(log_no_indexes, topic_map)
示例#10
0
def topic_map(abi):
    return get_topic_map(abi)