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

bug: mistake in the last block of invariant test chapter #1032

Open
cryptonomicon46 opened this issue Oct 29, 2023 · 0 comments
Open

bug: mistake in the last block of invariant test chapter #1032

cryptonomicon46 opened this issue Oct 29, 2023 · 0 comments
Labels
good first issue Good for newcomers T-to-fix Type: issue in docs
Milestone

Comments

@cryptonomicon46
Copy link

The chapter on invariant tests, Link
ends with the following block of code explaining how the actors are fuzzed.

function deposit(
    uint256 assets,
    uint256 actorIndexSeed
) public virtual useActor(actorIndexSeed) {
    asset.mint(currentActor, assets);

    asset.approve(address(token), assets);

    uint256 beforeBalance = asset.balanceOf(address(this)); //@Issue

    uint256 shares = token.deposit(assets, address(this));

    assertEq(asset.balanceOf(address(this)), beforeBalance - assets);

    sumBalanceOf += shares;

    sumDeposits[currentActor] += assets
}

However, in this case, since the useActor(actorIndexSeed) results in the asset being minted to the currentActor.
The beforeBalance should be accounted for the currentActor and not for address(this).

uint256 beforeBalance = asset.balanceOf(currentActor)); 

Further, to make this test accurate, the minted shares should go to the the currentActor by setting them as the receiver.
And then the balance check should be performed on the currentActor as detailed below.

 uint256 shares = token.deposit(assets,currentActor);
assertEq(asset.balanceOf(currentActor), beforeBalance - assets);

Here's the corrected block of code.

// Unbounded
function deposit(
    uint256 assets,
    uint256 actorIndexSeed
) public virtual useActor(actorIndexSeed) {
    asset.mint(currentActor, assets);

    asset.approve(address(token), assets);

    uint256 beforeBalance = asset.balanceOf(currentActor); //@Issue

    uint256 shares = token.deposit(assets, address(this));

    assertEq(asset.balanceOf(currentActor), beforeBalance - assets);

    sumBalanceOf += shares;

    sumDeposits[currentActor] += assets
}
@zerosnacks zerosnacks added the T-to-fix Type: issue in docs label Sep 16, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Sep 16, 2024
@zerosnacks zerosnacks changed the title Mistake in the last block of Invariant tests bug: mistake in the last block of invariant test chapter Sep 16, 2024
@zerosnacks zerosnacks added the good first issue Good for newcomers label Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers T-to-fix Type: issue in docs
Projects
No open projects
Status: Todo
Development

No branches or pull requests

2 participants