yearn / yearn-vaults

Yearn Vault smart contracts

Home Page:https://yearn.finance/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about recalculation of shares in withdraw

t4sk opened this issue · comments

commented

Should the recalculation of shares to burn on withdraw use freeFunds instead of totalAssets?

First calculation of shares to value uses freeFunds
https://github.com/yearn/yearn-vaults/blob/master/contracts/Vault.vy#L1048

Second calculation of value + totalLoss to shares uses totalAssets

https://github.com/yearn/yearn-vaults/blob/master/contracts/Vault.vy#L1101

Share holder loses less share when second calculation is performed with totalAssets

yes, you are right.

when the withdrawal results in losses AND there is some profit locked from the last harvest (< 6 hours since last harvest), pricePerShare used for calculating the amount of shares to burn is higher than pricePerShare used to calculate total value being withdrawn.

As the user needs to explicitly accept losses over 0.01% (by passing maxLoss arg), the vault needs to have been unable to retrieve enough value (taking into account losses, which is super rare) AND there needs to be some profit locked (usually a small amount), I would say this is a low impact / very low frequency bug. I would solve it, though, for the sake of correctness.

Also, you could argue that it is OK to use profits to "compensate" for losses, which is kind of what you are ultimately doing. But if that is the case, I don't think this has been done on purpose as current approach does not return a higher value, so it would require some changes too.

I invite you to solve this and create a PR with your solution.

Thanks!