mrgnlabs / marginfi-v2

Apache License 2.0
139 stars 59 forks source link

Cavey/lif #215

Open cavemanloverboy opened 1 month ago

cavemanloverboy commented 1 month ago

This PR introduces Liquid Insurance Funds to marginfi, allowing users to generate yield from liquidations and improving system robustness.

The following instructions are introduced: 1) Create a LIF for an existing bank. If there are any existing deposits, new shares are created for the admin. 2) Create user LIF account 3) User deposit into LIF 4) User withdraw request 5) User withdraw claim

The following instructions are changed: 1) If there is a LIF present, the group admin can only withdraw the corresponding value of their admin shares. 2) An admin deposit instruction is introduced to increment admin shares (a direct transfer in would be distributed to all shareholders as if it were liquidation yield)

The following tests are written: 0) Create LIF 1) User deposit/withdraw 2) admin deposit/withdraw 3) admin deposit/withdraw with a liquidation in between (increase share value) 4) admin deposit/withdraw with a bad debt handled in between (decrease share value)

Notes: 1) a LIF account has a maximum number of deposits/withdraws. Group admins may want to deposit into all banks. To handle this, admin shares are stored directly in the LIF, and are managed through separate instructions. 2) The admin withdraw function assumes the input value is token amount if no LIF is present, and is share amount if a LIF is present. This could perhaps be done a little more neatly via an enum that panics when in the wrong variant.