The developer’s complete guide to DEX data using Covalent

Josh Fernandez
Covalent
Published in
12 min readApr 20, 2022

The infancy and evolution of Web3 has given excitement, suspense, and curiosity for many traders, economists, financial traders, programmers, and hobbyists. Specifically, the implementation and success of cryptocurrency has given rise to a new way of earning and managing money called decentralized finance, or DeFi in short.

Decentralized finance provides users with a range of financial services similar to traditional financial industries such as banking, borrowing, insurance, and trading without the need to rely on centralized entities like banks. One of these emerging financial services is decentralized exchange (DEX) and DEX analytics dashboards and pricing feeds are in growing demand today.

In this article, I explain how you can build these tools with one unified API built by a data management start-up called Covalent. I will guide you through the different Web3 concepts that can help you prepare in building these tools, how you can start using Covalent’s API, and where you can go from there. This long-form article acts as a comprehensive teaching guide for developers and analysts who want to dive deeper into the Web3 space.

This article is published alongside April 20th’s Shipping Wednesday, which showcases Covalent’s XY=K suite of API endpoints for DEXs, which I will talk about later. Developers and DEXers can use the provided templates for building an analytics dashboard and coin ticker widget to use these new set of endpoints to view their DEX tokens and pools.

Getting started

To help keep you engaged in reading this article, I have divided it into 4 major parts:

  1. What are decentralized exchanges, automated market makers, and XY=K?
  2. What are Covalent’s XY=K endpoints?
  3. How can you build DEX tools with Covalent’s XY=K endpoints?
  4. Where do you go from here?

My hope is that you can leave this guide with a better understanding of how to build and use your custom DEX tools and Covalent’s suite of XY=K endpoints.

With that said, let’s get started!

Part 1: What are decentralized exchanges, automated market makers, and XY=K?

What is a decentralized exchange and why is it important?

According to Binance Academy, a DEX is an exchange where users trade directly from their own wallets. Unlike a centralized exchange (CEX), it does not require users to deposit funds to start trading and it does not hold their funds.

While CEXs have plenty of liquidity and allow large trades to happen, users do not hold custody of their assets. DEXs use smart contracts and on-chain transactions to reduce or eliminate the need for an intermediary.

There are two kinds of DEXs:

  • Order-book-based DEXs operate similarly to CEXs where users can buy and sell orders at either their chosen limit prices or at market prices. In this kind of DEXs, assets for the trade would be held in users’ wallets instead of the exchanges’ wallets.
  • Liquidity pool DEXs let users become the market makers by providing liquidity to a pair or pool of assets.

The XY=K endpoints that Covalent built are inspired by Uniswap, a liquidity pool DEX.

DEXs carry key limitations like lower liquidity, lack of blockchain interoperability (where you cannot trade tokens on various blockchains easily), and high gas costs resulting from a congested Ethereum network.

Despite these shortcomings, these exchanges are still in their infancy stage and are constantly in growing demand. Thus, participating in DEXs through the exchange itself or through building DEX tools is helpful because:

  • You can help maintain different liquidity pools and farms,
  • You can help Web3-related or tech companies participate in the Web3 movement, and
  • You will learn a lot about the Web3 space and how to be active in it, like how I’m writing and providing this guide.

Now, let’s dive into one of the major features of decentralized exchange: Automated Market Makers (AMMs).

What are Automated Market Makers (AMMs)?

One of the key tasks you can do in a DEX is swapping tokens, where you can pay money from one currency to buy money for another. A classic example is using US dollars (USDC) to buy Ethereum tokens (ETH) or using ETH to buy Uniswap tokens (UNI).

AMMs are protocols for DEXs that help give you the price between these two assets when you swap tokens. AMMs usually rely on a mathematical formula to price assets.

Currently, there are different formulas that AMMs use. However, the most fundamental and most well-known formula is the XY=K equation.

What is the XY=K equation?

XY=K is an equation you use for determining how much of one token, called X, is needed to swap for another token, called Y, in any DEX.

To give an example: let’s say we have an ETH-DAI liquidity pool with 20 ETH and 1,000 DAI. (Dai is the stablecoin cryptocurrency of MakerDAO) When swapping, this pool needs to maintain a constant product (K) of 20,000 (or 20 * 1,000). That means:

  • If you want to buy 1 ETH from the pool, you need to pay 52.63 DAI so that the pool can maintain the constant product of 20,000 (K) by having 19 ETH (X) and 1,052.63 DAI (Y). (Thus, 19 * 1052.63 = 20,000, satisfying XY=K.)
  • If you want to buy 5 ETH from the pool, you need to pay 333.33 DAI (or 66.67 DAI per ETH) so that the pool can maintain 20,000 by having 15 ETH and 1,333.33 DAI.
  • If you want to buy 10 ETH from the pool, you need to pay 1,000 DAI (or 100 DAI per ETH) so that the pool can maintain 20,000 by having 10 ETH and 2,000 DAI.
  • If you want to buy 19 ETH from the pool, you need to pay 19,000 DAI (or 1,000 DAI per ETH) so that the pool can maintain 20,000 by having 1 ETH and 20,000 DAI.

