Skip to content

Commit

Permalink
Merge pull request #10 from emre/account_history_fix
Browse files Browse the repository at this point in the history
Fix account history helper
  • Loading branch information
emre committed Feb 7, 2023
2 parents 004ed57 + e9222da commit dbacef9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 89 deletions.
10 changes: 4 additions & 6 deletions docs/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,17 @@ etc.
client = Client()
account = client.account('deepcrypto8')
one_week_ago = datetime.datetime.utcnow() -
datetime.timedelta(days=7)
one_week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
total_hive = 0
for op in account.history(
stop_at=one_week_ago,
filter=["transfer"]):
filter=["transfer_operation"]):
if op["to"] != "deepcrypto8":
continue
total_hive += Amount.from_asset(op["amount"]).amount
total_hive += Amount(op["amount"]).amount
print("Total HIVE transfers received", total_hive)
print("Total HIVE deposited to Binance", total_hive)
Getting account followers
Expand Down
30 changes: 23 additions & 7 deletions lighthive/helpers/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ def _get_account_history(self, account, index, limit,
if not exclude:
exclude = []
order = -1 if order == "desc" else 1

history = self.client.get_account_history(account, index, limit)
api_type = self.client.api_type
try:
history = self.client('account_history_api').get_account_history({
"account": account,
"start": index,
"limit": limit,
}).get("history", [])
finally:
self.client.api_type = api_type
for transaction in history[::order]:

created_at = parse(transaction[1]["timestamp"])
if start_at and order == -1 and created_at > start_at:
continue
Expand All @@ -56,7 +62,7 @@ def _get_account_history(self, account, index, limit,
if stop_at and order != -1 and created_at > stop_at:
raise StopOuterIteration

op_type, op_value = transaction[1]["op"]
op_type, op_value = transaction[1]["op"]["type"], transaction[1]["op"]["value"]
if op_type in exclude:
continue

Expand All @@ -74,10 +80,20 @@ def history(self, account=None, limit=1000,

# @todo: this can be faster with batch calls.
# consider a way to implement it.
max_index = self.client.get_account_history(account, -1, 0)[0][0]
if not max_index:
return
api_type = self.client.api_type
try:
account_history_result = self.client('account_history_api').get_account_history({
"account": account,
"start": -1,
"limit": 1
}).get("history", [])
finally:
self.client.api_type = api_type

if not account_history_result:
return None

max_index = account_history_result[0][0]
if order == "desc":
# Reverse history:
# Loop until we process all ops
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='lighthive',
version='0.4.0',
version='0.4.1',
packages=find_packages('.'),
url='http://github.com/emre/lighthive',
license='MIT',
Expand Down
43 changes: 1 addition & 42 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import lighthive.exceptions
import lighthive.node_picker
from lighthive.client import Client
from lighthive.helpers.account import Account
from lighthive.helpers.amount import Amount
from lighthive.helpers.event_listener import EventListener
from tests_mockdata import mock_block_25926363, mock_dygp_result, \
mock_block_25926364, mock_history, mock_history_max_index
mock_block_25926364


class TestClient(unittest.TestCase):
Expand Down Expand Up @@ -255,46 +254,6 @@ def test_reputation(self):

self.assertEqual(68.86, account.reputation())

def test_account_history_simple(self):
def match_max_index_request(request):
params = json.loads(request.text)["params"]
return params[1] == -1

def match_non_max_index_request(request):
params = json.loads(request.text)["params"]
return params[1] != -1

with requests_mock.mock() as m:
m.post(TestClient.NODES[0], json={
"result": mock_history_max_index},
additional_matcher=match_max_index_request)
m.post(TestClient.NODES[0], json={"result": mock_history},
additional_matcher=match_non_max_index_request)

account = Account(self.client)
history = list(account.history(account="hellosteem"))
self.assertEqual(3, len(history))

# check filter
history = list(
account.history(account="hellosteem", filter=["transfer"]))

self.assertEqual(2, len(history))

# check exclude
history = list(
account.history(account="hellosteem", exclude=["transfer"]))

self.assertEqual(1, len(history))

# check only_operation_data

history = list(
account.history(
account="hellosteem", only_operation_data=False))

self.assertEqual(3, history[0][0])


class TestEventListener(unittest.TestCase):

Expand Down
33 changes: 0 additions & 33 deletions tests_mockdata.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
# flake8: noqa
mock_history_max_index = [[3, {
'trx_id': '0000000000000000000000000000000000000000', 'block': 25641925,
'trx_in_block': 4294967295, 'op_in_trx': 0, 'virtual_op': 7,
'timestamp': '2018-09-03T17:24:48', 'op': ['fill_vesting_withdraw',
{'from_account': 'hellosteem',
'to_account': 'hellosteem',
'withdrawn': '187.37083 VESTS',
'deposited': '0.092 STEEM'}]}]]

mock_history = [[1,
{'trx_id': '985bb048e2068cdb311829ad3d76f4dc2947811a',
'block': 25153549, 'trx_in_block': 1, 'op_in_trx': 0,
'virtual_op': 0, 'timestamp': '2018-08-17T18:12:57',
'op': ['transfer',
{'from': 'hellosteem', 'to': 'sekhmet',
'amount': '0.001 STEEM', 'memo': ''}]}],
[2,
{'trx_id': 'bb1b6ddf13bcffe5bba8d55c3c37a5c672ff7309',
'block': 25153549, 'trx_in_block': 0, 'op_in_trx': 0,
'virtual_op': 0, 'timestamp': '2018-08-17T18:12:57',
'op': ['limit_order_create',
{'owner': 'hellosteem', 'orderid': 1534529209,
'amount_to_sell': '0.100 STEEM',
'min_to_receive': '100.000 SBD',
'fill_or_kill': False,
'expiration': '1969-12-31T23:59:59'}]}],
[3,
{'trx_id': '851c9a4ec9a32855b9981ea6b97c7911abaf8996',
'block': 25153418, 'trx_in_block': 0, 'op_in_trx': 0,
'virtual_op': 0, 'timestamp': '2018-08-17T18:06:24',
'op': ['transfer',
{'from': 'hellosteem', 'to': 'fabien',
'amount': '0.001 STEEM', 'memo': 'Test'}]}]]

mock_block_25926363 = {'jsonrpc': '2.0', 'result': [
{'trx_id': '0aeacf3dc2febe6e869e5ba7269ca005917b9737',
Expand Down

0 comments on commit dbacef9

Please sign in to comment.