Skip to content

Commit

Permalink
update: bets file path for multi bets
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav1998 committed Dec 6, 2024
1 parent 851eb5d commit de956ad
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions packages/valory/skills/market_manager_abci/behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@


BETS_FILENAME = "bets.json"
MULTI_BETS_FILENAME = "multi_bets.json"
READ_MODE = "r"
WRITE_MODE = "w"

Expand All @@ -58,6 +59,7 @@ def __init__(self, **kwargs: Any) -> None:
"""Initialize `BetsManagerBehaviour`."""
super().__init__(**kwargs)
self.bets: List[Bet] = []
self.multi_bets_filepath: str = self.params.store_path / MULTI_BETS_FILENAME
self.bets_filepath: str = self.params.store_path / BETS_FILENAME

def store_bets(self) -> None:
Expand All @@ -68,44 +70,48 @@ def store_bets(self) -> None:
return

try:
with open(self.bets_filepath, WRITE_MODE) as bets_file:
with open(self.multi_bets_filepath, WRITE_MODE) as bets_file:
try:
bets_file.write(serialized)
return
except (IOError, OSError):
err = f"Error writing to file {self.bets_filepath!r}!"
err = f"Error writing to file {self.multi_bets_filepath!r}!"
except (FileNotFoundError, PermissionError, OSError):
err = f"Error opening file {self.bets_filepath!r} in write mode!"
err = f"Error opening file {self.multi_bets_filepath!r} in write mode!"

self.context.logger.error(err)

def read_bets(self) -> None:
"""Read the bets from the agent's data dir as JSON."""
self.bets = []

if not os.path.isfile(self.bets_filepath):
_read_path = self.multi_bets_filepath
if not os.path.isfile(_read_path):
self.context.logger.warning(
f"No stored bets file was detected in {self.bets_filepath}. Assuming bets are empty."
f"No stored bets file was detected in {self.multi_bets_filepath}. Assuming trader is being run for the first time in multi-bets mode."
)
_read_path = self.bets_filepath
elif not os.path.isfile(_read_path):
self.context.logger.warning(
f"No stored bets file was detected in {self.bets_filepath}. Assuming bets are empty"
)
return

try:
with open(self.bets_filepath, READ_MODE) as bets_file:
with open(_read_path, READ_MODE) as bets_file:
try:
self.bets = json.load(bets_file, cls=BetsDecoder)
return
except (JSONDecodeError, TypeError):
err = (
f"Error decoding file {self.bets_filepath!r} to a list of bets!"
)
err = f"Error decoding file {_read_path!r} to a list of bets!"
except (FileNotFoundError, PermissionError, OSError):
err = f"Error opening file {self.bets_filepath!r} in read mode!"
err = f"Error opening file {_read_path!r} in read mode!"

self.context.logger.error(err)

def hash_stored_bets(self) -> str:
"""Get the hash of the stored bets' file."""
return IPFSHashOnly.hash_file(self.bets_filepath)
return IPFSHashOnly.hash_file(self.multi_bets_filepath)


class UpdateBetsBehaviour(BetsManagerBehaviour, QueryingBehaviour):
Expand Down

0 comments on commit de956ad

Please sign in to comment.