Skip to content

Commit a3aae14

Browse files
committed
feat: update the bets so that we store investments per vote
1 parent e9ba6ee commit a3aae14

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

packages/valory/skills/decision_maker_abci/behaviours/base.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,14 @@ def check_balance(self) -> WaitableConditionType:
355355
def update_bet_transaction_information(self) -> None:
356356
"""Get whether the bet's invested amount should be updated."""
357357
sampled_bet = self.sampled_bet
358-
# Update the bet's invested amount, the new bet amount is added to previously invested amount
359-
sampled_bet.invested_amount += self.synchronized_data.bet_amount
358+
359+
# Update the bet's invested amount
360+
updated = sampled_bet.update_investments(self.synchronized_data.bet_amount)
361+
if not updated:
362+
self.context.logger.error("Could not update the investments!")
363+
360364
# Update bet transaction timestamp
361365
sampled_bet.processed_timestamp = self.synced_timestamp
362-
# update no of bets made
363-
sampled_bet.n_bets += 1
364366
# Update Queue number for priority logic
365367
sampled_bet.queue_status = sampled_bet.queue_status.next_status()
366368

packages/valory/skills/decision_maker_abci/behaviours/decision_receive.py

-2
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,6 @@ def _update_selected_bet(
518518
self.context.logger.info(
519519
f"with the timestamp:{datetime.fromtimestamp(active_sampled_bet.processed_timestamp)}"
520520
)
521-
if prediction_response is not None:
522-
active_sampled_bet.n_bets += 1
523521

524522
self.store_bets()
525523

packages/valory/skills/market_manager_abci/bets.py

+57-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from typing import Any, Dict, List, Optional, Union
2929

3030

31+
YES = "yes"
32+
NO = "no"
3133
P_YES_FIELD = "p_yes"
3234
P_NO_FIELD = "p_no"
3335
CONFIDENCE_FIELD = "confidence"
@@ -136,18 +138,59 @@ class Bet:
136138
prediction_response: PredictionResponse = dataclasses.field(
137139
default_factory=get_default_prediction_response
138140
)
139-
invested_amount: int = 0
140141
position_liquidity: int = 0
141142
potential_net_profit: int = 0
142143
processed_timestamp: int = 0
143-
n_bets: int = 0
144144
queue_status: QueueStatus = QueueStatus.FRESH
145+
# a mapping from vote to investment amounts
146+
investments: Dict[str, List[int]] = dataclasses.field(default_factory=dict)
147+
148+
@property
149+
def yes_investments(self) -> List[int]:
150+
"""Get the yes investments."""
151+
return self.investments[self.yes]
152+
153+
@property
154+
def no_investments(self) -> List[int]:
155+
"""Get the no investments."""
156+
return self.investments[self.no]
157+
158+
@property
159+
def n_yes_bets(self) -> int:
160+
"""Get the number of yes bets."""
161+
return len(self.yes_investments)
162+
163+
@property
164+
def n_no_bets(self) -> int:
165+
"""Get the number of no bets."""
166+
return len(self.no_investments)
167+
168+
@property
169+
def n_bets(self) -> int:
170+
"""Get the number of bets."""
171+
return self.n_yes_bets + self.n_no_bets
172+
173+
@property
174+
def invested_amount_yes(self) -> int:
175+
"""Get the amount invested in yes bets."""
176+
return sum(self.yes_investments)
177+
178+
@property
179+
def invested_amount_no(self) -> int:
180+
"""Get the amount invested in no bets."""
181+
return sum(self.no_investments)
182+
183+
@property
184+
def invested_amount(self) -> int:
185+
"""Get the amount invested in bets."""
186+
return self.invested_amount_yes + self.invested_amount_no
145187

146188
def __post_init__(self) -> None:
147189
"""Post initialization to adjust the values."""
148190
self._validate()
149191
self._cast()
150192
self._check_usefulness()
193+
self.investments = {self.yes: [], self.no: []}
151194

152195
def __lt__(self, other: "Bet") -> bool:
153196
"""Implements less than operator."""
@@ -225,7 +268,7 @@ def _get_binary_outcome(self, no: bool) -> str:
225268
"""Get an outcome only if it is binary."""
226269
if self.outcomeSlotCount == BINARY_N_SLOTS:
227270
return self.get_outcome(int(no))
228-
requested_outcome = "no" if no else "yes"
271+
requested_outcome = NO if no else YES
229272
error = (
230273
f"A {requested_outcome!r} outcome is only available for binary questions."
231274
)
@@ -241,6 +284,17 @@ def no(self) -> str:
241284
"""Return the "no" outcome."""
242285
return self._get_binary_outcome(True)
243286

287+
def update_investments(self, amount: int) -> bool:
288+
"""Get the investments for the current vote type."""
289+
vote = self.prediction_response.vote
290+
if vote is None:
291+
return False
292+
293+
vote_name = self.get_outcome(vote)
294+
to_update = self.investments[vote_name]
295+
to_update.append(amount)
296+
return True
297+
244298
def update_market_info(self, bet: "Bet") -> None:
245299
"""Update the bet's market information."""
246300
if (

0 commit comments

Comments
 (0)