BlockPo / BlockPo-to-Tradelayer

Incubation Repo for the TradeLayer protocol, 0.2.0
http://www.tradelayer.org
Other
8 stars 8 forks source link

Last Mile Logic for Settlement #484

Closed patrickdugan closed 1 year ago

patrickdugan commented 1 year ago

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

https://github.com/BlockPo/BlockPo-to-Tradelayer/commit/751a38762b637a767dbb6084aefc9e7b55c07e82

   // 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.

patrickdugan commented 1 year ago

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.

patrickdugan commented 1 year ago

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