Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Github Actions CI - mypy, pytest, black #17

Merged
merged 30 commits into from
Jan 26, 2024
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
15 changes: 15 additions & 0 deletions .github/actions/python_prepare/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: "Prepare Python environment"
description: "Set up Python and install dependencies"
runs:
using: "composite"
steps:
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install Poetry
shell: bash
run: curl -sSL https://install.python-poetry.org | python3 -
- name: Install dependencies
shell: bash
run: poetry install
52 changes: 52 additions & 0 deletions .github/workflows/python_ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Python CI

on:
pull_request:
push:
branches: [main]
workflow_dispatch:

env:
MANIFOLD_API_KEY: ${{ secrets.MANIFOLD_API_KEY }}
SERP_API_KEY: ${{ secrets.SERP_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
BET_FROM_ADDRESS: ${{ secrets.BET_FROM_ADDRESS }}
BET_FROM_PRIVATE_KEY: ${{ secrets.BET_FROM_PRIVATE_KEY }}

jobs:
mypy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/python_prepare
- name: Run mypy
run: poetry run mypy

pytest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/python_prepare
- name: Run pytest
run: poetry run pytest

pytest-paid:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/python_prepare
- name: Run pytest
run: poetry run pytest
env:
RUN_PAID_TESTS: "1"
if: github.event_name == 'workflow_dispatch'
# TODO: Remove once the environment variables are fixed in the new repository.
continue-on-error: true

black:
kongzii marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/python_prepare
- name: Check with black
run: poetry run black --check .
77 changes: 76 additions & 1 deletion poetry.lock

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

14 changes: 0 additions & 14 deletions prediction_market_agent/ai_models/llama_ai_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,3 @@ def construct_llama_prompt(messages: list[Message]) -> str:
)
for message in messages
)


if __name__ == "__main__":
from dotenv import load_dotenv

load_dotenv()

model = ChatReplicateLLamaModel()
messages = [
Message(role=LlamaRole.user.value, content="Hello!"),
Message(role=LlamaRole.assistant.value, content="Bonjour!"),
]
completion = model.complete(messages)
print(completion)
7 changes: 0 additions & 7 deletions prediction_market_agent/ai_models/openai_ai_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,3 @@ def complete(self, messages: list[Message]) -> Optional[str]:
)
completion = response.choices[0].message.content
return completion


if __name__ == "__main__":
model = ChatOpenAIModel()
messages = [Message(role="user", content="Hello, how are you?")]
completion = model.complete(messages)
print(completion)
10 changes: 7 additions & 3 deletions prediction_market_agent/data_models/market_data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class OmenMarket(Market):
collateralToken: HexAddress
outcomes: list[str]
outcomeTokenAmounts: list[OmenOutcomeToken]
outcomeTokenMarginalPrices: list[xDai]
outcomeTokenMarginalPrices: t.Optional[list[xDai]]
fee: t.Optional[Wei]

@property
Expand All @@ -80,8 +80,12 @@ def collateral_token_contract_address_checksummed(self) -> ChecksumAddress:
return Web3.to_checksum_address(self.collateral_token_contract_address)

@property
def outcomeTokenProbabilities(self) -> list[Probability]:
return [Probability(float(x)) for x in self.outcomeTokenMarginalPrices]
def outcomeTokenProbabilities(self) -> t.Optional[list[Probability]]:
return (
[Probability(float(x)) for x in self.outcomeTokenMarginalPrices]
if self.outcomeTokenMarginalPrices is not None
else None
)

def get_outcome_index(self, outcome: str) -> int:
try:
Expand Down
9 changes: 1 addition & 8 deletions prediction_market_agent/markets/manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import typing as t
from prediction_market_agent import utils
from prediction_market_agent.tools.gtypes import Mana, mana_type
from prediction_market_agent.tools.utils import check_not_none
from prediction_market_agent.data_models.market_data_models import ManifoldMarket

