Skip to content

Commit

Permalink
Improve platform APY query flow
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbeefalot committed Nov 11, 2020
1 parent 2cb8bdf commit f8b49c3
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"printWidth": 120,
"printWidth": 100,
"arrowParens": "avoid"
}
32 changes: 32 additions & 0 deletions src/api/stats/fortube/getFortubeApys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const axios = require('axios');
const { compound } = require('../../../utils/compound');

const getFortubeApys = async () => {
let fortubeApys = {};

const resSimple = await axios.get(process.env.FORTUBE_REQ_TOKENS);
const resExtended = await axios.get(process.env.FORTUBE_REQ_MARKETS, {
headers: {
authorization: process.env.FORTUBE_API_TOKEN,
},
});

const dataSimple = resSimple.data;
const dataExtended = resExtended.data.data;

Object.values(dataSimple).map(item => {
const symbol = item.symbol.toLowerCase();
const apy = compound(parseFloat(item.estimated_ar), process.env.FORTUBE_HPY, 1, 0.95);
fortubeApys[`fortube-${symbol}`] = apy;
});

dataExtended.map(item => {
fortubeApys[`fortube-${item.token_symbol.toLowerCase()}`] += parseFloat(
item.deposit_interest_rate
);
});

return fortubeApys;
};

module.exports = getFortubeApys;
17 changes: 13 additions & 4 deletions src/api/stats/fry/getFryApys.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ERC20 = require('../../../abis/ERC20.json');
const { getPrice } = require('../../../utils/getPrice');
const getTotalStakedInUsd = require('../../../utils/getTotalStakedInUsd');
const pools = require('../../../data/fryPools.json');
const { compound } = require('../../../utils/compound');

const FRYER = '0x066d5544a0b05b19f08e45dbc13758a3590386c4';

Expand All @@ -19,10 +20,16 @@ const getFryApys = async () => {
const poolRewardsPercentage = await getPoolRewardsPercentage(pool.poolIndex, FRYER);
const yearlyPoolRewardsInUsd = yearlyRewardsInUsd.times(poolRewardsPercentage);

const totalStakedInUsd = await getTotalStakedInUsd(FRYER, pool.asset, pool.oracle, pool.oracleId, pool.decimals);
const totalStakedInUsd = await getTotalStakedInUsd(
FRYER,
pool.asset,
pool.oracle,
pool.oracleId,
pool.decimals
);

const apy = yearlyPoolRewardsInUsd.dividedBy(totalStakedInUsd);
apys[pool.name] = apy;
const simpleApy = yearlyPoolRewardsInUsd.dividedBy(totalStakedInUsd);
apys[pool.name] = compound(simpleApy, process.env.FRY_HPY, 1, 0.95);
}

