-
Notifications
You must be signed in to change notification settings - Fork 847
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce9fb26
commit 2423fe5
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const utils = require('../utils'); | ||
|
||
async function fetchPrice() { | ||
try { | ||
const response = await fetch('https://min-api.cryptocompare.com/data/generateAvg?fsym=ICP&tsym=USD&e=coinbase'); | ||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
const data = await response.json(); | ||
return data.RAW.PRICE; | ||
} catch (error) { | ||
throw new Error('There was a problem with the fetch operation:'); | ||
} | ||
} | ||
|
||
function parsePrometheusMetrics(data) { | ||
const lines = data.split('\n'); | ||
let metrics = new Map(); | ||
for (const line of lines) { | ||
if (line.startsWith('#')) { | ||
continue; | ||
} else { | ||
const [name, value, timestamp] = line.split(' '); | ||
metrics.set(name, value); | ||
} | ||
} | ||
return metrics; | ||
} | ||
|
||
async function fetchAndParseMetrics(url) { | ||
try { | ||
const response = await fetch(url, { | ||
method: 'GET', | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
const rawData = await response.text(); | ||
return parsePrometheusMetrics(rawData); | ||
} catch (error) { | ||
console.error('Error fetching metrics:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function fetchData() { | ||
const URL = "https://tsbvt-pyaaa-aaaar-qafva-cai.raw.icp0.io/metrics"; | ||
const res = await fetchAndParseMetrics(URL); | ||
const icp_price = await fetchPrice(); | ||
const E8S = 100000000; | ||
|
||
const tvl = Number(res.get("total_icp_deposited")) / E8S * icp_price || 0; | ||
|
||
const apy = Number(res.get("apy")) * 100 || 0; | ||
|
||
return [{ | ||
pool: `waterneuron-icp`, | ||
chain: 'icp', | ||
project: 'waterneuron', | ||
symbol: 'nICP', | ||
tvlUsd: tvl, | ||
apyBase: apy, | ||
underlyingTokens: ['nICP'], | ||
}]; | ||
} | ||
|
||
module.exports = { | ||
timetravel: false, | ||
apy: fetchData, | ||
url: 'https://waterneuron.fi', | ||
}; |