Presenting Dfyn V2 Architecture
Since our Dfyn V2 announcement in August, we have been iterating over Dfyn in stealth mode to bring a next-generation product that guarantees a world-class trading experience. As we approach the launch of Dfyn V2, it’s perhaps the right time to shed some more light on Dfyn’s enhanced architecture and framework, which we believe will allow Dfyn to be a one-stop solution for all your trading needs.
With a significant focus on concentrated liquidity pools to benefit traders and liquidity providers alike, Dfyn V2 also supports a first-of-its-kind tick-based on-chain limit orders, an RFQ engine, and a smart order router out of the box. These offerings are best-in-class features and either meet or beat any comparable DEX bar none.
Before we dive into our architecture, we’d like to take this opportunity to give a shoutout to our community, who have stayed patient and stayed with us through thick and thin, in addition to providing valuable feedback over the past few months.
Background
When we analyze the incidence point of the first bull run, it becomes abundantly clear that DEXes played a pivotal role. It is essential to make trades, earn interest on static income, take flash loans, etc. With the foresight that up-and-coming layer 1s and layer 2s will play a pivotal role in DeFi, we launched Dfyn on Polygon in August 2020 with a vision to achieve an inclusive multi-chain DeFi ecosystem with a feature-rich, easy-to-use DEX at the heart of it. In fact, as the first DEX on Polygon, Dfyn achieved many key milestones, including but not limited to:
- Reaching a peak TVL of $300 million
- Processing over $3 billion in trades
- Introducing vDFYN vaults for the community to get fee-sharing from the protocol
- Launching on multiple chains, including Polygon, Fantom, and Arbitrum
- Introducing Build-your-own-farms (BYOF), a first-of-its-kind no-code farming product on the Polygon network
- Integrating Router Protocol’s widget to offer cross-chain swaps within Dfyn
The next bull run will also require multiple performant DEXes that have utilized the bear cycle to expand their functionalities, improve upon their vulnerabilities, and bring innovation to the fore.
One of the first things we did while going back to the drawing board for Dfyn V2 was to perform a comprehensive internal analysis and external benchmarking to identify features we needed to improve upon.
Here are all the things we needed to fix in Dfyn V1
We found that the Uniswap V2 model adopted by Dfyn was causing multiple challenges to scale operations economically with a seamless user experience. These challenges included:
- Expensive Liquidity Mining: We are all well aware that a high TVL guarantees less slippage in large orders. However, users will always park their liquidity where they will get more rewards. Hence, we had to shell out more rewards to attract large amounts of liquidity.
- Competitive Landscape: As the market has matured, more DEXes with a concentrated liquidity model have come into the mix, making it increasingly difficult for Dfyn to offer competitive prices.
How is Dfyn overcoming these challenges?
We realized that the process of building such a comprehensive offering had to be segmented into individual milestones. For Dfyn V2, we are channeling our development efforts to facilitate four core pivots to our infrastructure.
On-chain Limit Orders
One of the most exciting innovations with Dfyn V2 is undoubtedly its on-chain limit orders. To achieve an on-chain limit order, Dfyn has introduced Limit Order Ticks, which allow for highly concentrated liquidity on individual ticks, resulting in enhanced price precision. In this context, ticks can be thought of as nothing more than an index. Each tick (index) will correspond to a specific price and liquidity. This enables users to add liquidity at a precise price, similar to how order book exchanges operate.
Concentrated Liquidity and Unified Pools
To understand this concept, we first need to familiarize ourselves with the traditional AMM approach. Most AMMs followed the constant product model, based on the x*y=k price curve. In such a model, liquidity is distributed evenly across the entire interval, i.e., one could trade their assets within the infinite interval (0, ∞).
With the concentrated liquidity mechanics, liquidity providers (LPs) can accumulate their capital to smaller price intervals than (0, ∞), which enables individualized price curves, infinitely higher capital efficiency, and deeper liquidity for traders.
The concentrated liquidity model can be implemented in two ways: a) As a tiered model where different pools are created for different fee tiers. b) There is only one pool that operates with a dynamic fee. In order to limit liquidity fragmentation and impermanent loss, we are adopting the second approach.
hRFQ (High-frequency Request for Quote) Engine
As would be apparent by the name, this model allows users to trade directly against other traders. Once users submit their request for a trade, the DEX requests quotes from multiple market makers and shows the best quote to the user. The user then has the option to accept or reject the trade. This model is highly capital efficient and incentivizes market makers to compete and provide the best prices to the user requesting quotes. Currently, RFQ-based orders are live on Polygon, with Avalanche and BSC to follow.
Dfyn Signal
Signal is Dfyn’s very own smart order routing system that will enable users to find the most optimal path for a trade. To do this, Dfyn Signal will:
Step 1) Fetch quotes from Dfyn’s RFQ engine, Dfyn V1 and V2 AMMs (across different pools), as well as quotes from external DEXes;
Step 2) Compute the best split, i.e., the split that guarantees the minimum slippage;
Step 3) Show it to the user on the UI (if called via the Dfyn UI) or abstract it in the form of transaction call data (if called via the Signal API/SDK).
By default, all the trades on Dfyn’s UI go through Dfyn Signal. Also, very importantly, users looking to integrate Dfyn Signal into their code can use Signal API or SDK.
Dfyn V2 Architecture
Now that we have a brief understanding of what Dfyn V2 offers, let us dive into the Dfyn V2 architecture, which mainly consists of 4 modules:
1) Periphery & Core Contracts
Core Contracts
Dfyn’s core contracts form the backbone of the Dfyn platform. They are responsible for handling the various functions of the platform, such as facilitating transactions, managing liquidity, and enforcing the platform’s rules. The three core contracts that act as the central point of control for the Dfyn platform are as follows:
- Concentrated Liquidity Pool — The ConcentratedLiquidityPool contract encapsulates the implementation of a pool in the Dfyn V2 protocol. It includes functions to mint (alter/create a mapping of the user’s position on the contract), burn (alter/remove user’s position on the contract), and swap tokens within the pool while tracking the coordinates of upper and lower price boundaries using ticks. The contract also handles the creation and management of limit orders.
- Pool Deployer — The ConcentratedLiquidityPoolFactory (a periphery contract) uses this contract to deploy new pools with the specified configurations.
- Vault — In the interest of minimizing security vulnerabilities in Dfyn V2, we are introducing a dedicated vault contract to store and handle all the user funds rather than storing them in pool contracts. This vault will allow users to borrow tokens through a Flash Loan mechanism. For the uninitiated, a flash loan, also termed as a One Block Borrow, is a loan that allows an entity to borrow a certain amount of tokens, given that they pay it back before the end of the same transaction.
Periphery Contracts
Periphery contracts are high-level contracts responsible for handling core contracts and maintaining position-level data for concentrated pool liquidity and limit orders. Dfyn V2 comprises five major periphery contracts:
- Router — The Dfyn Router contract is a smart contract that acts as a “router” for transactions on the Dfyn V2 platform. E.g., when a user wants to swap one token for another on Dfyn V2 AMM, they can use the Router contract to do so. The Router contract automatically finds the best exchange rate for the desired swap and executes the trade on the user’s behalf.
- Concentrated Pool Factory — The ConcentratedPoolFactory contract acts as a “factory” for creating new pools on the Dfyn V2 platform. Users can use this contract to create a new pool. Furthermore, in Dfyn V2, we have introduced multi-factory compatibility, meaning that users can create multiple types of pools (customized pools) with the help of different factory types.
- Concentrated Pool Manager — The ConcentratedPoolManager is responsible for handling liquidity in the concentrated liquidity pools. This contract has functions such as adding or removing liquidity and collecting fees. Moreover, this contract is responsible for minting NFTs to liquidity providers as proof of their positions and maintains position-level data.
- LimitOrder Manager — The LimitOrderManager contract handles limit orders in the concentrated liquidity pools — it has functions to create, claim & cancel limit orders. In addition, the LimitOrderManager is responsible for minting NFTs to limit order creators as proof of their limit orders and maintains position-level data.
- Quoter Contracts — Quoter contracts are responsible for fetching quotes from various liquidity pools in Dfyn V1, Dfyn V2, and external DEXes.
2) hRFQ Engine
As mentioned above, we have already launched an hRFQ (High-Frequency Request For Quote) engine, a no-slippage peer-to-peer trading technology between users and market makers. Our hRFQ engine consists of two modules:
- RFQ API — Responsible for getting the quote from the market makers with their signatures.
- RFQ Contract — Responsible for verifying the signature from the market makers, post which they handle the transfer of funds between the user and the market maker.
3) Dfyn Signal
In Dfyn V2, we are introducing Dfyn Signal, a multi-chain compatible smart trade-finding engine capable of finding the most optimal trade for the user by comparing quotes across different token paths across various protocols like Dfyn V1, V2, Dfyn’s hRFQ engine, and external DEXes. Dfyn Signal consists of two components:
- Signal API — Fetches the best trade for the users by comparing different token paths & protocols mentioned above. This API powers Dfyn’s UI.
- Signal SDK — To simplify the process of querying Dfyn Signal and executing the quotes, we have abstracted it in our JavaScript SDK.
4) Trade Engine
The trade engine is responsible for executing trades for the user across different protocols and hRFQ with the quote data provided by Dfyn Signal.
Dfyn V2 Workflow
Swap
Step 1) User comes to Dfyn UI with a trade request.
Step 2) Dfyn UI queries Dfyn Signal with the parameter of the trades.
Step 3) Dfyn Signal fetches a quote from all the relevant sources: Dfyn V1 & V2 AMM, Dfyn RFQ, and other external DEXes.
Step 4) Dfyn Signal computes the best trade split and returns it to the UI.
Step 5) If the user approves the quote, a trade is initiated via Dfyn’s trade engine.
Add Liquidity
Step 1) A user initiates a request to add liquidity to a particular pool via the ConcentratedPoolManager contract.
Step 2) The ConcentratedPoolManager asks the ConcentratedLiquidityPool contract (referred to as Pool in the GIF attached above) to create a mapping for the user and return the relevant details regarding the user’s liquidity, including the user’s share of the pool.
Step 3) Once it receives the details from the ConcentratedLiquidityPool, the ConcentratedPoolManager triggers to vault to take funds from the user.
Step 4) The Vault contract deducts the specified amount from the user’s wallet.
Step 5) The ConcentratedPoolManager generates an NFT for the user as proof of the user’s stake in the liquidity pool.
Step 6) The NFT is transferred to the user’s wallet.
Create Limit Order
Step 1) A user initiates a request to create a limit order against a particular pool via the LimitOrderPoolManager contract.
Step 2) The LimitOrderPoolManager asks the ConcentratedLiquidityPool contract (referred to as Pool in the GIF attached above) to create a mapping for the user and return the relevant details regarding the user’s limit order.
Step 3) Once it receives the details from the ConcentratedLiquidityPool, the LimitOrderPoolManager triggers to vault to take funds from the user.
Step 4) The Vault contract deducts the specified amount from the user’s wallet.
Step 5) The ConcentratedPoolManager generates an NFT for the user as proof of the user’s limit order against the liquidity pool.
Step 6) The NFT is transferred to the user’s wallet.
Concluding Remarks
At Dfyn, we have a saying, “Charge like a bull and build like a bear.” While the bull cycles allow us to increase our reach to a much larger demographic, the bear cycles allow us to conduct extensive introspection and strategize pivots that can extend product functionalities for a much better user experience. Keeping this philosophy in mind, we are actively working towards bringing Dfyn V2 to you as soon as possible. We hope the community is as excited about Dfyn V2 as we are.
About Dfyn
Dfyn’s vision is to become a one-stop solution for all trading-related activities. Dfyn is a fully decentralized multi-chain trading exchange that offers cross-chain swaps powered by Router Protocol and has now launched RFQ-based orders for better capital efficiency.
Website: https://dfyn.network/
Exchange: https://exchange.dfyn.network/#/
Telegram: https://t.me/Dfyn_HQ
Discord: https://discord.gg/yjM2fUUHvN
Twitter: https://twitter.com/_DFyn
Telegram Announcements: https://t.me/dfynofficial