Position Closure Payout Rules
General Payout Rules
Per the concept of locked_amount covered here, every Position has an allotment of tokens set aside from the Custody at initialization to pay back collateral, the loan, and some profits/losses. Beyond that limit, other rules apply. Let's walk through them in a waterfall.
We'll focus on a generic token being removed from a Position first, and then talk about specific things that happen for a token that represents a unit of collateral, a unit of loan, or a unit of profit/loss in a sidebar.
First, locked_amount is decremented during disbursement (by convention, locked_amount is generally guaranteed to be above loan + collateral) and while the tokens do reside in the actual Custody token account literally, the accounting is considered to come out of this locked pile tied to Position and the locked variable on Assets for the Custody where it is tracked as a whole.
Note that when the trader is paid out, he is being paid from the Custody token account.
Now locked_amount is expired to 0, locked is decremented by the removed amount, and we enter the Insurance Fund territory. We attempt to payout what is owed to the trader entirely from this separate token account that's tied to each Custody and funded entirely by separate fees, unredeemable by LPs, until it is empty.
Note that when the trader is paid out during this case, he is being paid from the Custody's insurance token account. The trader will receive funds from two different accounts at this stage (due to steps #1 and #2).
Finally, once the Insurance Fund is expired to 0, we start taking impairment losses on tokens that aren't locked and available for lending as determined as the difference between base and locked on Assets and incrementing the impairment_losses variable on Assets to permanently represent those losses taken.
Finally, if locked_amount goes below the new size_usd if the Position, and it isn't being completely closed or liquidated, it is reallocated to be at least the size_usd of the Position, and locked is similarly increased, if available. If these tokens aren't available to be locked, then the entire requested change (update_position) of the Position will fail in-transaction since it cannot be topped up with new allocated locked tokens meeting at least it's loan + collateral size.
Sidebar on Types of Token Movements
There are three different types of token closure payouts: collateral, loan, and profit/loss. Each touches different variables in the system. All involve locked_amount and locked being adjusted, as mentioned above, if the decrement takes place when moving tokens beneath the amount of locked_amount tokens.
Collateral: When collateral is being withdrawn, owned decreases but base does not, because owned represents base + collateral.
Loan: When loan is being wound down, nothing is being touched, so nothing other than locked_amount and locked change.
Last updated