Implementation of a program that manages on chain accounts to mint and redeem Redeemables tokens (UXD), hedged by a delta neutral position on underlying DEXes.
Basically the following scenario would happen sometimes:
1) We create the withdraw request (rebalancing request), we compute the amount withdrawable based on the profits at this time
2) Right after we create the withdraw request (and before the withdraw redeem) new profits arrive
3) The Withdraw Redeem is then triggered, but it is trying to withdraw more profits than we requested in step 1 (since profit increased in the meantime)
4) If (and only if) the liquidity is not enough for step3's profit, and someone else is also trying to withdraw that liquidity, credix is capping the redeem to the amount requested, making the rebalance fail
This PR fixes this:
1) create a reproduction unit test
2) take the request amount into account when doing the redeem
3) cleanup the maths wherever possible and wherever i'm testing
Investigating a rebalancing error: https://explorer.solana.com/tx/4Ui2dkt1RHKRbXQfq6yS5zqEyFCzJp7dQg2RbRZHEjyte7jmnyAeXP8iD6ggEr7HHmUCJpZPXLspTH9DiCKtqjgV
Basically the following scenario would happen sometimes:
This PR fixes this: