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 all commits
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
8 changes: 3 additions & 5 deletions test/audit_2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,10 @@ describe('Audit Tests 2', () => {

await vault.connect(charlie).withdraw(charlie.address, [5]);

expect(await underlying.balanceOf(charlie.address)).to.eq(
oldBalance.sub(1),
);
expect(await underlying.balanceOf(charlie.address)).to.eq(oldBalance);
});

it('price per share can be manipulated', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just curious to why was there a test for "pps can be manipulated"?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a test from the 2nd audit we did. The test proved (at the time) that it could be manipulated. We just kept it as it was

it('price per share can not be manipulated', async () => {
await addUnderlyingBalance(alice, '10000');
await underlying.mint(bob.address, parseUnits('10000'));
await underlying.mint(charlie.address, parseUnits('10000'));
Expand Down Expand Up @@ -285,7 +283,7 @@ describe('Audit Tests 2', () => {

await vault.connect(charlie).withdraw(charlie.address, [4]);

expect(Number(await underlying.balanceOf(charlie.address))).to.lessThan(
expect(Number(await underlying.balanceOf(charlie.address))).to.equal(
oldBalance,
);
});
Expand Down
4 changes: 2 additions & 2 deletions test/audit_3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('Audit Tests 3', () => {
// expect depositor3 can normally withdraw
await vault.connect(depositor3).withdraw(depositor3.address, [3]);
expect(await underlying.balanceOf(depositor3.address)).to.eq(
parseUnits('100000').sub(1),
parseUnits('100000'),
);
console.log(
'depositor3 underlying balance: ' +
Expand Down Expand Up @@ -293,7 +293,7 @@ describe('Audit Tests 3', () => {
// expect depositor3 can normally withdraw but must use forceWithdraw
await vault.connect(depositor3).forceWithdraw(depositor3.address, [3]);
expect(await underlying.balanceOf(depositor3.address)).to.eq(
parseUnits('100000').sub(1),
parseUnits('100000'),
);
console.log(
'depositor3 underlying balance: ' +
Expand Down
8 changes: 5 additions & 3 deletions test/strategy/yearn/YearnStrategy.fork.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ describe('Yearn Strategy (mainnet fork tests)', () => {
parseUnits('1000', await lusd.decimals()),
);

const depositAmt = parseUnits('1000');

await vault.connect(alice).deposit(
depositParams.build({
amount: parseUnits('1000'),
amount: depositAmt,
inputToken: lusd.address,
claims: [claimParams.percent(100).to(alice.address).build()],
}),
Expand All @@ -150,7 +152,7 @@ describe('Yearn Strategy (mainnet fork tests)', () => {
await vault.connect(alice).withdraw(alice.address, [1]);

const aliceBalance = await lusd.balanceOf(alice.address);
expect(aliceBalance).to.eq('999999999999999999999');
expect(aliceBalance).to.eq(depositAmt);
});

it('allows user to claim yield when Yearn Vault performs', async () => {
Expand Down Expand Up @@ -184,7 +186,7 @@ describe('Yearn Strategy (mainnet fork tests)', () => {
await vault.connect(alice).withdraw(alice.address, [1]);

const aliceBalance = await lusd.balanceOf(alice.address);
expect(aliceBalance).to.eq('1089999999999999999174');
expect(aliceBalance).to.eq('1089999999999999999175');
});

it('allows user to only do force withdrawal when Yearn Vault underperforms', async () => {
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
23 changes: 11 additions & 12 deletions test/vault/VaultSyncMode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('Vault in sync mode', () => {
await vault.connect(alice).withdraw(alice.address, [1]);

expect(await underlying.balanceOf(alice.address)).to.eq(
'999999999999999999999',
parseUnits('1000'),
);
});

Expand All @@ -265,7 +265,7 @@ describe('Vault in sync mode', () => {
await vault.connect(alice).withdraw(alice.address, [1]);

expect(await underlying.balanceOf(alice.address)).to.eq(
'999999999999999999999',
parseUnits('1000'),
);
});

Expand All @@ -285,7 +285,7 @@ describe('Vault in sync mode', () => {

expect(await underlying.balanceOf(vault.address)).to.eq(parseUnits('5'));
expect(await underlying.balanceOf(strategy.address)).to.eq(
'45000000000000000001',
parseUnits('45'),
);
});

Expand Down Expand Up @@ -332,7 +332,8 @@ describe('Vault in sync mode', () => {
await moveForwardTwoWeeks();
const tx = await vault.connect(alice).withdraw(alice.address, [1]);

expect(await underlying.balanceOf(vault.address)).to.eq('1');
// this should be zero and not one (to account dust) as per the above comment on line 324
expect(await underlying.balanceOf(vault.address)).to.eq('0');
expect(await underlying.balanceOf(strategy.address)).to.eq(
parseUnits('135'),
);
Expand Down Expand Up @@ -363,7 +364,7 @@ describe('Vault in sync mode', () => {

// vault had 115 before the withdrawal, and 100 was withdrawn, so it should have 115 - 100 = 15
expect(await underlying.balanceOf(vault.address)).to.eq(
'15000000000000000001'.toString(),
parseUnits('15').toString(),
);
expect(await underlying.balanceOf(strategy.address)).to.eq(
parseUnits('135'),
Expand All @@ -389,7 +390,7 @@ describe('Vault in sync mode', () => {
.partialWithdraw(alice.address, [1], [parseUnits('50')]);

expect(await underlying.balanceOf(alice.address)).to.eq(
'949999999999999999999',
parseUnits('950'),
);
});

Expand All @@ -411,7 +412,7 @@ describe('Vault in sync mode', () => {
.partialWithdraw(alice.address, [1], [parseUnits('50')]);

expect(await underlying.balanceOf(alice.address)).to.eq(
'949999999999999999999',
parseUnits('950'),
);
});

Expand All @@ -433,7 +434,7 @@ describe('Vault in sync mode', () => {

expect(await underlying.balanceOf(vault.address)).to.eq(parseUnits('10'));
expect(await underlying.balanceOf(strategy.address)).to.eq(
'90000000000000000001',
parseUnits('90'),
);
});

Expand Down Expand Up @@ -484,7 +485,7 @@ describe('Vault in sync mode', () => {
.connect(alice)
.partialWithdraw(alice.address, [1], [parseUnits('50')]);

expect(await underlying.balanceOf(vault.address)).to.eq('1');
expect(await underlying.balanceOf(vault.address)).to.eq('0');
expect(await underlying.balanceOf(strategy.address)).to.eq(
parseUnits('135'),
);
Expand Down Expand Up @@ -516,9 +517,7 @@ describe('Vault in sync mode', () => {
.partialWithdraw(alice.address, [1], [parseUnits('50')]);

// vault had 115 before the withdrawal, and 50 was withdrawn, so it should have 115 - 50 = 65
expect(await underlying.balanceOf(vault.address)).to.eq(
'65000000000000000001'.toString(),
);
expect(await underlying.balanceOf(vault.address)).to.eq(parseUnits('65'));
expect(await underlying.balanceOf(strategy.address)).to.eq(
parseUnits('135'),
);
Expand Down