-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
rc.py
187 lines (153 loc) · 7.25 KB
/
rc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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.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_dict(self, comment_dict):
"""Calc RC costs for a comment dict object
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_from_dict(comment_dict))
"""
op = operations.Comment(**comment_dict)
tx_size = self.get_tx_size(op)
permlink_length = len(comment_dict["permlink"])
parent_permlink_length = len(comment_dict["parent_permlink"])
return self.comment(tx_size=tx_size, permlink_length=permlink_length, parent_permlink_length=parent_permlink_length)
def comment(self, tx_size=1000, permlink_length=10, parent_permlink_length=10):
"""Calc RC for a comment"""
state_bytes_count = state_object_size_info["comment_object_base_size"]
state_bytes_count += state_object_size_info["comment_object_permlink_char_size"] * permlink_length
state_bytes_count += state_object_size_info["comment_object_parent_permlink_char_size"] * parent_permlink_length
resource_count = self.get_resource_count(tx_size, state_bytes_count)
return self.steem.get_rc_cost(resource_count)
def vote_dict(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)
return self.vote(tx_size=tx_size)
def vote(self, tx_size=210):
"""Calc RC for a vote"""
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_dict(self, transfer_dict):
"""Calc RC costs for a transfer dict object
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)
return self.transfer(tx_size=tx_size, market_op_count=market_op_count)
def transfer(self, tx_size=290, market_op_count=1):
"""Calc RC of a transfer"""
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_dict(self, custom_json_dict):
"""Calc RC costs for a custom_json
Example for calculating RC costs
.. code-block:: python
from beem.rc import RC
from collections import OrderedDict
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)
return self.custom_json(tx_size=tx_size)
def custom_json(self, tx_size=444):
resource_count = self.get_resource_count(tx_size)
return self.steem.get_rc_cost(resource_count)
def account_update_dict(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)
def claim_account(self, tx_size=300):
"""Claim account"""
resource_count = self.get_resource_count(tx_size, new_account_op_count=1)
return self.steem.get_rc_cost(resource_count)