> ## Documentation Index
> Fetch the complete documentation index at: https://docs.phylax.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# Harvest Increases Balance

> Make sure that the balance increases after a harvest

## When to Use This Pattern

Ensures harvest operations in yield farming protocols maintain integrity by preventing balance decreases and price per share manipulation. Critical for yield farming protocols (Beefy, Yearn, Harvest), liquidity mining programs, staking protocols with reward distribution, and auto-compounding vaults.

Incorrect harvest implementations could lead to loss of user funds, price per share manipulation enabling flash loan attacks, or protocol insolvency through improper reward distribution.

## What This Pattern Checks

Implements multi-layered approach to verify harvest operations:

* `_preTx()` / `_postTx()` with Reshiram snapshot reads: Capture vault balance and price per share before/after harvest
* `ph.loadStateAt()`: Read balance and price-per-share slots before and after the harvest call
* `registerFnCallTrigger()`: Trigger on harvest function calls
* Ensures balance hasn't decreased (can stay same if harvested recently)
* Confirms price per share hasn't decreased
* Prevents manipulation through multiple harvest calls

The assertion verifies each balance change is valid relative to previous state, ensuring no balance decreases occur at any point and maintaining protocol solvency.

For more information about cheatcodes, see the [Cheatcodes Documentation](/credible/cheatcodes-reference).

## Assertion Pattern

```solidity theme={null}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {Assertion} from "credible-std/Assertion.sol";
import {AssertionSpec} from "credible-std/SpecRecorder.sol";
import {PhEvm} from "credible-std/PhEvm.sol";

contract BeefyHarvestAssertion is Assertion {
    bytes32 internal constant BALANCE_SLOT = bytes32(uint256(0));
    bytes32 internal constant PRICE_PER_SHARE_SLOT = bytes32(uint256(1));

    constructor() {
        registerAssertionSpec(AssertionSpec.Reshiram);
    }

    function triggers() external view override {
        registerFnCallTrigger(this.assertionHarvestIncreasesBalance.selector, IBeefyVault.harvest.selector);
    }

    /// @notice Checks that a harvest does not reduce vault balance or price per share.
    function assertionHarvestIncreasesBalance() external view {
        address vault = ph.getAssertionAdopter();
        PhEvm.TriggerContext memory ctx = ph.context();
        PhEvm.ForkId memory preCall = PhEvm.ForkId({forkType: 2, callIndex: ctx.callStart});
        PhEvm.ForkId memory postCall = PhEvm.ForkId({forkType: 3, callIndex: ctx.callEnd});

        uint256 preBalance = uint256(ph.loadStateAt(vault, BALANCE_SLOT, preCall));
        uint256 postBalance = uint256(ph.loadStateAt(vault, BALANCE_SLOT, postCall));
        uint256 prePricePerShare = uint256(ph.loadStateAt(vault, PRICE_PER_SHARE_SLOT, preCall));
        uint256 postPricePerShare = uint256(ph.loadStateAt(vault, PRICE_PER_SHARE_SLOT, postCall));

        require(postBalance >= preBalance, "Harvest decreased balance");
        require(postPricePerShare >= prePricePerShare, "Price per share decreased after harvest");
    }
}

interface IBeefyVault {
    function harvest(bool badHarvest) external;
}
```

<Note>Full examples and mock protocol code are available in [credible-std](https://github.com/phylaxsystems/credible-std/tree/master/examples/assertions-book).</Note>

## Related Patterns

* [ERC4626 Assets to Shares](/assertions-book/assertions/ass12-erc4626-assets-to-shares)
* [ERC20 Cumulative Outflow Breaker](/assertions-book/assertions/ass20-erc20-drain)
* [Price Within Ticks](/assertions-book/assertions/ass15-price-within-ticks)
