Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: withdraw limits #178

Closed
wants to merge 4 commits into from
Closed

fix: withdraw limits #178

wants to merge 4 commits into from

Conversation

Schlagonia
Copy link
Collaborator

@Schlagonia Schlagonia commented Aug 31, 2023

Description

simulate the full withdraw flow to make sure maxRedeem and maxWithdraw fit the erc-4626 standard and vaults are compatible with each other.

Fixes # (issue)

Checklist

  • I have run vyper and solidity linting
  • I have run the tests on my machine
  • I have followed commitlint guidelines
  • I have rebased my changes to the latest version of the main branch

contracts/VaultV3.vy Show resolved Hide resolved
contracts/VaultV3.vy Show resolved Hide resolved
max_assets = have

# Check if there is a loss and a non-default value was set.
if loss > 0 and max_loss < MAX_BPS:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to allow max_loss = 100%?

nobody should use it but probably better to allow 100% losses

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the default value used in "redeem" calls if one is not given.

It shouldnt be used. But dont think there is another option we should enforce as a default on every vault

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is 99.999999999% a valid value, but not 100%?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% is a valid value and is used by default for redeems. But if its 100% then we have no need to waste the gas to check the loss is within the range

contracts/VaultV3.vy Show resolved Hide resolved
# If the loss is not within the allowed range.
if loss > max_assets * max_loss / MAX_BPS:
# The tx will revert. Can only withdraw idle.
max_assets = current_idle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to do this check (is loss >= max_loss?) every time we have a loss and return the max_assets at that point, returning the max amount of withdrawable assets before hitting a certain max_loss?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added here abb73b6

# lower unrealised loss to the proportion to the limit.
unrealised_loss = unrealised_loss * strategy_limit / max_withdraw
# Still count the unrealised loss as withdrawable.
max_withdraw = strategy_limit + unrealised_loss
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that returning max_assets including losses can lead to issues, as smart contracts would expect to receive the losses too.

also, it doesn't comply with erc4626

max received assets should be the output (i.e. do not include losses)

Copy link
Collaborator Author

@Schlagonia Schlagonia Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this was an issue i was unsure how to handle. But I believe we have to count it as "withdrawable" since it is unrealized in order to comply with the standard.

I wrote the reasoning in the comments. https://github.com/Schlagonia/yearn-vaults-v3/blob/limits/contracts/VaultV3.vy#L566-L572

Basically the conversion rates the vault uses don't yet know about that loss, so the amounts shouldn't adjust for it.

The maxRedeem would also be screwed up if we didnt account for the unrealized loss as withdrawable.

Its not an ideal way to do it but im pretty sure it has to be done this way to comply to the standard.

strategy_limit: uint256 = IStrategy(strategy).maxWithdraw(self)

# Adjust accordingly if there is a max withdraw limit.
if strategy_limit < max_withdraw - unrealised_loss:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you should be able to remove this block if you do:

max_withdraw: uint256 = min(needed, IStrategy(strategy).maxWithdraw(self))
unrealised_loss: uint256 = self._assess_share_of_unrealised_losses(strategy, max_withdraw)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also solves the case where max_withdraw < strategy_limit, where withdrawer would take 100% of the losses (and I don't think it should be this way anyway)

Copy link
Collaborator Author

@Schlagonia Schlagonia Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since .maxWithdraw(self) will already have accounted for the losses we cannot pass that value into the _assess_share_of_unrealised_losses. Otherwise our unrealised_losses will be reduced twice.

EX:

strategy_debt = 100
current_value = 90
unrealised_loss = 10

So if a user is trying to withdraw the full 100 the unrealised_loss should be 10.

needed = 100
strategy.maxWithdraw(vault) = 90
_assess_share_of_unrealised_losses(strategy, 90) = .1 * 90 = 9

contracts/VaultV3.vy Show resolved Hide resolved
contracts/VaultV3.vy Show resolved Hide resolved
@fp-crypto
Copy link
Collaborator

simulate the full withdraw flow to max sure

simulate the full withdraw flow to make

# The tx will revert. Can only withdraw idle.
max_assets = current_idle

return max_assets
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is underestimates the max_assets available in cases where partially iterating through the withdraw queue would have had tolerable losses.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abb73b6

Added to the other branch with the modules.

I Did not account for a scenario in which partial would revert but the full queue would not. Think that we shouldnt return a value that has amounts less than it that would revert. Could break things.

Perhaps that could be something to add to a preivewWithdraw/redeem

@Schlagonia
Copy link
Collaborator Author

changes merged with this PR 0387181

@Schlagonia Schlagonia closed this Oct 6, 2023
@Schlagonia Schlagonia deleted the limits branch October 6, 2023 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants