Skip to content
Snippets Groups Projects
Commit 827f41cb authored by Holger's avatar Holger
Browse files

Implementation of a RC costs calculation class

* get_rc_cost was added to steem to calculation RC costs from resource count

RC
* get_tx_size(), get_resource_count(), comment(), vote(), transfer() and custom_json() added
parent 2c2ce667
No related branches found
No related tags found
No related merge requests found
...@@ -13,3 +13,37 @@ STEEM_VOTE_REGENERATION_SECONDS = 432000 ...@@ -13,3 +13,37 @@ STEEM_VOTE_REGENERATION_SECONDS = 432000
STEEM_VOTING_MANA_REGENERATION_SECONDS = 432000 STEEM_VOTING_MANA_REGENERATION_SECONDS = 432000
STEEM_VOTE_DUST_THRESHOLD = 50000000 STEEM_VOTE_DUST_THRESHOLD = 50000000
STEEM_ROOT_POST_PARENT = '' STEEM_ROOT_POST_PARENT = ''
STATE_BYTES_SCALE = 10000
STATE_TRANSACTION_BYTE_SIZE = 174
STATE_TRANSFER_FROM_SAVINGS_BYTE_SIZE = 229
STATE_LIMIT_ORDER_BYTE_SIZE = 1940
RC_DEFAULT_EXEC_COST = 100000
STEEM_RC_REGEN_TIME = 60 * 60 * 24 * 5
state_object_size_info = {'authority_base_size': 4 * STATE_BYTES_SCALE,
'authority_account_member_size': 18 * STATE_BYTES_SCALE,
'authority_key_member_size': 35 * STATE_BYTES_SCALE,
'account_object_base_size': 480 * STATE_BYTES_SCALE,
'account_authority_object_base_size': 40 * STATE_BYTES_SCALE,
'account_recovery_request_object_base_size': 32 * STATE_BYTES_SCALE,
'comment_object_base_size': 201 * STATE_BYTES_SCALE,
'comment_object_permlink_char_size': 1 * STATE_BYTES_SCALE,
'comment_object_parent_permlink_char_size': 2 * STATE_BYTES_SCALE,
'comment_object_beneficiaries_member_size': 18 * STATE_BYTES_SCALE,
'comment_vote_object_base_size': 47 * STATE_BYTES_SCALE,
'convert_request_object_base_size': 48 * STATE_BYTES_SCALE,
'decline_voting_rights_request_object_base_size': 28 * STATE_BYTES_SCALE,
'escrow_object_base_size': 119 * STATE_BYTES_SCALE,
'limit_order_object_base_size': 76 * STATE_LIMIT_ORDER_BYTE_SIZE,
'savings_withdraw_object_byte_size': 64 * STATE_TRANSFER_FROM_SAVINGS_BYTE_SIZE,
'transaction_object_base_size': 35 * STATE_TRANSACTION_BYTE_SIZE,
'transaction_object_byte_size': 1 * STATE_TRANSACTION_BYTE_SIZE,
'vesting_delegation_object_base_size': 60 * STATE_BYTES_SCALE,
'vesting_delegation_expiration_object_base_size': 44 * STATE_BYTES_SCALE,
'withdraw_vesting_route_object_base_size': 43 * STATE_BYTES_SCALE,
'witness_object_base_size': 266 * STATE_BYTES_SCALE,
'witness_object_url_char_size': 1 * STATE_BYTES_SCALE,
'witness_vote_object_base_size': 40 * STATE_BYTES_SCALE}
operation_exec_info = {}
# This Python file uses the following encoding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import logging
import json
from .instance import shared_steem_instance
from beem.account import Account
from beem.constants import state_object_size_info
import hashlib
from binascii import hexlify, unhexlify
import os
from pprint import pprint
from beem.amount import Amount
from beembase import operations
from beembase.objects import Operation
from beembase.signedtransactions import Signed_Transaction
from beemgraphenebase.py23 import py23_bytes, bytes_types
class RC(object):
def __init__(
self,
steem_instance=None,
):
self.steem = steem_instance or shared_steem_instance()
def get_tx_size(self, op):
"""Returns the tx size of an operation"""
ops = [Operation(op)]
prefix = u"STEEM"
wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
ref_block_num = 34294
ref_block_prefix = 3707022213
expiration = "2016-04-06T08:29:27"
tx = Signed_Transaction(ref_block_num=ref_block_num,
ref_block_prefix=ref_block_prefix,
expiration=expiration,
operations=ops)
tx = tx.sign([wif], chain=prefix)
txWire = hexlify(py23_bytes(tx)).decode("ascii")
tx_size = len(txWire)
return tx_size
def get_resource_count(self, tx_size, state_bytes_count=0, new_account_op_count=0, market_op_count=0):
"""Creates the resource_count dictionary based on tx_size, state_bytes_count, new_account_op_count and market_op_count"""
resource_count = {"resource_history_bytes": tx_size}
resource_count["resource_state_bytes"] = state_object_size_info["transaction_object_base_size"]
resource_count["resource_state_bytes"] += state_object_size_info["transaction_object_byte_size"] * tx_size
resource_count["resource_state_bytes"] += state_bytes_count
resource_count["resource_new_accounts"] = new_account_op_count
if market_op_count > 0:
resource_count["resource_market_bytes"] = market_op_count
return resource_count
def comment(self, comment_dict):
"""Calc RC costs for a comment
Example for calculating RC costs
.. code-block:: python
from beem.rc import RC
comment_dict = {
"permlink": "test", "author": "holger80",
"body": "test", "parent_permlink": "",
"parent_author": "", "title": "test",
"json_metadata": {"foo": "bar"}
}
rc = RC()
print(rc.comment(comment_dict))
"""
state_bytes_count = state_object_size_info["comment_object_base_size"]
state_bytes_count += state_object_size_info["comment_object_permlink_char_size"] * len(comment_dict["permlink"])
state_bytes_count += state_object_size_info["comment_object_parent_permlink_char_size"] * len(comment_dict["parent_permlink"])
op = operations.Comment(**comment_dict)
tx_size = self.get_tx_size(op)
resource_count = self.get_resource_count(tx_size, state_bytes_count)
return self.steem.get_rc_cost(resource_count)
def vote(self, vote_dict):
"""Calc RC costs for a vote
Example for calculating RC costs
.. code-block:: python
from beem.rc import RC
vote_dict = {
"voter": "foobara", "author": "foobarc",
"permlink": "foobard", "weight": 1000
}
rc = RC()
print(rc.comment(vote_dict))
"""
op = operations.Vote(**vote_dict)
tx_size = self.get_tx_size(op)
state_bytes_count = state_object_size_info["comment_vote_object_base_size"]
resource_count = self.get_resource_count(tx_size, state_bytes_count)
return self.steem.get_rc_cost(resource_count)
def transfer(self, transfer_dict):
"""Calc RC costs for a transfer
Example for calculating RC costs
.. code-block:: python
from beem.rc import RC
from beem.amount import Amount
transfer_dict = {
"from": "foo", "to": "baar",
"amount": Amount("111.110 STEEM"),
"memo": "Fooo"
}
rc = RC()
print(rc.comment(transfer_dict))
"""
market_op_count = 1
op = operations.Transfer(**transfer_dict)
tx_size = self.get_tx_size(op)
resource_count = self.get_resource_count(tx_size, market_op_count=market_op_count)
return self.steem.get_rc_cost(resource_count)
def custom_json(self, custom_json_dict):
"""Calc RC costs for a custom_json
Example for calculating RC costs
.. code-block:: python
from beem.rc import RC
custom_json_dict = {
"json": [
"reblog", OrderedDict([("account", "xeroc"), ("author", "chainsquad"),
("permlink", "streemian-com-to-open-its-doors-and-offer-a-20-discount")
])
],
"required_auths": [],
"required_posting_auths": ["xeroc"],
"id": "follow"
}
rc = RC()
print(rc.comment(custom_json_dict))
"""
op = operations.Custom_json(**custom_json_dict)
tx_size = self.get_tx_size(op)
resource_count = self.get_resource_count(tx_size)
return self.steem.get_rc_cost(resource_count)
def account_update(self, account_update_dict):
"""Calc RC costs for account update"""
op = operations.Account_update(**account_update_dict)
tx_size = self.get_tx_size(op)
resource_count = self.get_resource_count(tx_size)
return self.steem.get_rc_cost(resource_count)
...@@ -32,7 +32,7 @@ from .wallet import Wallet ...@@ -32,7 +32,7 @@ from .wallet import Wallet
from .steemconnect import SteemConnect from .steemconnect import SteemConnect
from .transactionbuilder import TransactionBuilder from .transactionbuilder import TransactionBuilder
from .utils import formatTime, resolve_authorperm, derive_permlink, remove_from_dict, addTzInfo, formatToTimeStamp from .utils import formatTime, resolve_authorperm, derive_permlink, remove_from_dict, addTzInfo, formatToTimeStamp
from beem.constants import STEEM_VOTE_REGENERATION_SECONDS, STEEM_100_PERCENT, STEEM_1_PERCENT from beem.constants import STEEM_VOTE_REGENERATION_SECONDS, STEEM_100_PERCENT, STEEM_1_PERCENT, STEEM_RC_REGEN_TIME
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -460,6 +460,38 @@ class Steem(object): ...@@ -460,6 +460,38 @@ class Steem(object):
"""Returns the resource pool""" """Returns the resource pool"""
return self.rpc.get_resource_pool(api="rc")["resource_pool"] return self.rpc.get_resource_pool(api="rc")["resource_pool"]
def get_rc_cost(self, resource_count):
"""Returns the RC costs based on the resource_count"""
pools = self.get_resource_pool()
params = self.get_resource_params()
config = self.get_config()
dyn_param = self.get_dynamic_global_properties()
rc_regen = int(Amount(dyn_param["total_vesting_shares"], steem_instance=self)) / (STEEM_RC_REGEN_TIME / config["STEEM_BLOCK_INTERVAL"])
total_cost = 0
if rc_regen == 0:
return total_cost
for resource_type in resource_count:
curve_params = params[resource_type]["price_curve_params"]
current_pool = int(pools[resource_type]["pool"])
count = resource_count[resource_type]
count *= params[resource_type]["resource_dynamics_params"]["resource_unit"]
cost = self._compute_rc_cost(curve_params, current_pool, count, rc_regen)
total_cost += cost
return total_cost
def _compute_rc_cost(self, curve_params, current_pool, resource_count, rc_regen):
"""Helper function for computing the RC costs"""
num = int(rc_regen)
num *= int(curve_params['coeff_a'])
num = int(num) >> int(curve_params['shift'])
num += 1
num *= int(resource_count)
denom = int(curve_params['coeff_b'])
if int(current_pool) > 0:
denom += int(current_pool)
num_denom = num / denom
return int(num_denom) + 1
def rshares_to_sbd(self, rshares, not_broadcasted_vote=False, use_stored_data=True): def rshares_to_sbd(self, rshares, not_broadcasted_vote=False, use_stored_data=True):
""" Calculates the current SBD value of a vote """ Calculates the current SBD value of a vote
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment