Skip to content

Instantly share code, notes, and snippets.

@fbslo
Created July 23, 2021 09:56
Show Gist options
  • Save fbslo/c00ca4f89cd8d16c64d5c849b4b53ce9 to your computer and use it in GitHub Desktop.
Save fbslo/c00ca4f89cd8d16c64d5c849b4b53ce9 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
let lastCheck = new Date().getTime()
let cache = {}
async function start(){
if (new Date().getTime() - lastCheck < 1000 * 60 * 30 && cache.eth){
return cache;
} else {
let text = await getWebsite()
let btc = await getBTC(text)
let eth = await getETH(text)
let stable = await get4BELT(text)
let getStableRate = await getStableRateFunc(text)
cache = {
btc: btc.replace('%', ""),
eth: eth.replace('%', ""),
stable: stable.replace('%', ""),
stableRate: getStableRate
}
lastCheck = new Date().getTime()
}
return cache;
}
async function getWebsite(){
try {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox']
});
const page = await browser.newPage();
await page.goto('https://belt.fi/bsc');
await page.waitForTimeout(7000);
let text = await page.content()
await browser.close();
return text;
} catch (err) {
console.error(err);
return false
}
}
async function getBTC(text){
//let split website, and then lower our search field
text = text.split("Belt Vaults")
let x = `BTC"`;
let y = `beltBTC`;
let str = text[1]
let first = str.substring(str.indexOf(x) + x.length, str.lastIndexOf(y))
let second = first.substring(first.indexOf('APR') + 'APR'.length, first.lastIndexOf('Daily'))
let third = second.substring(second.indexOf('tvl">') + 'tvl">'.length, second.lastIndexOf('</span></div><div class="apr"><span class'))
return third;
}
async function getETH(text){
text = text.split("Belt Vaults")
let x = `ETH"`;
let y = `beltETH`;
let str = text[1]
let first = str.substring(str.indexOf(x) + x.length, str.lastIndexOf(y))
let second = first.substring(first.indexOf('APR') + 'APR'.length, first.lastIndexOf('Daily'))
let third = second.substring(second.indexOf('tvl">') + 'tvl">'.length, second.lastIndexOf('</span></div><div class="apr"><span class'))
let fourth = third.slice(-5);
return fourth;
}
async function get4BELT(text){
text = text.split("Belt LP Staking")
let x = `4Belt`;
let y = `4Belt BLP`;
let str = text[1]
let first = str.substring(str.indexOf(x) + x.length, str.lastIndexOf(y))
let second = first.substring(first.indexOf('APR') + 'APR'.length, first.lastIndexOf('Daily'))
let third = second.substring(second.indexOf('tvl">') + 'tvl">'.length, second.lastIndexOf('</span></div><div class="apr"><span class'))
let fourth = third.substring(0,6);
return fourth;
}
async function getStableRateFunc(text){
text = text.split("Belt LP Staking")
let x = `1 4Belt BLP`;
let y = `4Belt BLP`;
let str = text[1]
let first = str.substring(str.indexOf(x) + x.length, str.lastIndexOf(y))
let second = first.substring(first.indexOf('=') + '='.length, first.lastIndexOf('</span><div class="tvl">'))
let third = second.replace(" ", "").replace("$", "")
return third;
}
module.exports.get = start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment