-
Notifications
You must be signed in to change notification settings - Fork 9
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
Agent to replicate markets to Omen #61
Changes from 45 commits
dffb701
56dcede
2c79d73
f59d846
1e201d3
52b7bc1
54a4e1c
78a3513
d00b8ff
aa6a94b
2c73169
0ae0908
5d20946
ea928f5
30f7ecc
d100a09
a1535c0
86de9e8
b3f4124
b1ff99c
d5b1241
9f263e4
9dc3890
ad5ef62
5b3de61
bf98065
401af4c
6895620
082069d
a9e7470
e414107
da3c54a
81586ec
4d154ba
9bd41bb
5e8a300
ddc1b17
77b0738
bf05380
3218f9e
449c67d
1a59e7f
49fbbbd
bec89a0
6718904
57ef5b8
89f3cb3
e883829
de35a4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from datetime import datetime, timedelta | ||
|
||
import functions_framework | ||
import pytz | ||
from flask import Request | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
from prediction_market_agent_tooling.benchmark.utils import MarketSource | ||
from prediction_market_agent_tooling.config import APIKeys | ||
from prediction_market_agent_tooling.deploy.agent import DeployableAgent, MarketType | ||
from prediction_market_agent_tooling.gtypes import xdai_type | ||
from prediction_market_agent_tooling.markets.omen.omen import ( | ||
omen_create_market_deposit_tx, | ||
omen_replicate_from_tx, | ||
) | ||
|
||
|
||
class ReplicateSettings(BaseSettings): | ||
model_config = SettingsConfigDict( | ||
env_file=".env", env_file_encoding="utf-8", extra="ignore" | ||
) | ||
|
||
N_TO_REPLICATE: int | ||
INITIAL_FUNDS: str | ||
CLOSE_TIME_UP_TO_N_DAYS: int | ||
|
||
|
||
class DeployableReplicateToOmenAgent(DeployableAgent): | ||
def run( | ||
self, market_type: MarketType = MarketType.MANIFOLD, _place_bet: bool = True | ||
) -> None: | ||
keys = APIKeys() | ||
settings = ReplicateSettings() | ||
close_time_before = ( | ||
datetime.utcnow() + timedelta(days=settings.CLOSE_TIME_UP_TO_N_DAYS) | ||
).replace(tzinfo=pytz.UTC) | ||
initial_funds_per_market = xdai_type(settings.INITIAL_FUNDS) | ||
deposit_funds_per_replication = xdai_type( | ||
initial_funds_per_market * settings.N_TO_REPLICATE | ||
) | ||
|
||
print(f"Replicating from {MarketSource.MANIFOLD}.") | ||
# Deposit enough of xDai for all N markets to be replicated, so we don't re-deposit in case of re-tries. | ||
omen_create_market_deposit_tx( | ||
deposit_funds_per_replication, | ||
keys.bet_from_address, | ||
keys.bet_from_private_key, | ||
) | ||
omen_replicate_from_tx( | ||
market_source=MarketSource.MANIFOLD, | ||
n_to_replicate=settings.N_TO_REPLICATE, | ||
initial_funds=initial_funds_per_market, | ||
from_address=keys.bet_from_address, | ||
from_private_key=keys.bet_from_private_key, | ||
close_time_before=close_time_before, | ||
auto_deposit=False, | ||
) | ||
print(f"Replicating from {MarketSource.POLYMARKET}.") | ||
# Deposit enough of xDai for all N markets to be replicated, so we don't re-deposit in case of re-tries. | ||
omen_create_market_deposit_tx( | ||
deposit_funds_per_replication, | ||
keys.bet_from_address, | ||
keys.bet_from_private_key, | ||
) | ||
omen_replicate_from_tx( | ||
market_source=MarketSource.POLYMARKET, | ||
n_to_replicate=settings.N_TO_REPLICATE, | ||
initial_funds=initial_funds_per_market, | ||
from_address=keys.bet_from_address, | ||
from_private_key=keys.bet_from_private_key, | ||
close_time_before=close_time_before, | ||
auto_deposit=False, | ||
) | ||
print("Done.") | ||
|
||
|
||
@functions_framework.http | ||
def main(request: Request) -> str: | ||
DeployableReplicateToOmenAgent().run() | ||
return "Success" | ||
Comment on lines
+28
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The However, there are a couple of areas that could be improved:
Consider refactoring the code to reduce duplication and add error handling around external calls. Here's a suggested approach for refactoring: def replicate_markets(self, market_source):
print(f"Replicating from {market_source}.")
# Deposit enough of xDai for all N markets to be replicated, so we don't re-deposit in case of re-tries.
omen_create_market_deposit_tx(
self.deposit_funds_per_replication,
self.keys.bet_from_address,
self.keys.bet_from_private_key,
)
omen_replicate_from_tx(
market_source=market_source,
n_to_replicate=self.settings.N_TO_REPLICATE,
initial_funds=self.initial_funds_per_market,
from_address=self.keys.bet_from_address,
from_private_key=self.keys.bet_from_private_key,
close_time_before=self.close_time_before,
auto_deposit=False,
) And then call this method for each market source in the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
|
||
from prediction_market_agent_tooling.deploy.gcp.deploy import ( | ||
deploy_to_gcp, | ||
run_deployed_gcp_function, | ||
schedule_deployed_gcp_function, | ||
) | ||
from prediction_market_agent_tooling.deploy.gcp.utils import gcp_function_is_active | ||
|
||
fname = "replicate_markets" | ||
current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
deploy_to_gcp( | ||
gcp_fname=fname, | ||
requirements_file=None, | ||
extra_deps=[ | ||
"git+https://github.com/gnosis/prediction-market-agent-tooling.git@449c67df2ec02f61411e153565c5e3f8ba01dda1", | ||
"langchain", | ||
"langchain_openai", | ||
], | ||
function_file=os.path.join(current_dir, "agent_example.py"), | ||
memory=512, | ||
entrypoint_function_name="main", | ||
timeout=180, | ||
labels=None, | ||
env_vars=None, # TODO add env vars | ||
secrets=None, # TODO add secrets | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Would you like me to help with defining the environment variables and secrets or open a GitHub issue to track this task? |
||
) | ||
|
||
# Check that the function is deployed | ||
if not gcp_function_is_active(fname): | ||
raise RuntimeError("Failed to deploy the function") | ||
|
||
# Run the function | ||
response = run_deployed_gcp_function(fname) | ||
if not response.ok: | ||
raise RuntimeError("Failed to run the deployed function") | ||
|
||
# Schedule the function | ||
schedule_deployed_gcp_function(fname, cron_schedule="0 */6 * * *") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The handling of
env_vars
andsecrets
using JSON parsing allows for a more dynamic configuration. However, as previously mentioned, consider adding error handling for JSON parsing to gracefully handle parsing errors and improve robustness.Committable suggestion