Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #93 from kinecosystem/fix_history
Browse files Browse the repository at this point in the history
DP-370 Fix history
  • Loading branch information
Ron Serruya authored Aug 27, 2019
2 parents 4f860d8 + 845b356 commit 8f9e30c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
52 changes: 27 additions & 25 deletions kin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,31 +213,33 @@ async def get_account_tx_history(self, address: str, amount: Optional[int] = 10,

tx_list = []

requested_amount = amount if amount < MAX_RECORDS_PER_REQUEST else MAX_RECORDS_PER_REQUEST

horizon_response = await self.horizon.account_transactions(address,
cursor=cursor, limit=requested_amount,
order='desc' if descending else 'asc')

for transaction in horizon_response['_embedded']['records']:
raw_tx = RawTransaction(transaction)
if simple:
try:
simple_tx = SimplifiedTransaction(raw_tx)
tx_list.append(simple_tx)
except KinErrors.CantSimplifyError:
pass
else:
tx_list.append(raw_tx)
last_cursor = transaction['paging_token']

remaining_txs = amount - len(tx_list)
# if we got all the txs that we wanted, or there are no more txs
# TODO: paging does not work DP-370
if remaining_txs <= 0 or len(horizon_response['_embedded']['records']) < amount:
return tx_list
# If there are anymore transactions, recursively get the next transaction page
return tx_list.extend(await self.get_account_tx_history(address, remaining_txs, descending, last_cursor, simple))
while True:
requested_amount = min(amount, MAX_RECORDS_PER_REQUEST)

horizon_response = await self.horizon.account_transactions(address,
cursor=cursor, limit=requested_amount,
order='desc' if descending else 'asc')

current_loop_txs = []
for transaction in horizon_response['_embedded']['records']:
raw_tx = RawTransaction(transaction)
if simple:
try:
simple_tx = SimplifiedTransaction(raw_tx)
current_loop_txs.append(simple_tx)
except KinErrors.CantSimplifyError:
pass
else:
current_loop_txs.append(raw_tx)
cursor = transaction['paging_token']

amount -= len(current_loop_txs)
tx_list.extend(current_loop_txs)
# if we got all the txs that we wanted, or there are no more txs
if amount <= 0 or len(horizon_response['_embedded']['records']) < requested_amount:
return tx_list

# If there are more transactions, loop again to get the next transaction page

async def friendbot(self, address: str) -> str:
"""
Expand Down
10 changes: 5 additions & 5 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from time import sleep

from kin import KinClient, TEST_ENVIRONMENT, KinErrors
from kin import config
from kin import config, client


def test_create():
Expand Down Expand Up @@ -158,7 +158,7 @@ async def test_friendbot_fund(test_client):


@pytest.mark.asyncio
async def test_tx_history(test_client,test_account):
async def test_tx_history(test_client, test_account):
address = 'GA4GDLBEWVT5IZZ6JKR4BF3B6JJX5S6ISFC2QCC7B6ZVZWJDMR77HYP6'
await test_client.friendbot(address)
txs = []
Expand All @@ -171,16 +171,16 @@ async def test_tx_history(test_client,test_account):

history_ids = [tx.id for tx in tx_history]
# tx history goes from latest to oldest
txs.reverse()
history_ids.reverse()

assert txs == history_ids

# TODO: INCORRECT TESTING, broken
# test paging
config.MAX_RECORDS_PER_REQUEST = 2
client.MAX_RECORDS_PER_REQUEST = 4

tx_history = await test_client.get_account_tx_history(test_account.get_public_address(), amount=6)
history_ids = [tx.id for tx in tx_history]
history_ids.reverse()

assert txs == history_ids

Expand Down

0 comments on commit 8f9e30c

Please sign in to comment.