Optimizing DEX Liquidity Aggregation for Risk Assessment with Linear Programming

How we use linear programming to optimize routes for DEX liquidity aggregation that feeds the SmartLTV automated risk formula.

Eitan Katchka
B.Protocol
6 min readApr 18, 2024

--

Intro

The SmartLTV formula is used to automatically calculate the main risk parameter for lending platforms — Loan-To-Value (LTV) ratios. It does so based on objective risk-related data feeds that are available on-chain, combined with a risk level factor — a single numerical value that represents the subjective risk assumptions a protocol holds.

A crucial aspect of setting LTVs and assessing risk of an asset is evaluating its decentralized exchange (DEX) liquidity, as this parameter directly impacts the liquidation process and, consequently, the prevention of bad debt accumulation within DeFi lending platforms.

However, assessing liquidity across various DEXs and asset pairs poses a significant challenge. Liquidity often resides across multiple platforms and asset pairs, necessitating the optimization of routes for maximum efficiency, e.g. minimal slippage. To address this, we’ve developed a liquidity aggregation algorithm using linear programming. This algorithm aims to optimize liquidity routes, maximizing profits for liquidators while adhering to predefined constraints, including a maximum slippage which is determined by the lending platform’s liquidation bonus (aka liquidation penalty).

In this post, we’ll dive into the intricacies of aggregating liquidity for risk management. Additionally, we’ll explore the constraints imposed during optimization and provide insights into the development process. As with all our development work at B.Protocol, the code for SmartLTV and the liquidity aggregation algorithm is open-source, fostering transparency and collaboration within the DeFi community.

Liquidity Routing

The complexity of liquidity routing stems from the fragmented nature of DeFi liquidity. Liquidity is distributed across various DEXs, each with its own liquidity pools and pairs. When initiating a liquidation, it’s essential to identify the most efficient route to maximize profits while minimizing slippage, or maximizing the amount that can be liquidated up to the liquidation discount percentage the protocol sets which determines the max slippage level a liquidator will operate profitably.

We consider Direct Routes as routes that involve executing trades between a single pair of assets, even when these are fragmented across multiple DEXs. In the example graph below this relates to the $52.29m that can be swapped directly on the route of USDT → DAI.

Direct vs Indirect liquidity routes

In contrast, Indirect Routes leverage multiple swaps between different pairs, tapping into several liquidity pools to optimize trade execution. In the above example, this refers to the $52.63m that is swapped between USDT → USDC and then to DAI. Each of these steps accrue its own slippage.

However, navigating indirect routes requires sophisticated algorithms capable of identifying and capitalizing on liquidity opportunities across different pairs and platforms. For that reason we have used linear programming to find the optimal routes to aggregate the available liquidity in the market.

Linear Programming for Liquidity Aggregation

Linear programming is a mathematical technique used to optimize complex systems with linear constraints. It involves formulating an objective function to maximize or minimize, subject to a set of linear constraints. In the context of liquidity aggregation, linear programming enables us to optimize routes to maximize profits while adhering to predefined constraints, such as maximum slippage set by liquidation bonuses.

At its core, linear programming relies on linear relationships between variables, making it well-suited for modeling liquidity aggregation problems. By defining variables (asset pairs), constraints (slippage), and an objective function (maximizing profits for liquidators), we can leverage linear programming techniques to efficiently calculate the optimal routes of available liquidity across multiple DEXs and asset pairs that can be used during liquidations.

Taking the liquidation bonus into account, the algorithm aims to maximize the profits of the liquidator by checking all available pairs, calculating how much can be swapped in each route up to the liquidation bonus slippage, and checking the available DEX liquidity for each asset pair at 0.5% steps. We use all main blue-chip assets and blue chip stables to cover all possible routes.

We made a design decision to use 0.5% slippage steps, as for liquidations this is a sufficient granulation to get optimal results while capping our calculation efforts (other use cases can make more granulated calculations using smaller slippage steps).

We have set 2 sets of constraints to the algorithm -

  1. For every two intermediate pairs, for example DAI and USDC, x DAI is converted to y USDC, where the quantity and conversion rate of x is constrained by the actual DEX liquidity. For example, if the dex liquidity allows swapping 10m DAI in return to 9.95m USDC (0.5% slippage), and additional 20m DAI in return to 19.8m USDC (1% slippage), then we define the variables x_0.5, x_1, y_0.5, y_1, with y_0.5 = 0.995 * x_0.5 and x_0.5 <= 10m, y_1 = x_1 * 0.99 and x_1 <= 20m.
  2. Any liquidity that gets out of an asset must go in first.

The aggregator in work

Let’s look at a potential liquidation scenario and the way our liquidity aggregation algorithm works to optimize the routes involved.

The graph below plots the algorithm's output for one market — USDT/WETH.

In this example, the algorithm checked all the available routes between WETH and USDT, across all DEXs (on a single chain, in this example Ethereum mainnet), optimizing the routes for liquidating a max debt of 91.93m USDT with WETH collateral.

The liquidator had repaid the 91.93m USDT in return for discounted WETH collateral that will be seized from the platform and has to be swapped back into USDT. If the rebalancing to USDT won’t be profitable the liquidator won’t operate the liquidation, so the maximum tolerated slippage the liquidator would operate under is the liquidation bonus discount he got on the seized collateral.

The algorithm calculates the maximum amount of WETH that can be swapped back to USDT under the above constraints with a max slippage of 7%, which is the liquidation bonus set by the platform in this example.

In each step, the algorithm checks the available liquidity in each route for up to 7% slippage on the last dollar worth of WETH. In the above example, we can see that the direct route between WETH → USDT has “only” $42.41m available liquidity, less than half of the total amount. Another $45.29m is going through the WETH → USDC → USDT route, and more is going through WBTC and DAI. It’s worth noting that some of the WBTC routes is swapped directly for USDT, but some are going through DAI and USDC and only then into USDT.

The numbers shown in the graph above presents the aggregated liquidity in each of these routes, across all main DEXs, as it was calculated for slippage rates of 0.5% up to 7% in 0.5% steps (0.5, 1, 1.5… 7).

Conclusion

Optimizing liquidity aggregation for risk assessment is a multifaceted endeavor that requires sophisticated algorithms and careful consideration of various constraints. By leveraging linear programming techniques, we developed an algorithm that calculates the maximum profits for liquidators while maintaining risk exposure within pre-set limits.

Moving forward, continued research and development in liquidity aggregation will be essential to enhance risk management practices within the DeFi ecosystem. Future work can focus on automating the discovery phase of new liquidity pools and new DEXs that can be added to the calculated routes of available liquidity.

About B.Protocol

B.Protocol has been building open-source protocols and infrastructure for risk mitigation and assessment for the DeFi ecosystem since 2020. Through our research arm, RiskDAO, and its novel risk framework, we have supported over a dozen DeFi protocols with risk analysis, research, audits, and monitoring. Our Risk Oracle, together with the SmartLTV formula, and the Risk Level Index automate the process of setting risk parameters for lending platforms in a transparent way, building the next generation of DeFi risk management infrastructure.

Website | Twitter | Discord | Medium | Github

--

--