From 168c9863cbd6aa799eb88300e5fb1893e6b161bf Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 26 Jul 2022 14:14:57 -0400 Subject: [PATCH] Look for transfers after ledger + api trans load If we don't have a pos table built out already (in mem) we can't figure out the likely dst asset (since there's no pair entry to guide us) that we should use to search for withdrawal transactions; so move it later. Further this ports to the new api changes in `piker.pp`` that will land with #365. --- piker/brokers/kraken/broker.py | 62 +++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/piker/brokers/kraken/broker.py b/piker/brokers/kraken/broker.py index 6205e955..160bfca1 100644 --- a/piker/brokers/kraken/broker.py +++ b/piker/brokers/kraken/broker.py @@ -286,7 +286,7 @@ def trades2pps( ) log.info(f'Updated pps:\n{pformat(updated)}') - pp_entries, closed_pp_objs = table.dump_active('kraken') + pp_entries, closed_pp_objs = table.dump_active() pp_objs: dict[Union[str, int], Position] = table.pps pps: dict[int, Position] @@ -418,29 +418,6 @@ async def trades_dialogue( pos = has_pp(dst) if not pos: - # get transfers to make sense of abs balances. - likely_pair = { - bsuid[:3]: bsuid - for bsuid in table.pps - }.get(dst) - - if likely_pair: - # this was likely pp that had a withdrawal - # from the dst asset out of the account. - - xfer_trans = await client.get_xfers( - dst, - src_asset=likely_pair[3:], - ) - if xfer_trans: - updated = table.update_from_trans( - xfer_trans, - cost_scalar=1, - ) - log.info( - 'Updated {dst} from transfers:\n' - f'{pformat(updated)}' - ) # we have a balance for which there is no pp # entry? so we have to likely update from the @@ -454,6 +431,43 @@ async def trades_dialogue( table.update_from_trans(api_trans) pos = has_pp(dst) if not pos: + + # get transfers to make sense of abs balances. + # NOTE: we do this after ledger and API + # loading since we might not have an entry + # in the ``pps.toml`` for the necessary pair + # yet and thus this likely pair grabber will + # likely fail. + likely_pair = { + bsuid[:3]: bsuid + for bsuid in table.pps + }.get(dst) + if not likely_pair: + raise ValueError( + 'Could not find a position pair in ' + 'ledger for likely widthdrawal ' + f'candidate: {dst}' + ) + + if likely_pair: + # this was likely pp that had a withdrawal + # from the dst asset out of the account. + + xfer_trans = await client.get_xfers( + dst, + src_asset=likely_pair[3:], + ) + if xfer_trans: + updated = table.update_from_trans( + xfer_trans, + cost_scalar=1, + ) + log.info( + 'Updated {dst} from transfers:\n' + f'{pformat(updated)}' + ) + + if not has_pp(dst): raise ValueError( 'Could not reproduce balance:\n' f'dst: {dst}, {size}\n'