"""
Expand Down Expand Up @@ -67,11 +68,3 @@ def place_bet(amount: Mana, market_id: str, outcome: bool, api_key: str) -> None
raise Exception(
f"Placing bet failed: {response.status_code} {response.reason} {response.text}"
)


if __name__ == "__main__":
# A test run
market = pick_binary_market()
print(market.question)
print("Placing bet on market:", market.question)
place_bet(mana_type(2), market.id, True, utils.get_manifold_api_key())
25 changes: 21 additions & 4 deletions prediction_market_agent/markets/omen.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
OmenOutcomeToken,
)

OMEN_TRUE_OUTCOME = "Yes"
OMEN_FALSE_OUTCOME = "No"

with open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)), "../abis/omen_fpmm.abi.json"
Expand Down Expand Up @@ -111,7 +114,7 @@ def get_omen_markets(first: int, outcomes: list[str]) -> list[OmenMarket]:


def get_omen_binary_markets(limit: int) -> list[OmenMarket]:
return get_omen_markets(limit, ["Yes", "No"])
return get_omen_markets(limit, [OMEN_TRUE_OUTCOME, OMEN_FALSE_OUTCOME])


def pick_binary_market() -> OmenMarket:
Expand Down Expand Up @@ -403,7 +406,7 @@ def binary_omen_buy_outcome_tx(
from_address=from_address,
from_private_key=from_private_key,
market=market,
outcome="Yes" if binary_outcome else "No",
outcome=OMEN_TRUE_OUTCOME if binary_outcome else OMEN_FALSE_OUTCOME,
auto_deposit=auto_deposit,
)

Expand Down Expand Up @@ -476,5 +479,19 @@ def omen_sell_outcome_tx(
check_tx_receipt(withdraw_receipt)


if __name__ == "__main__":
pprint(get_omen_binary_markets(3))
def binary_omen_sell_outcome_tx(
amount: xDai,
from_address: ChecksumAddress,
from_private_key: PrivateKey,
market: OmenMarket,
binary_outcome: bool,
auto_withdraw: bool,
) -> None:
omen_sell_outcome_tx(
amount=amount,
from_address=from_address,
from_private_key=from_private_key,
market=market,
outcome=OMEN_TRUE_OUTCOME if binary_outcome else OMEN_FALSE_OUTCOME,
auto_withdraw=auto_withdraw,
)
32 changes: 6 additions & 26 deletions prediction_market_agent/tools/betting_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def get_market_moving_bet(
dx = (new_product - fixed_product) / na_y
"""
amounts = market.outcomeTokenAmounts
prices = market.outcomeTokenProbabilities
prices = check_not_none(
market.outcomeTokenProbabilities, "No probabilities, is marked closed?"
)
if len(amounts) != 2 or len(prices) != 2:
raise ValueError("Only binary markets are supported.")

Expand Down Expand Up @@ -191,7 +193,9 @@ def get_kelly_criterion_bet(
if len(market.outcomeTokenAmounts) != 2:
raise ValueError("Only binary markets are supported.")

current_p_yes = market.outcomeTokenProbabilities[0]
current_p_yes = check_not_none(
market.outcomeTokenProbabilities, "No probabilities, is marked closed?"
)[0]
outcome_index: OutcomeIndex = 0 if estimated_p_yes > current_p_yes else 1
estimated_p_win = estimated_p_yes if outcome_index == 0 else 1 - estimated_p_yes

Expand All @@ -210,27 +214,3 @@ def get_kelly_criterion_bet(
)
)
return wei_to_xdai(kelly_bet_wei), outcome_index


if __name__ == "__main__":
market_address = "0xa3e47bb771074b33f2e279b9801341e9e0c9c6d7"
market = get_market(market_address)

est_p_yes = Probability(0.1)
mov_bet = get_market_moving_bet(
market=market,
target_p_yes=est_p_yes,
verbose=True,
)
kelly_bet = get_kelly_criterion_bet(
market=market,
estimated_p_yes=est_p_yes,
max_bet=xdai_type(10), # This significantly changes the outcome
)

print(
f"Market moving bet: {mov_bet[0]:.2f} on {market.get_outcome_str(mov_bet[1])}"
)
print(
f"Kelly criterion bet: {kelly_bet[0]:.2f} on {market.get_outcome_str(kelly_bet[1])}"
)
4 changes: 0 additions & 4 deletions prediction_market_agent/tools/gnosis_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@ def get_balance(address: str) -> Wei:
).json()
balance = Wei(int(response["result"], 16)) # Convert hex value to int.
return balance


if __name__ == "__main__":
print(get_balance("0xf3318C420e5e30C12786C4001D600e9EE1A7eBb1"))
4 changes: 4 additions & 0 deletions prediction_market_agent/tools/gtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
Mana = NewType("Mana", Decimal) # Manifold's "currency"


def usd_type(amount: Union[str, int, float, Decimal]) -> USD:
return USD(Decimal(amount))


def wei_type(amount: Union[str, int]) -> Wei:
return Wei(int(amount))

Expand Down
8 changes: 0 additions & 8 deletions prediction_market_agent/tools/web_scrape_structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,3 @@ def pretty_html_from_page_content(page_content_html: str, remove_a_links: bool)
).prettify()
page_content_body_text_clean = prettify_html(page_content_body_text)
return page_content_body_text_clean


if __name__ == "__main__":
print(
web_scrape_structured(
"https://ambcrypto.com/predictions/gnosis-price-prediction"
)
)
Loading
Loading