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

Hotfix/staking kpi reachability #358

Merged
merged 103 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
dc02d62
add: invested amount variable to Bets class
keshav1998 Nov 21, 2024
49661aa
fix: invested amount update fix
keshav1998 Nov 21, 2024
6504518
edit: change invested amount variable name
keshav1998 Nov 21, 2024
d547c82
update: bet sampling functionality
keshav1998 Nov 21, 2024
530f52c
add: invested amount updation functionality
keshav1998 Nov 21, 2024
a9f44ed
update(bets.py): encapsulate invested amount attribute
keshav1998 Nov 21, 2024
583e8ee
add: queue number attribute to Bet class
keshav1998 Nov 21, 2024
c9aa1af
add: functionality to give priority to older queue
keshav1998 Nov 21, 2024
f5ead30
add: queue number update method
keshav1998 Nov 21, 2024
262a361
add: transaction processed timestamp attribute
keshav1998 Nov 21, 2024
fa76565
add: bet transaction infromation updation
keshav1998 Nov 21, 2024
fb81156
fix: current queue number update methodology
keshav1998 Nov 21, 2024
dffd5df
edit: make private variables public for easy access
keshav1998 Nov 22, 2024
76c4ae0
edit: investment amount update functionality
keshav1998 Nov 22, 2024
54d1963
edit: extract betplacement round name from variable
keshav1998 Nov 22, 2024
97d436b
fix: typo in samplinhg
keshav1998 Nov 22, 2024
cfe3169
fix: typo
annasambrook Nov 22, 2024
fefba99
fix: sampled_bet on update_bet_transaction_information
annasambrook Nov 22, 2024
dd52873
fix: remove return from update_bet_transaction_information
annasambrook Nov 22, 2024
627f5d5
comment: add comment for update_bet_transaction_information
annasambrook Nov 22, 2024
e046f7c
fix: imports ordering
keshav1998 Nov 22, 2024
b0ccc9f
Merge remote-tracking branch 'origin/hotfix/staking-kpi-reachability'…
annasambrook Nov 22, 2024
8d3b9af
fix: only increase current queue number if the queue has been exhausted.
annasambrook Nov 22, 2024
6e58090
Revert "fix: only increase current queue number if the queue has been…
annasambrook Nov 22, 2024
c949ae2
chore: generators
annasambrook Nov 22, 2024
2f6beed
Merge branch 'main' into hotfix/staking-kpi-reachability
keshav1998 Nov 28, 2024
b0d63ab
add: comments for logic of bet transaction update
keshav1998 Nov 28, 2024
4a18889
update: queue no to -1 for blacklisted bets
keshav1998 Nov 28, 2024
423a082
update: bet queue process
keshav1998 Nov 28, 2024
5090305
update: bet sampling process
keshav1998 Nov 28, 2024
f5732a1
update: change transaction variable to previous
keshav1998 Nov 28, 2024
bbded75
remove: transaction timestamp updation
keshav1998 Nov 28, 2024
14283a0
edit: configure txn processed time variable
keshav1998 Nov 28, 2024
b942963
revert: additon of txn timestamp attribute
keshav1998 Nov 28, 2024
f5b6cfe
revert: queue organisation process
keshav1998 Nov 28, 2024
6e03c80
add: other max queue number possibilities
keshav1998 Nov 29, 2024
c2999c9
add: edge case for non processed bets
keshav1998 Nov 29, 2024
bc15710
update: package hashes
keshav1998 Nov 29, 2024
51ddcc1
update: tox changes
keshav1998 Nov 29, 2024
22fb5e6
update: packahe hases post tox changes
keshav1998 Nov 29, 2024
fb2e900
Merge branch 'main' into hotfix/staking-kpi-reachability
keshav1998 Nov 29, 2024
0285b22
update: hases post main sync
keshav1998 Nov 29, 2024
09ab75d
refactor: optimise and streamline act of sampling
keshav1998 Nov 30, 2024
b1b7ef6
edit: sampled bet updation process
keshav1998 Nov 30, 2024
9c64add
add: n_bets increment for sampled bet
keshav1998 Nov 30, 2024
70e146d
fix: update bet txn info call placement
keshav1998 Nov 30, 2024
ea96f2c
update: blacklisting behaviour logic
keshav1998 Dec 1, 2024
f70e884
update: bet filtering process for sampling
keshav1998 Dec 1, 2024
3dec787
add: queue no processing during update bets
keshav1998 Dec 1, 2024
b371f2e
edit: simplify sorting logic
keshav1998 Dec 1, 2024
7a7ce6c
remove: older method of sampling using randomness
keshav1998 Dec 1, 2024
2b8393e
remove: unused import
keshav1998 Dec 1, 2024
6d3422c
fix: Ensure _compute_stop_trading always behaves as a generator
keshav1998 Dec 1, 2024
5333fac
refactor: Simplify _compute_stop_trading by removing unnecessary list
keshav1998 Dec 1, 2024
f5d5567
refactor: sampling process with two queues
keshav1998 Dec 2, 2024
dcc702e
add: queue transitions for sampled bets
keshav1998 Dec 2, 2024
c4c3adf
update: blacklisting mechanishm using queue transitions
keshav1998 Dec 2, 2024
aac3e7c
refactor: queueing behaviour
keshav1998 Dec 2, 2024
9905b4a
add: check point reach flag
keshav1998 Dec 2, 2024
67a2797
add: checkpoint check to market synced data
keshav1998 Dec 2, 2024
d5c8b4a
add: Queue status enum class
keshav1998 Dec 2, 2024
d643aff
Merge branch 'main' into hotfix/staking-kpi-reachability
keshav1998 Dec 2, 2024
9dc6228
update: package hashes
keshav1998 Dec 2, 2024
ad60caa
add: changes introduces by tox run
keshav1998 Dec 3, 2024
a79ea0e
Merge branch 'main' into hotfix/staking-kpi-reachability
keshav1998 Dec 3, 2024
a432519
fix: setup introduced by merging
keshav1998 Dec 3, 2024
4ef03c0
update: hashes
keshav1998 Dec 3, 2024
2f08b74
add: docstring to queue status
keshav1998 Dec 3, 2024
f6eef5d
refactor: sampling behaviour for understandibilty
keshav1998 Dec 4, 2024
038e87a
fix: queue transition for blacklisting
keshav1998 Dec 4, 2024
30a4c11
remove: unused imports
keshav1998 Dec 4, 2024
6b9afe5
lock: package hashes
keshav1998 Dec 4, 2024
3aa2568
relock: packages hasehs
keshav1998 Dec 4, 2024
a0260cd
update: stramline bet queue status transition
keshav1998 Dec 6, 2024
5a92650
add: comment for missing functionality
keshav1998 Dec 6, 2024
24df901
remove: unused imports
keshav1998 Dec 6, 2024
8f4677c
update: streamline queuestatus operation
keshav1998 Dec 6, 2024
328deea
add: setup method to Update bets behaviour
keshav1998 Dec 6, 2024
d2755f3
update: improve bet sorting by queues status
keshav1998 Dec 6, 2024
ae60986
update: sampled bet idx return type
keshav1998 Dec 6, 2024
bd1afd0
fix: bets decoder for bet format
keshav1998 Dec 6, 2024
2ff375d
bump: hashes for packages
keshav1998 Dec 6, 2024
98c50c5
fix: wrong Dict import
keshav1998 Dec 6, 2024
ad354de
bump: package hashes
keshav1998 Dec 6, 2024
110d60f
fix: queustatus fetching during read
keshav1998 Dec 6, 2024
cf41679
bump: update package hashes
keshav1998 Dec 6, 2024
cc08251
update: setup call not required
keshav1998 Dec 6, 2024
769e7a7
fix: none return from sampling
keshav1998 Dec 6, 2024
851eb5d
bump: update hashes
keshav1998 Dec 6, 2024
de956ad
update: bets file path for multi bets
keshav1998 Dec 6, 2024
c915459
update: streamline bets read path
keshav1998 Dec 6, 2024
35610f2
bump: update hashes with read changes
keshav1998 Dec 6, 2024
40e2c4c
fix: error raise on transition
keshav1998 Dec 6, 2024
c05a717
update: next status transition
keshav1998 Dec 6, 2024
fd35046
bump: update hashes
keshav1998 Dec 6, 2024
15c4100
fix: check whether the service has transacted first
Adamantios Dec 9, 2024
e0a0fad
chore: run generators
Adamantios Dec 9, 2024
584b4ab
fix: correctly determine whether a new epoch is reached
Adamantios Dec 11, 2024
8af2865
fix: do not move bets from reprocessed during the same epoch
Adamantios Dec 11, 2024
a02629c
refactor: make methods static
Adamantios Dec 11, 2024
c4b604a
refactor: remove unnecessary initialization
Adamantios Dec 11, 2024
e0d9f7a
test: update the cases for `CallCheckpointPayload`
Adamantios Dec 11, 2024
b8190fc
chore: run generators
Adamantios Dec 11, 2024
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 packages/valory/skills/decision_maker_abci/behaviours/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,21 @@ def check_balance(self) -> WaitableConditionType:
self._report_balance()
return True

