Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions scripts/mech_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
IPFS_ADDRESS = f"{HTTPS}gateway.autonolas.tech/ipfs/"
MECH_EVENTS_DB_VERSION = 3
DEFAULT_MECH_FEE = 10000000000000000
DEFAULT_FROM_TIMESTAMP = 0
DEFAULT_TO_TIMESTAMP = 2147483647
MECH_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/57238/mech/0.0.2"
SUBGRAPH_HEADERS = {
"Accept": "application/json, multipart/mixed",
Expand Down Expand Up @@ -290,7 +292,18 @@ def _get_mech_events(sender: str, event_cls: type[MechBaseEvent]) -> Dict[str, A
return sender_data.get(event_cls.event_name, {})


def get_mech_requests(sender: str) -> Dict[str, Any]:
def get_mech_requests(
sender: str,
from_timestamp: float = DEFAULT_FROM_TIMESTAMP,
to_timestamp: float = DEFAULT_TO_TIMESTAMP,
) -> Dict[str, Any]:
"""Returns the Mech requests."""

return _get_mech_events(sender, MechRequest)
all_mech_events = _get_mech_events(sender, MechRequest)
filtered_mech_events = {}
for event_id, event_data in all_mech_events.items():
block_timestamp = event_data["block_timestamp"]
if from_timestamp <= block_timestamp <= to_timestamp:
filtered_mech_events[event_id] = event_data

return filtered_mech_events
50 changes: 33 additions & 17 deletions trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2022-2023 Valory AG
# Copyright 2022-2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,6 @@

import datetime
import re
import time
from argparse import Action, ArgumentError, ArgumentParser, Namespace
from collections import defaultdict
from enum import Enum
Expand Down Expand Up @@ -159,6 +158,7 @@ class MarketState(Enum):
FINALIZING = 3
ARBITRATING = 4
CLOSED = 5
UNKNOWN = 6

def __str__(self) -> str:
"""Prints the market status."""
Expand Down Expand Up @@ -489,6 +489,31 @@ def _compute_totals(
)


def _get_market_state(market: Dict[str, Any]) -> MarketState:
try:
now = datetime.datetime.utcnow()

market_state = MarketState.CLOSED
if market[
"currentAnswer"
] is None and now >= datetime.datetime.utcfromtimestamp(
float(market.get("openingTimestamp", 0))
):
market_state = MarketState.PENDING
elif market["currentAnswer"] is None:
market_state = MarketState.OPEN
elif market["isPendingArbitration"]:
market_state = MarketState.ARBITRATING
elif now < datetime.datetime.utcfromtimestamp(
float(market.get("answerFinalizedTimestamp", 0))
):
market_state = MarketState.FINALIZING

return market_state
except Exception: # pylint: disable=broad-except
return MarketState.UNKNOWN


def _format_table(table: Dict[Any, Dict[Any, Any]]) -> str:
column_width = 14

Expand Down Expand Up @@ -642,9 +667,6 @@ def parse_user( # pylint: disable=too-many-locals,too-many-statements
creation_timestamp = float(fpmmTrade["creationTimestamp"])

fpmm = fpmmTrade["fpmm"]
answer_finalized_timestamp = fpmm["answerFinalizedTimestamp"]
is_pending_arbitration = fpmm["isPendingArbitration"]
opening_timestamp = fpmm["openingTimestamp"]

output += f' Question: {fpmmTrade["title"]}\n'
output += f' Market URL: https://aiomen.eth.limo/#/{fpmm["id"]}\n'
Expand All @@ -654,17 +676,7 @@ def parse_user( # pylint: disable=too-many-locals,too-many-statements
)
output += f' Trade date: {creation_timestamp_utc.strftime("%Y-%m-%d %H:%M:%S %Z")}\n'

market_status = MarketState.CLOSED
if fpmm["currentAnswer"] is None and time.time() >= float(
opening_timestamp
):
market_status = MarketState.PENDING
elif fpmm["currentAnswer"] is None:
market_status = MarketState.OPEN
elif is_pending_arbitration:
market_status = MarketState.ARBITRATING
elif time.time() < float(answer_finalized_timestamp):
market_status = MarketState.FINALIZING
market_status = _get_market_state(fpmm)

statistics_table[MarketAttribute.NUM_TRADES][market_status] += 1
statistics_table[MarketAttribute.INVESTMENT][
Expand Down Expand Up @@ -795,7 +807,11 @@ def get_mech_statistics(mech_requests: Dict[str, Any]) -> Dict[str, Dict[str, in
with open(RPC_PATH, "r", encoding="utf-8") as rpc_file:
rpc = rpc_file.read()

mech_requests = get_mech_requests(user_args.creator)
mech_requests = get_mech_requests(
user_args.creator,
user_args.from_date.timestamp(),
user_args.to_date.timestamp(),
)
mech_statistics = get_mech_statistics(mech_requests)

trades_json = _query_omen_xdai_subgraph(
Expand Down