Skip to content

Commit

Permalink
Replicator fixes (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Feb 5, 2025
1 parent e98ebe6 commit f428502
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 303 deletions.
526 changes: 252 additions & 274 deletions poetry.lock

Large diffs are not rendered by default.

21 changes: 8 additions & 13 deletions prediction_market_agent/agents/replicate_to_omen_agent/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
from prediction_market_agent_tooling.gtypes import xdai_type
from prediction_market_agent_tooling.loggers import logger
from prediction_market_agent_tooling.markets.markets import MarketType
from prediction_market_agent_tooling.markets.omen.omen import (
redeem_from_all_user_positions,
)
from prediction_market_agent_tooling.tools.langfuse_ import observe
from prediction_market_agent_tooling.tools.utils import utcnow
from pydantic import BaseModel
Expand All @@ -18,7 +15,7 @@
omen_unfund_replicated_known_markets_tx,
)
from prediction_market_agent.agents.replicate_to_omen_agent.omen_resolve_replicated import (
omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx,
omen_finalize_and_resolve_and_claim_back_all_replicated_markets_tx,
)
from prediction_market_agent.utils import APIKeys

Expand Down Expand Up @@ -62,20 +59,18 @@ def replicate(self, settings: ReplicateSettings) -> None:
keys = APIKeys()
now = utcnow()

logger.info(
f"Finalising, resolving and claiming back xDai from existing markets replicated by {keys.bet_from_address}."
)
omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx(
keys, realitio_bond=REPLICATOR_BOND
)

# Unfund markets as the first thing, to get back resources that we can use later in this script.
logger.info(
f"Unfunding soon to be known markets replicated by {keys.bet_from_address}."
)
omen_unfund_replicated_known_markets_tx(keys, saturation_above_threshold=0.9)

logger.info("Redeeming funds from previously unfunded markets.")
redeem_from_all_user_positions(keys)
logger.info(
f"Finalising, resolving and claiming back xDai from existing markets replicated by {keys.bet_from_address}."
)
omen_finalize_and_resolve_and_claim_back_all_replicated_markets_tx(
keys, realitio_bond=REPLICATOR_BOND
)

