diff --git a/kin/client.py b/kin/client.py index 0d71947..6838574 100644 --- a/kin/client.py +++ b/kin/client.py @@ -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: """ diff --git a/test/test_client.py b/test/test_client.py index da686c2..16da3d3 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -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(): @@ -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 = [] @@ -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