Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

@katzio katzio Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, why not append it as is (like you did in the else section)?
and log the error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just the way the method works, if you only want simple txs it skips everything else

else:
current_loop_txs.append(raw_tx)
cursor = transaction['paging_token']
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a use for cursor, guess it's a leftover

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used for the next request, line 220


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