API

Building a DEX Tracker with Streamlit and Python

In this tutorial, we'll explore how to connect to **CoinGecko API**to query on-chain, decentralized exchange (DEX) data to create a DEX tracker using Streamlit and Python. Our on-chain endpoints provide access to…

Building a DEX Tracker with Streamlit and Python Hero Image

In this tutorial, we’ll explore how to connect to **CoinGecko API**to query on-chain, decentralized exchange (DEX) data to create a DEX tracker using Streamlit and Python.

Our on-chain endpoints provide access to real-time price and liquidity data for 2M+ tokens across 100+ blockchains – this includes cryptocurrencies not yet listed on CoinGecko. Fetch information such as new crypto pools, price changes and transaction volumes, which are crucial for tracking the performance of different cryptocurrency pairs on DEXes.

What is Streamlit?

Streamlit is an open-source Python library that allows you to build interactive, shareable web applications for your data projects with ease. It’s designed for people with some Python programming experience. Used by many data scientists, machine learning engineers, and backend developers, Streamlit speeds up the process of creating beautiful data-driven applications without the need for extensive front-end development skills.

What is a DEX Tracker?

A DEX tracker is a tool or application that monitors and provides insights into the trading activities happening on decentralized exchanges. It can display various metrics, including liquidity, volume, price changes, and more, helping traders make informed trading decisions. CoinGecko’s sister product, GeckoTerminal, is an example of a DEX tracker.

Prerequisites

Before we go into the code snippets, let’s review the prerequisites for building a DEX tracker:

  • A basic understanding of Python – Python is a programming language known for its ease of use, powerful data manipulation libraries, and strong community support.
  • **Streamlit –**We’ll be using this App Starter Kit to bootstrap our dashboard.
  • CoinGecko API paid subscription– We’ll be calling the CoinGecko API’s /onchainendpoints to fetch live, on-chain data from DEXes, which is a paid exclusive endpoint. Ensure you have your Pro API key ready for authentication.

With that, let’s dive in!

How to Build a DEX Tracker in 5 Steps

Step 1. Setup

To get data on all the newest pools across all supported blockchain networks, we’ll query theNew Pools List endpoint. Such data can be particularly useful for crypto traders to identify new tokens and projects entering the market, evaluate the risk associated with each liquidity pool and spot potential arbitrage opportunities.

💡 Pro-tip: The equivalent page of this data on CoinGecko’s sister product, GeckoTerminal, is New Crypto Pools.

For this project, I would suggest using a virtual environment to install and isolate your dependencies.

Step 2. Boilerplate

We used this to get started:

If you want to just copy the code directly, you can download this repo on GitHub for the quickest setup.

Step 3. Display + API Utilities

To fetch and parse the API, you’ll first need to get the associated data, followed by parsing out the raw JSON response into a format acceptable for streamlit. In this example, we created a separate utils.py file to handle these various functions.

fetch_data is a generic decorated function for APIs.

process_data takes our newly captured API JSON response and converts it into a data frame.

get_top_changes specifically gets the top3 pairs with the greatest change in a specific column. In this example, we use price_change_percentage_h1

parse_crypto_pools is used to map the response to a flat format.

plot_price_change to plot the price changes in the last 24 hours

Here’s how it all comes together:

import json

import streamlit as st

import requests

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

# Function to fetch data from the API

@st.cache_data

def fetch_data(url, headers):

response = requests.get(url, headers=headers)

if response.status_code == 200:

return response.json()

else:

st.error(f'Failed to retrieve data. Status code: {response.status_code}')

return None

# Function to process the JSON data into a DataFrame

def process_data(data):

return pd.DataFrame([pool['attributes'] for pool in data['data']])

# Function to get top 3 pairs with the greatest 1-hour volume change

def get_top_changes(df, column_name='price_change_percentage_h1'):

# Ensure the column is of type float

df[column_name] = df[column_name].astype(float)

# Sort the DataFrame based on the volume change column

top_changes = df.sort_values(by=column_name, ascending=False).head(3)

# Include the 'transactions' column in the returned DataFrame

return top_changes[['name', column_name, 'transactions_h1_sells', 'transactions_h1_buys']]

def parse_crypto_pools(json_payload):

# Convert the JSON payload to a Python dictionary

data = json_payload

# List to hold parsed pool data

parsed_pools = []

# Iterate through each pool in the data

for pool in data['data']:

# Extract relevant information from each pool

