Skip to content

Commit

Permalink
Force DatetimeUTC everywhere (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Oct 4, 2024
1 parent d9904ee commit 05535b1
Show file tree
Hide file tree
Showing 44 changed files with 442 additions and 326 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/python_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ jobs:
python-version: [ '3.10.x', '3.11.x', '3.12.x' ]
test:
- name: Unit Tests
command: 'poetry run python -m pytest tests/ -p no:ape_test'
command: 'poetry run python -m pytest tests/ -p no:ape_test -vvv'
- name: Integration Tests
command: 'poetry run python -m pytest tests_integration/ -p no:ape_test'
command: 'poetry run python -m pytest tests_integration/ -p no:ape_test -vvv'
- name: Integration with Local Chain
command: 'poetry run python -m pytest tests_integration_with_local_chain/ --disable-isolation'
command: 'poetry run python -m pytest tests_integration_with_local_chain/ --disable-isolation -vvv'
name: pytest - Python ${{ matrix.python-version }} - ${{ matrix.test.name }}
steps:
- uses: actions/checkout@v2
Expand Down
8 changes: 5 additions & 3 deletions examples/monitor/match_bets_with_langfuse_traces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from typing import Any

import pandas as pd
Expand All @@ -24,7 +23,10 @@
get_trace_for_bet,
get_traces_for_agent,
)
from prediction_market_agent_tooling.tools.utils import get_private_key_from_gcp_secret
from prediction_market_agent_tooling.tools.utils import (
get_private_key_from_gcp_secret,
utc_datetime,
)


