From 3822a14bc58c82dc5b2fd8e3dfd01f6902f78570 Mon Sep 17 00:00:00 2001
From: crokkon <33018033+crokkon@users.noreply.github.com>
Date: Mon, 26 Nov 2018 12:19:06 +0100
Subject: [PATCH] steem.py: fix missing steem_instance for Amount()

* restructuring `sbd_to_rshares` to avoid redundant function calls and
streamline the calculations
---
 beem/steem.py | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

diff --git a/beem/steem.py b/beem/steem.py
index c5c9917d..55b44779 100644
--- a/beem/steem.py
+++ b/beem/steem.py
@@ -665,35 +665,31 @@ class Steem(object):
             sbd = Amount(sbd, self.sbd_symbol, steem_instance=self)
         if sbd['symbol'] != self.sbd_symbol:
             raise AssertionError('Should input SBD, not any other asset!')
-        reward_pool_sbd = self.get_median_price(use_stored_data=use_stored_data) * Amount(self.get_reward_funds(use_stored_data=use_stored_data)['reward_balance'])
-        if sbd.amount > reward_pool_sbd.amount:
-            raise ValueError('Provided more SBD than available in the reward pool.')
 
         # If the vote was already broadcasted we can assume the blockchain values to be true
         if not not_broadcasted_vote:
-            return sbd.amount / self.get_sbd_per_rshares(use_stored_data=use_stored_data)
+            return int(sbd.amount / self.get_sbd_per_rshares(use_stored_data=use_stored_data))
 
         # If the vote wasn't broadcasted (yet), we have to calculate the rshares while considering
         # the change our vote is causing to the recent_claims. This is more important for really
         # big votes which have a significant impact on the recent_claims.
-
-        # Get some data from the blockchain
         reward_fund = self.get_reward_funds(use_stored_data=use_stored_data)
-        reward_balance = Amount(reward_fund["reward_balance"], steem_instance=self).amount
-        recent_claims = float(reward_fund["recent_claims"])
         median_price = self.get_median_price(use_stored_data=use_stored_data)
-        SBD_price = (median_price * Amount(1, self.steem_symbol, steem_instance=self)).amount
+        recent_claims = int(reward_fund["recent_claims"])
+        reward_balance = Amount(reward_fund["reward_balance"], steem_instance=self)
+        reward_pool_sbd = median_price * reward_balance
+        if sbd > reward_pool_sbd:
+            raise ValueError('Provided more SBD than available in the reward pool.')
 
-        # This is the formular we can use to determine the "true" rshares
-        # We get this formular by some math magic using the previous used formulas
-        # FundsPerShare = (balance / (claims+newShares))*Price
-        # newShares = Amount / FundsPerShare
+        # This is the formula we can use to determine the "true" rshares.
+        # We get this formula by some math magic using the previous used formulas
+        # FundsPerShare = (balance / (claims + newShares)) * Price
+        # newShares = amount / FundsPerShare
         # We can now resolve both formulas for FundsPerShare and set the formulas to be equal
-        # (balance / (claims+newShares))*Price = Amount / newShares
+        # (balance / (claims + newShares)) * price = amount / newShares
         # Now we resolve for newShares resulting in:
-        # newShares = = claims * amount / (balance*price -amount)
-        rshares = recent_claims * sbd.amount / ((reward_balance * SBD_price) - sbd.amount)
-
+        # newShares = claims * amount / (balance * price - amount)
+        rshares = recent_claims * sbd.amount / ((reward_balance.amount * float(median_price)) - sbd.amount)
         return int(rshares)
 
     def rshares_to_vote_pct(self, rshares, steem_power=None, vests=None, voting_power=STEEM_100_PERCENT, use_stored_data=True):
-- 
GitLab