ERC-4626 Vault and Reentrancy Regression Tests
Turn vault accounting, share rounding and external-call safety into repeatable Foundry regressions against a local fixture.
Make units and support policy explicit
Seed an authorized local fixture with a supported ERC-20 and at least two depositors. Label assets in token units and shares in share units. Document fixed fees, interest assumptions and whether the vault supports callback or fee-on-transfer assets. Test rejection behavior for unsupported assets.
Turn previews into executable expectations
Under the same state and transaction, deposit and redeem should return at least the corresponding preview amounts. Mint and withdraw should cost no more than their previews. Cover the first deposit, dust, exact rounding boundaries, and withdrawal with a finite share allowance. Verify balances and events separately from the return value.
Check external-call safety and atomicity
For each transfer or strategy call, chart which accounting fields are consistent when control leaves the vault. Test allowed behavior with supported test doubles. For a transfer failure, assert that asset balances, allowance, share supply and accounting roll back. For callback-aware assets, inspect read-only observations during supported actions and require any externally visible state to obey the vault's policy.
Use only controlled actions in invariant tests
Record ghost variables for deposits, withdrawals and modeled yield or loss. Assert share supply matches modeled holders plus declared fee balances. Reconcile underlying custody, debt and strategy positions after each settled action. A simple vault with no yield can use a tighter model than a strategy vault.
// Inside a forge-std Test with a funded fixture and asset approval.
function testDepositMeetsPreview() public {
uint256 assets = 1_000_000;
uint256 quoted = vault.previewDeposit(assets);
uint256 sharesBefore = vault.balanceOf(address(this));
uint256 minted = vault.deposit(assets, address(this));
assertGe(minted, quoted);
assertEq(vault.balanceOf(address(this)) - sharesBefore, minted);
}
Frequently asked questions
- Should preview and execution always be exactly equal?
- Standard conformance uses directional bounds under the same conditions. Enforce equality only where your implementation guarantees it.
- Does one balance invariant cover reentrancy?
- No. Cover call boundaries, rollback, callbacks allowed by the asset policy and observations during external calls.