As you can see, as the amount of ETH you buy increases, the amount of DAI you have to pay increases at a growing rate. This equation ensures that the liquidity pool will never run dry, thus sustaining that pool for different trade values and different needs of the DEX’s user.

Source: Wikipedia — https://en.wikipedia.org/wiki/Decentralized_exchange

What are some examples of Automated Market Makers?

There are many examples of AMMs and tech companies building DEXs like Balancer (BAL) and Curve (CRV). In this article, we will take an in-depth look at two of the most popular AMMs: Uniswap and SushiSwap.

Uniswap

Uniswap is a good example of a decentralized cryptocurrency exchange. Created by Hayden Adams in November 2018, it is both the name of the company and the network protocol that it built. The Uniswap protocol is a decentralized token exchange protocol built on the Ethereum network.

On Uniswap, you can:

  • Simply swap your token directly from your wallet by sending your tokens from your wallet to Uniswap’s smart contract address and receiving your desired token in return,
  • Contribute as a liquidity provider and provide and withdraw liquidity, and
  • Participate in the platform’s Governance and vote on how Uniswap can be better for its users.

Uniswap is successful because of its innovation and use of liquidity pools (LPs) and the AMM that uses the XY=K equation.

  • Liquidity pools (LPs) are token reserves that sit on Uniswap’s smart contracts and are available for users to exchange tokens with.
  • As a review, the automated market maker algorithm is an algorithm that determines the prices of assets in the pool. As seen in our example above, it maintains a Constant Product (k) based on the amount of liquidity on both sides of the pool (namely, x and y).

In 2016, Ethereum co-founder Vitalik Buterin wrote a post on the Reddit forum and proposed an on-chain AMM that would make use of the XY=K equation. Adams picked up this algorithm and developed the Uniswap project from there.

SushiSwap

SushiSwap is another good DEX example. Inspired by Uniswap’s AMM design, you can swap, earn, lend, and borrow tokens on Ethereum. This platform is the second largest DEX by volume on the network after Uniswap and it uses its own token called SUSHI.

In SushiSwap, you can use the same functionalities that Uniswap has, plus:

  • Swap tokens and add and remove liquidity on blockchains other than Ethereum,
  • Place limit orders to buy or sell at a specific price or better,
  • Use their launchpad MISO (Minimal Initial SushiSwap Offering) to create a new project on the SushiSwap exchange easily,
  • Farm tokens using their Onsen program,
  • Lend and borrow tokens using their Kashi platform
  • Stake and receive a portion of the fees generated.

A few comments on these AMMs

The code for Uniswap, SushiSwap, and SpiritSwap (which I describe later in this article) are all open-source. You can take a look at their current features and works-in-progress in their GitHub repositories and contribute to their code by forking their current versions and working through them.

Now that you have a better picture of the structure and aspects of a decentralized exchange, let’s dive deep into how Covalent comes into that picture and why they are providing a suite of endpoints to help you build high-quality DEX tools.

Part 2: What are Covalent’s XY=K endpoints?

How does Covalent come into the picture?

For any single protocol to run on the XY=K engine, it needs a visual analytics front-end to provide users like you rich information about the health and performance of that ecosystem. To solve this problem, developers need to create custom queries for each protocol and get them curated and indexed before using them. Unfortunately, maintaining these custom queries requires a large amount of time and resources.

Covalent noticed an opportunity to improve time efficiency and remove the redundancy of creating similar data transformations. So, they built XY=K: the suite of Class B, generalized UniswapV2-like endpoints in their unified API for exchange on various blockchains.

Why did Covalent build the XY=K suite of endpoints?

The team introduced these endpoints to provide you with the richest and most robust on-chain DEX data and allow you to retrieve all the required data for any UniswapV2-like protocol (such as SpiritSwap and SushiSwap) on a variety of blockchains, simply by providing the name of the exchange and the correct chainID for the blockchain. As a result, these endpoints enable you to access any DEX-related protocol in a matter of minutes.

Because the team has extracted and indexed entire blockchains, the Covalent API can provide data on any protocol, any pair, and any token. These endpoints can be parametrized for any DEX implementation across any blockchain, giving you all the data you need in a common response format.

