@@ -413,19 +413,26 @@ async def trades_dialogue(
413
413
414
414
) -> AsyncIterator [dict [str , Any ]]:
415
415
416
- # XXX: required to propagate ``tractor`` loglevel to piker logging
416
+ # XXX: required to propagate ``tractor`` loglevel to `` piker`` logging
417
417
get_console_log (loglevel or tractor .current_actor ().loglevel )
418
418
419
419
async with get_client () as client :
420
420
421
+ if not client ._api_key :
422
+ raise RuntimeError (
423
+ 'Missing Kraken API key in `brokers.toml`!?!?' )
424
+
421
425
# TODO: make ems flip to paper mode via
422
426
# some returned signal if the user only wants to use
423
427
# the data feed or we return this?
424
- # await ctx.started(({}, ['paper']))
428
+ # else:
429
+ # await ctx.started(({}, ['paper']))
425
430
426
- if not client ._api_key :
427
- raise RuntimeError (
428
- 'Missing Kraken API key in `brokers.toml`!?!?' )
431
+ # NOTE: currently we expect the user to define a "source fiat"
432
+ # (much like the web UI let's you set an "account currency")
433
+ # such that all positions (nested or flat) will be translated to
434
+ # this source currency's terms.
435
+ src_fiat = client .conf ['src_fiat' ]
429
436
430
437
# auth required block
431
438
acctid = client ._name
@@ -444,10 +451,9 @@ async def trades_dialogue(
444
451
# NOTE: testing code for making sure the rt incremental update
445
452
# of positions, via newly generated msgs works. In order to test
446
453
# this,
447
- # - delete the *ABSOLUTE LAST* entry from accont 's corresponding
454
+ # - delete the *ABSOLUTE LAST* entry from account 's corresponding
448
455
# trade ledgers file (NOTE this MUST be the last record
449
- # delivered from the
450
- # api ledger),
456
+ # delivered from the api ledger),
451
457
# - open you ``pps.toml`` and find that same tid and delete it
452
458
# from the pp's clears table,
453
459
# - set this flag to `True`
@@ -486,40 +492,84 @@ async def trades_dialogue(
486
492
# and do diff with ledger to determine
487
493
# what amount of trades-transactions need
488
494
# to be reloaded.
489
- sizes = await client .get_balances ()
490
- for dst , size in sizes .items ():
495
+ balances = await client .get_balances ()
496
+ for dst , size in balances .items ():
491
497
# we don't care about tracking positions
492
498
# in the user's source fiat currency.
493
- if dst == client . conf [ ' src_fiat' ] :
499
+ if dst == src_fiat :
494
500
continue
495
501
496
- def has_pp (dst : str ) -> Position | bool :
497
- pps_dst_assets = {bsuid [:3 ]: bsuid for bsuid in table .pps }
498
- pair = pps_dst_assets .get (dst )
499
- pp = table .pps .get (pair )
502
+ def has_pp (
503
+ dst : str ,
504
+ size : float ,
500
505
501
- if (
502
- not pair or not pp
503
- or not math .isclose (pp .size , size )
504
- ):
505
- return False
506
+ ) -> Position | bool :
506
507
507
- return pp
508
+ src2dst : dict [str , str ] = {}
509
+ for bsuid in table .pps :
510
+ try :
511
+ dst_name_start = bsuid .rindex (src_fiat )
512
+ except (
513
+ ValueError , # substr not found
514
+ ):
515
+ # TODO: handle nested positions..(i.e.
516
+ # positions where the src fiat was used to
517
+ # buy some other dst which was furhter used
518
+ # to buy another dst..)
519
+ log .warning (
520
+ f'No src fiat { src_fiat } found in { bsuid } ?'
521
+ )
522
+ continue
508
523
509
- pos = has_pp (dst )
524
+ _dst = bsuid [:dst_name_start ]
525
+ if _dst != dst :
526
+ continue
527
+
528
+ src2dst [src_fiat ] = dst
529
+
530
+ for src , dst in src2dst .items ():
531
+ pair = f'{ dst } { src_fiat } '
532
+ pp = table .pps .get (pair )
533
+ if (
534
+ pp
535
+ and math .isclose (pp .size , size )
536
+ ):
537
+ return pp
538
+
539
+ elif (
540
+ size == 0
541
+ and pp .size
542
+ ):
543
+ log .warning (
544
+ f'`kraken` account says you have a ZERO '
545
+ f'balance for { bsuid } :{ pair } \n '
546
+ f'but piker seems to think `{ pp .size } `\n '
547
+ 'This is likely a discrepancy in piker '
548
+ 'accounting if the above number is'
549
+ "large,' though it's likely to due lack"
550
+ "f tracking xfers fees.."
551
+ )
552
+ return pp
553
+
554
+ return False
555
+
556
+ pos = has_pp (dst , size )
510
557
if not pos :
511
558
512
559
# we have a balance for which there is no pp
513
560
# entry? so we have to likely update from the
514
561
# ledger.
515
562
updated = table .update_from_trans (ledger_trans )
516
563
log .info (f'Updated pps from ledger:\n { pformat (updated )} ' )
517
- pos = has_pp (dst )
564
+ pos = has_pp (dst , size )
518
565
519
- if not pos and not simulate_pp_update :
566
+ if (
567
+ not pos
568
+ and not simulate_pp_update
569
+ ):
520
570
# try reloading from API
521
571
table .update_from_trans (api_trans )
522
- pos = has_pp (dst )
572
+ pos = has_pp (dst , size )
523
573
if not pos :
524
574
525
575
# get transfers to make sense of abs balances.
@@ -557,7 +607,7 @@ def has_pp(dst: str) -> Position | bool:
557
607
f'{ pformat (updated )} '
558
608
)
559
609
560
- if not has_pp (dst ):
610
+ if has_pp (dst , size ):
561
611
raise ValueError (
562
612
'Could not reproduce balance:\n '
563
613
f'dst: { dst } , { size } \n '
0 commit comments