class SimulatedOutcome(BaseModel):
Expand Down Expand Up @@ -160,7 +162,7 @@ def get_outcome_for_trace(
api_keys = APIKeys(BET_FROM_PRIVATE_KEY=private_key)

# Pick a time after pool token number is stored in OmenAgentMarket
start_time = datetime(2024, 9, 13)
start_time = utc_datetime(2024, 9, 13)

langfuse = Langfuse(
secret_key=api_keys.langfuse_secret_key.get_secret_value(),
Expand Down
21 changes: 16 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions prediction_market_agent_tooling/benchmark/agents.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import random
import typing as t
from datetime import datetime

from prediction_market_agent_tooling.benchmark.utils import (
OutcomePrediction,
Prediction,
)
from prediction_market_agent_tooling.gtypes import Probability
from prediction_market_agent_tooling.tools.utils import DatetimeUTC


class AbstractBenchmarkedAgent:
Expand Down Expand Up @@ -41,7 +41,7 @@ def check_and_predict(self, market_question: str) -> Prediction:
def is_predictable_restricted(
self,
market_question: str,
time_restriction_up_to: datetime,
time_restriction_up_to: DatetimeUTC,
) -> bool:
"""
Override if the agent can decide to not predict the question, before doing the hard work.
Expand All @@ -53,7 +53,7 @@ def is_predictable_restricted(
def predict_restricted(
self,
market_question: str,
time_restriction_up_to: datetime,
time_restriction_up_to: DatetimeUTC,
) -> Prediction:
"""
Predict the outcome of the market question.
Expand All @@ -65,7 +65,7 @@ def predict_restricted(
def check_and_predict_restricted(
self,
market_question: str,
time_restriction_up_to: datetime,
time_restriction_up_to: DatetimeUTC,
) -> Prediction:
"""
Data used must be restricted to the time_restriction_up_to.
Expand Down Expand Up @@ -94,7 +94,7 @@ def predict(self, market_question: str) -> Prediction:
)

def predict_restricted(
self, market_question: str, time_restriction_up_to: datetime
self, market_question: str, time_restriction_up_to: DatetimeUTC
) -> Prediction:
return self.predict(market_question)

Expand All @@ -117,6 +117,6 @@ def predict(self, market_question: str) -> Prediction:
)

def predict_restricted(
self, market_question: str, time_restriction_up_to: datetime
self, market_question: str, time_restriction_up_to: DatetimeUTC
) -> Prediction:
return self.predict(market_question)
8 changes: 4 additions & 4 deletions prediction_market_agent_tooling/deploy/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tempfile
import time
import typing as t
from datetime import datetime, timedelta
from datetime import timedelta
from enum import Enum
from functools import cached_property

Expand Down Expand Up @@ -67,7 +67,7 @@
from prediction_market_agent_tooling.tools.ipfs.ipfs_handler import IPFSHandler
from prediction_market_agent_tooling.tools.is_predictable import is_predictable_binary
from prediction_market_agent_tooling.tools.langfuse_ import langfuse_context, observe
from prediction_market_agent_tooling.tools.utils import DatetimeWithTimezone, utcnow
from prediction_market_agent_tooling.tools.utils import DatetimeUTC, utcnow
from prediction_market_agent_tooling.tools.web3_utils import ipfscidv0_to_byte32

MAX_AVAILABLE_MARKETS = 20
Expand Down Expand Up @@ -215,7 +215,7 @@ def deploy_gcp(
secrets: dict[str, str] | None = None,
cron_schedule: str | None = None,
gcp_fname: str | None = None,
start_time: DatetimeWithTimezone | None = None,
start_time: DatetimeUTC | None = None,
timeout: int = 180,
) -> None:
path_to_agent_file = os.path.relpath(inspect.getfile(self.__class__))
Expand Down Expand Up @@ -287,7 +287,7 @@ def run(self, market_type: MarketType) -> None:
raise NotImplementedError("This method must be implemented by the subclass.")

def get_gcloud_fname(self, market_type: MarketType) -> str:
return f"{self.__class__.__name__.lower()}-{market_type}-{datetime.now().strftime('%Y-%m-%d--%H-%M-%S')}"
return f"{self.__class__.__name__.lower()}-{market_type}-{utcnow().strftime('%Y-%m-%d--%H-%M-%S')}"


class DeployableTraderAgent(DeployableAgent):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from datetime import datetime
from typing import Any

from pydantic import BaseModel


class Metadata(BaseModel):
creationTimestamp: datetime
creationTimestamp: int
generation: int
name: str
namespace: str
Expand All @@ -15,12 +14,12 @@ class Metadata(BaseModel):


class Metadata1(BaseModel):
creationTimestamp: datetime | None
creationTimestamp: int | None
name: str


class Metadata2(BaseModel):
creationTimestamp: datetime | None
creationTimestamp: int | None
name: str


Expand Down
5 changes: 3 additions & 2 deletions prediction_market_agent_tooling/gtypes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import typing as t
from datetime import datetime
from typing import NewType, Union

from eth_typing.evm import ( # noqa: F401 # Import for the sake of easy importing with others from here.
Expand All @@ -17,6 +16,9 @@
Wei,
)

from prediction_market_agent_tooling.tools.datetime_utc import ( # noqa: F401 # Import for the sake of easy importing with others from here.
DatetimeUTC,
)
from prediction_market_agent_tooling.tools.hexbytes_custom import ( # noqa: F401 # Import for the sake of easy importing with others from here.
HexBytes,
)
Expand All @@ -32,7 +34,6 @@
Probability = NewType("Probability", float)
Mana = NewType("Mana", float) # Manifold's "currency"
USDC = NewType("USDC", float)
DatetimeWithTimezone = NewType("DatetimeWithTimezone", datetime)
ChainID = NewType("ChainID", int)
IPFSCIDVersion0 = NewType("IPFSCIDVersion0", str)

Expand Down
6 changes: 3 additions & 3 deletions prediction_market_agent_tooling/jobs/jobs_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import typing as t
from abc import ABC, abstractmethod
from datetime import datetime

from pydantic import BaseModel

Expand All @@ -9,14 +8,15 @@
FilterBy,
SortBy,
)
from prediction_market_agent_tooling.tools.utils import DatetimeUTC


class SimpleJob(BaseModel):
id: str
job: str
reward: float
currency: str
deadline: datetime
deadline: DatetimeUTC


class JobAgentMarket(AgentMarket, ABC):
Expand All @@ -29,7 +29,7 @@ def job(self) -> str:

@property
@abstractmethod
def deadline(self) -> datetime:
def deadline(self) -> DatetimeUTC:
"""Deadline for the job completion."""

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions prediction_market_agent_tooling/jobs/omen/omen_jobs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import typing as t
from datetime import datetime

from web3 import Web3

Expand All @@ -21,6 +20,7 @@
OmenSubgraphHandler,
SortBy,
)
from prediction_market_agent_tooling.tools.utils import DatetimeUTC


class OmenJobAgentMarket(OmenAgentMarket, JobAgentMarket):
Expand All @@ -32,7 +32,7 @@ def job(self) -> str:
return self.question

@property
def deadline(self) -> datetime:
def deadline(self) -> DatetimeUTC:
return self.close_time

def get_reward(self, max_bond: float) -> float:
Expand Down
28 changes: 11 additions & 17 deletions prediction_market_agent_tooling/markets/agent_market.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import typing as t
from datetime import datetime
from enum import Enum

from eth_typing import ChecksumAddress
Expand All @@ -18,8 +17,8 @@
TokenAmount,
)
from prediction_market_agent_tooling.tools.utils import (
DatetimeUTC,
check_not_none,
convert_to_utc_datetime,
should_not_happen,
utcnow,
)
Expand Down Expand Up @@ -52,23 +51,16 @@ class AgentMarket(BaseModel):
question: str
description: str | None
outcomes: list[str]
outcome_token_pool: dict[
str, float
] | None # Should be in currency of `currency` above.
outcome_token_pool: (
dict[str, float] | None
) # Should be in currency of `currency` above.
resolution: Resolution | None
created_time: datetime | None
close_time: datetime | None
created_time: DatetimeUTC | None
close_time: DatetimeUTC | None
current_p_yes: Probability
url: str
volume: float | None # Should be in currency of `currency` above.

_add_timezone_validator_created_time = field_validator("created_time")(
convert_to_utc_datetime
)
_add_timezone_validator_close_time = field_validator("close_time")(
convert_to_utc_datetime
)

@field_validator("outcome_token_pool")
def validate_outcome_token_pool(
cls,
Expand Down Expand Up @@ -182,7 +174,7 @@ def get_binary_markets(
limit: int,
sort_by: SortBy,
filter_by: FilterBy = FilterBy.OPEN,
created_after: t.Optional[datetime] = None,
created_after: t.Optional[DatetimeUTC] = None,
excluded_questions: set[str] | None = None,
) -> t.Sequence["AgentMarket"]:
raise NotImplementedError("Subclasses must implement this method")
Expand All @@ -193,13 +185,15 @@ def get_binary_market(id: str) -> "AgentMarket":

@staticmethod
def get_bets_made_since(
better_address: ChecksumAddress, start_time: datetime
better_address: ChecksumAddress, start_time: DatetimeUTC
) -> list[Bet]:
raise NotImplementedError("Subclasses must implement this method")

@staticmethod
def get_resolved_bets_made_since(
better_address: ChecksumAddress, start_time: datetime, end_time: datetime | None
better_address: ChecksumAddress,
start_time: DatetimeUTC,
end_time: DatetimeUTC | None,
) -> list[ResolvedBet]:
raise NotImplementedError("Subclasses must implement this method")

Expand Down
6 changes: 3 additions & 3 deletions prediction_market_agent_tooling/markets/data_models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import datetime
from enum import Enum
from typing import Annotated, TypeAlias

from pydantic import BaseModel, BeforeValidator, computed_field

from prediction_market_agent_tooling.gtypes import OutcomeStr, Probability
from prediction_market_agent_tooling.tools.utils import DatetimeUTC


class Currency(str, Enum):
Expand Down Expand Up @@ -40,7 +40,7 @@ class Bet(BaseModel):
id: str
amount: BetAmount
outcome: bool
created_time: datetime
created_time: DatetimeUTC
market_question: str
market_id: str

Expand All @@ -50,7 +50,7 @@ def __str__(self) -> str:

class ResolvedBet(Bet):
market_outcome: bool
resolved_time: datetime
resolved_time: DatetimeUTC
profit: ProfitAmount

@computed_field # type: ignore[prop-decorator]
Expand Down
Loading

0 comments on commit 05535b1

Please sign in to comment.