top of page

Backend Functionality

 

Author: Gavin Stein

Backend functionality will be split into two components.

  1. Component A: Data fetching and manipulation

  2. Component B: Database interaction

This document will also attempt to set out the database structure.

Component A: Data Fetching and Manipulation

This component is responsible for pulling raw data for each pool from on-chain sources and third-party APIs. Some of this raw data will be used in the predefined ranking algorithm. Minimum functionality will include the following:

Fetch APY data

Backend will engage with on-chain and third-party API sources to fetch the data necessary to calculate APY and DPY. This calculation is done in this component of the backend and then used in the ranking formula, as well as being pushed to the database.

Fetch Impermanent Loss Data

 

Backend will engage with on-chain and/or third-party API sources to fetch data necessary to calculate impermanent loss (IL). This calculation is done in this component of the backend and then used in the ranking formula, as well as being pushed to the database.

Fetch Market Data

 

Market data consists of below.

  • Total value locked in the pool

  • 24 Hr trading volume through the pool

  • Spread of reward asset

  • Depth of reward asset

  • Price of reward asset

This data is fetched from on-chain and/or third-party APIs. This data will be used in the ranking algorithm and pushed to the database.

Fetch Protocol Safety Rating

 

Protocol safety rating data will be sourced from www.defisafety.com. This site provides a percentage safety rating of each protocol, assessed in code & team, documentation, testing, security, and access controls. This data is used in the ranking algorithm and will also be pushed to the database. Because this site doesn’t have an API, the data will be retrieved using a web scraper like beautifulsoup4.

Calculate APY/DPY

Using relevant data (TBC), APY and DPY will be calculated and pushed to the database. This is data that will be displayed on the front end.

Calculate Impermanent Loss

Using relevant data (TBC), an impermanent loss will be calculated and pushed to the database. This is data that will be displayed on the front end.

Simple IL Calculation

If number of assets == 1

  • IL risk = None

Else if all of the assets are stablecoin

  • IL risk = None

Else if any of the assets are a stablecoin

  • IL risk = Medium

Otherwise

  • IL risk = high

Robust IL Calculation

BACKEND FUNCTIONALITY
Backend functionality will be split into two components.
Component A: Data Fetching and Manipulation
Fetch APY data
Fetch impermanent loss data
Fetch market data
Fetch protocol safety rating
Calculate impermanent loss​
Calculate APY/DPY
Simple IL calculation
Robust IL calculation

A dictionary of cryptocurrency assets is created from several sources, CoinGecko’s API, DeFi Tools, and a few hand-selected assets. This is used to grab price data from CoinGecko.

The impermanent loss calculation function takes two assets and a timeframe. Each asset’s price history is queried and truncated to the days to look back. The percentage change of each asset is calculated over this period, and a ratio r is defined using these values.

This function is then iterated over each relevant pool (only those with two assets at a 1:1 distribution are currently supported, and there are a handful of assets with missing price data) for a 30-day window to calculate the actual historical impermanent loss of each pool over the last 30 days. This data is then pushed to our backend database.

Sleep is implemented several times in this loop over the database to avoid overloading the CoinGecko API and DeFi Tools when retrieving price data, as they will return JSONDecodeError if too many requests are sent over a given time.

The list of stablecoins is from CoinMarketCap (https://coinmarketcap.com/view/stablecoin/)

Calculate Rank

Using a predefined algorithm, the rank of each pool will be calculated using data fetched through on-chain and third-party APIs. This rank will be pushed to the database.

Component B: Database Interaction

This component is responsible for all interactions with the database. Functionality is quite simple and will work as setters and getters for the data. The front-end and back-end component A will engage the database through this back-end component.

Get [Data Point]

All data stored in the database must be retrievable through a getter function that can be called by either other back-end functions or front-end functions.

Set [Data Point]

Data fetched by the back end will be stored in the database through this set functionality.

Database

Below is an example of the pool database updated by Component A via Component B.

Calculate rank
Component B: Database​
Get [data point]
Set [data point]
Database
backend pool.png

 

It may also be necessary to store a static database of ERC20 tokens for making on-chain function calls. This database will look something like the following.

backend pool 2.png
bottom of page