How to use AI to generate assertions
Important Note: AI tools can make mistakes when generating assertions. Always review AI-generated code carefully and test your assertions thoroughly before deployment. The goal of AI is to accelerate the development process, not replace manual verification.
Note: This guide focuses on using Cursor as the AI-powered IDE. If you’re using a different AI coding assistant, you’ll need to adjust the setup steps accordingly while following the same assertion generation principles.As the number of example assertions grows, we expect the quality of the generated assertions to improve. Similarly, we hope that more and more protocols will start clearly documenting their invariants, as it’s both an important step towards better security in general and towards more efficient assertions generation.
pcl
) and have a project set up. If you don’t have one set up yet, you can follow the pcl quickstart guide to get started.
Note: The assertions and tests presented in this section were generated with AI directly from the Origin Protocol codebase as a first version. They are provided as illustrative examples without further refinement and may not fully work in their current form without adjustments. However, they give a practical demonstration of how assertions could be implemented and tested for a real protocol. A developer familiar with Origin Protocol could likely adapt these examples to be production-ready with relatively minimal effort.We’ll demonstrate using Origin Protocol’s contracts as an example. These contracts are well-documented and include clear invariant definitions in their token logic documentation.
.a.sol
extensiontest_assertionBalancePreservation_Pass
).
vm.prank
, vm.expectRevert
, and other utilities to create comprehensive tests.
pcl test