Trail of Bits Blog
Follow
Spotting issues in DeFi with dimensional analysis
The text advocates for using dimensional analysis, borrowed from physics, to enhance the safety of DeFi smart contracts. It emphasizes that this technique can identify logical and arithmetic errors by ensuring the dimensional consistency of formulas, without requiring code changes. The core concept revolves around treating DeFi quantities like tokens, prices, and liquidity as distinct dimensions, preventing incorrect operations like adding tokens directly. The author provides examples of how dimensional analysis can uncover flaws in pricing and other calculations. Best practices are presented, highlighting the importance of explicit dimensional annotations in code, exemplified by Reserve Protocol's use of unit comments. The text concludes by suggesting that using dimensional annotations makes code easier to review, modify, and audit.