Skip to content

Commit

Permalink
remove band as oracle option
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbeefalot committed Feb 3, 2021
1 parent 2c31cf3 commit 3b18cbe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 284 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
"repository": "beefyfinance/beefy-app",
"license": "MIT",
"private": true,
"type" : "module",
"type": "module",
"dependencies": {
"@bandprotocol/bandchain.js": "^1.1.5",
"@download/blockies": "^1.0.3",
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
Expand Down
20 changes: 10 additions & 10 deletions src/features/configure/pools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2917,8 +2917,8 @@ export const pools = [
earnContractAddress: '0xD6fca61Edb1cD0259320A6E0899E8aD1044BBCda',
pricePerFullShare: 1,
tvl: 0,
oracle: 'band',
oracleId: 'BUSD/USD',
oracle: 'pancake',
oracleId: 'BUSD',
oraclePrice: 0,
depositsPaused: false,
status: 'active',
Expand All @@ -2941,8 +2941,8 @@ export const pools = [
earnContractAddress: '0x7bF55acDe006C398Bb605Ae564A8F832F80000ee',
pricePerFullShare: 1,
tvl: 0,
oracle: 'band',
oracleId: 'LINK/USD',
oracle: 'pancake',
oracleId: 'LINK',
oraclePrice: 0,
depositsPaused: false,
status: 'active',
Expand All @@ -2965,8 +2965,8 @@ export const pools = [
earnContractAddress: '0x63a2CEF28904956552714ddb226ceE96c3F0C9EF',
pricePerFullShare: 1,
tvl: 0,
oracle: 'band',
oracleId: 'DOT/USD',
oracle: 'pancake',
oracleId: 'DOT',
oraclePrice: 0,
depositsPaused: false,
status: 'active',
Expand All @@ -2989,8 +2989,8 @@ export const pools = [
earnContractAddress: '0xc16187F3eb15aEE949e7d784906d53fd7E364B26',
pricePerFullShare: 1,
tvl: 0,
oracle: 'band',
oracleId: 'USDT/USD',
oracle: 'pancake',
oracleId: 'USDT',
oraclePrice: 0,
depositsPaused: false,
status: 'active',
Expand All @@ -3013,8 +3013,8 @@ export const pools = [
earnContractAddress: '0x6F0Df0858542fC4A1c7f82c14fAA490d7763592F',
pricePerFullShare: 1,
tvl: 0,
oracle: 'band',
oracleId: 'BTC/USD',
oracle: 'pancake',
oracleId: 'BTCB',
oraclePrice: 0,
depositsPaused: false,
status: 'active',
Expand Down
90 changes: 36 additions & 54 deletions src/features/web3/fetchPrice.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import axios from 'axios';
import BandChain from '@bandprotocol/bandchain.js';
import { pools } from '../configure/pools'
import { pools } from '../configure/pools';

const endpoints = {
bakery: 'https://api.beefy.finance/bakery/price',
bakeryLp: 'https://api.beefy.finance/bakery/lps',
bandchain: 'https://poa-api.bandchain.org',
bakery: 'https://api.beefy.finance/bakery/price',
bakeryLp: 'https://api.beefy.finance/bakery/lps',
bdollarLp: 'https://api.beefy.finance/bdollar/lps',
coingecko: 'https://api.coingecko.com/api/v3/simple/price',
jetfuelLp: 'https://api.beefy.finance/jetfuel/lps',
kebabLp: 'https://api.beefy.finance/kebab/lps',
kebabLp: 'https://api.beefy.finance/kebab/lps',
narwhalLp: 'https://api.beefy.finance/narwhal/lps',
pancake: 'https://api.beefy.finance/pancake/price',
pancake: 'https://api.beefy.finance/pancake/price',
pancakeLp: 'https://api.beefy.finance/pancake/lps',
thugs: 'https://api.beefy.finance/thugs/tickers',
thugsLp: 'https://api.beefy.finance/thugs/lps',
thugs: 'https://api.beefy.finance/thugs/tickers',
thugsLp: 'https://api.beefy.finance/thugs/lps',
};

const WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
Expand All @@ -24,50 +22,36 @@ const WBNB_BUSD = `${WBNB}_${BUSD}`;
const CACHE_TIMEOUT_MS = 1 * 60 * 1000; // 1 minute(s)
const priceCache = {
cache: new Map(),
lastUpdated: undefined
}
lastUpdated: undefined,
};

function isCached(id) {
return priceCache.cache.has(id)
return priceCache.cache.has(id);
}

function getCachedPrice(id) {
return priceCache.cache.get(id);
}

function maybeUpdateCache() {
const currentTimestamp = new Date()
if (priceCache.lastUpdated && currentTimestamp.getTime() > priceCache.lastUpdated.getTime() + CACHE_TIMEOUT_MS) {
initializePriceCache()
const currentTimestamp = new Date();
if (
priceCache.lastUpdated &&
currentTimestamp.getTime() > priceCache.lastUpdated.getTime() + CACHE_TIMEOUT_MS
) {
initializePriceCache();
// console.trace('price cache updated')
}
}

// Fetch prices from different sources
// Expected format: { id1: 100, id2: 200 }
const fetchBand = async ids => {
try {
const bandchain = new BandChain(endpoints.bandchain);
const response = await bandchain.getReferenceData(ids);
const prices = {}
response.forEach(price => {
prices[price.pair] = price.rate
})
return prices;
} catch (err) {
console.error(err);
return {};
}
};

const fetchCoingecko = async ids => {
try {
const response = await axios.get(endpoints.coingecko, {
params: { ids: ids.join(','), vs_currencies: 'usd' },
});
const prices = {}
const prices = {};
for (let id in response.data) {
prices[id] = response.data[id].usd
prices[id] = response.data[id].usd;
}
return prices;
} catch (err) {
Expand All @@ -90,7 +74,7 @@ const fetchThugs = async () => {
try {
const response = await axios.get(endpoints.thugs);
const bnb = response.data[WBNB_BUSD]['last_price'];
const prices = {}
const prices = {};

for (let pair in response.data) {
const ticker = response.data[pair];
Expand All @@ -104,7 +88,7 @@ const fetchThugs = async () => {
price = bnb * ticker['last_price'];
}

prices[pair] = price
prices[pair] = price;
}

return prices;
Expand All @@ -114,7 +98,7 @@ const fetchThugs = async () => {
}
};

const fetchLP = async (endpoint) => {
const fetchLP = async endpoint => {
try {
const response = await axios.get(endpoint);
return response.data;
Expand All @@ -135,52 +119,50 @@ const fetchBakery = async () => {
};

const oracleEndpoints = {
'band': (ids) => fetchBand(ids),
'bakery-lp': () => fetchLP(endpoints.bakeryLp),
'bakery': () => fetchBakery(),
'bdollar-lp':() => fetchLP(endpoints.bdollarLp),
'coingecko': (ids) => fetchCoingecko(ids),
bakery: () => fetchBakery(),
'bdollar-lp': () => fetchLP(endpoints.bdollarLp),
coingecko: ids => fetchCoingecko(ids),
'jetfuel-lp': () => fetchLP(endpoints.jetfuelLp),
'narwhal-lp': () => fetchLP(endpoints.narwhalLp),
'pancake': () => fetchPancake(),
pancake: () => fetchPancake(),
'pancake-lp': () => fetchLP(endpoints.pancakeLp),
'thugs': () => fetchThugs(),
thugs: () => fetchThugs(),
'thugs-lp': () => fetchLP(endpoints.thugsLp),
'kebab-lp': () => fetchLP(endpoints.kebabLp)
}
'kebab-lp': () => fetchLP(endpoints.kebabLp),
};

export async function initializePriceCache () {
const currentTimestamp = new Date()
priceCache.lastUpdated = currentTimestamp
export async function initializePriceCache() {
const currentTimestamp = new Date();
priceCache.lastUpdated = currentTimestamp;

const oracleToIds = new Map();
pools.forEach(pool => {
if (!oracleToIds.has(pool.oracle)) {
oracleToIds.set(pool.oracle, []);
}
oracleToIds.get(pool.oracle).push(pool.oracleId);
})
});

const promises = [...oracleToIds.keys()].map(key => oracleEndpoints[key](oracleToIds.get(key)));
const results = await Promise.all(promises);
const allPrices = results.reduce((accPrices, curPrices) => ({...accPrices, ...curPrices}), {});
const allPrices = results.reduce((accPrices, curPrices) => ({ ...accPrices, ...curPrices }), {});
[...oracleToIds.values()].flat().forEach(id => priceCache.cache.set(id, allPrices[id]));
}


export const fetchPrice = async ({ id }) => {
if (id === undefined) {
console.error('Undefined pair');
return 0;
}
let counter = 0 // safe guard, though it shouldn't happen

let counter = 0; // safe guard, though it shouldn't happen
while (!isCached(id) && counter < 10) {
// console.trace(id, 'price not cached');
counter++;
}

maybeUpdateCache()
maybeUpdateCache();

return getCachedPrice(id) ? getCachedPrice(id) : 0;
};
Loading

0 comments on commit 3b18cbe

Please sign in to comment.