`kraken`: make pps work with arbitrary deposits

Factor and fix dst <- src pair parsing into a new func
`get_likely_pair()` and use throughout initial position loading; solves
a parsing bug for src asset balances which aren't only 3 chars long..
a terrible assumption.
kraken_deposits_fixes
Tyler Goodlet 2023-02-08 23:43:57 -05:00
parent d690ad2bab
commit 1346c33f04
1 changed files with 49 additions and 25 deletions

View File

@ -496,9 +496,46 @@ async def trades_dialogue(
for dst, size in balances.items(): for dst, size in balances.items():
# we don't care about tracking positions # we don't care about tracking positions
# in the user's source fiat currency. # in the user's source fiat currency.
if dst == src_fiat: if (
dst == src_fiat
or not any(
dst in bsuid for bsuid in table.pps
)
):
log.warning(
f'Skipping balance `{dst}`:{size} for position calcs!'
)
continue continue
def get_likely_pair(
dst: str,
bsuid: str,
src_fiat: str = src_fiat
) -> str:
'''
Attempt to get the likely trading pair masting
a given destination asset `dst: str`.
'''
try:
src_name_start = bsuid.rindex(src_fiat)
except (
ValueError, # substr not found
):
# TODO: handle nested positions..(i.e.
# positions where the src fiat was used to
# buy some other dst which was furhter used
# to buy another dst..)
log.warning(
f'No src fiat {src_fiat} found in {bsuid}?'
)
return
likely_dst = bsuid[:src_name_start]
if likely_dst == dst:
return bsuid
def has_pp( def has_pp(
dst: str, dst: str,
size: float, size: float,
@ -506,26 +543,11 @@ async def trades_dialogue(
) -> Position | bool: ) -> Position | bool:
src2dst: dict[str, str] = {} src2dst: dict[str, str] = {}
for bsuid in table.pps: for bsuid in table.pps:
try: likely_pair = get_likely_pair(dst, bsuid)
dst_name_start = bsuid.rindex(src_fiat) if likely_pair:
except ( src2dst[src_fiat] = dst
ValueError, # substr not found
):
# TODO: handle nested positions..(i.e.
# positions where the src fiat was used to
# buy some other dst which was furhter used
# to buy another dst..)
log.warning(
f'No src fiat {src_fiat} found in {bsuid}?'
)
continue
_dst = bsuid[:dst_name_start]
if _dst != dst:
continue
src2dst[src_fiat] = dst
for src, dst in src2dst.items(): for src, dst in src2dst.items():
pair = f'{dst}{src_fiat}' pair = f'{dst}{src_fiat}'
@ -578,11 +600,11 @@ async def trades_dialogue(
# in the ``pps.toml`` for the necessary pair # in the ``pps.toml`` for the necessary pair
# yet and thus this likely pair grabber will # yet and thus this likely pair grabber will
# likely fail. # likely fail.
likely_pair = { for bsuid in table.pps:
bsuid[:3]: bsuid likely_pair = get_likely_pair(dst, bsuid)
for bsuid in table.pps if likely_pair:
}.get(dst) break
if not likely_pair: else:
raise ValueError( raise ValueError(
'Could not find a position pair in ' 'Could not find a position pair in '
'ledger for likely widthdrawal ' 'ledger for likely widthdrawal '
@ -595,6 +617,8 @@ async def trades_dialogue(
xfer_trans = await client.get_xfers( xfer_trans = await client.get_xfers(
dst, dst,
# TODO: not all src assets are
# 3 chars long...
src_asset=likely_pair[3:], src_asset=likely_pair[3:],
) )
if xfer_trans: if xfer_trans: