BlockPo/BlockPo-to-Tradelayer

Last Mile Logic for Settlement

Closed this issue · 2 comments

From our testing we can identify that profit and loss is transferring available assets to reserve per Ale's May update:

751a387

   // reseting PNL
   assert(update_register_map(elem->getAddr(), contract_traded, 0, PNL));

   // passing  from margin to balance
   assert(update_register_map(elem->getAddr(), contract_traded, -remainingMargin, MARGIN));
   assert(update_tally_map(elem->getAddr(), cd.collateral_currency, remainingMargin, BALANCE));

However what is not happening is a movement from Reserve to Available, negative PnL is just getting trapped in limbo, not being paid out.

Why isn't remainingMargin moving to available balance?

We can fix this by looping through PNLs and mapping the realized negative PNL to reduction in margin balance and mapping realized positive PNL to increase in available balance, and assert that this nets 0 across all operations. Instead of couching this operation atomically inside MDex based on reducing net position, it should occur in the settlement function in tradelayer.cpp around line 8300.

We're going to:

  1. add a column to the register for per-Block-PNL
  2. add a function to call that populations the register with those values calculated on a formula of:
    this_block_mark_price - last_block_mark_price * notional * position
  3. add a function in the settlement code to map the per-Block-PNL to credits and debits in the tallymap to the available balance (for credits) or against the margin balance (for debits), which in combination with existing logic of moving balance to margin, should keep it all flowing smoothly.

This was redundant the problem was asserts, we removed that problem and are now dealing with math logic in the guts.