Skip to content

Commit

Permalink
Merge pull request #91 from crokkon/generic_symbols
Browse files Browse the repository at this point in the history
Using generic asset symbols
  • Loading branch information
holgern committed Oct 4, 2018
2 parents e1859a0 + e391bf6 commit bd5da28
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 81 deletions.
26 changes: 14 additions & 12 deletions beem/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def print_info(self, force_refresh=False, return_str=False, use_table=False, **k
t.add_row(["Vote Value", "%.2f $" % (self.get_voting_value_SBD())])
t.add_row(["Last vote", "%s ago" % last_vote_time_str])
t.add_row(["Full in ", "%s" % (self.get_manabar_recharge_time_str(vote_mana))])
t.add_row(["Steem Power", "%.2f STEEM" % (self.get_steem_power())])
t.add_row(["Steem Power", "%.2f %s" % (self.get_steem_power(), self.steem.steem_symbol)])
t.add_row(["Balance", "%s, %s" % (str(self.balances["available"][0]), str(self.balances["available"][1]))])
if False and bandwidth["allocated"] > 0:
t.add_row(["Remaining Bandwidth", "%.2f %%" % (remaining)])
Expand Down Expand Up @@ -1549,7 +1549,7 @@ def get_curation_reward(self, days=7):
:param int days: limit number of days to be included int the return value
"""
stop = addTzInfo(datetime.utcnow()) - timedelta(days=days)
reward_vests = Amount("0 VESTS", steem_instance=self.steem)
reward_vests = Amount(0, self.steem.vests_symbol, steem_instance=self.steem)
for reward in self.history_reverse(stop=stop, use_block_num=False, only_ops=["curation_reward"]):
reward_vests += Amount(reward['reward'], steem_instance=self.steem)
return self.steem.vests_to_sp(reward_vests.amount)
Expand Down Expand Up @@ -2245,7 +2245,7 @@ def transfer_to_vesting(self, amount, to=None, account=None, **kwargs):
to = self # powerup on the same account
else:
to = Account(to, steem_instance=self.steem)
amount = self._check_amount(amount, "STEEM")
amount = self._check_amount(amount, self.steem.steem_symbol)

to = Account(to, steem_instance=self.steem)

Expand All @@ -2271,7 +2271,7 @@ def convert(self, amount, account=None, request_id=None):
account = self
else:
account = Account(account, steem_instance=self.steem)
amount = self._check_amount(amount, "SBD")
amount = self._check_amount(amount, self.steem.sbd_symbol)
if request_id:
request_id = int(request_id)
else:
Expand Down Expand Up @@ -2391,6 +2391,7 @@ def cancel_transfer_from_savings(self, request_id, account=None, **kwargs):
return self.steem.finalizeOp(op, account, "active", **kwargs)

def _check_amount(self, amount, symbol):
print(amount)
if isinstance(amount, (float, integer_types)):
amount = Amount(amount, symbol, steem_instance=self.steem)
elif isinstance(amount, string_types) and amount.replace('.', '', 1).replace(',', '', 1).isdigit():
Expand All @@ -2402,9 +2403,9 @@ def _check_amount(self, amount, symbol):
return amount

def claim_reward_balance(self,
reward_steem='0 STEEM',
reward_sbd='0 SBD',
reward_vests='0 VESTS',
reward_steem=0,
reward_sbd=0,
reward_vests=0,
account=None, **kwargs):
""" Claim reward balances.
By default, this will claim ``all`` outstanding balances. To bypass
Expand All @@ -2428,9 +2429,9 @@ def claim_reward_balance(self,
# if no values were set by user, claim all outstanding balances on
# account

reward_steem = self._check_amount(reward_steem, "STEEM")
reward_sbd = self._check_amount(reward_sbd, "SBD")
reward_vests = self._check_amount(reward_vests, "VESTS")
reward_steem = self._check_amount(reward_steem, self.steem.steem_symbol)
reward_sbd = self._check_amount(reward_sbd, self.steem.sbd_symbol)
reward_vests = self._check_amount(reward_vests, self.steem.vests_symbol)

if reward_steem.amount == 0 and reward_sbd.amount == 0 and reward_vests.amount == 0:
reward_steem = account.balances["rewards"][0]
Expand All @@ -2445,6 +2446,7 @@ def claim_reward_balance(self,
"reward_vests": reward_vests,
"prefix": self.steem.prefix,
})
print(op)
return self.steem.finalizeOp(op, account, "posting", **kwargs)

def delegate_vesting_shares(self, to_account, vesting_shares,
Expand All @@ -2465,7 +2467,7 @@ def delegate_vesting_shares(self, to_account, vesting_shares,
to_account = Account(to_account, steem_instance=self.steem)
if to_account is None:
raise ValueError("You need to provide a to_account")
vesting_shares = self._check_amount(vesting_shares, "VESTS")
vesting_shares = self._check_amount(vesting_shares, self.steem.vests_symbol)

op = operations.Delegate_vesting_shares(
**{
Expand All @@ -2489,7 +2491,7 @@ def withdraw_vesting(self, amount, account=None, **kwargs):
account = self
else:
account = Account(account, steem_instance=self.steem)
amount = self._check_amount(amount, "VESTS")
amount = self._check_amount(amount, self.steem.vests_symbol)

op = operations.Withdraw_vesting(
**{
Expand Down
19 changes: 11 additions & 8 deletions beem/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _parse_json_data(self, comment):
]
for p in sbd_amounts:
if p in comment and isinstance(comment.get(p), (string_types, list, dict)):
comment[p] = Amount(comment.get(p, "0.000 SBD"), steem_instance=self.steem)
comment[p] = Amount(comment.get(p, "0.000 %s" % (self.steem.sbd_symbol)), steem_instance=self.steem)

# turn json_metadata into python dict
meta_str = comment.get("json_metadata", "{}")
Expand Down Expand Up @@ -276,7 +276,7 @@ def is_comment(self):
def reward(self):
""" Return the estimated total SBD reward.
"""
a_zero = Amount("0 SBD", steem_instance=self.steem)
a_zero = Amount(0, self.steem.sbd_symbol, steem_instance=self.steem)
total = Amount(self.get("total_payout_value", a_zero), steem_instance=self.steem)
pending = Amount(self.get("pending_payout_value", a_zero), steem_instance=self.steem)
return total + pending
Expand All @@ -285,7 +285,7 @@ def is_pending(self):
""" Return if the payout is pending (the post/comment
is younger than 7 days)
"""
a_zero = Amount("0 SBD", steem_instance=self.steem)
a_zero = Amount(0, self.steem.sbd_symbol, steem_instance=self.steem)
total = Amount(self.get("total_payout_value", a_zero), steem_instance=self.steem)
post_age_days = self.time_elapsed().total_seconds() / 60 / 60 / 24
return post_age_days < 7.0 and float(total) == 0
Expand Down Expand Up @@ -421,7 +421,10 @@ def get_author_rewards(self):
total_payout = Amount(self["total_payout_value"], steem_instance=self.steem)
curator_payout = Amount(self["curator_payout_value"], steem_instance=self.steem)
author_payout = total_payout - curator_payout
return {'pending_rewards': False, "payout_SP": Amount("0 SBD", steem_instance=self.steem), "payout_SBD": Amount("0 SBD", steem_instance=self.steem), "total_payout_SBD": author_payout}
return {'pending_rewards': False,
"payout_SP": Amount(0, self.steem.steem_symbol, steem_instance=self.steem),
"payout_SBD": Amount(0, self.steem.sbd_symbol, steem_instance=self.steem),
"total_payout_SBD": author_payout}

median_price = Price(self.steem.get_current_median_history(), steem_instance=self.steem)
beneficiaries_pct = self.get_beneficiaries_pct()
Expand All @@ -434,7 +437,7 @@ def get_author_rewards(self):
author_tokens -= benefactor_tokens

sbd_steem = author_tokens * self["percent_steem_dollars"] / 20000.
vesting_steem = median_price.as_base("STEEM") * (author_tokens - sbd_steem)
vesting_steem = median_price.as_base(self.steem.steem_symbol) * (author_tokens - sbd_steem)

return {'pending_rewards': True, "payout_SP": vesting_steem, "payout_SBD": sbd_steem, "total_payout_SBD": author_tokens}

Expand Down Expand Up @@ -470,7 +473,7 @@ def get_curation_rewards(self, pending_payout_SBD=False, pending_payout_value=No
pending_rewards = False
total_vote_weight = self["total_vote_weight"]
if not self["allow_curation_rewards"]:
max_rewards = Amount("0 STEEM", steem_instance=self.steem)
max_rewards = Amount(0, self.steem.steem_symbol, steem_instance=self.steem)
unclaimed_rewards = max_rewards.copy()
elif not self.is_pending():
max_rewards = Amount(self["curator_payout_value"], steem_instance=self.steem)
Expand All @@ -482,13 +485,13 @@ def get_curation_rewards(self, pending_payout_SBD=False, pending_payout_value=No
if pending_payout_value is None:
pending_payout_value = Amount(self["pending_payout_value"], steem_instance=self.steem)
elif isinstance(pending_payout_value, (float, integer_types)):
pending_payout_value = Amount(pending_payout_value, "SBD", steem_instance=self.steem)
pending_payout_value = Amount(pending_payout_value, self.steem.sbd_symbol, steem_instance=self.steem)
elif isinstance(pending_payout_value, str):
pending_payout_value = Amount(pending_payout_value, steem_instance=self.steem)
if pending_payout_SBD:
max_rewards = (pending_payout_value * 0.25)
else:
max_rewards = median_price.as_base("STEEM") * (pending_payout_value * 0.25)
max_rewards = median_price.as_base(self.steem.steem_symbol) * (pending_payout_value * 0.25)
unclaimed_rewards = max_rewards.copy()
pending_rewards = True

Expand Down
22 changes: 11 additions & 11 deletions beem/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def __init__(self, account, account_history=[], steem_instance=None):
def reset(self):
""" Resets the arrays not the stored account history
"""
self.own_vests = [Amount("0 VESTS", steem_instance=self.steem)]
self.own_steem = [Amount("0 STEEM", steem_instance=self.steem)]
self.own_sbd = [Amount("0 SBD", steem_instance=self.steem)]
self.own_vests = [Amount(0, self.steem.vests_symbol, steem_instance=self.steem)]
self.own_steem = [Amount(0, self.steem.steem_symbol, steem_instance=self.steem)]
self.own_sbd = [Amount(0, self.steem.steem_symbol, steem_instance=self.steem)]
self.delegated_vests_in = [{}]
self.delegated_vests_out = [{}]
self.timestamps = [addTzInfo(datetime(1970, 1, 1, 0, 0, 0, 0))]
Expand Down Expand Up @@ -313,14 +313,14 @@ def parse_op(self, op, only_ops=[], enable_rewards=False, enable_out_votes=False
amount = Amount(op['amount'], steem_instance=self.steem)
# print(op)
if op['from'] == self.account["name"]:
if amount.symbol == "STEEM":
if amount.symbol == self.steem.steem_symbol:
self.update(ts, 0, 0, 0, amount * (-1), 0)
elif amount.symbol == "SBD":
elif amount.symbol == self.steem.sbd_symbol:
self.update(ts, 0, 0, 0, 0, amount * (-1))
if op['to'] == self.account["name"]:
if amount.symbol == "STEEM":
if amount.symbol == self.steem.steem_symbol:
self.update(ts, 0, 0, 0, amount, 0)
elif amount.symbol == "SBD":
elif amount.symbol == self.steem.sbd_symbol:
self.update(ts, 0, 0, 0, 0, amount)
# print(op, vests)
# self.update(ts, vests, 0, 0)
Expand All @@ -330,14 +330,14 @@ def parse_op(self, op, only_ops=[], enable_rewards=False, enable_out_votes=False
current_pays = Amount(op["current_pays"], steem_instance=self.steem)
open_pays = Amount(op["open_pays"], steem_instance=self.steem)
if op["current_owner"] == self.account["name"]:
if current_pays.symbol == "STEEM":
if current_pays.symbol == self.steem.steem_symbol:
self.update(ts, 0, 0, 0, current_pays * (-1), open_pays)
elif current_pays.symbol == "SBD":
elif current_pays.symbol == self.steem.sbd_symbol:
self.update(ts, 0, 0, 0, open_pays, current_pays * (-1))
if op["open_owner"] == self.account["name"]:
if current_pays.symbol == "STEEM":
if current_pays.symbol == self.steem.steem_symbol:
self.update(ts, 0, 0, 0, current_pays, open_pays * (-1))
elif current_pays.symbol == "SBD":
elif current_pays.symbol == self.steem.sbd_symbol:
self.update(ts, 0, 0, 0, open_pays * (-1), current_pays)
# print(op)
return
Expand Down
52 changes: 40 additions & 12 deletions beem/steem.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def get_median_price(self, use_stored_data=True):
quote=Amount(median_price['quote'], steem_instance=self),
steem_instance=self
)
return a.as_base("SBD")
return a.as_base(self.sbd_symbol)

def get_block_interval(self, use_stored_data=True):
"""Returns the block interval in seconds"""
Expand Down Expand Up @@ -508,7 +508,7 @@ def get_sbd_per_rshares(self, not_broadcasted_vote_rshares=0, use_stored_data=Tr

fund_per_share = reward_balance / (recent_claims)
median_price = self.get_median_price(use_stored_data=use_stored_data)
SBD_price = (median_price * Amount("1 STEEM", steem_instance=self)).amount
SBD_price = (median_price * Amount(1, self.steem_symbol, steem_instance=self)).amount
return fund_per_share * SBD_price

def get_steem_per_mvest(self, time_stamp=None, use_stored_data=True):
Expand Down Expand Up @@ -639,8 +639,8 @@ def sbd_to_rshares(self, sbd, not_broadcasted_vote=False, use_stored_data=True):
elif isinstance(sbd, string_types):
sbd = Amount(sbd, steem_instance=self)
else:
sbd = Amount(sbd, 'SBD', steem_instance=self)
if sbd['symbol'] != 'SBD':
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:
Expand All @@ -659,7 +659,7 @@ def sbd_to_rshares(self, sbd, not_broadcasted_vote=False, use_stored_data=True):
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 STEEM", steem_instance=self)).amount
SBD_price = (median_price * Amount(1, self.steem_symbol, steem_instance=self)).amount

# 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
Expand Down Expand Up @@ -728,8 +728,8 @@ def sbd_to_vote_pct(self, sbd, steem_power=None, vests=None, voting_power=STEEM_
elif isinstance(sbd, string_types):
sbd = Amount(sbd, steem_instance=self)
else:
sbd = Amount(sbd, 'SBD', steem_instance=self)
if sbd['symbol'] != 'SBD':
sbd = Amount(sbd, self.sbd_symbol, steem_instance=self)
if sbd['symbol'] != self.sbd_symbol:
raise AssertionError()
rshares = self.sbd_to_rshares(sbd, not_broadcasted_vote=not_broadcasted_vote, use_stored_data=use_stored_data)
return self.rshares_to_vote_pct(rshares, steem_power=steem_power, vests=vests, voting_power=voting_power, use_stored_data=use_stored_data)
Expand Down Expand Up @@ -1025,7 +1025,7 @@ def clear(self):
# -------------------------------------------------------------------------
# Account related calls
# -------------------------------------------------------------------------
def claim_account(self, creator, fee="0 STEEM", **kwargs):
def claim_account(self, creator, fee=None, **kwargs):
""""Claim account for claimed account creation.
When fee is 0 STEEM a subsidized account is claimed and can be created
Expand All @@ -1034,8 +1034,9 @@ def claim_account(self, creator, fee="0 STEEM", **kwargs):
:param str creator: which account should pay the registration fee (RC or STEEM)
(defaults to ``default_account``)
:param str fee: when set to 0 STEEM, claim account is paid by RC
:param str fee: when set to 0 STEEM (default), claim account is paid by RC
"""
fee = fee if fee is not None else "0 %s" % (self.steem_symbol)
if not creator and config["default_account"]:
creator = config["default_account"]
if not creator:
Expand Down Expand Up @@ -1070,7 +1071,7 @@ def create_claimed_account(
store_owner_key=False,
json_meta=None,
combine_with_claim_account=False,
fee="0 STEEM",
fee=None,
**kwargs
):
""" Create new claimed account on Steem
Expand Down Expand Up @@ -1132,6 +1133,7 @@ def create_claimed_account(
the blockchain
"""
fee = fee if fee is not None else "0 %s" % (self.steem_symbol)
if not creator and config["default_account"]:
creator = config["default_account"]
if not creator:
Expand Down Expand Up @@ -1516,7 +1518,7 @@ def witness_update(self, signing_key, url, props, account=None, **kwargs):
"url": url,
"block_signing_key": signing_key,
"props": props,
"fee": Amount("0.000 STEEM", steem_instance=self),
"fee": Amount(0, self.steem_symbol, steem_instance=self),
"prefix": self.prefix,
})
return self.finalizeOp(op, account, "active", **kwargs)
Expand Down Expand Up @@ -1853,7 +1855,7 @@ def _build_comment_options_op(self, author, permlink, options,

options['beneficiaries'] = beneficiaries

default_max_payout = "1000000.000 SBD"
default_max_payout = "1000000.000 %s" % (self.sbd_symbol)
comment_op = operations.Comment_options(
**{
"author":
Expand Down Expand Up @@ -1888,3 +1890,29 @@ def get_apis(self):
if api not in api_list:
api_list.append(api)
return api_list

def _get_asset_symbol(self, asset_id):
""" get the asset symbol from an asset id
:@param int asset_id: 0 -> SBD, 1 -> STEEM, 2 -> VESTS
"""
for asset in self.chain_params['chain_assets']:
if asset['id'] == asset_id:
return asset['symbol']
raise KeyError("asset ID not found in chain assets")

@property
def sbd_symbol(self):
""" get the current chains symbol for SBD (e.g. "TBD" on testnet) """
return self._get_asset_symbol(0)

@property
def steem_symbol(self):
""" get the current chains symbol for STEEM (e.g. "TESTS" on testnet) """
return self._get_asset_symbol(1)

@property
def vests_symbol(self):
""" get the current chains symbol for VESTS """
return self._get_asset_symbol(2)

0 comments on commit bd5da28

Please sign in to comment.