Skip to content

Commit

Permalink
Merge pull request #23 from evangriffiths/evan/bet_amount
Browse files Browse the repository at this point in the history
Pass in generic BetAmount to `place_bet`
  • Loading branch information
evangriffiths authored Feb 6, 2024
2 parents 108589d + 101ce7c commit 8b71d62
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ Execute `main.py` with optional arguments:

```bash
% python main.py --help
usage: main.py [-h] [--agent-type {langchain,autogen,always_yes}] [--auto-bet AUTO_BET]
Usage: main.py [OPTIONS]

optional arguments:
-h, --help show this help message and exit
--agent-type {langchain,autogen,always_yes}
--auto-bet AUTO_BET If true, does not require user input to place the bet.
Picks one market and answers it, optionally placing a bet.

Options:
--market-type [manifold|omen] [default: MarketType.MANIFOLD]
--agent-type [langchain|autogen|always_yes|coin_flip|llamaindex|metagpt|crewai|custom_openai|custom_llama]
[default: AgentType.ALWAYS_YES]
--auto-bet / --no-auto-bet [default: no-auto-bet]
--help Show this message and exit.
```

## Testing
Expand Down
7 changes: 2 additions & 5 deletions agent.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import typer
import time
import logging
import typing as t
from decimal import Decimal
from datetime import timedelta

import prediction_market_agent as pma
from prediction_market_agent.tools.gtypes import xDai, Mana
from prediction_market_agent.markets.all_markets import (
MarketType,
get_bet_amount,
get_binary_markets,
place_bet,
omen,
manifold,
)
from prediction_market_agent.agents.abstract import AbstractAgent
from prediction_market_agent.agents.all_agents import AgentType, get_agent
Expand Down Expand Up @@ -49,8 +47,7 @@ def main(

place_bet(
market=agent_market.original_market,
amount_mana=Mana(amount),
amount_xdai=xDai(amount),
amount=get_bet_amount(amount, market_type),
outcome=answer,
keys=keys,
omen_auto_deposit=True,
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from prediction_market_agent.tools.gtypes import xDai, Mana
from prediction_market_agent.markets.all_markets import (
MarketType,
get_bet_amount,
omen,
manifold,
get_binary_markets,
Expand Down Expand Up @@ -51,8 +52,7 @@ def main(
)
place_bet(
market=market.original_market,
amount_mana=Mana(amount),
amount_xdai=xDai(amount),
amount=get_bet_amount(amount, market_type),
outcome=result,
keys=keys,
omen_auto_deposit=True,
Expand Down
18 changes: 15 additions & 3 deletions prediction_market_agent/data_models/market_data_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal
from enum import Enum
import typing as t
from pydantic import BaseModel
from web3 import Web3
Expand All @@ -14,6 +16,16 @@
from datetime import datetime


class Currency(str, Enum):
xDai = "xDai"
Mana = "Mana"


class BetAmount(BaseModel):
amount: Decimal
currency: Currency


class AgentMarket(BaseModel):
"""
Common market class that can be created from vendor specific markets.
Expand All @@ -23,7 +35,7 @@ class AgentMarket(BaseModel):
id: str
question: str
outcomes: list[str]
bet_amount_currency: str
bet_amount_currency: Currency
original_market: t.Union["OmenMarket", "ManifoldMarket"]


Expand All @@ -32,7 +44,7 @@ class OmenMarket(BaseModel):
https://aiomen.eth.limo
"""

BET_AMOUNT_CURRENCY: t.ClassVar[str] = "xDai"
BET_AMOUNT_CURRENCY: t.ClassVar[Currency] = Currency.xDai

id: HexAddress
title: str
Expand Down Expand Up @@ -107,7 +119,7 @@ class ManifoldMarket(BaseModel):
https://manifold.markets
"""

BET_AMOUNT_CURRENCY: t.ClassVar[str] = "Mana"
BET_AMOUNT_CURRENCY: Currency = Currency.Mana

id: str
question: str
Expand Down
59 changes: 38 additions & 21 deletions prediction_market_agent/markets/all_markets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from decimal import Decimal
import typing as t
from enum import Enum
from prediction_market_agent.data_models.market_data_models import BetAmount, Currency

from prediction_market_agent.markets import manifold, omen
from prediction_market_agent.tools.gtypes import Mana, xDai
from prediction_market_agent.tools.utils import should_not_happen, check_not_none
from prediction_market_agent.utils import APIKeys

Expand All @@ -10,6 +14,15 @@ class MarketType(str, Enum):
OMEN = "omen"


def get_bet_amount(amount: Decimal, market_type: MarketType) -> BetAmount:
if market_type == MarketType.OMEN:
return BetAmount(amount=amount, currency=Currency.xDai)
elif market_type == MarketType.MANIFOLD:
return BetAmount(amount=amount, currency=Currency.Mana)
else:
raise ValueError(f"Unknown market type: {market_type}")


@t.overload
def get_binary_markets(
market_type: t.Literal[MarketType.MANIFOLD],
Expand Down Expand Up @@ -47,25 +60,29 @@ def place_bet(
outcome: bool,
keys: APIKeys,
omen_auto_deposit: bool,
amount_mana: t.Optional[manifold.Mana] = None,
amount_xdai: t.Optional[omen.xDai] = None,
amount: BetAmount,
) -> None:
manifold.place_bet(
amount=check_not_none(amount_mana),
market_id=market.id,
outcome=outcome,
api_key=check_not_none(keys.manifold),
) if isinstance(
market, manifold.ManifoldMarket
) else omen.binary_omen_buy_outcome_tx(
amount=check_not_none(amount_xdai),
from_address=check_not_none(keys.bet_from_address),
from_private_key=check_not_none(keys.bet_from_private_key),
market=market,
binary_outcome=outcome,
auto_deposit=omen_auto_deposit,
) if isinstance(
market, omen.OmenMarket
) else should_not_happen(
f"Unknown market {market}."
)
if isinstance(market, manifold.ManifoldMarket):
if amount.currency != Currency.Mana:
raise ValueError(f"Manifold bets are made in Mana. Got {amount.currency}.")
amount_mana = Mana(amount.amount)
manifold.place_bet(
amount=check_not_none(amount_mana),
market_id=market.id,
outcome=outcome,
api_key=check_not_none(keys.manifold),
)
elif isinstance(market, omen.OmenMarket):
if amount.currency != Currency.xDai:
raise ValueError(f"Omen bets are made in xDai. Got {amount.currency}.")
amount_xdai = xDai(amount.amount)
omen.binary_omen_buy_outcome_tx(
amount=check_not_none(amount_xdai),
from_address=check_not_none(keys.bet_from_address),
from_private_key=check_not_none(keys.bet_from_private_key),
market=market,
binary_outcome=outcome,
auto_deposit=omen_auto_deposit,
)
else:
should_not_happen(f"Unknown market {market}.")

0 comments on commit 8b71d62

Please sign in to comment.