Skip to content

Commit

Permalink
Merge branch 'peter/events' into peter/stealing
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Sep 30, 2024
2 parents 5a83924 + b33ebc0 commit 180902e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
5 changes: 3 additions & 2 deletions prediction_market_agent_tooling/markets/omen/data_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typing as t
from datetime import datetime

import pytz
from pydantic import BaseModel
from web3 import Web3

Expand Down Expand Up @@ -218,7 +219,7 @@ def openingTimestamp(self) -> int:

@property
def opening_datetime(self) -> datetime:
return datetime.fromtimestamp(self.openingTimestamp)
return datetime.fromtimestamp(self.openingTimestamp, tz=pytz.UTC)

@property
def close_time(self) -> datetime:
Expand Down Expand Up @@ -469,7 +470,7 @@ class OmenBet(BaseModel):

@property
def creation_datetime(self) -> datetime:
return datetime.fromtimestamp(self.creationTimestamp)
return datetime.fromtimestamp(self.creationTimestamp, tz=pytz.UTC)

@property
def boolean_outcome(self) -> bool:
Expand Down
15 changes: 14 additions & 1 deletion prediction_market_agent_tooling/tools/langfuse_client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from langfuse.client import TraceWithDetails
from pydantic import BaseModel

from prediction_market_agent_tooling.loggers import logger
from prediction_market_agent_tooling.markets.data_models import (
PlacedTrade,
ProbabilisticAnswer,
Expand Down Expand Up @@ -146,9 +147,21 @@ def get_trace_for_bet(
else:
# In-case there are multiple traces for the same market, get the closest
# trace to the bet
bet_timestamp = convert_to_utc_datetime(bet.created_time)
closest_trace_index = get_closest_datetime_from_list(
convert_to_utc_datetime(bet.created_time),
bet_timestamp,
[t.timestamp for t in traces_for_bet],
)

# Sanity check: Let's say the upper bound for time between
# `agent.process_market` being called and the bet being placed is 20
# minutes
candidate_trace = traces_for_bet[closest_trace_index]
if abs(candidate_trace.timestamp - bet_timestamp).total_seconds() > 1200:
logger.info(
f"Closest trace to bet has timestamp {candidate_trace.timestamp}, "
f"but bet was created at {bet_timestamp}. Not matching"
)
return None

return traces_for_bet[closest_trace_index]
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import os

from web3 import Web3

from prediction_market_agent_tooling.config import APIKeys
from prediction_market_agent_tooling.tools.contract import DebuggingContract

RUN_PAID_TESTS = os.environ.get("RUN_PAID_TESTS", "0") == "1"


def mint_new_block(keys: APIKeys, web3: Web3) -> None:
"""
Mints a new block on the web3's blockchain.
Useful for tests that debends on chain's timestamp, this will update it.
"""
DebuggingContract().inc(keys, web3)
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time
from datetime import timedelta

from ape_test import TestAccount
from eth_account import Account
Expand All @@ -20,6 +19,7 @@
wei_to_xdai,
xdai_to_wei,
)
from tests.utils import mint_new_block


def test_connect_local_chain(local_web3: Web3) -> None:
Expand Down Expand Up @@ -102,8 +102,8 @@ def test_fresh_account_has_less_than_minimum_required_balance(

def test_now(local_web3: Web3, test_keys: APIKeys) -> None:
# we need to mint a new block to update timestamp
DebuggingContract().inc(test_keys, local_web3)
allowed_difference = 10 # seconds
mint_new_block(test_keys, local_web3)
allowed_difference = 15 # seconds
chain_timestamp = DebuggingContract().getNow(local_web3)
utc_timestamp = int(utcnow().timestamp())
assert (
Expand All @@ -114,7 +114,7 @@ def test_now(local_web3: Web3, test_keys: APIKeys) -> None:
def test_now_failed(local_web3: Web3, test_keys: APIKeys) -> None:
# Sleep a little to let the local chain go out of sync without updating the block
time.sleep(5)
allowed_difference = 10 # seconds
allowed_difference = 5 # seconds
chain_timestamp = DebuggingContract().getNow(local_web3)
utc_timestamp = int(utcnow().timestamp())
assert (
Expand All @@ -124,11 +124,11 @@ def test_now_failed(local_web3: Web3, test_keys: APIKeys) -> None:

def test_now_datetime(local_web3: Web3, test_keys: APIKeys) -> None:
# we need to mint a new block to update timestamp
DebuggingContract().inc(test_keys, local_web3)
allowed_difference = 10 # seconds
mint_new_block(test_keys, local_web3)
allowed_difference = 15 # seconds
chain_datetime = DebuggingContract().get_now(local_web3)
utc_datetime = utcnow()
actual_difference = abs(chain_datetime - utc_datetime)
assert actual_difference <= timedelta(
seconds=allowed_difference
actual_difference = (utc_datetime - chain_datetime).total_seconds()
assert (
actual_difference <= allowed_difference
), f"chain_datetime and utc_datetime differ by more than {allowed_difference} seconds: {chain_datetime=} {utc_datetime=} {actual_difference=}"
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def test_place_bet_with_autodeposit(

# Check that we have xdai funds, but no wxdai funds
initial_balances = get_balances(address=test_keys.bet_from_address, web3=local_web3)
assert initial_balances.wxdai == xdai_type(0)
assert np.isclose(initial_balances.wxdai, xdai_type(0))
assert initial_balances.xdai > xdai_type(0)

# Try to place a bet with 90% of the xDai funds
Expand Down

0 comments on commit 180902e

Please sign in to comment.