def update_bet_transaction_information(self) -> None:
"""Get whether the bet's invested amount should be updated."""
self.read_bets()
# Update the bet's invested amount, the new bet amount is added to previous invested amount
self.bets[
self.synchronized_data.sampled_bet_index
].invested_amount += self.synchronized_data.bet_amount

# Update bet transaction timestamp
self.bets[
self.synchronized_data.sampled_bet_index
].transaction_processed_timestamp = self.synced_timestamp
annasambrook marked this conversation as resolved.
Show resolved Hide resolved
self.store_bets()
keshav1998 marked this conversation as resolved.
Show resolved Hide resolved
return
Adamantios marked this conversation as resolved.
Show resolved Hide resolved

def send_message(
self, msg: Message, dialogue: Dialogue, callback: Callable
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
Trade,
)
from packages.valory.skills.decision_maker_abci.states.redeem import RedeemRound
from packages.valory.skills.decision_maker_abci.states.bet_placement import (
BetPlacementRound,
)
from packages.valory.skills.market_manager_abci.graph_tooling.requests import (
FetchStatus,
MAX_LOG_SIZE,
Expand Down Expand Up @@ -966,6 +969,9 @@ def async_act(self) -> Generator:
if not success:
return None

if self.synchronized_data.tx_submitter == BetPlacementRound.auto_round_id():
self.update_bet_transaction_information()
keshav1998 marked this conversation as resolved.
Show resolved Hide resolved

payload: Optional[RedeemPayload]
if self.benchmarking_mode.enabled:
payload = self._benchmarking_act()
Expand Down
46 changes: 40 additions & 6 deletions packages/valory/skills/decision_maker_abci/behaviours/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,49 @@ def processable_bet(self, bet: Bet) -> bool:

def _sampled_bet_idx(self, bets: List[Bet]) -> int:
"""
Sample a bet and return its id.

The sampling logic is relatively simple at the moment.
It simply selects the unprocessed bet with the largest liquidity.
Sample a bet and return its index.

The sampling logic follows the specified priority logic:
1. Filter out all the bets that have a processed_timestamp != 0 to get a list of new bets.
2. If the list of new bets is not empty:
2.1 Order the list in decreasing order of liquidity (highest liquidity first).
2.2 For bets with the same liquidity, order them in decreasing order of market closing time (openingTimestamp).
3. If the list of new bets is empty:
3.1 Order the bets in decreasing order of invested_amount.
3.2 For bets with the same invested_amount, order them in increasing order of processed_timestamp (least recently processed first).
3.3 For bets with the same invested_amount and processed_timestamp, order them in decreasing order of liquidity.
3.4 For bets with the same invested_amount, processed_timestamp, and liquidity, order them in decreasing order of market closing time (openingTimestamp).
keshav1998 marked this conversation as resolved.
Show resolved Hide resolved

:param bets: the bets' values to compare for the sampling.
:return: the id of the sampled bet, out of all the available bets, not only the given ones.
:return: the index of the sampled bet, out of all the available bets, not only the given ones.
"""
return self.bets.index(max(bets))

# Filter out all the best with the smallest queue number
keshav1998 marked this conversation as resolved.
Show resolved Hide resolved
least_queue_number = min([bet.queue_no for bet in bets])
priority_bets = [bet for bet in bets if bet.queue_no == least_queue_number]

# Filter out all the bets that have a processed_timestamp == 0
new_in_priority_bets = [bet for bet in bets if bet.processed_timestamp == 0]

if new_in_priority_bets:
keshav1998 marked this conversation as resolved.
Show resolved Hide resolved
# Order the list in Decreasing order of liquidity
new_in_priority_bets.sort(
key=lambda bet: (bet.scaledLiquidityMeasure, bet.openingTimestamp),
reverse=True,
)
return self.bets.index(new_in_priority_bets[0])
else:
# Order first in Decreasing order of invested_amount
priority_bets.sort(
key=lambda bet: (
bet.invested_amount,
-bet.processed_timestamp, # Increasing order of processed_timestamp
bet.scaledLiquidityMeasure,
bet.openingTimestamp,
),
reverse=True,
)
return self.bets.index(priority_bets[0])

def _sample(self) -> Optional[int]:
"""Sample a bet, mark it as processed, and return its index."""
Expand Down
14 changes: 14 additions & 0 deletions packages/valory/skills/market_manager_abci/behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, **kwargs: Any) -> None:
"""Initialize `BetsManagerBehaviour`."""
super().__init__(**kwargs)
self.bets: List[Bet] = []
self.current_queue_number: int = 0
self.bets_filepath: str = self.params.store_path / BETS_FILENAME