pool_info = {

'address': pool['attributes']['address'],

'name': pool['attributes']['name'],

'pool_created_at': pool['attributes']['pool_created_at'],

'fdv_usd': pool['attributes'].get('fdv_usd'),

'market_cap_usd': pool['attributes'].get('market_cap_usd'),

'volume_usd_h1': pool['attributes']['volume_usd']['h1'],

'volume_usd_h24': pool['attributes']['volume_usd']['h24'],

'price_change_percentage_h1': json.loads(pool['attributes']['price_change_percentage']['h1']),

'price_change_percentage_h24': json.loads(pool['attributes']['price_change_percentage']['h24']),

'transactions_h1_buys': pool['attributes']['transactions']['h1']['buys'],

'transactions_h1_sells': pool['attributes']['transactions']['h1']['sells'],

}

parsed_pools.append(pool_info)

return parsed_pools

def plot_price_change(df, log_scale=False):

pool_names = df['name']

# Directly use the float values from the series

price_changes_24h = [float(change) if change else 0 for change in df['price_change_percentage_h1']]

fig, ax = plt.subplots()

sns.barplot(x='price_change_percentage_h1', y='name', data=df, ax=ax)

ax.set(xlabel='Price Change Percentage (24h)', title='Price Change Percentage (24h) by Pool')

return fig

4. Main Dashboard

For the main.py, we’ll reserve this for logic related to constants, UI configuration, and running the main application.

import streamlit as st

import pandas as pd

import numpy as np

from utils import fetch_data, get_top_changes, parse_crypto_pools, plot_price_change

# Constants

BASE_URL = 'https://pro-api.coingecko.com/api/v3/onchain'

ENDPOINT = '/networks/new_pools?page=1'

API_URL = BASE_URL + ENDPOINT

HEADERS = {'accept': 'application/json', 'x-cg-pro-api-key':'INSERT_API_KEY_HERE'}

# Set the page to wide mode

st.set_page_config(layout="wide")

# Main app code

st.title('Cryptocurrency Pool Data Viewer')

# Fetch and process data

raw_data = fetch_data(API_URL, HEADERS)

if raw_data:

df = pd.DataFrame(parse_crypto_pools(raw_data))

top_volume_changes = get_top_changes(df, 'price_change_percentage_h1')

# Display the top 3 price changes

st.subheader('⭐️ Top 3 Pairs by 1-Hour Price Change')

columns = st.columns(3) # Create three columns for the top 3 changes

for index, (col, row) in enumerate(zip(columns, top_volume_changes.iterrows())):

with col:

st.metric(label=row[1]['name'], value=f"{row[1]['price_change_percentage_h1']:.2f}%")

st.text(f"Buys in last hour: {row[1]['transactions_h1_buys']}")

st.text(f"Sells in last hour: {row[1]['transactions_h1_sells']}")

# Create two columns for the data table and the bar chart

col1, col2 = st.columns([2,3])

with col1:

# Display data table in the first column

st.subheader('🏓 Data Overview')

st.write("", df)

with col2:

# Visualization: Bar Chart for Price Change Percentage in the second column

st.subheader("📊 Price Change Percentage (24h)")

if 'price_change_percentage_h1' in df.columns:

fig_price_change = plot_price_change(df)

st.pyplot(fig_price_change, use_container_width=True)

The Final Result: Crypto Pools DEX Tracker on Streamlit

If you’ve setup your dependencies and code properly, you can now run streamlit run main.py in your terminal, after which the below dashboard should appear in your browser:

By following these steps, you can easily build a DEX dashboard to visualize on-chain data from decentralized exchanges. This guide serves as a foundation, and you can expand it by adding more features and interactivity.

Enhancing Your Dashboard: 5 Popular On-Chain DEX API Endpoints

For more details on enhancing your dashboard, you may refer to this Streamlit documentation as well as leverage other popular on-chain DEX data endpoints on CoinGecko API, exclusively for paid subscribers:

  1. /onchain/networks/{network}/pools – Get top pools on a network
  2. /onchain/networks/{network}/trending_pools – Get trending pools on a network
  3. /onchain/networks/{network}/tokens/{token_address}/pools – Get top pools for a token, with a provided token contract address
  4. /onchain/search/pools – Search for pool address, token address or token symbols on a network
  5. /onchain/networks/{network} /pools/{pool_address}/ohlcv/{timeframe} – Get the OHLCV chart data of a crypto pool based on a pool address on a network.

Interested in more how-to guides leveraging CoinGecko API? Check out this walk-through on how to build an artificial intelligence crypto chatbot.