# Dynamically Concentrated Liquidity Provision ## Introduction On-chain liquidity management is a fundamental building block for blockchain activity. Recently, greater demand for on-chain liquidity has surged, allowing decentralized exchanges (DEXs) to eclipse centralized exchanges (CEXs) in on-chain trading volume. However the [value proposition]((https://crocswap.medium.com/unraveling-a-puzzle-a-per-wallet-analysis-of-eth-usdc-liquidity-on-uniswap-v3-a00b0f836ac3)) for a liquidity provider (LP) on-chain is not great. LPing on DEXs also carries the risk of accruing impermanent loss, [which can eat into LP profits.](https://arxiv.org/pdf/2111.09192.pdf) ### Concentrated Liquidity [Concentrated liquidity](https://docs.uniswap.org/protocol/concepts/V3-overview/concentrated-liquidity), introduced in the [Uniswap V3 whitepaper](https://uniswap.org/whitepaper-v3.pdf), aims to increase the capital efficiency for LPs, thereby increasing the LP value proposition. Prospective liquidity providers consider three components when providing concentrated liquidity - liquidity price range, concentration density, and transaction fee. **Components** - What is the optimal price range to supply liquidity? - How concentrated is the liquidity within the price range? - What is the optimal transaction fee for providing liquidity? In the previous [Uniswap V2 paradigm](https://uniswap.org/whitepaper.pdf), liquidity was concentrated uniformly along the curve. Concentrated liquidity allows LPs to customize the liquidity density where they expect a lot of volume. Concentrated liquidity is similar to choosing different [energy densities](https://energyeducation.ca/encyclopedia/Energy_density) in the energy industry. Energy sources with larger densities of concentration will be more efficient than energy sources with smaller densities. Similarly, liquidity sources with higher concentration densities will also be more efficient. However, unlike the energy industry, where prices are derived based on market supply and demand, liquidity transaction prices are inefficient because they do not adapt to the market. ### Active Liquidity Management * How big is this industry? * What are the inherent flaws? * Has anyone made great liquidity vaults yet? - https://forum.gelato.network/t/release-g-uni-as-separate-dao-from-gelato-rebrand-it-as-arrakis-finance/364 - https://www.arrakis.finance/ // https://defillama.com/protocol/arrakis-finance - https://medium.com/arrakis-finance/uniswap-v3-position-gain-or-loss-for-specific-impermanent-loss-85372f7023fb - https://medium.com/arrakis-finance/selecting-a-uniswap-v3-range-da34d8741089 - Arrakis Finance just provides vault management overlays for DAOs or other people to execute strategies/set a management fee. - Charm Finance has a nifty rebalancing tool that uses range orders to rebalance. Rebalancing is not optimal, it's done on a fixed frequency point of view. - ### Kalman Filtering Kalman filtering is a lightweight [bayesian technique](https://towardsdatascience.com/kalman-filtering-an-intuitive-guide-based-on-bayesian-approach-49c78b843ac7) used to accurately estimate guidance, navigation, and control of vehicles such as aircraft and spacecraft. Kalman filtering is frequently used in time series analysis, signal processing, and econometrics. Due to low time complexity of the algorithmic implementation, Kalman filtering has a strong theoretical basis to facilitate dynamically concentrated liquidity. ### Kalman Filtering with Uni v3 pools One limitation of the on-chain Kalman filter is storage is expensive <font color=DC143C>cite source confirming/denying that you can get unexpensive data to compute rolling window </font>. What are the available resources that the Kalman filter can use as an input signal? The [uniswap v3 pool contract](https://docs.uniswap.org/protocol/reference/core/UniswapV3Pool#snapshotcumulativesinside) has a `mint()` function with iput parameters lower liquidity `tickLower`, `tickUpper`, liquidity `amount`, and calldata `data`. There is also a `snapshotsCumulativeInside` function that returns a snapshot of the tick cumulative, seconds per liquidity and seconds inside a tick range. The Kalman filter on a Uni v3 pool could adjust the concentration amount and range (note that "range" is technically an "area") based on the amount of time that the liquidity is within range. If there is a way to measure price fluctuation in the time period, then the Kalman filter could be used on the price fluctation period to estimate the liquidity range and tick size. The increase in tick size can be inversely related to the liquidity range. The larger the liquidity range gets, the larger (less granular) the tick size becomes. The shorter the liquidity range is, the larger the tick size should be. The fee is also inversely proportional to the liquidity range. If there is a large liquidity range, this means the LPs are supplying liquidity at a larger spread. The larger spread implies more uncertainty and warrants a higher fee. If there is a small spread, the fee should be lower. The smaller spread implies more certainty and thus a lower fee is warranted. ## Crocswap Crocswap is a [new CFMM type](https://crocswap-assets-public.s3.us-east-2.amazonaws.com/CrocSwap_Whitepaper.pdf) that allows LPs to use range orders to provide bounded, concentrated liquidity on a pre-defined grid of prices. As the token price moves across the grid, atomic liquidity is deployed within the range bound. LPs stop earning fees if the token price moves outside the concentrated liquidity range. Crocswap is the suggested CFMM DEX of choice because it contains built-in support for concentrated liquidity. ### Doug 8/4/22 - Uni v3 and Crocswap ticks use the same curve representation - 24-bit ticks map as the log_1.0001 of sqrt(price). Liquidity is sqrt(xy). Is the big difference between uni v3 and crocswap that liquidity ATOMICALLY goes up and down between the ticks? - Crocswap supports "non-reversible" concentrated liquidity which functions as a limit order. This is useful for liqudiity vaults bc for mass rebalancing, you can set a non-reversibe order that's just out of range. The limit order wil be atomically filled and unlike Uni v3 range order, you don't have to worry about removing liquidity quickly before price moves back ### Tick Grid Tick prices represent every possible price, divided evenly within the bounded range. Each tick change is equivalent to moving the price by one basis point (0.01%). Tick size is a pool parameter set at initialization and differs for different pool types. Setting a larger tick size increases the average gas efficiency of massive market-moving swaps. Selecting a smaller tick size increases liquidity concentration by increasing the granularity of range orders. LPs can add more liquidity for the same amount of capital at smaller tick sizes. Generally speaking, smaller tick sizes will equate to lower fees. ## Kalman Filtering Estimate Confidence Intervals as a Proxy for Tick Size Kalman filtering provides a lightweight method to estimate tick sizes and LP range bounds on-chain. Kalman filtering variables could be price and time. Run Kalman filtering on price and get a confidence interval for that price. The middle of that price is the mean price, and the confidence intervals are the concentrated liquidity range bound. If the confidence level decreases, this implies that the price is stabilizing around the mean price. There is sufficient liquidity for current market conditions, so liquidity concentration can start being relaxed by increasing the tick size. If the confidence level increases, market conditions are volatile and need more liquidity concentrated at this range. There is insufficient liquidity, so tick size decreases and thickens the concentration levels of liquidity. ### Velo <> Crocswap Some questions remain as to how Velo <> Crocswap <> Kalman Filtering will work together: * Will LPs be migrated from Velo to Crocswap if Kalman filtering is implemented? * Since Crocswap LPs are fungible tokens, Velo can integrate the ve(3,3) voting/bribing scheme to Crocswap LP tokens, adding a composable layer over Crocswap? ### Unused References 1. [Uniswap v3 subgraph](https://github.com/Uniswap/v3-subgraph/blob/bf03f940f17c3d32ee58bd37386f26713cff21e2/schema.graphql#L75) 2. [Query Uniswap v3 subgraph examples](https://github.com/atiselsts/uniswap-v3-liquidity-math) ### Readings - https://medium.com/@DeFiScientist/rebalancing-vs-passive-strategies-for-uniswap-v3-liquidity-pools-754f033bdabc - https://medium.com/charmfinance/introducing-alpha-vaults-an-lp-strategy-for-uniswap-v3-ebf500b67796 - https://medium.com/blockchain-development-notes/charms-alpha-vault-the-minimalist-uniswap-v3-lp-strategy-23a059c924b - https://medium.com/@DeFiScientist/uniswap-v3-a-quant-framework-to-model-yield-farming-returns-941a1600425e - https://lambert-guillaume.medium.com/on-chain-volatility-and-uniswap-v3-d031b98143d1#:~:text=Computing%20the%20Implied%20Volatility%20from%20Uniswap%20v3&text=Implied%20volatility%20ranges%20from%2049,and%20the%20ETH%2FsETH2%20pair. - ### TODO: - Clean up Uniswap v3 section, buid Uni v3 and crocswap into a monolith section that leads into backtesting. - Need stronger reasoning around rebalancing - Need to research minimalist cost function for Kalman Filter - MSE? - (bonus) How does higher fees incentivize changes to arbitrage MEV? Reference analysis of uniswap paper - (bonus) How much concentrated liq do you need to become more price-stable compared to ambient liq like uni v2? # Pre-Meeting KM and range order - "virtual" range order? - is it possible to have an overlap between range order/liquidity? I think this is what Doug was talking about with crocswap's limit orders where this can be done atomically? - Goal of Kalman filter is to figure out (with best confidence intervals) where majority of trading takes place and concentrate liquidity dynamically around that. # Meeting Notes - KF with range filters clients would be sushi (trident), uni v3, and crocswap. Would want liquidity vaults as ERC20s so its tradeable on Velo - Can do calculations for uni v3 ethereum, wrap in erc20 on optimism, and do calculations on optimism. - L2s will subsidize app builders - MVP - Spec and jupyter code - Uni v3 subgraph data, - Aloe is similar to v3 LP managers, can get in touch as needed - https://aloe.capital/ - jimmy is part of pseudo team, and pseudo is getting a lot of attention atm. AOL - artist owned liquidity. - psuedoswap v2 has support for concentrated liquidity, V3 for NFTs. Allows LPs to create 1/3 different pools (trade, buy only, sell only). NFT value gets aggregated with router contract. - has a different spin on range orders....bonding functions are applied to pool internal price - has product market fit bc a lot of trading occurs during the floors. - paying money only to pool directly, not artists or the platform - https://twitter.com/suhail/status/1555908332107362304