for replicate_config in settings.REPLICATE:
if now.timetuple().tm_yday % replicate_config.every_n_days:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
OmenAgentMarket,
omen_create_market_tx,
omen_remove_fund_market_tx,
redeem_from_all_user_positions,
)
from prediction_market_agent_tooling.markets.omen.omen_contracts import sDaiContract
from prediction_market_agent_tooling.markets.omen.omen_subgraph_handler import (
Expand Down Expand Up @@ -226,7 +227,7 @@ def omen_unfund_replicated_known_markets_tx(
# Optionally, if `saturation_above_threshold` is provided, skip markets that are not saturated to leave some free money motivation for agents.
if (
saturation_above_threshold is not None
and not market.is_resolved
and market.is_open
and not (
market.current_p_yes > saturation_above_threshold
or market.current_p_no > saturation_above_threshold
Expand All @@ -244,3 +245,6 @@ def omen_unfund_replicated_known_markets_tx(
market=OmenAgentMarket.from_data_model(market),
shares=None,
)

logger.info("Redeeming funds from unfunded markets.")
redeem_from_all_user_positions(api_keys)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import timedelta
from functools import partial

from prediction_market_agent_tooling.config import APIKeys
from prediction_market_agent_tooling.gtypes import (
ChecksumAddress,
HexAddress,
Expand All @@ -15,6 +14,9 @@
RealityQuestion,
RealityResponse,
)
from prediction_market_agent_tooling.markets.omen.omen import (
redeem_from_all_user_positions,
)
from prediction_market_agent_tooling.markets.omen.omen_resolving import (
Resolution,
claim_bonds_on_realitio_questions,
Expand All @@ -31,6 +33,11 @@
from prediction_market_agent_tooling.tools.utils import DatetimeUTC, utcnow
from pydantic import BaseModel

from prediction_market_agent.agents.ofvchallenger_agent.ofv_resolver import (
ofv_answer_binary_question,
)
from prediction_market_agent.utils import APIKeys


class ClaimResult(BaseModel):
claimed_question_ids: list[HexBytes]
Expand All @@ -44,7 +51,7 @@ class FinalizeAndResolveResult(BaseModel):


@observe()
def omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx(
def omen_finalize_and_resolve_and_claim_back_all_replicated_markets_tx(
api_keys: APIKeys,
realitio_bond: xDai,
) -> FinalizeAndResolveResult:
Expand All @@ -54,6 +61,9 @@ def omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx(

now = utcnow()

# Claim back as the first thing, so we have resources to work with.
claimed = claim_all_bonds_on_reality(api_keys, finalized_before=now)

# Fetch markets created by us that are already open, but no answer was submitted yet or they are challengable.
get_omen_binary_markets_common_filters = partial(
OmenSubgraphHandler().get_omen_binary_markets,
Expand All @@ -77,7 +87,7 @@ def omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx(
(
m,
(
find_resolution_on_other_markets(m)
find_resolution_on_other_markets_or_using_resolver(m, api_keys)
if not is_invalid(m.question_title)
else Resolution.CANCEL
),
Expand Down Expand Up @@ -114,18 +124,51 @@ def omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx(
api_keys,
created_finalized_markets,
)
# Redeem from the resolved markets.
redeem_from_all_user_positions(api_keys)
balances_after_resolution = get_balances(public_key)
logger.info(f"{balances_after_resolution=}")

claimed = claim_all_bonds_on_reality(api_keys, finalized_before=now)

return FinalizeAndResolveResult(
finalized=finalized_markets,
resolved=resolved_markets,
claimed=claimed,
)


@observe()
def find_resolution_on_other_markets_or_using_resolver(
market: OmenMarket,
api_keys: APIKeys,
) -> Resolution | None:
# Try to find resolution on other markets.
resolution = find_resolution_on_other_markets(market)

# Sometimes questions can be no longer found (for example Manifold allows to rephrase questions),
# in that case, resolve it with our resolver.
if resolution is None:
logger.info(
"[REPLICATOR-RESOLUTION-NOT-FOUND] Resolution not found on other markets. Trying to resolve manually."
)
try:
fact_check = ofv_answer_binary_question(market.question_title, api_keys)
resolution = (
None
if fact_check is None or fact_check.factuality is None
else Resolution.from_bool(fact_check.factuality)
)
except Exception as e:
logger.exception(
f"Exception while getting factuality for market {market.url=}. Skipping. Exception: {e}"
)
else:
logger.info(
f"[REPLICATOR-RESOLUTION-FOUND] Resolution {resolution} found on other markets for {market.url=}."
)

return resolution


@observe()
def filter_replicated_markets_to_answer(
markets: list[tuple[OmenMarket, Resolution | None]],
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ langchain-community = "*"
langchain-openai = "*"
numexpr = "*"
flaml = "^2.2.0,<2.3.0" # Bug in newer version, because they started importing xgboost, without requiring it as a dependency.
beautifulsoup4 = "*"
beautifulsoup4 = "^4,<4.13" # Bug in typing of newer version
google-search-results = "*"
pytest = "*"
llama-index = "~0.9.0"
Expand All @@ -33,7 +33,7 @@ poetry = "^1.7.1"
poetry-plugin-export = "^1.6.0"
functions-framework = "^3.5.0"
cron-validator = "^1.0.8"
prediction-market-agent-tooling = "0.57.14"
prediction-market-agent-tooling = "0.57.16"
pydantic-settings = "^2.1.0"
autoflake = "^2.2.1"
isort = "^5.13.2"
Expand Down
4 changes: 2 additions & 2 deletions scripts/replicate_for_hackathon.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
omen_replicate_from_tx,
)
from prediction_market_agent.agents.replicate_to_omen_agent.omen_resolve_replicated import (
omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx,
omen_finalize_and_resolve_and_claim_back_all_replicated_markets_tx,
)
from prediction_market_agent.utils import APIKeys

Expand All @@ -39,7 +39,7 @@ def main(
)

if resolve:
omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx(
omen_finalize_and_resolve_and_claim_back_all_replicated_markets_tx(
keys, realitio_bond=OMEN_DEFAULT_REALITIO_BOND_VALUE
)
return
Expand Down
35 changes: 32 additions & 3 deletions scripts/replicator_stats.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from datetime import datetime, timedelta
from datetime import timedelta
from pprint import pprint

import typer
from prediction_market_agent_tooling.markets.omen.omen import OmenAgentMarket
from prediction_market_agent_tooling.markets.omen.omen_contracts import (
OmenRealitioContract,
)
from prediction_market_agent_tooling.markets.omen.omen_subgraph_handler import (
OmenSubgraphHandler,
)
from prediction_market_agent_tooling.tools.parallelism import par_generator
from prediction_market_agent_tooling.tools.utils import utcnow
from prediction_market_agent_tooling.tools.web3_utils import wei_to_xdai
from tqdm import tqdm

from prediction_market_agent.agents.replicate_to_omen_agent.deploy import (
Expand All @@ -15,7 +20,7 @@


def main() -> None:
now = datetime.now()
now = utcnow()
markets = OmenSubgraphHandler().get_omen_binary_markets(
limit=None,
creator=REPLICATOR_ADDRESS,
Expand Down Expand Up @@ -43,10 +48,22 @@ def main() -> None:
markets_closing_in_more_than_30_days = [
m for m in markets if m.opening_datetime > now + timedelta(days=30)
]
markets_closing_in_more_than_3_days = [
m for m in markets if m.opening_datetime > now + timedelta(days=3)
]
markets_closing_in_more_than_1_days = [
m for m in markets if m.opening_datetime > now + timedelta(days=1)
]

reality_balance = wei_to_xdai(OmenRealitioContract().balanceOf(REPLICATOR_ADDRESS))

stats = {
"reality_balance": reality_balance,
"markets created": len(markets),
"open markets": len([m for m in markets if m.is_open]),
"markets without an answer": len(
[m for m in markets if m.currentAnswer is None]
),
"liquidity in open markets": sum(
[
OmenAgentMarket.from_data_model(m).get_liquidity().amount
Expand All @@ -61,6 +78,18 @@ def main() -> None:
if not m.is_open
]
),
"liquidity in open markets closing in more than 1 days": sum(
[
OmenAgentMarket.from_data_model(m).get_liquidity().amount
for m in markets_closing_in_more_than_1_days
]
),
"liquidity in open markets closing in more than 3 days": sum(
[
OmenAgentMarket.from_data_model(m).get_liquidity().amount
for m in markets_closing_in_more_than_3_days
]
),
"liquidity in open markets closing in more than 30 days": sum(
[
OmenAgentMarket.from_data_model(m).get_liquidity().amount
Expand All @@ -71,7 +100,7 @@ def main() -> None:
[
OmenAgentMarket.from_data_model(m).get_liquidity().amount
for m in markets
if m.opening_datetime > datetime.now() + timedelta(days=180)
if m.opening_datetime > now + timedelta(days=180)
]
),
"avg bets per market closing in less than 30 days": (
Expand Down
6 changes: 3 additions & 3 deletions scripts/resolve_replicated_on_omen.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from pprint import pprint

import typer
from prediction_market_agent_tooling.config import APIKeys
from prediction_market_agent_tooling.gtypes import private_key_type
from web3 import Web3

from prediction_market_agent.agents.replicate_to_omen_agent.deploy import (
REPLICATOR_BOND,
)
from prediction_market_agent.agents.replicate_to_omen_agent.omen_resolve_replicated import (
omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx,
omen_finalize_and_resolve_and_claim_back_all_replicated_markets_tx,
)
from prediction_market_agent.utils import APIKeys

# Use without the pretty exceptions, because they make the error stack unusable here.
app = typer.Typer(pretty_exceptions_show_locals=False)
Expand All @@ -36,7 +36,7 @@ def main(
SAFE_ADDRESS=safe_address_checksum,
)

result = omen_finalize_and_resolve_and_claim_back_all_markets_based_on_others_tx(
result = omen_finalize_and_resolve_and_claim_back_all_replicated_markets_tx(
api_keys=api_keys, realitio_bond=REPLICATOR_BOND
)
pprint(result.model_dump())
Expand Down

0 comments on commit f428502

Please sign in to comment.