1818Order api and machinery
1919
2020'''
21- from collections import ChainMap , defaultdict
2221from contextlib import (
2322 asynccontextmanager as acm ,
2423 aclosing ,
5251from piker .accounting ._mktinfo import (
5352 MktPair ,
5453)
54+ from piker .clearing import (
55+ OrderDialogs ,
56+ )
5557from piker .clearing ._messages import (
5658 Order ,
5759 Status ,
@@ -124,7 +126,7 @@ async def handle_order_requests(
124126 client : Client ,
125127 ems_order_stream : tractor .MsgStream ,
126128 token : str ,
127- apiflows : dict [ int , ChainMap [ dict [ str , dict ]]] ,
129+ apiflows : OrderDialogs ,
128130 ids : bidict [str , int ],
129131 reqids2txids : dict [int , str ],
130132
@@ -188,6 +190,7 @@ async def handle_order_requests(
188190 try :
189191 txid : str = reqids2txids [reqid ]
190192 except KeyError :
193+
191194 # XXX: not sure if this block ever gets hit now?
192195 log .error ('TOO FAST EDIT' )
193196 reqids2txids [reqid ] = TooFastEdit (reqid )
@@ -221,7 +224,11 @@ async def handle_order_requests(
221224 'type' : order .action ,
222225 }
223226
224- psym : str = order .symbol .upper ()
227+ # XXX strip any .<venue> token which should
228+ # ONLY ever be '.spot' rn, until we support
229+ # futes.
230+ bs_fqme : str = order .symbol .rstrip ('.spot' )
231+ psym : str = bs_fqme .upper ()
225232 pair : str = f'{ psym [:3 ]} /{ psym [3 :]} '
226233
227234 # XXX: ACK the request **immediately** before sending
@@ -260,7 +267,7 @@ async def handle_order_requests(
260267 await ws .send_msg (req )
261268
262269 # placehold for sanity checking in relay loop
263- apiflows [ reqid ]. maps . append ( msg )
270+ apiflows . add_msg ( reqid , msg )
264271
265272 case _:
266273 account = msg .get ('account' )
@@ -440,10 +447,7 @@ async def open_trade_dialog(
440447 acc_name = 'kraken.' + acctid
441448
442449 # task local msg dialog tracking
443- apiflows : defaultdict [
444- int ,
445- ChainMap [dict [str , dict ]],
446- ] = defaultdict (ChainMap )
450+ apiflows = OrderDialogs ()
447451
448452 # 2way map for ems ids to kraken int reqids..
449453 ids : bidict [str , int ] = bidict ()
@@ -706,7 +710,7 @@ async def handle_order_updates(
706710 ws : NoBsWs ,
707711 ws_stream : AsyncIterator ,
708712 ems_stream : tractor .MsgStream ,
709- apiflows : dict [ int , ChainMap [ dict [ str , dict ]]] ,
713+ apiflows : OrderDialogs ,
710714 ids : bidict [str , int ],
711715 reqids2txids : bidict [int , str ],
712716 table : PpTable ,
@@ -921,7 +925,7 @@ async def handle_order_updates(
921925 ),
922926 src = 'kraken' ,
923927 )
924- apiflows [ reqid ]. maps . append ( status_msg .to_dict ())
928+ apiflows . add_msg ( reqid , status_msg .to_dict ())
925929 await ems_stream .send (status_msg )
926930 continue
927931
@@ -1057,7 +1061,7 @@ async def handle_order_updates(
10571061 ),
10581062 )
10591063
1060- apiflows [ reqid ]. maps . append ( update_msg )
1064+ apiflows . add_msg ( reqid , update_msg )
10611065 await ems_stream .send (resp )
10621066
10631067 # fill msg.
@@ -1136,9 +1140,8 @@ async def handle_order_updates(
11361140 )
11371141 continue
11381142
1139- # update the msg chain
1140- chain = apiflows [reqid ]
1141- chain .maps .append (event )
1143+ # update the msg history
1144+ apiflows .add_msg (reqid , event )
11421145
11431146 if status == 'error' :
11441147 # any of ``{'add', 'edit', 'cancel'}``
@@ -1148,11 +1151,16 @@ async def handle_order_updates(
11481151 f'Failed to { action } order { reqid } :\n '
11491152 f'{ errmsg } '
11501153 )
1154+
1155+ symbol : str = 'N/A'
1156+ if chain := apiflows .get (reqid ):
1157+ symbol : str = chain .get ('symbol' , 'N/A' )
1158+
11511159 await ems_stream .send (BrokerdError (
11521160 oid = oid ,
11531161 # XXX: use old reqid in case it changed?
11541162 reqid = reqid ,
1155- symbol = chain . get ( ' symbol' , 'N/A' ) ,
1163+ symbol = symbol ,
11561164
11571165 reason = f'Failed { action } :\n { errmsg } ' ,
11581166 broker_details = event
0 commit comments