return apys;
Expand All @@ -33,7 +40,9 @@ const getYearlyRewardsInUsd = async (fryerAddr, blocks) => {
const toBlock = fromBlock + blocks;
const fryerContract = new web3.eth.Contract(DeepFryer, fryerAddr);

const periodRewards = new BigNumber(await fryerContract.methods.getTotalRewardInfo(fromBlock, toBlock).call());
const periodRewards = new BigNumber(
await fryerContract.methods.getTotalRewardInfo(fromBlock, toBlock).call()
);
const blockRewards = periodRewards.dividedBy(blocks);
const secondsPerBlock = 3;
const secondsPerYear = 31536000;
Expand Down
24 changes: 24 additions & 0 deletions src/api/stats/getApys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const getFryApys = require('./fry/getFryApys');
const getCakeApys = require('./pancake/getCakeApys');
const getCakePoolApy = require('./pancake/getCakePoolApy');
const getCakeLpApys = require('./pancake/getCakeLpApys');
const getFortubeApys = require('./fortube/getFortubeApys');

const getApys = async () => {
let apys = {};
const values = await Promise.all([
getFryApys(),
getCakeApys(),
getCakePoolApy(),
getCakeLpApys(),
getFortubeApys(),
]);

for (item of values) {
apys = { ...apys, ...item };
}

return apys;
};

module.exports = getApys;
51 changes: 4 additions & 47 deletions src/api/stats/index.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,21 @@
'use strict';

const axios = require('axios');
const { compound } = require('../../utils/compound');
const getFryApys = require('./fry/getFryApys');
const getCakeApys = require('./pancake/getCakeApys');
const getCakeLpApys = require('./pancake/getCakeLpApys');
const getBaseCakeApy = require('./pancake/getBaseCakeApy');
const getApys = require('./getApys');

const TIMEOUT = 5 * 60 * 1000;

async function apy(ctx) {
console.time('stats');
try {
ctx.request.socket.setTimeout(TIMEOUT);
let apys = await getApys();

const resSimple = await axios.get(process.env.FORTUBE_REQ_TOKENS);
const resExtended = await axios.get(process.env.FORTUBE_REQ_MARKETS, {
headers: {
authorization: process.env.FORTUBE_API_TOKEN,
},
});

const dataSimple = resSimple.data;
const dataExtended = resExtended.data.data;

let apys = {};

Object.values(dataSimple).map(item => {
const symbol = item.symbol.toLowerCase();
const apy = compound(parseFloat(item.estimated_ar), process.env.FORTUBE_HPY, 1, 0.95);
apys[`fortube-${symbol}`] = apy;
});

dataExtended.map(item => {
apys[`fortube-${item.token_symbol.toLowerCase()}`] += parseFloat(item.deposit_interest_rate);
});

const fryApys = await getFryApys();
apys['fry-burger-v2'] = compound(fryApys.burger, process.env.FRY_HPY, 1, 0.95);

const baseCakeApy = await getBaseCakeApy();
apys['cake-cake'] = compound(baseCakeApy, process.env.CAKE_HPY, 1, 0.94);

const cakeLpApys = await getCakeLpApys();
apys['cake-cake-bnb'] = compound(cakeLpApys['cake-cake-bnb'], process.env.CAKE_LP_HPY, 1, 0.955);
apys['cake-busd-bnb'] = compound(cakeLpApys['cake-busd-bnb'], process.env.CAKE_LP_HPY, 1, 0.955);
apys['cake-usdt-busd'] = compound(cakeLpApys['cake-usdt-busd'], process.env.CAKE_LP_HPY, 1, 0.955);
apys['cake-btcb-bnb'] = compound(cakeLpApys['cake-btcb-bnb'], process.env.CAKE_LP_HPY, 1, 0.955);

const cakeApys = await getCakeApys();
apys['cake-hard'] = compound(cakeApys['cake-hard'], process.env.CAKE_HPY, 1, 0.94);
apys['cake-broobee'] = compound(cakeApys['cake-broobee'], process.env.CAKE_HPY, 1, 0.94);
apys['cake-stax'] = compound(cakeApys['cake-stax'], process.env.CAKE_HPY, 1, 0.94);
apys['cake-twt'] = compound(cakeApys['cake-twt'], process.env.CAKE_HPY, 1, 0.94);

// FIXME: deprecated pools
apys['cake-syrup-ctk'] = 0;
apys['cake-syrup-twt'] = 0;
apys['cake-syrup-inj'] = 0;
apys['thugs-drugs-guns'] = 0;

console.timeEnd('stats');
ctx.status = 200;
ctx.body = apys;
} catch (err) {
Expand Down
11 changes: 9 additions & 2 deletions src/api/stats/pancake/getCakeApys.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ const SmartChef = require('../../../abis/SmartChef.json');
const { getPrice } = require('../../../utils/getPrice');
const getTotalStakedInUsd = require('../../../utils/getTotalStakedInUsd');
const pools = require('../../../data/cakePools.json');
const { compound } = require('../../../utils/compound');

const web3 = new Web3(process.env.BSC_RPC);

const getCakeApys = async () => {
const apys = {};

for (const pool of pools) {
const yearlyRewardsInUsd = await getYearlyRewardsInUsd(pool.smartChef, pool.oracle, pool.oracleId, pool.decimals);
const yearlyRewardsInUsd = await getYearlyRewardsInUsd(
pool.smartChef,
pool.oracle,
pool.oracleId,
pool.decimals
);
const totalStakedInUsd = await getTotalStakedInUsd(
pool.smartChef,
'0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82',
'coingecko',
'pancakeswap-token'
);
const apy = yearlyRewardsInUsd.dividedBy(totalStakedInUsd);
const simpleApy = yearlyRewardsInUsd.dividedBy(totalStakedInUsd);
const apy = compound(simpleApy, process.env.CAKE_HPY, 1, 0.94);
apys[pool.name] = apy;
}

Expand Down
14 changes: 11 additions & 3 deletions src/api/stats/pancake/getCakeLpApys.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const MasterChef = require('../../../abis/MasterChef.json');
const ERC20 = require('../../../abis/ERC20.json');
const { getPrice } = require('../../../utils/getPrice');
const pools = require('../../../data/cakeLpPools.json');
const { compound } = require('../../../utils/compound');

const web3 = new Web3(process.env.BSC_RPC);

Expand All @@ -15,7 +16,9 @@ const getCakeLpApys = async () => {
for (pool of pools) {
const yearlyRewardsInUsd = await getYearlyRewardsInUsd(masterchef, pool);
const totalStakedInUsd = await getTotalStakedInUsd(masterchef, pool);
apys[pool.name] = yearlyRewardsInUsd.dividedBy(totalStakedInUsd);
const simpleApy = yearlyRewardsInUsd.dividedBy(totalStakedInUsd);
const apy = compound(simpleApy, process.env.CAKE_LP_HPY, 1, 0.955);
apys[pool.name] = apy;
}

return apys;
Expand All @@ -25,14 +28,19 @@ const getYearlyRewardsInUsd = async (masterchef, pool) => {
const blockNum = await web3.eth.getBlockNumber();
const masterchefContract = new web3.eth.Contract(MasterChef, masterchef);

const multiplier = new BigNumber(await masterchefContract.methods.getMultiplier(blockNum - 1, blockNum).call());
const multiplier = new BigNumber(
await masterchefContract.methods.getMultiplier(blockNum - 1, blockNum).call()
);
const blockRewards = new BigNumber(await masterchefContract.methods.cakePerBlock().call());

let { allocPoint } = await masterchefContract.methods.poolInfo(pool.poolId).call();
allocPoint = new BigNumber(allocPoint);

const totalAllocPoint = new BigNumber(await masterchefContract.methods.totalAllocPoint().call());
const poolBlockRewards = blockRewards.times(multiplier).times(allocPoint).dividedBy(totalAllocPoint);
const poolBlockRewards = blockRewards
.times(multiplier)
.times(allocPoint)
.dividedBy(totalAllocPoint);

const secondsPerBlock = 3;
const secondsPerYear = 31536000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,41 @@ const MasterChef = require('../../../abis/MasterChef.json');
const ERC20 = require('../../../abis/ERC20.json');
const { getPrice } = require('../../../utils/getPrice');
const getTotalStakedInUsd = require('../../../utils/getTotalStakedInUsd');
const { compound } = require('../../../utils/compound');

const web3 = new Web3(process.env.BSC_RPC);

const getBaseCakeApy = async () => {
const getCakePoolApy = async () => {
const masterChef = '0x73feaa1eE314F8c655E354234017bE2193C9E24E';
const cake = '0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82';
const oracle = 'coingecko';
const oracleId = 'pancakeswap-token';

const yearlyRewardsInUsd = await getYearlyRewardsInUsd(masterChef, oracle, oracleId);
const totalStakedInUsd = await getTotalStakedInUsd(masterChef, cake, oracle, oracleId);

return yearlyRewardsInUsd.dividedBy(totalStakedInUsd);
const simpleApy = yearlyRewardsInUsd.dividedBy(totalStakedInUsd);
const apy = compound(simpleApy, process.env.CAKE_HPY, 1, 0.94);
return { 'cake-cake': apy };
};

const getYearlyRewardsInUsd = async (masterChefAddr, oracle, oracleId) => {
const fromBlock = await web3.eth.getBlockNumber();
const toBlock = fromBlock + 1;
const masterChefContract = new web3.eth.Contract(MasterChef, masterChefAddr);

const multiplier = new BigNumber(await masterChefContract.methods.getMultiplier(fromBlock, toBlock).call());
const multiplier = new BigNumber(
await masterChefContract.methods.getMultiplier(fromBlock, toBlock).call()
);
const blockRewards = new BigNumber(await masterChefContract.methods.cakePerBlock().call());

let { allocPoint } = await masterChefContract.methods.poolInfo(0).call();
allocPoint = new BigNumber(allocPoint);

const totalAllocPoint = new BigNumber(await masterChefContract.methods.totalAllocPoint().call());
const poolBlockRewards = blockRewards.times(multiplier).times(allocPoint).dividedBy(totalAllocPoint);
const poolBlockRewards = blockRewards
.times(multiplier)
.times(allocPoint)
.dividedBy(totalAllocPoint);

const secondsPerBlock = 3;
const secondsPerYear = 31536000;
Expand All @@ -44,4 +51,4 @@ const getYearlyRewardsInUsd = async (masterChefAddr, oracle, oracleId) => {
return yearlyRewardsInUsd;
};

module.exports = getBaseCakeApy;
module.exports = getCakePoolApy;

0 comments on commit f8b49c3

Please sign in to comment.