def store_bets(self) -> None:
Expand Down Expand Up @@ -93,6 +94,7 @@ def read_bets(self) -> None:
with open(self.bets_filepath, READ_MODE) as bets_file:
try:
self.bets = json.load(bets_file, cls=BetsDecoder)

keshav1998 marked this conversation as resolved.
Show resolved Hide resolved
return
except (JSONDecodeError, TypeError):
err = (
Expand Down Expand Up @@ -130,6 +132,7 @@ def _process_chunk(self, chunk: Optional[List[Dict[str, Any]]]) -> None:
bet = Bet(**raw_bet, market=self._current_market)
index = self.get_bet_idx(bet.id)
if index is None:
bet.queue_no = self.current_queue_number
self.bets.append(bet)
else:
self.bets[index].update_market_info(bet)
Expand All @@ -138,6 +141,12 @@ def _update_bets(
self,
) -> Generator:
"""Fetch the questions from all the prediction markets and update the local copy of the bets."""

# Extract the last queue number from the bets
# This is used to determine the next queue number
if self.bets:
self.current_queue_number = max(bet.queue_no for bet in self.bets) + 1
Adamantios marked this conversation as resolved.
Show resolved Hide resolved

while True:
can_proceed = self._prepare_fetching()
if not can_proceed:
Expand All @@ -154,6 +163,11 @@ def _update_bets(
if self.synced_time >= bet.openingTimestamp - self.params.opening_margin:
bet.blacklist_forever()

# Extract the last queue number from the bets
# This is used to determine the next queue number
if self.bets:
self.current_queue_number = max(bet.queue_no for bet in self.bets)
keshav1998 marked this conversation as resolved.
Show resolved Hide resolved

# truncate the bets, otherwise logs get too big
bets_str = str(self.bets)[:MAX_LOG_SIZE]
self.context.logger.info(f"Updated bets: {bets_str}")
Expand Down
3 changes: 3 additions & 0 deletions packages/valory/skills/market_manager_abci/bets.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ class Bet:
prediction_response: PredictionResponse = dataclasses.field(
default_factory=get_default_prediction_response
)
invested_amount: float = 0.0
position_liquidity: int = 0
potential_net_profit: int = 0
processed_timestamp: int = 0
transaction_processed_timestamp: int = 0
keshav1998 marked this conversation as resolved.
Show resolved Hide resolved
n_bets: int = 0
queue_no: int = 0

def __post_init__(self) -> None:
"""Post initialization to adjust the values."""
Expand Down
Loading