mrgnlabs / marginfi-v2

Apache License 2.0
158 stars 66 forks source link

More flexible flashloan health check #213

Open cavemanloverboy opened 4 months ago

cavemanloverboy commented 4 months ago

Summary

With these changes, a flashloan can be initialized regardless of account health. However, a flashloan can only be finalized if one of following conditions are true:

1) The account is in good health 2) The account is in bad health and the health at the end of the flashloan has improved from the beginning of the flashloan.

Note that we don't simply check if health improved. as users should be allowed to worsen their health in case 1 so long as their account started and ended in good health. To facilitate the check in 2), 16 bytes have been taken from the marginfi account padding to store the account health at the start of the flashloan.

Testing

This PR adds two tests for flashloans for accounts that begin in bad health: 1) SUCCESS test case where a user with a marginfi account in bad health does not worsen its health during a flashloan 2) FAILURE test case where a user with a marginfi account in bad health worsens its health during a flashloan