Avoid crash on trades ledger msgs

Just ignore them for now using new `match:` syntax B)
but we'll do incremental update sooon!

Resolves #311
notokeninwswrapper
Tyler Goodlet 2022-06-29 17:24:38 -04:00
parent 9106d13dfe
commit fcd7e0f3f3
1 changed files with 56 additions and 51 deletions

View File

@ -70,6 +70,7 @@ class Trade(BaseModel):
def pack_positions(
acc: str,
trades: dict
) -> list[Any]:
positions: dict[str, float] = {}
vols: dict[str, float] = {}
@ -342,11 +343,13 @@ async def trades_dialogue(
# TODO: maybe add multiple accounts
n.start_soon(handle_order_requests, client, ems_stream)
# pull and deliver trades ledger
acc_name = 'kraken.' + client._name
trades = await client.get_trades()
log.info(
f'Loaded {len(trades)} trades from account `{acc_name}`'
)
position_msgs = pack_positions(acc_name, trades)
await ctx.started((position_msgs, (acc_name,)))
# Get websocket token for authenticated data stream
@ -355,33 +358,31 @@ async def trades_dialogue(
# lol wtf is this..
assert resp['error'] == []
token = resp['result']['token']
async with (
ctx.open_stream() as ems_stream,
trio.open_nursery() as n,
):
# TODO: maybe add multiple accounts
n.start_soon(handle_order_requests, client, ems_stream)
# Process trades msg stream of ws
async with open_autorecon_ws(
open_autorecon_ws(
'wss://ws-auth.kraken.com/',
fixture=subscribe,
token=token,
) as ws:
) as ws,
trio.open_nursery() as n,
):
# task for processing inbound requests from ems
n.start_soon(handle_order_requests, client, ems_stream)
# begin trade event processing
async for msg in process_trade_msgs(ws):
for trade in msg:
# check the type of packaged message
assert type(trade) == Trade
match trade:
# prepare and send a filled status update
case Trade():
filled_msg = BrokerdStatus(
reqid=trade.reqid,
time_ns=time.time_ns(),
account='kraken.spot',
account=acc_name,
status='filled',
filled=float(trade.size),
reason='Order filled by kraken',
@ -399,7 +400,6 @@ async def trades_dialogue(
# https://github.com/pikers/piker/issues/296
remaining=0,
)
await ems_stream.send(filled_msg.dict())
# send a fill msg for gui update
@ -417,12 +417,17 @@ async def trades_dialogue(
await ems_stream.send(fill_msg.dict())
case _:
log.warning(f'Unhandled trades msg: {trade}')
await tractor.breakpoint()
async def process_trade_msgs(
ws: NoBsWs,
):
'''
Parse and pack data feed messages.
Parse and pack trades subscription messages, deliver framed
sequences of messages?
'''
sequence_counter = 0