# Application: Proof of Concept Hooks & Developer Documentation ## Introduction Subtrahend Labs is a small team of two researchers focused on AMM design. Together, we have published several papers chronicling and investigating the mathematics of the AMM space. Our team consists of a software engineer (0xcacti) experienced with on-chain and open-source code development, and a Ph.D. mathematician (Dr. Nick) who has an enthusiasm for communication and clear expository writing. In the past, we collaborated to produce lucid and approachable writing and research focused on AMMs and tokenomics. Some topics of our research include investigations into alternative fee structures for AMMs, the creation of novel token performance metrics, and macro-economic token analysis. In the course of our research, we have generated several ideas for improving the on-chain trading experience. We will use this RFP as an opportunity to research and implement our hook ideas (described in Project Scope). Our Team: 0xcacti - - Solidity developer focused on gas optimization. Implemented a highly optimized implementation of tic-tac-toe in Solidity that leverages bit manipulation and slot packing to increase efficiency and decrease gas costs - https://github.com/0xcacti/tictactoe - Created a JSON Parser to enable NFT Developers to easily verify that generated JSON metadata is valid - https://github.com/0xcacti/valid-json - 0xcacti regularly competes in CTF Challenges with some of the top Solidity developers in crypto. He is currently top 10 in these challenges. - https://www.curta.wtf/leaderboard - Rust developer with some experience building tooling for the Ethereum ecosystem. Snipe is a blocktime converter. Use it to move to and from human-readable timestamps and Ethereum block numbers. - https://github.com/0xcacti/snipe Dr. Nick - - NYU mathematics professor - Ph.D. from CUNY Graduate Center where he studied partial differential equations - https://academicworks.cuny.edu/gc_etds/1369/ - Current research explores new AMM protocols, information entropy, and complex systems modeling - Dr. Nick has published in the Journal of Structured Finance - https://www.pm-research.com/content/iijstrfin/26/4/105 - Dr. Nick is an enthusiastic writer who can effectively communicate complex ideas, illustrated by his mathematical writing and his student reviews on RateMyProfessor - https://subtrahendlabs.com/Research - https://www.ratemyprofessors.com/professor/2345762 Relevant Work: - [Intro to AMMs](https://github.com/subtrahend-labs/research/blob/main/intro-to-amms.pdf): A Mathematical Introduction to the Constant Product and Concentrated Liquidity AMMs - [The Passive Price Formula](https://github.com/subtrahend-labs/research/blob/main/the-passive-price-formula.pdf): A measure of token popularity in the confusing world of DeFi and AMMs - [Internal vs External Fees](https://github.com/subtrahend-labs/research/blob/main/internal-vs-external-fees.pdf): Exploring opposing fee structures in the Constant Product AMM - [The VAPY](https://github.com/subtrahend-labs/research/blob/main/the-vapy.pdf): Proposing a more accurate measure of APY for DeFi protocols ## Project Scope Our project will achieve all of the RFP deliverables. Specifically, we will build three proof of concept hook implementations along with developer documentation hosted on GitHub. We will create posts about each of these hooks for [UniswapHooks.com](http://uniswaphooks.com/). Finally, we will produce writing that discusses the design decisions made while building the hooks and provides user stories for both LPs and traders looking to interact with the hooks. Moreover, because these hook ideas require non-trivial mathematical foundations, we will also provide well-written research papers that motivate, derive, and explain the relevant mathematics. We plan to produce this work for the following three hook ideas. #### Internal Fees for Concentrated Liquidity While Uniswap V2 stores transaction fees internally to the liquidity pool, V3 stores fees externally. Trying to store fees internally in V3 while maintaining proper accounting is mathematically non-trivial, but we have produced a complete solution to this mathematical problem. Please see our [ introductory paper ](https://github.com/subtrahend-labs/research/blob/main/internal-fees-for-concentrated-liquidity-pre-paper.pdf) on this topic for a high-level overview of our methodology. One virtue of this approach is that it allows for fees to compound in a way that is currently unavailable in V3 and initial simulations show that this can be beneficial for the LPs. #### Robust Oracles (extrema resistant TWAP) Current TWAPs can be easily manipulated in scenarios when validators on Ethereum know that they will propose some number of blocks in a row. There exist several proposed mechanisms to limit the effects of oracle manipulation. One popular suggestion is the process of windsorizing (filtering of extreme values) in the calculation of the geometric mean price. Additionally, we have been doing research into alternative extrema-resistant price metrics that borrow tools from the theory of information entropy. We believe that there exists an abundance of unexplored statistical methods for achieving extrema resistance and we believe that hooks provide an effective mechanism to explore and implement these methods. #### Automatic Liquidation Prevention Hooks enable arbitrary logic to be executed after an interaction with a pool. This opens up an interesting design space in which one can use Uniswap pool interactions as a sort of chron job on Ethereum. While there are many considerations around security and design, we envision an automatic check (after each swap) to see if an oracle price has moved close to an LP's liquidation price on some external collateralized debt position. When this is the case, we can then execute an automatic top-up of the LP's collateral to prevent liquidation systematically. This use case provides a plethora of design decisions that should be answered. ### Approach While this is not a small amount of work, our approach to completing this RFP is to break it into three major components and to complete each in sequence. We view the three primary components of this project as research, implementation, and exposition. #### Research: This component involves producing research on each hook that seeks to answer questions like: What is the best way to implement this? How effective with the given mechanism be in production? etc. This research will inform an effective and efficient buildout of the hook implementations. We already have significant progress on this component. Our work includes a complete mathematical description of how to distribute fees internally in a concentrated liquidity model and an exploration of the effectiveness of windsorization vs. other extrema resistance methods. In completing this component, we will polish our existing work and we will answer the following additional questions: - Internal Fees: - To what extent do internal fees provide non-trivial benefits to LPs in concentrated liquidity pools? - Are there particular market regimes where internal fees are preferable over external fees? - Robust Oracles: - Document and catalog various approaches to designing robust oracles - Provide theoretical and statistical evidence that our oracles are indeed robust against extrema - Liquidation Prevention: - What are effective price differences (between liquidation and current) at which to perform top-up? - How much should funds should be used to top up? - What are effective refund mechanisms for the swapper who will pay for the top-up? #### Implementation: This component of the project involves producing implementations of each hook complete with testing and developer documentation. Here is a brief list of deliverables. - Implementations of each hook as Foundry projects - Unit and invariant testing for each hook - Gas cost esitmations - Developer documentation hosted on GitHub #### Exposition: As the final component of the project, we will produce expository writing on exactly what we built, why and how we built it, and how users can interact with it. We plan to do this both through a series of posts on [ UniswapHooks.com ](http://uniswaphooks.com/) and a narrative-oriented book in the style of Jeiwan's [ UniswapV3Book ](https://uniswapv3book.com/). We believe that by explaining the development process in book form, we will produce a coherent and consumable, well-exampled, end-to-end developer guide for future hook developers. Our book will guide readers through the intricacies of hook development, beginning with an in-depth exploration of the V4 hook architecture, followed by step-by-step tutorials on building these hooks, and finally delving into the rationale behind design decisions and descriptions of user interactions. As an appendix to the book, we will include all necessary mathematical exposition to understand and audit our work. ## Planning and Budgeting We are requesting the full budget for the RFP ($50,000) to support the research, development, and analysis of the proof of concept hooks, as well as the hosting and maintenance of the supporting developer book and documentation. #### Road Map (3 months) - Research (Completed by day 45) - Produce research describing our approach and implementation strategy - Answer the above-proposed questions for each hook - Hooks Implemented with Testing (Completed by day 60) - Hook implementing our internal fee distribution mechanism - Hook enabling an extrema resistant oracle using windsorization or another method - Hook implementing automatic liquidation prevention for a major money market like Compound or AAVE - Publication (Completed by day 90) - Posts for all hooks produced and published on [ UniswapHooks.com ](http://uniswaphooks.com/) - Hook developer book written and hosted - Supplemental mathematical appendices published ## Deliverables - Three research papers detailing the methodology and mathematical background for the implementation of the hook - Three hook implementations including unit and invariant testing and gas cost estimations - Developer documentation for each hook - Three posts on [UniswapHooks.com](http://uniswaphooks.com/), one for each hook - A comprehensive development book walking through the development process that discusses design decisions and user stories