<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Deeplink on Medium]]></title>
        <description><![CDATA[Stories by Deeplink on Medium]]></description>
        <link>https://medium.com/@deeplinknetwork?source=rss-927ca3b92f78------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2B1PNV_NY3jMZdII38ts7g.png</url>
            <title>Stories by Deeplink on Medium</title>
            <link>https://medium.com/@deeplinknetwork?source=rss-927ca3b92f78------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 26 May 2026 10:09:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@deeplinknetwork/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Eta X V1.2: Speed, Scale, and Efficiency]]></title>
            <link>https://medium.com/@deeplinknetwork/eta-x-v1-2-speed-scale-and-efficiency-56008f63b5ca?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/56008f63b5ca</guid>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Tue, 04 Apr 2023 03:58:42 GMT</pubDate>
            <atom:updated>2023-04-19T11:43:36.787Z</atom:updated>
            <content:encoded><![CDATA[<p>By: Jack Lodge</p><p>Originally published in Deeplink Labs <a href="https://medium.com/deeplink-labs/eta-x-v1-2-speed-scale-and-efficiency-4b21b4dee1b">Publication</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/0*KyutIPYGOUfeyL8l.png" /></figure><p>The core concepts and motivation behind Eta X are explored in great detail in our previous article:</p><p>However, we will briefly summarize Eta X V1 for readers who are unfamiliar with our progress.</p><p>The growth of decentralized finance has led to an increase in the number of DEXs, which provide a trustless solution for trading crypto assets. Eta X stands out by offering a platform that is resilient to manipulation and provides the best possible trade routes as calculated by reverse engineering publicly available smart contracts and their respective AMM conservation functions, without engaging in any biased practices.</p><p>Eta X is a decentralized exchange (DEX) aggregator and smart order routing (SOR) tool developed by Deeplink that can help traders find the most profitable trade routes by considering factors such as price impact, liquidity, and gas fees. Eta X V1 uses a modified Djisktra algorithm to traverse and validate paths from the token being sold to the token being purchased.</p><p>To evaluate the profitability of each route, Eta X V1 uses reverse-engineered conservation functions from each exchange to calculate a minimum return and price impact percentage. The algorithm returns a list of recommended routes for a user in descending order of highest returns. While early versions of Eta X are simply analytics tools to inform traders, the ultimate goal of Eta X is to facilitate the execution of profitable trade opportunities via wallet and smart contract integration.</p><p>Eta X V1 proved to be an effective proof-of-concept, as such, we have since been expanding in several areas such as the inclusion of several new DEXs, optimizing data collection, order splitting, and mass query APIs.</p><p>One of the most noticeable improvements from Eta X V1.0 to V1.2 was the introduction of liquidity pool data caching. Early proof-of-concept iterations of Eta X were loaded down by ad-hoc GQL queries to subgraphs hosted by The Graph, this meant that a user’s query would trigger several queries on Eta X’s backend, essentially double-handling data retrieval. While this was fine for early testing purposes, Eta X is growing, and thus its data collection and caching functionality must mature along with it.</p><p>Previous Data Collection Method</p><p>Caching is a technique that involves storing frequently accessed data in a temporary storage location, such as in memory or on disk, to reduce the time and resources required to retrieve that data from its original source. In many cases, caching is significantly faster than ad hoc retrieval, which involves accessing data directly from its original source every time it is needed.</p><p>In the context of Eta X, caching can provide significant benefits by reducing the time and resources required to retrieve pool data from decentralized exchanges (DEXs). Since Eta X relies on up-to-date pool data to perform smart order routing, the speed at which this data can be accessed and processed is critical to both the application’s performance and user experience.</p><p>Eta X V2 and V3 now keep a running cache of all liquidity pools contained within each supported DEX in the backend. This way each time a user queries Eta X, the backend must only retrieve the relevant data via filtering it out of this pre-existing cache, rather than fetching it from third-party sources on the fly. This addition has removed Eta X’s greatest bottleneck, and has reduced data retrieval time down from a few seconds to less than one second, while still retaining comparable data granularity and currentness.</p><p>This is achieved by creating a hash map of all liquidity pools from supported DEXs to their respective relevant datapoints. Pool data is updated continuously and asynchronously by querying the relevant subgraphs for each DEX. The pools are refreshed periodically at a rate that is close to the limit of The Graph’s rate limiters, but still within the limits to ensure that the updates are successful. While this does mean that pools are no longer at the time of an end user’s request, the time difference between cache refreshes is negligible for the vast majority of use-cases. However, rest assured that we are working on even faster methods involving circumventing The Graph entirely, and leveraging direct on-chain queries and datasets provided by L3 Atom.</p><p>New Data Collection Method</p><p>In line with the aforementioned data filtering process that is triggered upon retrieving relevant liquidity pool data from the cache, we have also introduced the ability to filter the DEXs on which a user would like to identify trade routes across. The specific reasons why a user would like to limit their trades to a given exchange may vary from protocol loyalty to ease of integration, for instance, while one DEX may have fantastic liquidity, it may not be so easily integrated with your DApp as another. In the frontend GUI, a user can simply tick or untick the DEXs they wish to route through; when querying the API, a user can simply pass a list of strings including the DEXs they wish to include, an empty list indicates all DEXs shall be queried.</p><p>This filtering process is achieved by retrieving relevant liquidity pools by token ID from the greater hash map of liquidity pools. Pools are gathered in order of their most relevant metric for trade optimality, such as reserves or liquidity, and are merged into a smaller list of pools to be used in SOR for that particular user’s query. A DEX and its respective liquidity pools are included or excluded in this filtering process depending on whether or that DEX is selected in the user’s query.</p><p>You can try Eta X NonML V2 yourself at the following link:<br><a href="https://etax.deeplink.network/">https://etax.deeplink.network</a></p><p>The source code for Eta X can be found here:<br><a href="https://github.com/Deeplink-Network/EtaX-V1">https://github.com/Deeplink-Network/EtaX-V1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56008f63b5ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Eta X APIs: Enhancing Decentralized Trading Integration with Open-source SOR and DEX Aggregator…]]></title>
            <link>https://medium.com/@deeplinknetwork/eta-x-apis-enhancing-decentralized-trading-integration-with-open-source-sor-and-dex-aggregator-24af063f3e0?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/24af063f3e0</guid>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Tue, 04 Apr 2023 03:58:06 GMT</pubDate>
            <atom:updated>2023-04-19T11:46:20.434Z</atom:updated>
            <content:encoded><![CDATA[<h3>Eta X APIs: Enhancing Decentralized Trading Integration with Open-source SOR and DEX Aggregator Endpoints</h3><p>By: Jack Lodge</p><p>Originally published in Deeplink Labs <a href="https://medium.com/deeplink-labs/eta-x-apis-enhancing-decentralized-trading-integration-with-open-source-sor-and-dex-aggregator-12e66517c58">Publication</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/0*HcPfrva_QEV7lIpF.png" /></figure><p>The core concepts and motivation behind Eta X are explored in great detail in our previous article:</p><p>However, we will briefly summarize Eta X V1 for readers who are unfamiliar with our progress.</p><p>The growth of decentralized finance has led to an increase in the number of DEXs, which provide a trustless solution for trading crypto assets. Eta X stands out by offering a platform that is resilient to manipulation and provides the best possible trade routes as calculated by reverse engineering publicly available smart contracts and their respective AMM conservation functions, without engaging in any biased practices.</p><p>Eta X is a decentralized exchange (DEX) aggregator and smart order routing (SOR) tool developed by Deeplink that can help traders find the most profitable trade routes by considering factors such as price impact, liquidity, and gas fees. Eta X V1 uses a modified Djisktra algorithm to traverse and validate paths from the token being sold to the token being purchased.</p><p>To evaluate the profitability of each route, Eta X V1 uses reverse-engineered conservation functions from each exchange to calculate a minimum return and price impact percentage. The algorithm returns a list of recommended routes for a user in descending order of highest returns. While early versions of Eta X are simply analytics tools to inform traders, the ultimate goal of Eta X is to facilitate the execution of profitable trade opportunities via wallet and smart contract integration.</p><p>In addition to the Eta X user interface, we have also developed a public API endpoint through which users may integrate Eta X’s services directly into their own projects and introduces the ability to make queries en masse. The API currently has two endpoints:</p><h3>Order Router</h3><h3>Order Router Split</h3><p>Eta X API is a powerful tool that can revolutionize the way decentralized trading is integrated into various applications. But why is it so useful, and why should traders and developers care?</p><p>The Eta X APIs are designed to be easy to integrate into various applications, even for those who are not proficient in programming. The API documentation is interactive and provides users with easy-to-use tools to construct API calls and get results without writing any code. This can help to reduce the technical barriers to entry for decentralized trading and enable more people to access the benefits of DeFi.</p><p>By leveraging these APIs, traders and developers stand to benefit from accessing multiple DEXs and calculating the most profitable trade routes, traders can unlock new possibilities and increase their chances of making profitable trades. For instance, users can introduce the functionality of Eta X to a DApp for distribution, or incorporate its insights into their own trading tools.</p><p>The API is built on open-source smart order routing and DEX aggregator endpoints, which means that the results generated by Eta X API are transparent and explainable. This helps to ensure that traders can make informed decisions based on objective data and avoid any potential conflicts of interest or manipulation. As an open-source and community-driven project that reflects the needs and preferences of its users, developers can contribute to its development and make it even more powerful and versatile. This prospect is enhanced via user voting to determine which DEXs to support, making it a community-driven project that reflects the needs and preferences of its users.</p><p>In this video, we take a closer look at the API documentation for Deeplink’s Eta X project. The API documentation is interactive and allows users to easily construct API calls and get results without writing any code.</p><p>As shown in the demonstration, users can enter the queries they wish to make without needing any prior programming knowledge, the site will then generate shell commands or code snippets for NodeJS, Ruby, PHP, and Python. This is intended to provide both traders and developers with an easy way to integrate our services in a more lightweight way, as opposed to interacting with them via our own frontend interface.</p><p>The documentation and playground for Eta X’s API can be found here.</p><p><a href="https://restapi.deeplink.network/reference/order_router-1">https://restapi.deeplink.network/reference/order_router-1</a></p><p>You can try the Eta X NonML V3 app yourself at the following link:<br><a href="https://etax3.deeplink.network/">https://etax3.deeplink.network/</a></p><p>The source code for Eta X NonML V1 can be found here:<br><a href="https://github.com/Deeplink-Network/EtaX-V1">https://github.com/Deeplink-Network/EtaX-V1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24af063f3e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Eta X V1.3: Expanded DEX Support and Larger Trades With Order Splitting]]></title>
            <link>https://medium.com/@deeplinknetwork/eta-x-v1-3-expanded-dex-support-and-larger-trades-with-order-splitting-d306c52ccaef?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/d306c52ccaef</guid>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Tue, 04 Apr 2023 03:12:21 GMT</pubDate>
            <atom:updated>2023-04-19T11:49:01.397Z</atom:updated>
            <content:encoded><![CDATA[<p>By: Jack Lodge</p><p>Originally published in Deeplink Labs <a href="https://medium.com/deeplink-labs/eta-x-v1-3-expanded-dex-support-and-larger-trades-with-order-splitting-91ac0fe2cd70">Publication</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oc8qUgtLjhmVx1Zj" /></figure><p>The core concepts and motivation behind Eta X are explored in great detail in our previous article:</p><p>However, we will briefly summarize Eta X V1 for readers who are unfamiliar with our progress.</p><h3>Why is Eta X Important?</h3><p>The growth of decentralized finance has led to an increase in the number of DEXs, which provide a trustless solution for trading crypto assets. Eta X stands out by offering a platform that is resilient to manipulation and provides the best possible trade routes as calculated by reverse engineering publicly available smart contracts and their respective AMM conservation functions, without engaging in any biased practices.</p><h3>What is Eta X?</h3><p>Eta X is a decentralized exchange (DEX) aggregator and smart order routing (SOR) tool developed by Deeplink that can help traders find the most profitable trade routes by considering factors such as price impact, liquidity, and gas fees. Eta X V1 uses a modified Djisktra algorithm to traverse and validate paths from the token being sold to the token being purchased.</p><p>To evaluate the profitability of each route, Eta X V1 uses reverse-engineered conservation functions from each exchange to calculate a minimum return and price impact percentage. The algorithm returns a list of recommended routes for a user in descending order of highest returns. While early versions of Eta X are simply analytics tools to inform traders, the ultimate goal of Eta X is to facilitate the execution of profitable trade opportunities via wallet and smart contract integration.</p><h3>Order Splitting</h3><p>One of the largest new features introduced to Eta X since our last publication is order splitting, a feature that facilitates trades that are larger than could be executed within a single pool or even single path of swaps across pools.</p><p>For example, if you wanted to exchange 7,500,000 AGIX (approximately $36,66,849.18 USD) for the maximum amount of AGIX, previous iterations of Eta X would suggest trading in whole quantities across Uniswap V3:</p><p>This trade route would incur an averaged 7.37% price impact, a gas fee of 0.00147753598 ETH (approximately $2.19 USD), and a total minimum return of 485,955 LINK (approximately $3,563,750.99 USD). Eta X however, can do better.</p><p>With our new order-splitting feature, Eta X will suggest breaking this trade into smaller trades. In effect, this mitigates the effects of price impact by making use of the distributed liquidity of DeFi.</p><p>By breaking the trade down into two smaller trades, we can now observe an average price impact of 5.47%, a cumulative gas fee of 0.00247769267 ETH (approximately $4.38 USD), and a total minimum return of 497,211 LINK (approximately $3,646,296.87 USD) — in this case, saving you $82,545.88 USD in value that would otherwise have been lost to price impact, and this is on top of the savings already made by the old version of the router.</p><p><strong>How it Works</strong></p><p>Essentially, Eta X takes the routes identified, and if any exceed a given price impact threshold of 10%, it will then calculate a maximum amount that can be put into the best route until the price impact threshold is reached, then it will repeat with the next, and so on. You can think of this as though the trade were being poured into a bucket, once that bucket fills, it begins to spill over into the next bucket, and so on, until the amount traded is distributed across as many buckets as needed.</p><p>The function works by starting at the end of the path (i.e., the pool containing the buy token) and calculating the amount of sell token that can be swapped in that pool without exceeding the maximum price impact limit. It then moves back to the previous pool in the path and calculates the amount of sell token needed to swap the amount calculated in the previous pool, as well as the amount of the sell token in the current pool required to not break the price impact limit. The minimum of these two values is taken as the maximum amount of sell token that can be swapped in the current pool without exceeding the price impact limit. This process is repeated until the start of the path (i.e., the pool containing the sell token) is reached.</p><p>The function uses the relevant constant product formula to calculate the expected price impact of swapping the sell token for the buy token in each pool. It also takes into account any adjustment needed for Sushiswap subgraphs.</p><p>Once the maximum amount of sell token that can be swapped in the path without exceeding the price impact limit is determined, the function returns this amount as the maximum sell amount for the order. If the maximum sell amount is less than the total sell amount, the order is split into multiple smaller orders and this process is repeated until the entire sell amount is swapped through the path.</p><h3>Additional DEX Support</h3><p>In the Time since our last publication we have expanded Eta X’s DEX aggregation functionality to include:</p><p>The addition of Uniswap V3 and Curve in particular ( <a href="https://www.coingecko.com/en/exchanges/decentralized">the two top DEXs by volume according to CoinGecko</a>) has expanded the effectiveness of Eta X’s SOR capabilities dramatically, by enabling the inclusion of some of DeFi’s most liquid pools.</p><h3>DEX Community Suggestions</h3><p>Deeplink’s R&amp;D team is vigilantly searching for the most opportune DEXs to include within the scope of Eta X, dYdX, and Hashflow being targets of interest to us right now. However, we would like to invite you, the community, to have a say in what DEXs you would like to see included in Eta X. As such, we have included a community submissions box for DEX suggestions in which a user can vote on a set of contenders from a list of considerations, or suggest a completely new DEX by entering its name or a contract address such as its deployer or factory contract.</p><p>To use this feature, simply click the ‘+ Add New’ button located under the tickboxes for DEX filtering.</p><p>You can try the Eta X NonML V3 app yourself at the following link:<br><a href="https://etax3.deeplink.network/">https://etax3.deeplink.network/</a></p><p>The source code for Eta X NonML V1 can be found here:<br><a href="https://github.com/Deeplink-Network/EtaX-V1">https://github.com/Deeplink-Network/EtaX-V1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d306c52ccaef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Eta X: Democratizing Decentralized Trading with Community-Driven DEX Integration]]></title>
            <link>https://medium.com/@deeplinknetwork/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-97e8b34ebf7b?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/97e8b34ebf7b</guid>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Tue, 04 Apr 2023 03:11:44 GMT</pubDate>
            <atom:updated>2023-04-19T11:47:11.486Z</atom:updated>
            <content:encoded><![CDATA[<p>By: Jack Lodge</p><p>Originally published in Deeplink Labs <a href="https://medium.com/deeplink-labs/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-5b055e81c882">Publication</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/0*Uon8fgM_z2TYd4Q9.png" /></figure><p>· <a href="https://medium.com/deeplink-labs/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-5b055e81c882#b989">Eta X V1 Recap</a><br>· <a href="https://medium.com/deeplink-labs/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-5b055e81c882#0aef">Why is Eta X Important?</a><br> ∘ <a href="https://medium.com/deeplink-labs/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-5b055e81c882#839b">What is Eta X?</a><br> ∘ <a href="https://medium.com/deeplink-labs/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-5b055e81c882#e0d3">DEX Community Suggestions</a><br> ∘ <a href="https://medium.com/deeplink-labs/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-5b055e81c882#5c89">Why is Community-driven DEX Expansion Important?</a><br>· <a href="https://medium.com/deeplink-labs/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-5b055e81c882#90d2">Try It Out Yourself</a></p><p>The core concepts and motivation behind Eta X are explored in great detail in our previous article:</p><p>However, we will briefly summarize Eta X V1 for readers who are unfamiliar with our progress.</p><p>The growth of decentralized finance has led to an increase in the number of DEXs, which provide a trustless solution for trading crypto assets. Eta X stands out by offering a platform that is resilient to manipulation and provides the best possible trade routes as calculated by reverse engineering publicly available smart contracts and their respective AMM conservation functions, without engaging in any biased practices.</p><h3>What is Eta X?</h3><p>Eta X is a decentralized exchange (DEX) aggregator and smart order routing (SOR) tool developed by Deeplink that can help traders find the most profitable trade routes by considering factors such as price impact, liquidity, and gas fees. Eta X V1 uses a modified Dijkstra algorithm to traverse and validate paths from the token being sold to the token being purchased.</p><p>To evaluate the profitability of each route, Eta X V1 uses reverse-engineered conservation functions from each exchange to calculate a minimum return and price impact percentage. The algorithm returns a list of recommended routes for a user in descending order of highest returns. While early versions of Eta X are simply analytics tools to inform traders, the ultimate goal of Eta X is to facilitate the execution of profitable trade opportunities via wallet and smart contract integration.</p><h3>DEX Community Suggestions</h3><p>Deeplink’s R&amp;D team is vigilantly searching for the most opportune DEXs to include within the scope of Eta X, dYdX, and Hashflow being targets of interest to us right now. However, we would like to invite you, the community, to have a say in what DEXs you would like to see included in Eta X. As such, we have included a community submissions box for DEX suggestions in which a user can vote on a set of contenders from a list of considerations, or suggest a completely new DEX by entering its name or a contract address such as its deployer or factory contract.</p><h3>Why is Community-driven DEX Expansion Important?</h3><p>With hundreds of DEXs currently available and more popping up each day, it can be challenging for users to navigate and access the liquidity they need. This is one of the biggest draws of DEX aggregators and smart order routers, tools that enable you to compare and route orders across multiple DEXs seamlessly.</p><p>With so many DEXs out there, some thought is required when selecting which ones to support. Traditionally, these decisions are made internally by the aggregator’s team or management, generally based on factors such as liquidity, volume, demand, and ease of integration. However, this approach can be subjective, centralized, and biased toward certain projects or stakeholders.</p><p>To address this, we are embracing a community-driven approach to expanding our ensemble of supported DEXs by inviting you to vote on which to add next. We believe that by democratizing the decision-making process we are enabling greater transparency, inclusivity, and fairness to the ecosystem — we want to hear your thoughts, and your favorite DEXs based on your own research, experience, and priorities.</p><p>Furthermore, this is in strong alignment with Eta X’s core value of agnosticism, we intend to foster a platform free of bias, completely free of the hidden mechanisms laced into other aggregators. In the true spirit of Web 3, we wish to embrace the wider community and to foster innovation, competition, and diversity from across this decentralized ecosystem to bring you the best opportunities for liquidity, pricing, and efficiency.</p><p>You can try the Eta X NonML V3 app yourself at the following link:<br><a href="https://etax3.deeplink.network/">https://etax3.deeplink.network/</a></p><p>Don’t forget to vote on your DEX of choice! To use this feature, simply click the ‘+ Add New’ button located under the tickboxes for DEX filtering, and vote for your selection either from the list of our favorites or by entering your own by typing in its name or pasting a relevant contract address.</p><p>The source code for Eta X NonML V3 can be found here:<br><a href="https://github.com/Deeplink-Network/EtaX-V1">https://github.com/Deeplink-Network/EtaX-V1</a></p><p><em>Originally published at </em><a href="https://medium.com/deeplink-labs/eta-x-democratizing-decentralized-trading-with-community-driven-dex-integration-5b055e81c882"><em>https://medium.com</em></a><em> on April 4, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97e8b34ebf7b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Eta X V1: Conservation Function and Pathfinding-based DEX Aggregation and Smart Order Routing]]></title>
            <link>https://medium.com/@deeplinknetwork/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-44dbc794ab7d?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/44dbc794ab7d</guid>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Wed, 08 Feb 2023 02:22:46 GMT</pubDate>
            <atom:updated>2023-02-08T02:22:46.657Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>by: Jack Lodge</strong></p><p><a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac">Original post could be found here</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/0*w1DxmcuomHEVawxo.png" /></figure><h3>Contents</h3><p>· <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#8c97">Contents</a><br>· <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#a8e6">Introduction</a><br>· <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#280f">What is Eta X?</a><br>· <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#bef0">Why is Eta X Important for Crypto’s Future?</a><br>· <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#9961">How Does Eta X Work?</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#6130">On-chain Data Collection</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#64f7">Liquidity Pools and Swap-hops as Bidirectional Network Graphs</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#bbe2">Pathfinding Algorithms for Order Routing</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#6947">Price Impact Calculation and Route Selection</a><br>· <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#18db">Future Work</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#aea3">Expanding DEX Support</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#62a7">Data Collection Optimization</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#484e">Order Splitting</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#f751">Larger Graphs and More Powerful Pathfinding Algorithms</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#0790">Predictive Analytics</a><br>∘ <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#3a9c">Order Execution</a><br>· <a href="https://medium.com/deeplink-labs/eta-x-v1-conservation-function-and-pathfinding-based-dex-aggregation-and-smart-order-routing-2de4ab096eac#d916">References</a></p><h3>Introduction</h3><p>This article outlines Deeplink’s latest project, Eta X. This version of Eta X is a proof of concept intended to demonstrate our smart order routing techniques but can be used analytically to inform a user of optimal trade paths, and can even highlight arbitrage opportunities across pools and exchanges. Some preliminary reading on the subjects discussed below can be found in our Medium publications:</p><ul><li><a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e">Smart Order Routing: A Comprehensive Guide</a></li><li><a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796">Pathfinding Algorithms for DEX Aggregation and Smart Order Routing</a></li><li><a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c">Machine Learning Applications in DEX Aggregation and Smart Order Routing</a></li></ul><h3>What is Eta X?</h3><p>Eta X is an open-source initiative to build an agnostic decentralized exchange (DEX) aggregator and price discovery engine/smart order router (SOR). It has been designed from first principles to be universal, scalable, and unbiased via an adaptable method of reverse engineering the conservation functions employed in the automated market maker (AMM) algorithms of DEX liquidity pools.</p><p>A live demonstration of Eta X V1 can be found here:</p><h3><a href="https://etax.deeplink.network/">EtaX v1</a></h3><h3><a href="https://etax.deeplink.network/">Eta X is an open-source initiative to build an agnostic price discovery engine, smart order router (SOR) framework for…</a></h3><p><a href="https://etax.deeplink.network/">etax.deeplink.network</a></p><p>The source code can be found here:</p><h3><a href="https://github.com/Deeplink-Network/EtaX-V1">GitHub — Deeplink-Network/EtaX-V1</a></h3><h3><a href="https://github.com/Deeplink-Network/EtaX-V1">You can’t perform that action at this time. You signed in with another tab or window. You signed out in another tab or…</a></h3><p><a href="https://github.com/Deeplink-Network/EtaX-V1">github.com</a></p><h3>Why is Eta X Important for Crypto’s Future?</h3><p>As decentralized finance (DeFi) grows, so does the number of exchanges on which blockchain-based assets can be traded. DEXs are seeing more and more adoption as the community trends towards trustless solutions in light of a string of centralized sources of fraud throughout the crypto ecosystem in recent years. In alignment with the philosophy of blockchains, DEXs address this issue by providing platforms that are resilient to manipulation by malicious insiders via distributed systems, algorithmic market making, open-source code review, and the Darwinian-esque selection of trustworthy and useful tools by the free market.</p><p>A DEX aggregator is a service that brings liquidity pools from multiple DEXs together in one platform and is generally packaged with some form of SOR. These aggregators can be compared analogously with services from other industries such as <a href="https://www.google.com/travel/flights">Google Flights</a> or <a href="https://digg.com/">Digg</a>. However, (much like many aggregators in other industries) DEX aggregators are generally not agnostic and have built-in biases that reward certain parties involved. This often takes the form of routing orders through liquidity pools which have reward schemes set up in favor of the protocol doing the routing, or via agreements with DEXs who wish to have more volume through their pools (which is proportional to revenue from fees).</p><p>Eta X strives to provide entirely unbiased DEX aggregation and SOR by abstaining entirely from engaging in such practices. Eta X will always provide the best possible routes as calculated by reverse engineering publicly available smart contracts and their respective AMM conservation functions.</p><h3>How Does Eta X Work?</h3><p>At its core, Eta X V1 is a pathfinding algorithm that uses AMM conservation functions to identify the best prices for exchanging one crypto asset for another by trading across a selection of routes across DEXs. Conservation functions are the largest factor in determining the profitability of trading within a given liquidity pool, to surmise, the conservation function determines the percentage by which you will change the price of the assets in the pool when you make a trade in it, this percentage change incurs a loss on your end. This iteration of the service is primarily designed around the XYK conservation function but has been designed with modularity and scalability in mind, and other functions such as constant mean product are being integrated presently.</p><h3>On-chain Data Collection</h3><p>Version 1 of Eta X relies on subgraphs provided by <a href="https://thegraph.com/">The Graph</a>, an indexing protocol for querying blockchain networks (Ethereum, in this case). Currently, the service queries Uniswap V2 and SushiSwap, as they are similar in design and both use the XYK conservation function.</p><p>A user inputs two crypto assets, one which they wish to sell, and one which they wish to buy, along with the amount that they wish to sell. Queries are then sent which find pools that correspond to the relevant permutations of the two given assets, these permutations can be seen in the table below, where pools contain token0 and token1, and we are selling token A for token B:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RA2k2NZJziB4jDhM.png" /></figure><p>Table 1: Token Query Permutations</p><p>These tokens are queried and sorted for values such as liquidity in USD or reserves in USD, as these are generally a somewhat decent indicator of their resistance to price impact and volatility-induced slippage. It is worth noting that Not Token A: Not Token B and Not Token B: Not Token A are not included but likely will be in future iterations, as they were found to increase query times without generally providing better paths. It may be the case that some set theory principles may be employed to collect pools that are not a given token but are also selected from the list of ‘not given tokens’ retrieved in queries 1 and 3, however, this complexity would also increase query times — further testing is required to evaluate the effectiveness of these approaches.</p><h3>Liquidity Pools and Swap-hops as Bidirectional Network Graphs</h3><p>One of the best ways to negate losses to price impact is to distribute your order across liquidity pools. This can be accomplished either by breaking your order down into smaller parts and trading smaller amounts through multiple pools, by using intermediate pools to bridge a trade across multiple pools (sometimes called swap-hops), or a combination of both.</p><p>Swap -hops are best understood by a real-world example, swapping UNI for LINK can be done directly in one pool if such a pool exists on the given exchange. Assuming this pool exists, it may have low liquidity, which would result in large losses to price impact — low liquidity/reserves can also be indicative of volatility, a primary factor in losses to slippage. If there exist at least two pools containing at least one of the two given assets, a swap-hop can potentially facilitate a trade route that is less susceptible to both price impact and slippage. For example, WETH-UNI and WETH-LINK pools could be used to make a trade as follows:</p><ol><li>Sell UNI for WETH in the first pool</li><li>Take that WETH and sell it for LINK in the second pool</li></ol><p>In this case, if we were to swap 1,000 UNI ($6256) for LINK directly in the Uniswap UNI-LINK pool we would incur a 9.7% loss to price impact, receiving 87 LINK and paying 0.0007 ETH in gas fees. Whereas the route traversing UNI-WETH → WETH-LINK across Uniswap pools only incurs a 0.075% price impact, receiving 97.5 LINK and paying 0.0014 ETH in gas fees. In this real-world scenario, Eta X has saved the user $68.86 worth of UNI.</p><p>In order to find these swap-hop routes, Eta X creates a bidirectional network graph of relevant liquidity pools, where each node in the graph represents a liquidity pool. Each pool is then connected to any other pool with one or more assets in common, for example, a DAI-USDC pool would connect to a DAI-WETH pool, but not a WETH-UNI pool. This is explained in more depth in our article on <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796">pathfinding for SOR</a>, the following graph depicts the graph derived from a set of pools queried from a USDC and WETH trade query, pools in this visualization are sized by their total locked value (TVL, a strong indicator of resistance to price impact and slippage):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Yfay6IrR2ywjg7gt" /></figure><h3>Pathfinding Algorithms for Order Routing</h3><p>Network graphs are an optimal representation of data for pathfinding optimization, as such, representing liquidity pools in this fashion opens our system up to the many well-established pathfinding algorithms of graph theory. While the number of DEXs factored into calculations are limited to single digits, simple-yet-effective pathfinding algorithms such as Bellman-Ford suffice in negligible runtime. Despite the O(V3) time complexity for our near V2 worst-case scenario, the graphs are small enough to negate this impact.</p><p>However, simply implementing the Bellman-Ford algorithm in its original form raises some issues with path validity. For example, while the traditional Bellman-Ford algorithm may see a path selling USDC for WETH along the route of pools USDC-DAI → USDC-LINK → LINK-WETH as valid, it is not actually valid for our purposes, as this path represents the following sequence of swaps:</p><ol><li>Sell USDC for DAI in the USDC-DAI pool</li><li>Take that DAI and sell it in the USDC-LINK pool — here we see our issue, this pool does not accept DAI</li></ol><p>Therefore, we have to include some extra checks to ensure path validity. In this system, a path is valid if the token being sold in the current pool must exist in the next pool, and if the final output token is the token being purchased. A single node is also considered a valid path if it contains both of the assets involved in the swap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/0*yCP_kq1iXId28vhP" /></figure><p>The algorithm for this process is as follows:</p><ol><li>Enter the first node with the asset being sold</li><li>Take the partner asset of that node to the next node, if the partner symbol does not exist in that node, the path is invalid</li><li>Repeat this until reaching the final node, if the final output asset is not the asset being purchased, the path is invalid</li></ol><h3>Price Impact Calculation and Route Selection</h3><p>Now that we have created a graph and a modified Bellman-Ford algorithm to traverse and validate paths from the token being sold to the token being bought, we need to evaluate the profitability of each path identified. While there are certainly other factors that contribute to the profitability of an order route, price impact, slippage, and gas fees are two of the most important. As such, for each exchange, we have reverse-engineered the AMM conservation function to calculate a minimum return and price impact percentage and grab gas fee estimates directly from the Ethereum network.</p><p>For DEXs using the XYK conservation in their pools such as Uniswap V2 and SushiSwap V2, assets are priced via the following equation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*erfboPoM5FRCN5k5.png" /></figure><p>Where <em>x </em>is the reserves of token0, <em>y </em>is the reserves of token1 and <em>k </em>is a constant. Let’s say we want to swap token <em>x </em>for token <em>y</em>, we can use the following equations to determine the impacted price, minimum return, and price impact percentage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_jUsgjX11OkMGlOL.png" /></figure><p>Where <em>x_new</em> is the new reserve of token <em>x </em>after depositing <em>x_input</em>, <em>y_new </em>is the new reserve of token <em>y </em>after adjusting to maintain the constant <em>k</em>, <em>r_min</em> is the minimum amount of token <em>y </em>returned from the swap, <em>p_y, </em>and <em>p_y_new </em>are the prices of token <em>y </em>before and after the swap respectively, and <em>P </em>is the price impact percentage.</p><p>Using these equations our pathfinding algorithm is modified further to calculate the price impact of each swap in each route. These calculations are done after the simple paths are calculated, so as to avoid calculating price impact for invalid routes, which means that even though we are doubling over the small list of valid routes, we are not wasting computations on price impact calculations for the many invalid routes.</p><p>These routes are then sorted by returns and a list of recommended routes is returned to a user in descending order of highest returns.</p><h3>Future Work</h3><p>Having proven the concept and effectiveness of SOR by price impact-based pathfinding techniques, development on Eta X can be further improved along several verticals.</p><h3>Expanding DEX Support</h3><p>One of the most obvious ways to extend the capabilities of Eta X is to simply include more DEXs. The system has been designed such that doing so is a relatively streamlined and modular process, however, some work is required to reverse engineer the ever-increasingly complex conservation functions of emerging DeFi protocols. SushiSwap is a fork of Uniswap and has been seamlessly integrated with Eta X as of now, <a href="https://balancer.fi/">Balancer</a> is next on the roadmap on account of its popularity and solid subgraph support.</p><p>Price impact calculation algorithms have already been written for Balancer’s multi-asset pools, which use a constant mean product conservation function, as opposed to the standard constant product conservation function devised by Uniswap. While the change may appear inconsequential at first glance, there are some major differences in the calculation of price impact. Balancer’s pools are kept in correct ratios by the following equation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0tsSg4pujSTVIKXj.png" /></figure><p>Where <em>V </em>is a constant, <em>Bt </em>is the balance of token <em>t</em>, and <em>Wt </em>is the proportional weight of that token in the pool. Balancer pools can hold up to 8 tokens in a liquidity pool, where each token is assigned a weight from 0–100. For example, a pool containing:</p><ul><li>200 WETH with a weight of 60</li><li>100,000 USDC with a weight of 20</li><li>100,000 DAI with a weight of 20</li></ul><p>The constant <em>V </em>is given by:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-yDvPDKRnCqKmd9X.png" /></figure><p>The amount of tokens received in a Balancer pool swap is given by the following formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HPqjTRaZpa7bz6GG.png" /></figure><p>Where <em>A_O </em>is the amount of token being swapped out of the pool, <em>A_i </em>is the amount of token 1 in, <em>Bi </em>is the balance of token being swapped into the pool, <em>s </em>is the swap fee of the pool, <em>Wi </em>is the weight of the token being swapped in, and <em>WO</em> is the weight of the token being swapped out.</p><p><strong>Community Suggestions</strong></p><p>While we will be working to add the most popular and useful exchanges through our own vetting method, we would like to invite the community to suggest their own additions to Eta X’s DEX pool. To achieve this, Eta X V2 will include a DEX suggestion form where users can import a DEX and request its addition to Eta X.</p><h3>Data Collection Optimization</h3><p>As powerful as The Graph is, it is not without some latency limitations. Fetching pool data is by far the biggest bottleneck in the current iteration of the system, constituting up to 80% of the runtime per query in worst-case scenarios. In response to this, we are looking into tighter integration with <a href="https://gdafund.medium.com/open-crypto-data-initiative-1e096ccbf0e6">L3 Atom</a> for faster and more reliable feeds by grabbing data directly from Ethereum via Infura and deriving the relevant values manually. Additionally, we may also be able to squeeze more out of The Graph by either migrating over to their decentralized subgraphs or by hosting our own subgraphs.</p><p>Additionally, caching methods may improve the latency of the current implementation by querying large sums of pool data periodically and retrieving this data per user input, as opposed to the current PoC method of querying subgraphs with each user input.</p><h3>Order Splitting</h3><p>Order splitting is a crucial piece of SOR, and will be a major area of focus for Eta X V2. Order splitting involves breaking an order down into smaller parts to be distributed across multiple pools or routes. This concept is currently in the research stage of development, in which we are exploring options such as dynamic algorithms for splitting orders by liquidity, total value locked (TVL), and other such factors. We are also exploring the possibility of leveraging deep reinforcement learning (DRL) for order splitting purposes, in this process, a DRL agent would be tasked with splitting orders using the algorithms of Eta X in such a way that maximizes returns. This can be done off-chain in such a way that does not require any actual crypto to be transferred, thanks to the use of price-impact calculations. However, this may miss some important caveats of volatility and slippage, factors that may need to be considered as parameters in our model.</p><h3>Larger Graphs and More Powerful Pathfinding Algorithms</h3><p>As we add DEXs to the search space of routes, the number of nodes in a given graph will expand rapidly. This may mean that dynamic algorithms such as Bellman-Ford will begin to struggle to find paths within a reasonable timeframe. In this case, we may need to employ algorithms more suited to large graphs. The intricacies of pathfinding algorithms for this problem are explored in more depth in this <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#8c66">article</a>. While still an example of dynamic programming, the Floyd-Warshall algorithm is particularly effective for worst-case scenarios of v2 graphs, where Bellman-Ford takes is O(v3) for each node, Floyd-Warshall is O(v3) for the entire graph.</p><p>Another promising approach would be the inclusion of heuristic algorithms such as ant colony optimization or genetic algorithms, both of which are particularly effective at finding near-optimal paths through large graphs.</p><h3>Predictive Analytics</h3><p>While price impact calculations go a long way in estimating the profitability of an order route, we will likely want to factor in other datapoints to account for potential slippage losses to volatility. This may be an area in which complex data collection and machine learning could benefit the system greatly. For instance, we may be able to attribute risk scores to pools by assessing their liquidity and volume over time, this may even allow us to identify malicious or faulty pools (which most certainly do exist on many DEXs).</p><p>Were models to be trained via historical datapoints, it may even be possible to predict data somewhat ahead of time by using Ethereum’s mempool data to retrieve transaction information before they are processed by the blockchain and their target smart contracts.</p><h3>Order Execution</h3><p>Early iterations of Eta X are intended to demonstrate the methodology and capabilities of the SOR algorithms designed in-house by Deeplink, while also being an analytical tool through which users can identify trade opportunities. Later stages of Eta X are intended to facilitate the execution of these trade opportunities via wallet and smart contract integration.</p><h3>References</h3><ul><li><a href="https://github.com/graphprotocol/research/blob/master/papers/whitepaper/the-graph-whitepaper.pdf">https://github.com/graphprotocol/research/blob/master/papers/whitepaper/the-graph-whitepaper.pdf</a></li><li><a href="https://balancer.fi/whitepaper.pdf">https://balancer.fi/whitepaper.pdf</a></li><li><a href="https://uniswap.org/whitepaper.pdf">https://uniswap.org/whitepaper.pdf</a></li><li><a href="https://docs.sushi.com/">https://docs.sushi.com/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44dbc794ab7d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DeepBrew is Live!]]></title>
            <link>https://medium.com/@deeplinknetwork/deepbrew-is-live-c0b3a5df8567?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0b3a5df8567</guid>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Mon, 23 Jan 2023 13:09:11 GMT</pubDate>
            <atom:updated>2023-01-23T13:09:11.826Z</atom:updated>
            <content:encoded><![CDATA[<h4>Connecting off-chain deep learning to on-chain Ethereum</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4V_bsuxCmpjhFLvGQOpejQ.png" /><figcaption>DeepBrew Game Metrics</figcaption></figure><h3>Background of the project</h3><p>DeepBrew is blockchain-native adaptation of the classic ‘The Beer Game’, a macroeconomic supply chain scenario, to demonstrate a proof of concept to Deeplink’s core vision — combining off-chain machine learning into on-chain environments e.g. smart contracts.</p><p>The research project aimed at progressing the synthesis of machine learning and blockchain technologies. In particular DeepBrew involves the augmentation of Ethereum smart contracts with deep reinforcement learning agency to enable intelligence and dynamism beyond the scope of traditional contracts while still maintaining the decentralization and security aspects of the EVM. Later in this article, you will be able to see the methodologies and the workflow we have used to execute the off-to-on-chain reinforcement learning systems.</p><p>In this <a href="https://medium.com/@deeplinknetwork/deepbrew-the-on-chain-drl-beer-game-as-deeplink-poc-6d029223d2cc">article</a> and <a href="https://medium.com/@deeplinknetwork/deepkeeper-litepaper-on-chain-deep-q-learning-agents-8c2074b9ccc1">thesis</a>, we initially talked about bringing machine learning systems in smart contracts and the blockchain. We shared how the game runs entirely off-chain, and exists as a set of Python scripts which interface with a private Ethereum testnet via Web3.py (the ERC20 BEER token however is deployed as a Solidity smart contract to the testnet).</p><p>The last version was done so by creating an API which broadcasts the model’s outputs, and an oracle, which takes these outputs for use in Solidity contracts.</p><p>This game functions by displaying Beer game deep learning training results and interactions between on-chain and off-chain and does not have any commercial application or utility.</p><h3>Live Performance</h3><p>Watch the game unfold on the Goerli Ethereum testnet in real-time, as agents swap BEER for CASH tokens in accordance with instructions from the deep reinforcement learning environment and model keeper script.</p><h4><a href="https://v1.deeplink.network/"><strong>Link to DeepBrew Demo page</strong></a></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cvjj3H9EvYZgd4tTcuZjIA.png" /><figcaption>DeepBrew Animation</figcaption></figure><p>You will also be able to observe the game’s transactions pulled directly from the Goerli ledger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gxDdpeIAQ0YHHsmLm5Y9ww.png" /><figcaption>DeepBrew transactions</figcaption></figure><h3>Architecture</h3><p>DeepBrew has been devised to provide a dynamic optimisation problem involving transactions and the management of complex systems. A soft actor-critic deep reinforcement learning algorithm is then trained against rule-based agents. Finally, variables from this model and environment are passed on to prompt the execution of an Ethereum smart contract via an oracle, demonstrating an intelligent ‘on-chain agent’.</p><p>The following architecture outlines the framework for connecting off-chain machine learning to on-chain smart contract execution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sBToR0pFcb8igyUWk618Zg.png" /></figure><h3>Deep Reinforcement Learning Model (Soft Actor-critic)</h3><p>The selected model for optimizing The Beer Game is a relatively lightweight soft actor critic deep Q-learning model, a type of reinforcement learning model which builds on the traditional Q-learning actor-critic framework of policy adjustment via Q-functions by making two estimates for Q-values in an effeort to avoid overvaluing rewards. The model is separated into an actor and critic, the actor makes actions in the space, and the critic evaluates the effectiveness of those actions. This is done via clipped double Q-learning which takes the minimum of two Q-value estimates using the following function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/0*Ahxg2TzkLZiZuku0.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/0*I2Lmt4WXXHl1-ZV1.png" /></figure><h3>On-chain Machine Learning Workflow</h3><p>This project was undertaken largely in order to develop a methodology for the implementation of more practical off-to-on-chain reinforcement learning systems. This workflow can be broken down into following steps:</p><p>1. Recreate your Web3 problem as accurately as possible in a local testnet such as Ganache</p><ul><li>Local testnets are recommended for this stage as they run dramatically faster than public testnets, and have admin functionality</li><li>This may be less daunting of a task than it sounds, as the code on which Web3 ecosystem reside is publicly available</li></ul><p>2. Convert this problem into a reinforcement learning environment</p><ul><li>OpenAI’s Gym class is a good starting point</li></ul><p>3. Train a reinforcement learning model to optimize your problem</p><p>4. Deploy your system onto a public testnet such as Goerli for bugtesting purposes</p><ul><li>Sending transactions on a public blockchain is more involved than on a local one</li><li>This will also give you a sense for the real-world execution speeds you can expect</li></ul><p>5. Once this model performs satisfactorily, leverage transfer learning techniques to deploy your model onto the mainnet</p><ul><li>Transfer learning allows the model to begin training from where the prototypes left off, rather than deploying a randomly acting agent onto the mainnet with real funds</li><li>It is strongly advised that safety measures such as spending limits are put in place to keep the model from doing anything extreme</li></ul><h3>Applications and future work</h3><p>The applications of this technique are broad in scope, and can be applied to any problem in which dynamism and intelligence would benefit a Web3-based use-case. Some examples of these applications include but are not limited to:</p><ul><li>DeFi Capital Efficiency</li><li>On-chain Algorithms</li><li>On-chain Credit Scores</li><li>Artificially Intelligent Smart Order Routing</li><li>DEX Aggregation</li></ul><h3>What’s next?</h3><p>Stay tuned for ETA X V1.</p><p>Eta X is an open-source initiative to build an agnostic decentralized exchange (DEX) aggregator and price discovery engine/smart order router (SOR). It has been designed from first principles to be universal, scalable, and unbiased via an adaptable method of reverse engineering the conservation functions employed in the automated market maker (AMM) algorithms of DEX liquidity pools.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0b3a5df8567" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DeepKeeper Litepaper: On-chain Deep Q-learning Agents]]></title>
            <link>https://medium.com/@deeplinknetwork/deepkeeper-litepaper-on-chain-deep-q-learning-agents-8c2074b9ccc1?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c2074b9ccc1</guid>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Tue, 01 Nov 2022 08:42:36 GMT</pubDate>
            <atom:updated>2022-12-13T05:39:18.608Z</atom:updated>
            <content:encoded><![CDATA[<h4>V 1.2</h4><p>by Jack Lodge</p><p>This paper proposes the first steps towards on-chain artificial intelligence, a layer 2 solution which demonstrates the potential of on-chain deep reinforcement learning is presented, along with the methodology behind its implementation.</p><p>This project acts as a demonstration of this technology, and to develop a framework on which more complex technologies can be built. While an undoubtably revolutionary technology, smart contracts are not without limitations. Contrary to their name, they are rigid, and for the most part, not capable of intelligent execution at the same levels which their off-chain counterparts enable. The potential for smart contracts is currently hindered by these limitations, the prospect of decentralized finance (DeFi), and many other intricate use cases in the spaces of Industry 4.0 and Web 3.0 require solutions of rapidly increasing complexity.</p><p>The introduction of artificial intelligence into the execution of on-chain environments will be a key development in bringing these technologies further into the mainstream. This is an ongoing project and is thus subject to change, should superior approaches to any of the following sections be identified along the way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*unPgnfEAqCjde3_8roOW_g.png" /></figure><p><a href="https://drive.google.com/file/d/1LIqygsYXSC_1MseXl3eQqPyojQEuOaLe/view?usp=share_link">Google Drive: Sign-in</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c2074b9ccc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pathfinding Algorithms for DEX Aggregation and Smart Order Routing]]></title>
            <link>https://medium.com/@deeplinknetwork/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-c85154c21cc5?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/c85154c21cc5</guid>
            <category><![CDATA[aggregator]]></category>
            <category><![CDATA[crypto]]></category>
            <category><![CDATA[decentralized-exchange]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[smart-order-routing]]></category>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Fri, 30 Sep 2022 11:31:27 GMT</pubDate>
            <atom:updated>2022-11-17T02:27:47.561Z</atom:updated>
            <content:encoded><![CDATA[<p>by Jack Lodge</p><p><a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796"><em>Originally posted at Deeplink Labs Publication</em></a></p><p>· <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#ae6a">Introduction</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#cdab">What is Pathfinding?</a><br>· <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#1bd6">Glossary</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#ac20">Smart Order Routing</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#3cda">DEX Aggregation</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#8b07">Graphs</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#0972">Weighted Graphs</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#74dd">Directed Graphs</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#99c4">Temporal Graphs</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#f101">Graph Traversal</a><br>· <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#8c66">Metrics and Nuances of Pathfinding Algorithms</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#25b5">Informed vs. Uninformed Algorithms</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#242e">Completeness</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#2b6d">Termination</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#44d2">Admissibility</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#ebce">Big O Notation</a><br>· <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#9823">Common Pathfinding Algorithms</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#2221">Rapidly-exploring Random Trees</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#a9e2">Greedy Algorithms</a><br>∘ <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#c2d0">Dynamic Algorithms</a><br>· <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#244d">Heuristic Algorithms</a><br>· <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#9327">Representing Smart Order Routing for DEX Aggregation as a Pathfinding Problem</a><br>· <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796#7a62">References</a></p><h3>Introduction</h3><p>This article outlines the ongoing research Deeplink is undertaking regarding the framing of both smart order routing and DEX aggregation as pathfinding problems. A brief overview of what pathfinding is and some common techniques are provided, along with a detailed explanation of how this field of study applies to these problems, and finally, a collection of related works are investigated.</p><h3>What is Pathfinding?</h3><p>Pathfinding is the computational field of identifying the shortest route between two points. Pathfinding algorithms are used across many industries and applications including search engines, video games, GPS navigation software, and robotics, to name a few.</p><p>One of the simplest examples to consider is a GPS route optimization algorithm in which nodes are landmarks or destinations, and edges are the paths between those locations, weighted by the physical distances, a pathfinding algorithm could find the shortest route through these locations from one location to another by traversing the edges in such a way that minimizes the total weight traversed.</p><p>Another example, reminiscent of <a href="https://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg">Euler’s Königsberg Bridges problem</a>, can be found in infrastructure planning such as logistics, motorway, or railway design. For instance, when designing complex systems such as a metro, it is important to minimize redundant travel in the layout of the train lines in order to avoid congestion and slow journeys. One fundamental approach to doing this is to use pathfinding algorithms to identify the shortest Euler path between all relevant metro stations, in other words, the shortest path which visits each station at least once. A system of stations can also be broken down into discrete lines, each of which can have its own shortest Euler path. This can be seen in the following representation courtesy of Paragon Routing, a pathfinding and route optimization software provider.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*prIC3G1S88gs3KtX" /><figcaption>Source: <a href="https://www.paragonrouting.com/en-gb/ultimate-guides/post/the-ultimate-guide-to-route-optimization/">https://www.paragonrouting.com/en-gb/ultimate-guides/post/the-ultimate-guide-to-route-optimization/</a></figcaption></figure><h3>Glossary</h3><p>In previous articles we have outlined both <a href="http://test/">smart order routing</a> and <a href="http://test/">DEX aggregation</a> in some depth, to fully grasp the necessity for pathfinding algorithms in enhancing these spaces, we recommend reading those articles. However, a brief summary of each will also be provided here, along with a few important preliminary concepts.</p><h3>Smart Order Routing</h3><p>Smart order routing (SOR) is an automated process in which orders on exchanges are handled with the intent of attaining the most desirable path across trading venues. In a DEX, this generally takes the form of finding the optimal path of swaps across a set of liquidity pools in order to take advantage of the liquidity depth of those pools, and mitigate the effects of fragmented liquidity. The primary cause for concern regarding this liquidity fragmentation being negative slippage, losses incurred on account of a change in spot price in the time between an order being placed and executed.</p><h3>DEX Aggregation</h3><p>A DEX is a trading venue consisting of a set of liquidity pools that facilitate the exchange of assets without a central authority or the need for users to forfeit custody over their assets. DEXs are prone to the aforementioned issues of liquidity fragmentation as their asset pairs are segmented into liquidity pools, and as more and more venues arise, the overall market liquidity becomes more and more thinly spread.</p><p>DEX aggregators expose traders to more liquidity than any one DEX could provide by aggregating and connecting the services of multiple DEXs. A DEX aggregator can be thought of analogously to services like Expedia or Google Flights, which aggregate offerings from numerous airlines into one comparative service, giving users access to the best possible options for their needs.</p><h3>Graphs</h3><p>In computer science, pathfinding is most commonly represented using graphs-sets of connected nodes. In a graph, nodes can be thought of as places, computers, people, etc. depending on the application, while the connecting edges can be thought of as whatever relates those nodes to one another, e.g. an edge connecting two nodes representative of cities may be representative of a highway, or an edge connecting two asset pools may represent a swap between those two pools.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/0*FGE3aKvZWmHC_emU" /></figure><p>It is worth noting that there are many other representations of spaces which can be used in pathfinding, such as grids, 2D or 3D spaces, or even more abstract data structures such as octrees and quadtrees. However, graphs are the most widely used and most easily related to our use case.</p><h3>Weighted Graphs</h3><p>Graph edges can also be weighted, attributing values to the connections between nodes. For instance, the weight in the two cities example may be the physical distance between the cities, or in the asset pools example, the weight may indicate the fees incurred by making that swap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/0*pMrv-g8RyH3sqnWU" /></figure><h3>Directed Graphs</h3><p>Additionally, graphs can be directed, that is, some or all edges can only be traversed in one direction. That is, a node can point to another node without that node pointing back to it. For example, if the only path between points A and B is a unidirectional road, that would be a directed edge connecting those two nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/0*V4GsrXE_ayCnXOFK" /></figure><p>However, some nodes may still be traveled to from one another in a directed graph, this is represented by two arrows pointing from each node to the other. Furthermore, nodes can also point at themselves, indicating that it is a valid move to travel from that node to itself — this is known as a loop.</p><h3>Temporal Graphs</h3><p>Temporal graphs represent nodes and connections which change over time. For example, a node may only be accessible temporarily, or the weight of an edge may not be constant over time. Temporal graphs are a relatively recent development in graph theory, and can dramatically increase the complexity of tasks such as graph traversal. Temporal graphs are somewhat of an emerging field and are quite complex in nature, for further reading on the topic, <a href="https://www.researchgate.net/profile/Mohamed_Mourad_Lafifi/post/If_an_agent_travels_from_node_to_node_in_a_graph_what_algorithms_are_relevant_to_predicting_the_topology_and_size_of_the_graph_while_travelling/attachment/5e5b7ca73843b0499feb6352/AS%3A864205230071809%401583053991603/download/An+Introduction+to+Temporal+Graphs+_+An+Algorithmic+Perspective.pdf">here is a good resource</a> that explores them in depths beyond the scope of this article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*ilAhQhWRA1LveBum" /><figcaption>Source: <a href="https://towardsdatascience.com/temporal-graph-networks-ab8f327f2efe">https://towardsdatascience.com/temporal-graph-networks-ab8f327f2efe</a></figcaption></figure><h3>Graph Traversal</h3><p>When representing spaces as graphs, pathfinding can be represented as graph traversals. This often involves finding the shortest path to connect two or more points, by using the least edges, or by accumulating the least weight along the paths connecting those nodes. Alternatively, graph traversal can find a path that visits each node in a graph in such a way that similarly minimizes the cost of doing so.</p><h3>Metrics and Nuances of Pathfinding Algorithms</h3><p>The following section outlines some terms and concepts which are useful in understanding the effectiveness or applicability of a given pathfinding algorithm.</p><h3>Informed vs. Uninformed Algorithms</h3><p>Informed algorithms (such as greedy search) have some information regarding their own goal, provided by the function which is used to estimate the closeness of its current state to its goal state (destination node, in our case).</p><p>Conversely, uninformed algorithms (such as depth-first and breadth-first search) do not have information regarding their own goal.</p><h3>Completeness</h3><p>A pathfinding algorithm is considered complete if it is guaranteed to complete execution, i.e. it will not get stuck running forever.</p><h3>Termination</h3><p>A pathfinding algorithm can have one or more termination conditions, requirements by which it will cease execution, such as finding the solution if it exists.</p><h3>Admissibility</h3><p>A pathfinding algorithm is admissible if an optimal solution is guaranteed to be found, provided enough time, memory, and computation are provided.</p><h3>Big O Notation</h3><p>Big O notation is the standard mathematical notation for describing the behavior of a function or algorithm as a limit when a relevant variable approaches a specific value such as infinity. It is used to express the efficiency of the algorithm, for example, we can say that the Bellman-Ford equation takes O(VE) time, meaning that we can expect a given graph with V vertices (nodes) and E edges to be solved by the Bellman-Ford algorithm in V*E steps.</p><h3>Common Pathfinding Algorithms</h3><p>Pathfinding algorithms can largely be divided into one of three main categories, dynamic, greedy, or heuristic. This section will outline the differences between these approaches and provide some famous examples of each.</p><h3>Rapidly-exploring Random Trees</h3><p>Rapidly exploring random tree (RRT) can be either greedy or dynamic depending on how the specific implementation is tweaked. RRT search is useful for exploring high-dimensional spaces, particularly, it is used to find open-loop trajectories in nonlinear systems. As such, it may be useful in the event that simple graphs do not adequately represent the complexity of the system at hand. RRT achieves its traversal by extending random branches into the largest unexplored regions of a given space; given enough time, it will give a relatively optimal path to the desired location. It can also be useful when the exact location of the goal object is not known. It is an industry-standard algorithm for pathfinding and route optimization in autonomous vehicles and robotics, as can be seen in this demonstration created by the author of this article:</p><p>RRT SLAM (simultaneous localization and mapping) Demonstration, source: Jack Ryan Lodge</p><p>The blue lines on the right-hand window depict the branches of the robot’s RRT-based pathfinding algorithm as it explores the building.</p><h3>Greedy Algorithms</h3><p>A greedy algorithm is one that builds a solution piece by piece, at each step it chooses the option which yields the most immediate benefits, in other words, the ‘greedy’ option. With greedy algorithms, there is no guarantee that an optimal solution will ever be found, though they are generally more efficient regarding memory usage and time complexity than their counterparts.</p><p><strong>Djikstra</strong></p><p>Djikstra’s algorithm is a simple yet effective greedy algorithm for finding the shortest path between one node and all other nodes in a graph, or it can be used to give the shortest distance between any two given nodes. It essentially calculates the distance of each node one-by-one from a given source node, updating a list of shortest paths each time a longer path is undercut. Many other algorithms have built on this concept but retain the principle of iteratively searching for near-optimal paths and replacing them once better ones are identified until the most optimal path is found.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/0*WbzAP4sfXckSfLR8.gif" /><figcaption>Djikstra’s Algorithm</figcaption></figure><p><strong>A*</strong></p><p>A* builds on Djikstra’s algorithm by introducing a heuristic function that can be used to prioritize nodes, in other words, it is an informed algorithm. Heuristics can dramatically improve the performance of A* if the problem facilitates such an optimal heuristic, and the heuristic is optimally designed. Similarly to Djikstra’s algorithm, A* begins at a source node, it then aims to create a path to a destination with the smallest possible cost by creating and minimizing a tree of paths between source and destination nodes until the terminal criteria are reached — The shortest of these paths is then selected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*MN7TpmRnsq3vBbrm.gif" /><figcaption>A* Algorithm</figcaption></figure><h3>Dynamic Algorithms</h3><p>When solving parts of a problem (sub-problems), dynamic algorithms store the solutions to those sub-problems along the way then make decisions at each step based on the current situation <em>and </em>the solution to previously solved sub-problems when calculating for optimal solutions. Unlike greedy algorithms, dynamic algorithms are guaranteed to find optimal solutions (if one exists). However, as a trade-off, they are generally more taxing in terms of both memory and time complexity than greedy algorithms.</p><p><strong>Bellman-Ford</strong></p><p>The Bellman-Ford algorithm serves essentially the same purpose as Djikstra’s algorithm and A*, and is often considered the best of the three approaches. It can be slower, but is more versatile, as it can handle graphs with mixed signed weights (positive and negative). It does this by introducing a relaxation equation which essentially uses relative distances between nodes via the <a href="https://en.wikipedia.org/wiki/Triangle_inequality">triangle inequality theorem</a> to calculate the distance of between nodes by comparing that distance with other known distances.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*o8JHTr_me4rQNxDu.gif" /><figcaption>Bellman-Ford Algorithm</figcaption></figure><h3>Heuristic Algorithms</h3><p>Heuristic algorithms are designed to solve a specific decision problem quickly and efficiently by sacrificing some optimality and/or completeness. They are best used as approaches to problems to which there is no known way to reach an optimal solution in a reasonable amount of time or computational effort. They provide no guarantee of optimality but can find near-optimal, or local maxima/minima solutions for problems that remain intractable to greedy or dynamic algorithms. The most famous and immediately recognizable example of this would be the artificial neural networks found in deep learning models for artificial intelligence.</p><p><strong>Ant Colony Optimization</strong></p><p>Ant colony optimization algorithms aim to find paths through graphs in a multi-agent approach. Artificial ant agents perform local search algorithms and leave behind digital pheromones for their peers, together, the cumulative pheromone trails left behind by the entire collection of artificial ant agents will often find optimal or near-optimal paths, even in extremely large or complex spaces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*N2F_S9rFPOPdPJlo.gif" /><figcaption>ACO as a solution to the <a href="https://en.wikipedia.org/wiki/Travelling_salesman_problem">traveling salesman problem</a></figcaption></figure><p><strong>Genetic Algorithms</strong></p><p>Genetic algorithms are based on biological natural selection, the underlying process behind evolution. A population of agents are generated which perform a set of actions corresponding to their ‘genetics’, a set of traits unique to each individual, these agents have defined goals and metrics of success. The more successful an agent is in achieving the desired result, the more likely its genes are to be copied into the next generation. In the context of pathfinding, this generally involves agents who traverse the given graph with the lowest energy spent, or most resources acquired passing their genes on to the next generation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*WDngWa822HXG1rcT.gif" /><figcaption>A genetic algorithm solving a pathfinding problem, source: <a href="https://github.com/Yaaximus/genetic-algorithm-path-planning">https://github.com/Yaaximus/genetic-algorithm-path-planning</a></figcaption></figure><h3>Representing Smart Order Routing for DEX Aggregation as a Pathfinding Problem</h3><p>In order to understand why pathfinding is a useful concept when devising DEX aggregator SOR algorithms, it is important to remember that a DEX, and consequently a DEX aggregator, is essentially a collection of liquidity pools. These pools act as a point between a pair of assets at which they can be swapped. The following outlines an example in which a DEX is depicted as a graph, over which a pathfinding algorithm is run in order to make a saving on a trade.</p><p><strong>DISCLAIMER: THIS IS A HYPOTHETICAL, DEMONSTRATIVE EXAMPLE OF HOW SOR CAN BE TREATED AS A PATHFINDING PROBLEM</strong></p><p>We can consider this sea of liquidity pools as a graph, in which the pools are nodes, and the possible swaps between them are edges. We can continue this and consider it as a weighted graph, where the weights may represent the profitability or viability of swapping between two pools. For example, consider the following fictional, relatively DAI-centric DEX consisting of 6 liquidity pools (ETH-WBTC, DAI-WBTC, ETH-DAI, DAI-USDT, DAI-FTM, USDT-FTM).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*T3hYZLBj2c-tXuCa" /></figure><p>Immediately we can see that there does exist a pool such that our swap could be completed, the ETH-DAI pool, however, let’s explore why this may not be the best approach. At each of these nodes, one of the listed tokens can be swapped for the other, in either direction. This means that after swapping at one pool, the other asset is now possessed, as such, after trading with a pool, the new asset can be traded with any pools which also contain that asset. We will demonstrate this relationship by created a directed graph of potential swap sequences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*kTjHio-RjDjoT7vj" /></figure><p>Now let’s consider a scenario in which a trader wishes to swap 100 ETH for as much DAI as possible. We will mark any pools containing ETH as green source nodes at which the path begins, and any pools containing DAI as orange destination nodes at which the path can end, blue nodes denote pools that are neither sources nor destinations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*GyRzEs9N2VHCzpDb" /></figure><p>Note that the ETH-DAI pool is a gradient of green and orange, as the path can both begin and end here. Additionally, source nodes have gained a loop that connects this node to itself, this helps in both illustrating the fact that this is also a potential path (representative of the initial swap required to enter the graph), and in the calculations of our pathfinding algorithms.</p><p>Next, we assign weights to the edges connecting the nodes. We can think of this as the expected slippage percentage of trading in the pool being pointed at, and we can assume that this is calculated via a variety of metrics that factor in liquidity concentration, liquidity spread, volatility, etc. — you can read more about what causes slippage in our <a href="http://test/">article on smart order routing.</a> It may be the case that these edges require machine learning techniques in order to calculate, some techniques for doing so are explored in our article on <a href="http://test/">machine learning techniques for DEX aggregation and SOR</a>, but for the purposes of this explanation, the numbers have been arbitrarily selected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*2TZoz_67S-c9nKdz" /></figure><p>Note that the swapping of WBTC for DAI is assigned a negative weight, indicating a potential for a 0.2% profit via arbitrage. It is also worth mentioning that algorithms such as Djikstra’s would not be able to handle situations such as this, due to their inability to traverse multi-signed graphs.</p><p>We can see that the simplest trade would be to simply use one pool, exchanging ETH for DAI directly, incurring a 5% loss to slippage (5 ETH, or $10,274.31 USD as of the time of this article’s writing). However, let’s examine our other source node, the ETH-WBTC pool, by identifying the shortest paths between it and all other pools. Any of the previously discussed pathfinding algorithms could handle this task, but in this demonstration we will arbitrarily do so by running the Bellman-Ford algorithm over the graph, starting at the ETH-WBTC pool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*TrFL9Tf6WqCItmYn" /></figure><p>Green edges indicate the routes from the ETH-WBTC node to all other nodes in the graph. The numbers in the nodes indicate the cost (accumulative percentage losses to slippage) to reach those nodes. Here we can see that by taking advantage of the arbitrage opportunity when trading WBTC for DAI, we can mitigate some of our losses to slippage. Let’s assume our algorithm selects a 70–30 split, transacting 70 ETH first in the ETH-WBTC, then trading that WBTC for DAI, and 70 ETH directly in the ETH-DAI pool.</p><blockquote><em>0.3*0.5 + 0.7(0.1–0.02) = 0.206%</em></blockquote><p>This indicates a saving of 4.79%, or approximately $9,857.24 USD when compared to the simple method of only using one pool. Remembering that these values were arbitrarily selected and that real DEXs stand to lose even more to slippage (see a real example in <a href="http://test/">our article on DEX aggregators</a>).</p><p>Furthermore, this approach is merely demonstrative as to how SOR can be thought of as a pathfinding problem, far more complex pathfinding approaches would likely be required, and would likely yield far more impressive results. One reason for the necessity of incorporating more complex pathfinding techniques is the sheer size of graphs relating to a large DEX aggregator. Our example used a hypothetical DEX with only 7 liquidity pools, Real DEXs and DEX aggregators may be dealing with thousands of nodes, which may result in an exponentially large number of edges. To traverse such graphs with perfect optimality is intractable in the timescales required for a usable service. As such, it may be the case that algorithms that specialize in near optimality (such as ACO or genetic algorithms) may be required in order to keep pace with the complexity requirements.</p><p>Additionally, it may be the case that the construction of the graph and its edges is a more complicated task than traversing the graph once created. Selecting valid pools, deciding whether to split orders along with the size and number of those splits, assigning weights</p><h3>References</h3><ol><li>Harinder Kaur Sidhu, University of Windsor, “Performance Evaluation of Pathfinding Algorithms”, <a href="https://downloads.hindawi.com/journals/complexity/2021/5511802.pdf">https://downloads.hindawi.com/journals/complexity/2021/5511802.pdf</a>, 2021</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c85154c21cc5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning Applications in DEX Aggregation and Smart Order Routing]]></title>
            <link>https://medium.com/@deeplinknetwork/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-fb50880fb390?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb50880fb390</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[decentralized-exchange]]></category>
            <category><![CDATA[dex-aggregator]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Thu, 29 Sep 2022 20:55:56 GMT</pubDate>
            <atom:updated>2022-11-17T02:33:25.627Z</atom:updated>
            <content:encoded><![CDATA[<p>by Jack Lodge</p><p><a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c"><em>Originally posted at Deeplink Labs Publication</em></a></p><p>· <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#f0cd">Introduction</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#f0c7">Smart Order Routing</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#4e19">DEX Aggregation</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#234d">Artificial Intelligence, Machine Learning, and Deep Learning</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#abb4">Deep Reinforcement Learning</a><br>· <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#fc19">How Can Machine Learning Improve Smart Order Routers and DEX Aggregators?</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#48b1">Slippage</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#4f91">Liquidity Concentration</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#8aa6">Liquidity Volatility</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#e69f">Liquidity Distribution</a><br>· <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#e0e7">Deep Reinforcement Learning for Smart Order Routers and DEX Aggregators</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#9f36">On-chain Agents</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#3a8e">Amalgamating Web 3 and Machine Learning</a><br>· <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#649c">Related Work</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#2d8d">Reinforcement Learning for Optimization Problems</a><br>∘ <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#0bb2">Genetic Algorithms for Smart Order Routing and Automated Trading</a><br>· <a href="https://medium.com/deeplink-labs/machine-learning-applications-in-dex-aggregation-and-smart-order-routing-c542b4b1243c#c435">References</a></p><h3>Introduction</h3><p>This article explores Deeplink’s ongoing research towards machine learning approaches and applications in both smart order routing and DEX aggregation. A brief overview of preliminary concepts is provided, along with a detailed explanation of how this field of study applies to the problems at hand, and finally, a collection of related works are investigated.</p><p>In previous articles we have outlined both <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e">smart order routing</a> and <a href="http://test/">DEX aggregation</a> in some depth, to fully grasp the necessity for pathfinding algorithms in enhancing these spaces, we recommend reading those articles. However, a brief summary of each will also be provided here, along with a few important preliminary concepts.</p><h3>Smart Order Routing</h3><p>Smart order routing (SOR) is an automated process in which orders on exchanges are handled with the intent of attaining the most desirable path across trading venues. In a DEX, this generally takes the form of finding the optimal path of swaps across a set of liquidity pools in order to take advantage of the liquidity depth of those pools and mitigate the effects of fragmented liquidity. The primary cause for concern regarding this liquidity fragmentation is negative slippage, losses incurred on account of a change in spot price in the time between an order being placed and executed.</p><h3>DEX Aggregation</h3><p>A DEX is a trading venue consisting of a set of liquidity pools that facilitate the exchange of assets without a central authority or the need for users to forfeit custody over their assets. DEXs are prone to the aforementioned issues of liquidity fragmentation as their asset pairs are segmented into liquidity pools, and as more and more venues arise, the overall market liquidity becomes more and more thinly spread.</p><p>DEX aggregators expose traders to more liquidity than any one DEX could provide by aggregating and connecting the services of multiple DEXs. A DEX aggregator can be thought of analogously to services like Expedia or Google Flights, which aggregate offerings from numerous airlines into one comparative service, giving users access to the best possible options for their needs.</p><h3>Artificial Intelligence, Machine Learning, and Deep Learning</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*ySIcSLveQOqnNHYA" /></figure><p>Artificial intelligence refers to intelligence demonstrated by machines, a sentiment in which intelligence refers to the decision-making capabilities generally associated with biological intelligence. Machine learning is a subset of artificial intelligence which refers to algorithms that are able to ‘learn’. Deep learning is a subset of machine learning originating from the <a href="https://en.wikipedia.org/wiki/Perceptron">McCulloch-Pitts neuron and Rosenblatt’s perceptron</a>, which models the way in which biological neurons process information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*mhXwWflOp7X5vKYs" /></figure><h3>Deep Reinforcement Learning</h3><p>Deep reinforcement learning (DRL) builds on the foundations of deep learning by further introducing aspects from biology and psychology via the introduction of reinforcement learning techniques. DRL agents learn to solve problems or make predictions by attempting to maximize a cumulative reward, akin to an incentive and penalty model. An example reward function for a DRL agent learning to play the game Snake might reward the agent for closing the distance between the snake’s head and apple, and penalize the agent for crashing into itself or walls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/0*ne5A4hTZr301qOaK" /></figure><h3>How Can Machine Learning Improve Smart Order Routers and DEX Aggregators?</h3><p>In DEX aggregation and SOR, liquidity is key. Allowing your algorithms to make deep correlations between liquidity concentration, distribution, and volatility will allow your systems to outperform those which do not consider these factors in such depth.</p><p>Machine learning techniques are used broadly across the traditional finance sectors, and it is only a natural progression that these beneficial applications should eventually see their way into DeFi. Machine learning is often used in traditional SOR to assess and identify factors pertaining to liquidity and volatility in order to ascertain the opportunistic routes, pricing, and order sizing — we believe that many of these practices are directly translatable to DEX aggregation and SOR.</p><p>By introducing liquidity indicators via the high speed, high granularity data feeds provided by L3 Atom, machine learning models can identify complex correlations between these factors at dimensionalities beyond the capabilities of human traders, and can use these correlations to outperform services that lack such depth of insight. One major area which stands to benefit from this technology is the avoidance of slippage.</p><h3>Slippage</h3><p>The nuances and causes of slippage are explored in more depth in our article on <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e">smart order routing</a>, but in essence, slippage is the difference between expected and actual price execution when placing an order on a trading venue. This generally occurs when the asset’s price changes in between the time of placing an order, and the time that that order is executed. Slippage can be positive or negative, meaning that the price difference can either be of benefit or detriment to the margins of the trader in question. However, negative slippage is generally what is meant when the term is mentioned.</p><p>Machine learning techniques can be employed to provide predictive insight towards price movements, volatility, and liquidity indicators, all of which play major roles in slippage, and in avoiding it. The following sections outline some areas which can be factored into machine learning-driven predictive analytics and agent-based decision-making algorithms to provide users with the most optimal trades.</p><h3>Liquidity Concentration</h3><p>In the context of DEXs, liquidity concentration refers to the available liquidity within a given liquidity pool. Due to the nature of AMMs in DEXs, slippage due to placing large orders is often even more of an issue than in other types of venues, as the liquidity concentration of DEX liquidity pools is generally on the smaller side relative to other sources of liquidity. When swapping tokens in a DEX, you are essentially adding one asset to the liquidity pool while simultaneously removing another, the AMM conservation function then automatically rebalances the ratio of these two tokens. This means that trades in pools with small liquidity, and conservation functions can overcompensate and cause dramatic price swings as the proportional ratio will be more affected — conducive to major slippage. This can be thought of analogously as the fluid displacement of a boat (an order) in a body of water (a liquidity pool); a dingy in a river will virtually have no impact on the water level, but a yacht in a pool certainly will.</p><p>Liquidity concentration primarily gives us information on the potential slippage within a single pool. Factoring in the liquidity concentration of individual pools when aggregating and routing through a sea of venues is a crucial element in facilitating optimal trade opportunities. The introduction of this data to machine learning models can open avenues for routing which are beyond the naked eye’s ability to comprehend the implications of liquidity depth. This is similar in concept to Balancer’s smart order routing technique of linearizing spot prices in liquidity pools in order to estimate the change in spot price resulting from an order being placed, predictions which are then used in selecting the optimal set of pools to route through. A more detailed breakdown of how this works can be found in our article on <a href="http://placeholder/">automated market makers</a>.</p><h3>Liquidity Volatility</h3><p>With the concept of liquidity concentration in mind, we can generalize that larger liquidity pools tend to be less volatile than smaller ones, as each individual trade makes less of an impact on the pool’s overall liquidity. However, liquidity concentration is only one factor of a pool’s volatility (in both price and liquidity). Volatility metrics are a broadly studied econometric subject with much of their own nuance, a pool’s volatility is tied to its liquidity depth, the price volatility of its underlying assets, its volume, its reputability, and many more external and internal factors. Due to its unwieldy and complex nature, volatility is also a topic to which machine learning is particularly suited.</p><p>Research such as this research article published in Hindawi Complexity in 2021, <a href="https://downloads.hindawi.com/journals/complexity/2021/5511802.pdf">‘Forecasting Volatility of Stock Index: Deep Learning Model with Likelihood-Based Loss Function‘</a>, which employed long short-term memory (LSTM) deep learning techniques to predict the volatility of stock indices. In this project, Fang Jia and Boli Yang fed historical volatility data points into their deep learning models and compared their performance against a popular traditional econometrics model known as autoregressive moving average and generalized conditional heteroscedasticity (ARMA-GARCH).</p><p>The models created were likelihood-based loss LSTM and deep neural networks (DNN), and mean squared error (MSE) LSTM and DNN (four models in total, compared against ARMA-GARCH). The likelihood-loss LSTM outperformed ARMA-GARCH along with the other deep learning models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*f2BoCN5D8lK42gDd" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*p515IGmqCruNtVJL" /></figure><p>These techniques can be adapted to fit the DEX aggregation and SOR problem by creating a volatility prediction LSTM model which uses historical data on the volatility of individual pools. This data would essentially be a time series mapping of orders to their respective impact on the market over time within a given pool, creating a continuous historical record of that pool’s volatility in response to orders — data which can be used to estimate the impact a given order would have on the pool’s liquidity, and the potential slippage it may incur. This can also be augmented with historical price movement data to provide the model with a broader view of the system.</p><p>Once trained, the model could then predictively assess a pool’s volatility by running over recent transaction data, but can also tap into unfilled transactions by accessing the blockchain’s mempool — a ledger in which blockchain transactions wait before being processed as transactions and permanently appended to the chain.</p><h3>Liquidity Distribution</h3><p>If liquidity concentration can be thought of as a body of water, liquidity distribution can be thought of as a system of bodies of water connected by rivers and streams. A user may want to take a bird’s eye view of these individual bodies of water (liquidity pools) and consider them as a network, before deciding which one would best fit their boat (order). It may even be the case that their boat is too big for any of the pools, and would be best broken down into smaller boats and placed across several.</p><p>In other words, liquidity distribution gives us insight into potential slippage across a network of liquidity pools. In the context of a DEX (and even moreso in a DEX aggregator), it is not hard to see why this information would be of great value when routing orders.</p><p>Liquidity distribution is key whenever large transactions are involved, particularly when the number of assets involved is above a threshold where orders in such quantities would immensely disrupt a pool, or may even be larger than the entire liquidity of that pool. In such cases, it would be best for both the trader and the ecosystem at large to disperse that order across multiple channels, so as not to bring the supply chain to a grinding halt — as this would certainly cause a price movement that would almost certainly incur unfavorable slippage on that trader’s end, and could cascade into larger negative impacts across the network.</p><h3>Deep Reinforcement Learning for Smart Order Routers and DEX Aggregators</h3><p>Using machine learning models to generate predictive metrics for use in more traditional algorithmic approaches has proven to be quite an effective solution, for instance, such models could be used in order to generate the weights connecting liquidity pool nodes as described in our article on <a href="https://medium.com/deeplink-labs/pathfinding-algorithms-for-dex-aggregation-and-smart-order-routing-9e9feaf6b796">pathfinding algorithms for DEX aggregators and SOR.</a></p><h3>On-chain Agents</h3><p>It may be the case that the use of agent-based machine learning techniques such as deep reinforcement learning allows models to form that interpret and react to correlated variables in ways that we may fail to detect as humans. In essence, an on-chain agent is a blockchain-native computational agent, capable of processing data, learning, and carrying out actions such as transacting on the blockchain.</p><h3>Amalgamating Web 3 and Machine Learning</h3><p>Bringing deep reinforcement learning execution to blockchains is an area of great interest to Deeplink, and has been the subject of direct research for some time now. Stay tuned to our publication channels for an update on a project centered on exactly such techniques.</p><p>Reinforcement learning agents require an environment in which to act. As such, in order to facilitate on-chain agents, we must first convert our Web 3 problem into a reinforcement learning comparable environment. The most popular way of converting any given problem into a reinforcement learning environment is via the use of <a href="https://github.com/openai/gym">OpenAI’s Gym API</a>, a reinforcement learning class framework for Python. This involves breaking the given problem into iterable steps which can be represented by the following Python functions:</p><p><strong>__init__(self)</strong></p><ul><li>Used to establish the variables used for reinforcement learning, namely, the observation space (the space of all possible observations an agent can make about this environment) and the action space (the space of all possible actions an agent can make in this environment).</li></ul><p><strong>step(self, action)</strong></p><ul><li>This is called once every ‘step’ of the environment, in the case of a game, this may be one frame or one turn.</li></ul><p><strong>reset(self)</strong></p><ul><li>This both starts the environment on its first run and resets it once an episode has concluded.</li></ul><p>In the context of Web 3 problems such as DEX aggregation and SOR, this can be a relatively complex process, not only must the scenario be translated into discrete, iterable steps, it must Web 3 connectivity must be natively built into the environment itself. Essentially, this process can be thought of as building a DApp for your reinforcement learning environment, a DApp that either transacts directly on its own via Web3.py/Web3.js, or which acts more as a keeper for smart contracts, instructing them to transact via TX variables or oracle integration. This script may also need to read data from the blockchain such as balances, transaction hashes, etc., all of which can also be handled via the use of the Web 3 libraries.</p><p>It is also likely that in use cases such as DEX aggregation and SOR, model training is best to be done in simulated environments, rather than on live exchanges with real assets. This can be done on private, command line test nets such as Ganache, or on live test nets such as Rinkeby or Goerli. The benefit of private testnets is the ability to control funds without needing to request testnet funds via a faucet, however, this may limit capabilities somewhat, as private test nets such as Ganache are incompatible with oracle functionality. Additionally, if the acquisition of testnet funds via faucets is an issue for your project, it may be a suitable workaround to deploy representative ERC20 (or other) tokens as stand-ins for testing purposes.</p><p>DApps can then be built on top of that test net which simulates the scenario which you wish to optimize, generally, this will be built into the reinforcement learning environment script, but may be external to that — in which case, the environment script can simply fetch data from the script running the simulations. For instance, it is likely that a proper backtesting engine (a paper trading, simulated exchange, which may or may not use real data from the exchange/s it is mirroring) would be required to properly train and test a DEX aggregation and SOR deep reinforcement learning agent. Backtesting engines can be more complex than simply APIs which read from an exchange, for instance, it may be wise to incorporate functionality such that the agent’s own orders incur an impact on the market itself. One way of approaching this is to aggregate real market orders which add up to the agent’s order and to consider those as the agent’s order in that time period.</p><h3>Related Work</h3><p>In addition to domain-specific conceptual research on the ways in which machine learning can be applied to our specific problem, Deeplink is also conducting an extensive literature review into cutting-edge machine learning techniques that may be leveraged for our purposes. In this section written by <a href="https://scholar.google.co.in/citations?user=Vt5jm0IAAAAJ&amp;hl=en">Priyanka Pursani Israni</a>, some of our more interesting findings are explored.</p><h3>Reinforcement Learning for Optimization Problems</h3><p>Various studies have utilized reinforcement learning for optimization problems like game development, network optimization, etc. NVIDIA (Roy et al., 2021) has unveiled a new technique that makes use of artificial intelligence to construct circuits that are more effective, quicker, and smaller. The result is an increased level of functionality with each new generation of chips. It shows that Deep Reinforcement Learning can teach AI how to build these circuits from scratch. Figure 2 depicts the architecture of the proposed approach while fig.3. shows that the proposed method Prefix RL outperforms other state-of-the-art techniques.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*IiDiCtRUxh7QrC_c" /><figcaption>Fig 2. The architecture of the proposed model [2].</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*6qMcXMHw12_J7tHj" /><figcaption>Fig 3. Comparison of the proposed method with state-of-the-art methods.</figcaption></figure><p>The authors came up with MLGO1 (Perolat et al., 2021), a framework for systematically adding ML techniques to an industrial compiler called LLVM. For demonstration purposes, the description of how and why the machine learning models are used in place of heuristics to improve LLVM’s in lining-for-size optimization is presented. When compared to state-of-the-art LLVM -Oz, the lining-for-size model was trained using two different ML algorithms: Policy Gradient and Evolution Strategies. After months of active development, the same model, trained on a single corpus, generalizes well to a variety of real-world targets as well as to the same set of targets. This trained model property is useful for deploying ML techniques in real-world settings.</p><p>The researchers of DeepMind have proposed DeepNash (Perolat et al., 2022), an autonomous agent capable of mastering the game of Stratego with imperfect information, has been proposed by DeepMind researchers. It can play the game at a level comparable to that of a human expert. To learn Stratego on its own, DeepNash employs a model-free, deep reinforcement learning approach that does not rely on search. By directly modifying the underlying multi-agent learning dynamics, the Regularised Nash Dynamics (R-NaD) algorithm, a core component of DeepNash, converges to an approximate Nash equilibrium rather than “cycling” around it. On the Gravon games platform, where it faced off against human Stratego experts, DeepNash achieved a yearly (2022) and all-time top-3 rank, outclassing the state-of-the-art AI methods currently in use. The overview of the stratego and the proposed algorithm is given in fig. 4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*YCUJeFoXOwzEx2t-" /><figcaption>Fig. 4. Overview of Stratego game and DeepNash approach.</figcaption></figure><p>In the proposed research (Humphreys et al., 2022), the team develops a semiparametric model-based agent that can forecast future policies and values based on future behavior in a specific state. They also incorporate a retrieval mechanism that allows the model to draw on data from a sizable dataset to help inform their predictions. The authors examined this strategy in Go, a difficult game where the large combinatorial state space favors generalization over direct matching to previous experiences. Moreover, they used fast approximate nearest neighbor techniques to retrieve useful information from a dataset containing tens of millions of states used in expert demonstrations. This is a compelling demonstration of the value of large-scale retrieval in RL agents, as paying attention to it significantly improves prediction accuracy and game-play performance compared to simply using these demonstrations as training trajectories.</p><p>The AI economist (Zheng et al., 2022) introduced by Salesforce AI is a Reinforcement Learning (RL) system that outperforms Alternative Tax Systems by learning dynamic tax policies to maximize equality and productivity in simulated economies. The AI Economist significantly outperforms baselines in improving both utilitarian social welfare and the trade-off between equality and productivity in spatiotemporal economies. It does this even though new ways of avoiding taxes are coming up. It also takes into account new specialization of labor, interactions between agents, and changes in behavior. The findings prove that a two-level, deep RL approach to economics is complementary to economic theory and paves the way for an AI-based strategy to design and comprehend economic policy.</p><h3>Genetic Algorithms for Smart Order Routing and Automated Trading</h3><p>The research reviewed in this section focuses on genetic algorithms and machine learning techniques for automated trading, in contrast to the use of reinforcement learning for general optimization problem proposed above.</p><p>(Xu, 2015) has proposed a continuous-time, partial equilibrium model on the optimal strategies of HFTs without any learning or manipulative ingredients to rationalize the pinging activities that were observed in the data. By analyzing past message traffic, the author can reconstruct limit order books and provide a characterization of the optimal strategies employed by HFT when my model is solved using a viscosity metric. The model’s implications on pinging activities are then compared to the data. The result shows that pinging is not always a way to trick people and can be seen as a part of HFTs’ dynamic trading strategies.</p><p>(Liu, 2015) has proposed a Shortfall (IS) strategy using an agent-based simulation technique. The author focused to create an artificial stock market to analyze the optimal execution strategies. Mechanisms for order formation, market clearing, and information dissemination are also developed for that market. The methods utilized are genetic algorithms for numerical optimization.</p><p>(Xu and Carruthers,2018) have proposed the machine learning methods like Random forest regressor, gradient boosting regressor, multilayer perceptron regressor, and logistic regression for placing aggressive orders (orders intended to be filled immediately) by minimizing client transaction fees and achieving the best price from a transaction. Also, the proposed method determines the appropriate venue for an aggressive order. Moreover, to make a decision ensemble voting i.e., to combine all four ML models are employed. The data is collected from their trading systems which include level II data for all on-the-run US Treasury bonds from multiple venues in 2017.</p><p>(Kearns &amp; Nevmyvaka, 2013) has introduced the advantages and disadvantages of a machine learning approach to HFT and market microstructure. The authors have also taken into account the issues of pure execution across time and space, as well as the challenges of forecasting profitable shifts in strategy. They also discussed ML approaches for smart order routing in dark pools and reinforcement learning for optimized trade execution. From the study, the authors concluded that the ML techniques cannot give better optimization due to their black box nature but if focused on feature engineering and fine-tuning the hyperparameters then it’s good to go for ML methods.</p><h3>References</h3><ol><li>Sarker, I, “.Deep Cybersecurity: A Comprehensive Overview from Neural Network and Deep Learning Perspective.,” <em>SN Computer Science. 2. 10.1007/s42979–021–00535–6. , </em>2021.</li><li>Roy, R., Raiman, J., Kant, N., Elkin, I., Kirby, R., Siu, M., … &amp; Catanzaro, B. (2021, December). Prefixrl: Optimization of parallel prefix circuits using deep reinforcement learning. In <em>2021 58th ACM/IEEE Design Automation Conference (DAC)</em> (pp. 853–858). IEEE.</li><li>Trofin, M., Qian, Y., Brevdo, E., Lin, Z., Choromanski, K., &amp; Li, D. (2021). Mlgo: a machine learning guided compiler optimizations framework. <em>arXiv preprint arXiv:2101.04808</em>.</li><li>Perolat, J., de Vylder, B., Hennes, D., Tarassov, E., Strub, F., de Boer, V., … &amp; Tuyls, K. (2022). Mastering the Game of Stratego with Model-Free Multiagent Reinforcement Learning. <em>arXiv preprint arXiv:2206.15378</em>.</li><li>Humphreys, P. C., Guez, A., Tieleman, O., Sifre, L., Weber, T., &amp; Lillicrap, T. (2022). Large-Scale Retrieval for Reinforcement Learning. <em>arXiv preprint arXiv:2206.05314</em>.\</li><li>Zheng, S., Trott, A., Srinivasa, S., Parkes, D. C., &amp; Socher, R. (2022). The AI Economist: Taxation policy design via two-level deep multiagent reinforcement learning. <em>Science advances</em>, <em>8</em>(18), eabk2607.</li><li>Xu, J. (2015, November). Optimal strategies of high frequency traders. In <em>AFA 2015 Boston Meetings Paper</em>.</li><li>Liu, C. (2015). <em>Optimal Execution Strategies: A Computational Finance Approach</em> (Master’s thesis, University of Waterloo).</li><li>Renyuan Xu, Isaac Carruthers, “Machine Learning for Limit-Order Routing in Cash Treasury”. Published on June 2018 by Quantitative Brokers.</li><li>Kearns, M., &amp; Nevmyvaka, Y. (2013). Machine learning for market microstructure and high frequency trading. <em>High Frequency Trading: New Realities for Traders, Markets, and Regulators</em>.</li><li>Hindawi, Complexity, Fang Jia, Boli Yang, “Forecasting Volatility of Stock Index: Deep Learning Model with Likelihood-Based Loss Function”, 2022 <a href="https://downloads.hindawi.com/journals/complexity/2021/5511802.pdf">https://downloads.hindawi.com/journals/complexity/2021/5511802.pdf</a></li><li>OpenAI Gym, OpenAI, “gym”, <a href="https://github.com/openai/gym">https://github.com/openai/gym</a>, 2022</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb50880fb390" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Smart Order Routing: A Comprehensive Guide]]></title>
            <link>https://medium.com/@deeplinknetwork/smart-order-routing-a-comprehensive-guide-1908f872b8e7?source=rss-927ca3b92f78------2</link>
            <guid isPermaLink="false">https://medium.com/p/1908f872b8e7</guid>
            <category><![CDATA[smart-router]]></category>
            <category><![CDATA[smart-order-routing]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[guidelines]]></category>
            <dc:creator><![CDATA[Deeplink]]></dc:creator>
            <pubDate>Wed, 28 Sep 2022 17:04:09 GMT</pubDate>
            <atom:updated>2022-11-17T02:35:06.849Z</atom:updated>
            <content:encoded><![CDATA[<p>by Jack Lodge</p><p><a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e"><em>Originally posted at Deeplink Labs Publication</em></a></p><h3>Contents</h3><p>· <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#4551">Contents</a><br>· <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#2431">Introduction</a><br>· <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#affd">What Are Smart Order Routers?</a><br>· <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#016e">Why Do We Need Smart Order Routers?</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#10c2">Slippage</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#7123">Scarce Liquidity</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#dc92">Fragmented Liquidity</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#f0aa">Market Volatility and Price Inconsistencies</a><br>· <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#0b32">Nuances of Smart Order Routing</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#e820">What is a Route in the Stock Market?</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#66a2">What is a Route in Crypto?</a><br>· <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#681b">Existing Solutions</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#fe19">Uniswap Auto Router</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#22bf">SFOX Smart Routing and Net-Price Routing</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#d5f5">1Inch Pathfinder</a><br>∘ <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#dcbb">Balancer Smart Order Router</a><br>· <a href="https://medium.com/deeplink-labs/smart-order-routing-a-comprehensive-guide-928915a34f6e#be06">References</a></p><h3>Introduction</h3><p>This article is intended to instill the reader with a basic understanding of what smart order routing is in both the traditional finance and cryptocurrency spaces. Additionally, the ‘whys’ and ‘hows’ are explored, along with some existing standout solutions in crypto.</p><h3>What Are Smart Order Routers?</h3><p>The phrase ‘smart order router’ can be broken down into its constituent parts for some insight into its meaning. ‘Smart’ refers to the intelligence involved in rapid and optimal decision making, an ‘order’ is some type of instruction given with the expectation that that action will be taken, and ‘routing’ refers to setting a course aimed at reaching a final destination.</p><p>In online trading, smart order routing (SOR) is an automated process for handling orders with the intent of attaining the most desirable path across trading venues. SOR algorithms do so by following a set of rules which primarily factor liquidity into calculations intended to identify the best way of executing a given trade. In general, SORs seek liquidity.</p><p>Originating from the equities market, SOR was conceived in response to the fractured liquidity caused by the ever-increasing number of electronic trading venues and platforms. Now prevalent in all areas of electronic trading, SOR takes advantage of this fragmented liquidity by finding optimal routes for orders across venues trading a number of assets in different amounts at different prices in order to minimize losses.</p><p>Although the terms are sometimes incorrectly interchanged, SORs are not trading algorithms, SORs only consider where an order is directed, and at what price. Trading algorithms on the other hand tend to deal with the ‘what’ and ‘when’ of placing orders.</p><h3>Why Do We Need Smart Order Routers?</h3><p>The primary objective of SOR is to provide traders with optimal chances of price improvements and occasionally even identify opportunities in which the trader stands to profit from trade imbalances. They do so by following algorithmic procedures informed by liquidity and volatility data, automatically identifying the best orders to place across venues, given the desired swap. Some routers will be entirely automated and built into the execution of trading bots, while others will require manual input and serve more as a tool to human traders.</p><h3>Slippage</h3><p>To understand why SOR is a necessity in the world of electronic trading, it is important to first understand the concept of slippage. Slippage refers to the difference between the expected and actual prices of a trade, it occurs in all forms of markets and is particularly problematic in times of high volatility.</p><figure><img alt="Source: https://www.babypips.com/forexpedia/slippage" src="https://cdn-images-1.medium.com/max/644/0*846GhmaBkfibfcM5" /><figcaption>Source: <a href="https://www.babypips.com/forexpedia/slippage">https://www.babypips.com/forexpedia/slippage</a></figcaption></figure><p>Slippage generally occurs when the <a href="https://www.investopedia.com/terms/b/bid-askspread.asp">bid/ask spread</a> (the difference between the highest price a buyer is willing to pay and the lowest price a seller is willing to accept) changes between the time an order is requested and the time that the order is executed. These changes in asset price can be self-induced when placing orders of significant magnitude, as your own alteration to the supply and demand cycle can alter the price before the trade executes due to keen-eyed traders and algorithms observing your order’s placement. Slippage can also occur when there is insufficient volume at the given price point to sustain the current bid/ask spread, i.e. when there are not enough buyers and/or sellers at the given levels.</p><p>Slippage can be positive in scenarios where the expected and actual prices of an asset benefit the trader in question, yielding a profit in cases such as the bid price increasing in short trades. However, slippage is more often negative, incurring a loss in the events such as the ask price increasing in long trades. The following hypothetical example taken from <a href="https://www.investopedia.com/terms/s/slippage.asp">Investopedia</a> may help in understanding the mechanics of negative slippage:</p><ul><li>A given bid/ask price is $183.50/$183.57 on a broker interface</li><li>A market order for 100 shares is placed, intending to be filled at $183.53</li><li>In the time between placing the order and the order’s execution, the bid/ask spread lifts to $183.54/183.57</li><li>This incurs a $0.04 per share or $4 total negative slippage for the 100-share trade</li></ul><p>For a more tangible and crypto-centric example of slippage, see section 2.1 of <a href="http://test/">our article on DEX aggregators</a>.</p><p>SOR primarily aims to avoid incurring slippage losses by identifying liquidity and volatility factors across a number of sources and may also include differing order types such as limit or market orders, and more complex timed strategies such as trailing stop orders. In doing so, a SOR algorithm carves out a ‘route’ between buyer and seller, a route that may span multiple venues in order to take advantage of those venues’ liquidity depth and volatility metrics.</p><h3>Scarce Liquidity</h3><p>Cryptocurrency market venues often suffer from liquidity scarcity, rendering large transactions either impossible or infeasible due to the various losses incurred. This is largely due to the issue explored in the next section but is also a symptom of the relatively small market cap of cryptocurrencies relative to traditional markets.</p><h3>Fragmented Liquidity</h3><p>As previously mentioned, SORs both take advantage of and aim to mend the problem of fragmented liquidity, an issue facing the various cryptocurrency spaces as the number of trading venues continues to increase. Liquidity within venues is generally somewhat endemic to that ecosystem, meaning that the liquidity of one asset in that venue is separated from the liquidity of that asset in other venues. This can dramatically reduce the readily available liquidity of any given asset, unless tools are used to bridge these venues in an intelligent manner, such as via the implementation of SOR.</p><h3>Market Volatility and Price Inconsistencies</h3><p>Cryptocurrencies are not generally known for their stability (barring non-algorithmic stablecoin examples). Volatile assets are extremely prone to slippage, combined with inconsistent asset pricing across venues, SOR offers a lot of value in the way of both loss mitigation and in the potential for arbitrage opportunities.</p><h3>Nuances of Smart Order Routing</h3><p>SORs come in many forms specialized to their own ecosystems and objectives. Generally speaking, the goal is to find the best route for an order; but this can mean different things in different use cases. SORs may break orders up, spreading orders across venues, they may favor fill speed rate over fill price or vice versa. Additionally, more advanced techniques such as <a href="https://www.investopedia.com/articles/markets/050614/introduction-dark-pools.asp">dark pools</a> (private exchanges on which transactions are not made public, useful in the aforementioned cases in which your own large order would noticeably affect the market) may be leveraged in cases where extremely large orders are being placed, with the intent of minimizing immediate market reaction; or they can be configured to optimize dynamic ratio indicators such as <a href="https://www.investopedia.com/terms/v/vwap.asp#:~:text=The%20volume%2Dweighted%20average%20price%20(VWAP)%20is%20a%20measurement,by%20the%20volume%20of%20trades.">volume-weighted average price</a> (VWAP) or <a href="https://en.wikipedia.org/wiki/Time-weighted_average_price#:~:text=In%20finance%2C%20time%2Dweighted%20average,achieve%20the%20TWAP%20or%20better.">time-weighted average price</a> (TWAP).</p><h3>What is a Route in the Stock Market?</h3><p>In traditional markets, an order placed through a broker will be passed on to market makers or order flow agreement partners for execution. The broker is the middleman in this arrangement and facilitates the routes through which your order can be connected with its counterpart offer/s, often through order flow arrangements with third-party liquidity providers known as market makers.</p><p>Orders placed through <a href="https://www.investopedia.com/terms/e/ecn.asp">electronic communication networks</a> (ECN) are able to cut out the middleman broker, routing trades directly. ECNs use order books that automatically match buyers to sellers via algorithms that are generally centered on best price execution.</p><h3>What is a Route in Crypto?</h3><p>In crypto, a SOR generally searches for the most efficient route to swap tokens for another token type. SOR for centralized exchanges (CEX) is comparable to the SOR techniques used in ECN, as CEXs similarly use order books, and routing consists of matching buyers to sellers across the order book in accordance with some algorithmically optimized goal.</p><p>However, in the case of decentralized exchanges (DEX), SOR deviates somewhat in functionality from its traditional market counterpart. A more comprehensive definition of DEXs can be found in <a href="http://test/">this article</a>, but they are essentially exchanges that do away with third-party authority, enabling users to exchange assets with one another without the need to forfeit custody of their assets. This is made possible by liquidity pool smart contracts, pools of (generally two types of) tokens that users can swap for one another which use conservation functions such as the <a href="https://medium.com/phoenix-finance/understanding-the-xyk-model-of-pooled-liquidity-7340fdc20d9c">XYK model</a> in order to algorithmically price assets — these functions are often referred to as automated market makers (AMM), as they effectively fill the same role as traditional market makers by buying and selling assets to facilitate market swaps, only they do so algorithmically. In its simplest form, a user wishing to swap token A for token B will deposit token A into an A:B liquidity pool, receiving the appropriate amount of token B in return.</p><p>DEXs are however by no means impervious to the aforementioned issues of liquidity scarcity, liquidity fragmentation, and market volatility, and as such, are susceptible to slippage all the same. In fact, in many cases these factors are amplified within the current state of DEXs, as liquidity pools are many in number, fragmenting liquidity and leaving only small amounts for each pool; furthermore, liquidity issues tend to cause price fluctuations on account of the pool’s conservation functions acting to quickly rebalance the pool’s asset ratio.</p><p>Due to the nature of AMMs in DEXs, slippage due to placing large orders is often even more of an issue than in other types of venues, as the liquidity concentration of DEX liquidity pools is generally on the smaller side relative to other sources of liquidity. When swapping tokens in a DEX, you are essentially adding one asset to the liquidity pool while simultaneously removing another, the AMM conservation function then automatically rebalances the ratio of these two tokens. This means that trades in pools with small liquidity, conservation functions can overcompensate and cause dramatic price swings as the proportional ratio will be more affected — conducive to major slippage. This can be thought of analogously as the fluid displacement of a boat (an order) in a body of water (a liquidity pool); a dingy in a river will virtually have no impact on the water level, but a yacht in a pool certainly will.</p><p>With all of these factors in play, it is generally infeasible to execute large trades in DEXs without incurring large losses to slippage. To combat these effects, DEX SORs can scour across various liquidity pools in order to find swaps with the most optimal price for the user. Additionally, they may also take advantage of the liquidity depth of several pools rather than simply relying on the available liquidity of a single pool, potentially even making use of any profitable arbitrage opportunities along the way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*dZlBsQll50Fe3MKP" /><figcaption>Source: <a href="https://www.seaquake.io/blog/smart-order-routing-the-force-behind-a-balanced-market/">https://www.seaquake.io/blog/smart-order-routing-the-force-behind-a-balanced-market/</a></figcaption></figure><h3>Existing Solutions</h3><p>There are a number of existing approaches toward SOR in crypto offered by various groups, in this section, we will examine a few of them.</p><h3>Uniswap Auto Router</h3><p>Uniswap’s automated liquidity protocol allows users to swap one asset for another. To facilitate this, they have implemented an auto-router that makes a series of swaps across multiple pairs (the ‘route’) to attain the desired asset in the event of a trader creating an order for an asset that is not included in the pair pool they are trading through.</p><p>The route from one asset to another may consist of segments (points at which tokens are exchanged for other tokens along the way) between pairs. Though each segment will contribute to the overall cost of the trade in the form of gas fees.</p><p>In the event of a large order being placed or low liquidity in the pair pool, routes may also consist of multiple paths. By hedging routes, orders are protected against a singular point of loss to slippage. Introducing multiple paths will also increase the gas fees required to complete the trade as each path adds at least one additional transaction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*iWsj_K8hVFG52yTW" /><figcaption>Source: <a href="https://medium.com/@ValveFinance/building-blocks-for-dex-router-construction-analysis-acc03b9f15d8">https://medium.com/@ValveFinance/building-blocks-for-dex-router-construction-analysis-acc03b9f15d8</a></figcaption></figure><p>Routes can be split across up to seven routes, which enables a trader to take advantage of the liquidity depth of different pools. The following diagram depicts a scenario where a trader saves $134,689 on account of the Auto Router’s splitting function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*kvp96dt0i5UGrkgb" /></figure><p>The Auto Router also factors gas fees into its calculations for cost efficiency — small trades will execute with minimal hops to reduce the computations which incur gas fees. This can be seen in the following example where the net cost is reduced by 10% by not making an additional trade along the route.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*OGVqlqFaJIovz4Si" /></figure><p>The Auto Router comes complete with a user interface through which a trader can view the path of their trade before execution.</p><p>In an analysis of 4,429 on-chain swaps from June 1, 2021, Uniswap claims the Auto Router improved pricing on 13.97% of all trades and 36.84% on trades between the top ten tokens by TVL.</p><p>As of mid-December, 2021, Uniswap updated its Auto Router by introducing the following improvements:</p><p><strong>Uniswap V2 and V3 Support</strong></p><p>The Auto Router now further improves trade prices by routing trades across all Uniswap pools, including V2 and V3 pools, while optimizing gas fees in the process — meaning that traders need not consider which version of Uniswap they are using.</p><p><strong>Layer 2 Network Support</strong></p><p>All Uniswap Layer 2 networks are now supported by the Auto Router, this includes Optimistic Ethereum and Arbitrum. The intent is to combine the trade pricing efficiency of the Auto Router with the low gas fees associated with Layer 2 networks.</p><p><strong>Gas Aware Route Splitting Across V2 and V3</strong></p><p>Trades can now be split across both V2 and V3 liquidity pools to find the best price across both, improvements brought about by the introduction of a SwapRouter smart contract. To save gas fees, old router prices are compared against new router prices, and the lower of the two is selected.</p><p><strong>Real-Time Gas Estimates</strong></p><p>Gas fees may now be estimated via the application’s interface, allowing traders to further gauge the profitability of a swap.</p><p><strong>Smarter Auto Slippage</strong></p><p>Gas estimates are incorporated into slippage calculations to automatically set slippage based on the trader’s expected gas cost and trade size. For smaller trades, slippage tolerance is adjusted to improve the odds of a trade successfully executing, in order to avoid situations where the gas fees involved with failed trades require additional transactions — and thus, higher gas fees.</p><p>In this update, Uniswap also introduced a number of developer tools:</p><p>Auto Router npm Package</p><p>The Auto Router has been packaged onto the npm registry, allowing developers to quickly and easily import it into their projects.</p><p><strong>SwapRouter02.sol Contract</strong></p><p>The SwapRouter contract is fully open source and can be viewed on the project’s public GitHub repository.</p><p><strong>Swap and Add</strong></p><p>The router contract now offers atomic swap-and-add functionality, allowing liquidity provision of only one of the assets in a pool, or any ratio of the two assets (as opposed to the traditional requirement of offering a set ratio of both assets in a pool as liquidity provision).</p><p><strong>Oracle Slippage</strong></p><p>Auto Router allows a limit to be set on the relative price impact of trades as measured against V3 oracles, rather than only measuring against on-chain liquidity. For example: “a user swapping ETH for USDC could specify that their swap can only execute within one percentage point of the five-minute ETH/USDC time-weighted average price”.</p><h3>SFOX Smart Routing and Net-Price Routing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*EIrGJfQx-yHtZBt6" /></figure><p>SFOX approaches SOR with its Smart Routing algorithm, which handles orders in such a way that aims to take the best opportunity throughout a range of different trading venues.</p><p>In 2020, SFOX improved upon its Smart Routing solution by introducing its new Net-Price Routing (NPR), which touts a supposed 28% average saving on trading fees. NPR aims to address issues relating to uneven fees in crypto, and the fact that many smart order routers calculate using the gross prices of assets. NPR finds the route to execute a trade that yields the best net price, factoring fees in.</p><h3>1Inch Pathfinder</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*IbiIVJ8nvmQC4URF" /><figcaption>Source: <a href="https://blog.1inch.io/introducing-1inch-v2-defis-fastest-and-most-advanced-aggregation-protocol-c42573dc3f85">https://blog.1inch.io/introducing-1inch-v2-defis-fastest-and-most-advanced-aggregation-protocol-c42573dc3f85</a></figcaption></figure><p>1inch’s Pathfinder API includes a price discovery and routing algorithm which they use for identifying optimal paths for token swaps across liquidity pools over a number of exchanges. In particular, it is designed to take advantage of <a href="https://www.investopedia.com/terms/m/marketdepth.asp">market depth</a> to bridge between source and destination tokens when performing swaps, while considering other factors such as gas fees.</p><blockquote><em>“Thanks to improved quotes, 1inch offers, for instance, a rate for 1 sBTC-sUSD that’s better than Uniswap’s offer by almost 98%.”</em></blockquote><h3>Balancer Smart Order Router</h3><p>Balancer’s SOR solution is an “off-chain linear optimization of routing orders across pools for best price execution”. It takes an amount of input tokens and desired tokens to trade them for, then returns a list of pools and amounts that should be traded to maximize the amount of returned tokens. The tool is available for free as an npm package for use by developers who aim to implement order routing across pools.</p><p>Balancer sees SOR as an optimization problem where the aim is to find the path through a set of Balancer Pools with the highest net yield after gas costs. The algorithm adds pools to a trading set until there are no pools left where the net gain from trading with the pool would exceed the gas cost.</p><p>The current Balancer SOR runs off-chain but is EVM-tractable by design in preparation for a future on-chain release. In order to achieve EVM-tractability, the function used to calculate the spot price of a Balancer pool has been linearized — this is also useful in adjusting for the post-trade spot price change and slippage.</p><p>We can expect the price to increase after trading due to slippage, the larger the trade, the larger the resultant change in the spot price. This linearized approximation of the spot price is exactly that, an estimate, the following diagram shows the real (non-linear) spot price after a trade compared with the linearized approximation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*ugRVQfJiWN_pzonE" /></figure><p>As the spot prices of all Balancer Pools are linearized, Balancer interpolates prices and amounts to simplify their optimization problem. Imagine three Balancer Pools containing the token which is being traded, the estimated prices (EP) interest are defined as sets of prices under the following conditions:</p><ol><li>There is a pool with exactly that initial spot price</li></ol><p>OR</p><ol><li>The spot prices of two pools ‘cross’ at that price</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/0*yqRlIxu-UsGlYMfY" /></figure><p>An Example SOR Optimization Problem</p><p>For the following walkthrough, Balancer considers only Pools 1 and 2 for their explanation.</p><blockquote><em>“Let A be the amount of token in (Ai) to be traded on pool 1 such that its price increases from EP1 (initial spot price of pool 1) to EP2 (initial spot price of pool 2). The SOR’s solution for any amount of tokens lower than A is simply: “trade the entire amount with pool 1.”</em></blockquote><blockquote><em>When the amount traded is greater than A, SOR will start including pool 2 in the solution, as not doing so would mean the trader is trading some amount (Ai — A) for a higher price than they could with pool 2.</em></blockquote><blockquote><em>The solution for trading an amount B + C can be found by interpolating the trades that result in EP2 and EP3. By trading C with pool 1 and B with pool 2, both pools end at the same price (Final Price) which means that the best solution was found.”</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*6Z9OKqiPrlxrqs60" /></figure><p>Essentially, the algorithm predicts spot prices in pools as a function of the amount to be traded, identifies price points of interest, and aims to find swaps with pools that yield greater returns depending on the amount the trader actually trades.</p><h3>References</h3><ol><li>“Introducing the Auto Router”, Uniswap Protocol, Sep. 16, 2021.<a href="https://uniswap.org/blog/auto-router"> https://uniswap.org/blog/auto-router</a> (accessed Sep. 19, 2022).</li><li>“Auto Router V2”, Uniswap Protocol, Dec. 16, 2021.<a href="https://uniswap.org/blog/auto-router-v2"> https://uniswap.org/blog/auto-router-v2</a> (accessed Sep. 19, 2022).</li><li>“Smart Order Router.”<a href="https://balancer.gitbook.io/balancer/smart-contracts/sor"> https://balancer.gitbook.io/balancer/smart-contracts/sor</a> (accessed Sep. 19, 2022).</li><li>“Better Prices, Lower Fees: Announcing Net-Price Routing by SFOX”, SFOX, Feb. 05, 2020.<a href="https://www.sfox.com/blog/sfox-pricing-crypto-trading-net-price-routing/"> https://www.sfox.com/blog/sfox-pricing-crypto-trading-net-price-routing/</a> (accessed Sep. 19, 2022).</li><li>“Introducing 1inch v2 — DeFi’s fastest and most advanced aggregation protocol”, 1inch Network, Nov. 6, 2020, <a href="https://blog.1inch.io/introducing-1inch-v2-defis-fastest-and-most-advanced-aggregation-protocol-c42573dc3f85">https://blog.1inch.io/introducing-1inch-v2-defis-fastest-and-most-advanced-aggregation-protocol-c42573dc3f85</a> (accessed Sep. 19, 2022).</li><li>“Slippage”, Investopedia, May. 10, 2021, <a href="https://www.investopedia.com/terms/s/slippage.asp">https://www.investopedia.com/terms/s/slippage.asp</a> (accessed Sep. 19, 2022).</li><li>“Slippage”, Babypips, <a href="https://www.babypips.com/forexpedia/slippage">https://www.babypips.com/forexpedia/slippage</a> (accessed Sep. 19, 2022).</li><li>“Smart Order Routing (SOR)”, Babypips, <a href="https://www.babypips.com/forexpedia/sor">https://www.babypips.com/forexpedia/sor</a> (accessed Sep. 19, 2022).</li><li>“Understanding the XYK model of pooled liquidity”, Ryan Tian, Jan. 2, 2020, <a href="https://medium.com/phoenix-finance/understanding-the-xyk-model-of-pooled-liquidity-7340fdc20d9c">https://medium.com/phoenix-finance/understanding-the-xyk-model-of-pooled-liquidity-7340fdc20d9c</a> (accessed Sep. 19, 2022).</li><li>“Smart Order Routing — the Force behind a Balanced Market”, Seauake, Jul. 7, 2020, <a href="https://www.seaquake.io/blog/smart-order-routing-the-force-behind-a-balanced-market/">https://www.seaquake.io/blog/smart-order-routing-the-force-behind-a-balanced-market/</a> (accessed Sep. 19, 2022).</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1908f872b8e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>