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

Added boolean flag to control IPFS upload #470

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ BET_FROM_PRIVATE_KEY=
OPENAI_API_KEY=
GRAPH_API_KEY=
PINATA_API_KEY=
PINATA_API_SECRET=
PINATA_API_SECRET=
ENABLE_IPFS_UPLOAD=
7 changes: 7 additions & 0 deletions prediction_market_agent_tooling/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class APIKeys(BaseSettings):
LANGFUSE_HOST: t.Optional[str] = None
LANGFUSE_DEPLOYMENT_VERSION: t.Optional[str] = None

ENABLE_IPFS_UPLOAD: t.Optional[bool] = None
PINATA_API_KEY: t.Optional[SecretStr] = None
PINATA_API_SECRET: t.Optional[SecretStr] = None

Expand Down Expand Up @@ -151,6 +152,12 @@ def default_enable_langfuse(self) -> bool:
and self.LANGFUSE_HOST is not None
)

@property
def enable_ipfs_upload(self) -> bool:
return check_not_none(
self.ENABLE_IPFS_UPLOAD, "ENABLE_IPFS_UPLOAD missing in the environment."
gabrielfior marked this conversation as resolved.
Show resolved Hide resolved
)
gabrielfior marked this conversation as resolved.
Show resolved Hide resolved

@property
def pinata_api_key(self) -> SecretStr:
return check_not_none(
Expand Down
15 changes: 10 additions & 5 deletions prediction_market_agent_tooling/deploy/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pydantic import BaseModel, BeforeValidator, computed_field
from typing_extensions import Annotated
from web3 import Web3
from web3.constants import HASH_ZERO

from prediction_market_agent_tooling.config import APIKeys
from prediction_market_agent_tooling.deploy.betting_strategy import (
Expand Down Expand Up @@ -302,7 +303,6 @@ def __init__(
) -> None:
super().__init__(enable_langfuse=enable_langfuse)
self.place_bet = place_bet
self.ipfs_handler = IPFSHandler(APIKeys())

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
user_id = market.get_user_id(api_keys=APIKeys())
Expand Down Expand Up @@ -522,16 +522,21 @@ def store_prediction(
if processed_market.answer.reasoning
else ""
)
ipfs_hash = self.ipfs_handler.store_agent_result(
IPFSAgentResult(reasoning=reasoning)
)

ipfs_hash_decoded = HexBytes(HASH_ZERO)
if keys.enable_ipfs_upload:
logger.info("Storing prediction on IPFS.")
ipfs_hash = IPFSHandler(keys).store_agent_result(
IPFSAgentResult(reasoning=reasoning)
)
ipfs_hash_decoded = ipfscidv0_to_byte32(ipfs_hash)

tx_hashes = [
HexBytes(HexStr(i.id)) for i in processed_market.trades if i.id is not None
]
prediction = ContractPrediction(
publisher=keys.public_key,
ipfs_hash=ipfscidv0_to_byte32(ipfs_hash),
ipfs_hash=ipfs_hash_decoded,
tx_hashes=tx_hashes,
estimated_probability_bps=int(processed_market.answer.p_yes * 10000),
)
Expand Down
30 changes: 29 additions & 1 deletion tests_integration_with_local_chain/markets/omen/test_omen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
private_key_type,
xDai,
xdai_type,
IPFSCIDVersion0,
)
from prediction_market_agent_tooling.loggers import logger
from prediction_market_agent_tooling.markets.agent_market import FilterBy, SortBy
Expand Down Expand Up @@ -59,7 +60,11 @@
from prediction_market_agent_tooling.tools.balances import get_balances
from prediction_market_agent_tooling.tools.hexbytes_custom import HexBytes
from prediction_market_agent_tooling.tools.utils import utcnow
from prediction_market_agent_tooling.tools.web3_utils import wei_to_xdai, xdai_to_wei
from prediction_market_agent_tooling.tools.web3_utils import (
wei_to_xdai,
xdai_to_wei,
ipfscidv0_to_byte32,
)
from tests_integration_with_local_chain.conftest import create_and_fund_random_account

DEFAULT_REASON = "Test logic need to be rewritten for usage of local chain, see ToDos"
Expand Down Expand Up @@ -476,6 +481,29 @@ def test_add_predictions(local_web3: Web3, test_keys: APIKeys) -> None:
assert stored_predictions[0] == p


def test_add_prediction_with_empty_ipfs_hash(
local_web3: Web3, test_keys: APIKeys
) -> None:
agent_result_mapping = OmenAgentResultMappingContract()
market_address = test_keys.public_key
dummy_transaction_hash = (
"0x3750ffa211dab39b4d0711eb27b02b56a17fa9d257ee549baa3110725fd1d41b"
)
p = ContractPrediction(
tx_hashes=[HexBytes(dummy_transaction_hash)],
estimated_probability_bps=5454,
ipfs_hash=ipfscidv0_to_byte32(IPFSCIDVersion0("")),
publisher=test_keys.public_key,
)

agent_result_mapping.add_prediction(test_keys, market_address, p, web3=local_web3)
stored_predictions = agent_result_mapping.get_predictions(
market_address, web3=local_web3
)
assert len(stored_predictions) == 1
assert stored_predictions[0] == p


def test_place_bet_with_prev_existing_positions(
local_web3: Web3, test_keys: APIKeys
) -> None:
Expand Down
Loading