Closed patrickdugan closed 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.
This was redundant the problem was asserts, we removed that problem and are now dealing with math logic in the guts.
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
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.