## Invariants Invariants are a key testing concept when fuzzing smart contracts. Invariants are properties of the system that should always hold true. We can use the *invariant* keyword in Hypothesis to check these invariants. The *invariant* keyword automatically runs a function after every rule that's executed. If the invariant is false, an exception is raised. Let's write an invariant for our stablecoin protocol: ```python @invariant def protocol_must_have_more_value_than_total_supply(self): total_supply = self.dsc.total_supply() weth_deposited = self.weth.balance_of(self.dsce.address) wbtc_deposited = self.wbtc.balance_of(self.dsce.address) weth_value = self.dsc.get_usd_value(self.weth, weth_deposited) wbtc_value = self.dsc.get_usd_value(self.wbtc, wbtc_deposited) assert (weth_value + wbtc_value) >= total_supply ``` This invariant checks that the total value of the collateral deposited (WETH and WBTC) is greater than or equal to the total supply of the stablecoin. We can run our fuzzer with this invariant to see if it holds true. ```bash mox test-s stablecoin_fuzzer ``` This will run the fuzzer and automatically test our invariant. If any rule breaks the invariant, the fuzzer will throw an exception.
Invariants are a key testing concept when fuzzing smart contracts.
Invariants are properties of the system that should always hold true. We can use the invariant keyword in Hypothesis to check these invariants.
The invariant keyword automatically runs a function after every rule that's executed. If the invariant is false, an exception is raised.
Let's write an invariant for our stablecoin protocol:
This invariant checks that the total value of the collateral deposited (WETH and WBTC) is greater than or equal to the total supply of the stablecoin.
We can run our fuzzer with this invariant to see if it holds true.
This will run the fuzzer and automatically test our invariant. If any rule breaks the invariant, the fuzzer will throw an exception.
A powerful guide to using Invariants in Hypothesis fuzzing - This lesson describes the Invariant keyword within the Hypothesis testing framework, and explains how to use it to define important properties of the system that should always hold true.
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on November 7, 2025
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on November 7, 2025