From 4e6596824903c71cdb0cdcb3dbba63ebc060b454 Mon Sep 17 00:00:00 2001 From: Adamantios Date: Wed, 6 Sep 2023 13:30:48 +0300 Subject: [PATCH] refactor: use a function to remove the fraction --- .../valory/skills/decision_maker_abci/behaviours/base.py | 8 ++++++++ .../decision_maker_abci/behaviours/decision_receive.py | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/valory/skills/decision_maker_abci/behaviours/base.py b/packages/valory/skills/decision_maker_abci/behaviours/base.py index acc562d1d..889a40483 100644 --- a/packages/valory/skills/decision_maker_abci/behaviours/base.py +++ b/packages/valory/skills/decision_maker_abci/behaviours/base.py @@ -58,6 +58,14 @@ SAFE_GAS = 0 +def remove_fraction_wei(amount: int, fraction: float) -> int: + """Removes the given fraction from the given integer amount and returns the value as an integer.""" + if 0 <= fraction <= 1: + keep_percentage = 1 - fraction + return int(amount * keep_percentage) + raise ValueError(f"The given fraction {fraction!r} is not in the range [0, 1].") + + class DecisionMakerBaseBehaviour(BaseBehaviour, ABC): """Represents the base class for the decision-making FSM behaviour.""" diff --git a/packages/valory/skills/decision_maker_abci/behaviours/decision_receive.py b/packages/valory/skills/decision_maker_abci/behaviours/decision_receive.py index c0c3a6034..8478c5ff8 100644 --- a/packages/valory/skills/decision_maker_abci/behaviours/decision_receive.py +++ b/packages/valory/skills/decision_maker_abci/behaviours/decision_receive.py @@ -28,6 +28,7 @@ from packages.valory.skills.decision_maker_abci.behaviours.base import ( DecisionMakerBaseBehaviour, WaitableConditionType, + remove_fraction_wei, ) from packages.valory.skills.decision_maker_abci.models import ( MechInteractionResponse, @@ -273,7 +274,7 @@ def _is_profitable(self, confidence: float, vote: int) -> bool: """Whether the decision is profitable or not.""" bet = self.synchronized_data.sampled_bet bet_amount = self.params.get_bet_amount(confidence) - net_bet_amount = int(bet_amount * (1 - self.wei_to_native(bet.fee))) + net_bet_amount = remove_fraction_wei(bet_amount, self.wei_to_native(bet.fee)) num_shares, available_shares = self._calc_binary_shares(net_bet_amount, vote) mech_price = self.synchronized_data.mech_price bet_threshold = self.params.bet_threshold