From fc2d6cfb6a10221b123d78c4faa33b3e52edc6d5 Mon Sep 17 00:00:00 2001 From: Angelos Kappos Date: Thu, 9 May 2024 17:17:12 +0200 Subject: [PATCH] IndigoProtocol/ Add Yields for Indigo Stability Pools (#1301) * IndigoProtocol/ Add Yields for Indigo Stability Pools - create appropriate functionality to get APY for each Stability Pool - fetch Prices/APR/AssetAnalytics/ADA price to USD - calculate TVL * IndigoProtocol/ Add Indigo Stability Pools - update response of TVL - update APY rewards * IndigoProtocol/ Add Indigo Stability Pools - update response of TVL - update APY rewards --- src/adaptors/indigo/http-helper.js | 22 ++++++++ src/adaptors/indigo/index.js | 84 ++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/adaptors/indigo/http-helper.js create mode 100644 src/adaptors/indigo/index.js diff --git a/src/adaptors/indigo/http-helper.js b/src/adaptors/indigo/http-helper.js new file mode 100644 index 0000000000..3022dc700a --- /dev/null +++ b/src/adaptors/indigo/http-helper.js @@ -0,0 +1,22 @@ +const axios = require("axios") + +async function get(endpoint, options) { + try { + return (await axios.get(endpoint, options)).data + } catch (e) { + throw new Error(`Failed to get ${endpoint}`) + } +} + +async function post(endpoint, body, options) { + try { + return (await axios.post(endpoint, body, options)).data + } catch (e) { + throw new Error(`Failed to post ${endpoint}`) + } +} + +module.exports = { + get, + post +} \ No newline at end of file diff --git a/src/adaptors/indigo/index.js b/src/adaptors/indigo/index.js new file mode 100644 index 0000000000..946ade49e9 --- /dev/null +++ b/src/adaptors/indigo/index.js @@ -0,0 +1,84 @@ +const { get, post } = require('./http-helper') + +// Get Pool Data for each Stability Pool +async function apy() { + const stabilityPools = await fetchStabilityPools(); + const adaPriceUsd = await fetchAdaPriceToUsd(); + + return Promise.all(stabilityPools.map(async (pool) => { + const adaRewardsKey = `sp_${pool.asset}_ada`; + const indyRewardsKey = `sp_${pool.asset}_indy`; + + const [adaApy, indyApy] = await Promise.all([ + fetchApr(adaRewardsKey), + fetchApr(indyRewardsKey) + ]); + + const assetAnalytics = await fetchAssetAnalytics(pool.asset); + const tvlUsd = await calculateTvlUsd(assetAnalytics, adaPriceUsd); + + return { + pool: pool.asset, + chain: "Cardano", + project: "indigo", + symbol: pool.asset, + apyReward: (adaApy || 0) + (indyApy || 0), + rewardTokens: ['ADA', 'INDY'], + underlyingTokens: [pool.asset], + tvlUsd: Number(tvlUsd), + }; + })); +} + +// fetch stability pools +async function fetchStabilityPools() { + return await get(`https://analytics.indigoprotocol.io/api/stability-pools`); +} + +// Fetch APR for each Stability Pool +async function fetchApr(key) { + return await post(`https://analytics.indigoprotocol.io/api/apr/?key=${key}`) + .then(res => { + return parseFloat(res.value) || 0; + }) + .catch((error) => { + console.error(`Error fetching APR for ${key}:`, error); + return 0; + }); +} + +// Fetch iAsset analytics for a specific asset +async function fetchAssetAnalytics(assetName) { + try { + const response = await get(`https://analytics.indigoprotocol.io/api/assets/${assetName}/analytics`); + return response[assetName]; + } catch (error) { + console.error(`Error fetching analytics for ${assetName}:`, error); + return null; + } +} + +// Fetch the ADA price in USD +async function fetchAdaPriceToUsd() { + try { + const response = await get('https://analytics.indigoprotocol.io/api/price?from=ADA&to=USD'); + return response.price; + } catch (error) { + console.error('Error fetching ADA price:', error); + return 0; + } +} + +// Calculate TVL in USD +async function calculateTvlUsd(assetAnalytics, adaPriceUsd) { + if (!assetAnalytics) return 0; + + return assetAnalytics.totalValueLocked * adaPriceUsd; +} + +module.exports = { + timetravel: false, + apy: apy, + url: 'https://app.indigoprotocol.io/stability-pools', +}; +