Let’s take a look at how Covalent is embedding cross-chain functionality in their endpoints and why this feature helps you in building Web3 tools such as DEX dashboards and pricing feeds.

Cross-chain DEXs and their benefits and challenges

If you remember, SushiSwap is one of the biggest DEXs in the market right now.

Although SushiSwap received inspiration from Uniswap’s AMM design, the company realized that users would find it helpful if they were allowed to trade and provide liquidity for several blockchains, not just one. Thus, the SushiSwap platform became a cross-chain decentralized exchange and competitors are seeing the benefits of building these kinds of DEXs.

Advantages

  • Switching between multiple networks becomes easier, especially for those that are currently underused in DeFi. It does not take a long time to learn them.
  • Swapping tokens and adding and removing liquidity are faster and users can receive better prices for these transactions.
  • They improve blockchain interoperability so that different chains can interact and send and receive information with one another without the need of a third party.

Disadvantages

  • Multi-chain accessibility is restricted. DEX aggregators are ERC-20 based and can only connect to Ethereum liquidity pools.

In a similar manner, Covalent created its endpoints with cross-chain capabilities in mind by letting users like you simply provide the exchange’s name and the correct chain ID for the blockchain.

What are some successful use cases for these endpoints?

SpiritSwap

SpiritSwap is a DEX company that lets you earn money by trading, staking, and farming tokens on the Fantom Opera chain. The platform uses the SPIRIT token and its design is based on Uniswap’s AMM.

In SpiritSwap, you can:

  • Swap tokens and add and remove liquidity (just like Uniswap and SushiSwap)
  • Use their bridge to bring compatible tokens from other networks
  • Perform liquidity farming to generate high returns or rewards
  • Leverage trading using their ape mode
  • Participate in the Governance of the platform

SpiritSwap uses Covalent’s XY=K endpoints to build their DEX dashboard and provide the above features for their users. Specifically, they extract trading fee annual percentage rates (APRs) for their LPs, portfolio balances, and token prices.

Again, you can contribute to their open-source code on their GitHub page.

DashDEX

Sabelo Khwanzi used the XY=K endpoints to create DashDEX at one of Gitcoin’s DeFi and cross-chain interoperability hackathons to visualize key analytics around pools, swaps, liquidity, volumes, lending and borrowing stats, and trading pair positions. This project won in the DEX category!

To learn more about his project, you can watch his demo on one of the episodes of Covalent’s One Billion BUIDLs podcast.

Now that you have a better understanding of these endpoints, Covalent’s motivation for them, and how they are used in practice, let’s begin building DEX dashboards and pricing feeds!

Part 3: How can you build DEX tools with Covalent’s XY=K endpoints?

You can use the XY=K endpoints to retrieve data in standard JSON or CSV formats and display different views on your analytics website. Specifically, use the provided templates for building DEX dashboards and coin ticker widgets.

Build DEX dashboards

A DEX dashboard lets you view details about tokens and pools and properties such as liquidity, volume, transactions, and price.

An example of Covalent’s DEX dashboard

To build one, start with Covalent’s DEX dashboard template and customize views and settings as you see fit. Use this template on any (or multiple) Covalent supported blockchains to visualize key analytics around:

  • Pools, swaps, liquidity, and volumes
  • Lending and borrowing stats
  • Trading pair positions, leverage, and level of profitability
  • Fees
  • Yield farmers

Data presented can include both on-chain and off-chain information.

Build on-chain pricing feeds and oracles (like a coin ticker pricing widget)

On-chain pricing feeds and oracles lets you leverage the pool data to determine what the “exchange rate” is between two assets and use that as a price feed.

For example, you can embed Covalent’s recent DEX ticker widget template on your analytics site to help your users view your DEX tokens and pools in a few minutes.

The link to this widget will be coming soon. Stay tuned!

Part 4: Where do you go from here?

Congratulations! You are now building DEX tools and using them for either learning or improving your Web3 financial literacy. Here are some next steps for you after reading this guide.

What can you do to help in making these endpoints better?

Covalent is constantly researching and building improved sets of endpoints to use UniswapV2-like AMMs as pricing oracles. However, if you would like to receive feedback and support right now, you can head to the dedicated feedback-and-support channel on our Discord server.

To request new features on the API, users can head to the Covalent Governance Forum and make requests within the ‘Development’ category here. Feature requests will not yet go through a formal voting process on this forum. However, as we continue to decentralize the Covalent Network, our forum will facilitate more and more decision-making through the community.

Where can I learn more about DEXs, AMMs, and other topics on DeFi?

On Covalent’s XY=K endpoints and building tools using them

On learning more about decentralized finance

And that’s it!

Stay tuned for more long-form articles and teaching guides on different Web3 topics, written by the Covalent team.

--

--