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 to dust issue on unforced withdrawals under special case #247

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions contracts/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ contract Vault is
revert VaultCannotWithdrawMoreThanAvailable();

// Amount of shares the _amount is worth
uint256 amountShares = _computeShares(
uint256 sharesToBurn = _computeShares(
_amount,
_totalShares,
_totalUnderlyingMinusSponsored
Expand All @@ -1112,13 +1112,12 @@ contract Vault is
uint256 claimerShares = (_amount * _claim.totalShares) /
_claim.totalPrincipal;

if (!_force && amountShares > claimerShares)
if (!_force && sharesToBurn > claimerShares)
revert VaultMustUseForceWithdrawToAcceptLosses();

uint256 sharesToBurn = amountShares;
bool haircut = sharesToBurn > claimerShares;

if (_force && amountShares > claimerShares)
sharesToBurn = claimerShares;
if (haircut) sharesToBurn = claimerShares;

claimer[_deposit.claimerId].totalShares -= sharesToBurn;
claimer[_deposit.claimerId].totalPrincipal -= _amount;
Expand All @@ -1134,11 +1133,14 @@ contract Vault is
deposits[_tokenId].amount -= _amount;
}

uint256 amount = _computeAmount(
sharesToBurn,
_totalShares,
_totalUnderlyingMinusSponsored
);
uint256 amount = _amount;
if (haircut) {
amount = _computeAmount(
sharesToBurn,
_totalShares,
_totalUnderlyingMinusSponsored
);
}

emit DepositWithdrawn(_tokenId, sharesToBurn, amount, _to, isFull);

Expand Down
27 changes: 27 additions & 0 deletions test/vault/Vault.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2949,6 +2949,33 @@ describe('Vault', () => {

await vault.connect(admin).exitUnpause();
});

it('leaves no dust on withdraw when force is false', async () => {
const toDeposit = parseUnits('100');
const prevBal = await underlying.balanceOf(alice.address);

const params = depositParams.build({
amount: toDeposit,
inputToken: underlying.address,
claims: [
claimParams.percent(50).to(alice.address).build(),
claimParams.percent(50).to(bob.address).build(),
],
});

await vault.connect(alice).deposit(params);

await moveForwardTwoWeeks();
await addYieldToVault('100');

await vault
.connect(alice)
.partialWithdraw(alice.address, [2], [parseUnits('1')]);

const newBal = await underlying.balanceOf(alice.address);

expect(prevBal.sub(newBal)).to.eq(toDeposit.sub(parseUnits('1')));
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this test failing without your changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test because of the subtraction of 1 done in line 2977 to take the dust into account. Similarly many other tests also had a +1 or -1 in the end to account for the dust. This fix, fixes that issue so now we dont have to account for dust in the tests thus also making the tests more readable and simpler. I have made the changes to the tests and they are passing now.

});

describe('claimYield', () => {
Expand Down