Skip to content

Commit

Permalink
Merge pull request #459 from pikers/kraken_deposits_fixes
Browse files Browse the repository at this point in the history
`kraken`: make pps work with arbitrary deposits
  • Loading branch information
goodboy authored Feb 12, 2023
2 parents d690ad2 + 1346c33 commit 31fc2d7
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions piker/brokers/kraken/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,36 +496,58 @@ async def trades_dialogue(
for dst, size in balances.items():
# we don't care about tracking positions
# 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

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(
dst: str,
size: float,

) -> Position | bool:

src2dst: dict[str, str] = {}
for bsuid in table.pps:
try:
dst_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}?'
)
continue

_dst = bsuid[:dst_name_start]
if _dst != dst:
continue

src2dst[src_fiat] = dst
for bsuid in table.pps:
likely_pair = get_likely_pair(dst, bsuid)
if likely_pair:
src2dst[src_fiat] = dst

for src, dst in src2dst.items():
pair = f'{dst}{src_fiat}'
Expand Down Expand Up @@ -578,11 +600,11 @@ def has_pp(
# 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:
for bsuid in table.pps:
likely_pair = get_likely_pair(dst, bsuid)
if likely_pair:
break
else:
raise ValueError(
'Could not find a position pair in '
'ledger for likely widthdrawal '
Expand All @@ -595,6 +617,8 @@ def has_pp(

xfer_trans = await client.get_xfers(
dst,
# TODO: not all src assets are
# 3 chars long...
src_asset=likely_pair[3:],
)
if xfer_trans:
Expand Down

0 comments on commit 31fc2d7

Please sign in to comment.