text: const BigNumber = require('bignumber.js');
  const { bscWeb3: web3 } = require('../../../utils/web3');
  
  const SmartChef = require('../../../abis/SmartChef.json');
  const fetchPrice = require('../../../utils/fetchPrice');
  const { getTotalStakedInUsd } = require('../../../utils/getTotalStakedInUsd');
  const { compound } = require('../../../utils/compound');
  const { BASE_HPY, BSC_CHAIN_ID } = require('../../../constants');
  const getBlockNumber = require('../../../utils/getBlockNumber');
  
  const smartChef = '0x114d54e18eb4A7Dc9bB8280e283E5799D4188E3f';
  const oracle = 'tokens';
  const oracleId = 'NAUT';
  const decimals = '1e8';
  
  const getNautApy = async () => {
    const [yearlyRewardsInUsd, totalStakedInUsd] = await Promise.all([
      getYearlyRewardsInUsd(smartChef, oracle, oracleId, decimals),
      getTotalStakedInUsd(
        smartChef,
        '0x288ea5437c7aad045a393cee2f41e548df24d1c8',
        'lps',
        'naut-naut-bnb',
      ),
    ]);
  
    const simpleApy = yearlyRewardsInUsd.dividedBy(totalStakedInUsd);
    const apy = compound(simpleApy, BASE_HPY, 1, 0.94);
    // console.log('naut', simpleApy.valueOf(), apy, totalStakedInUsd.valueOf(), yearlyRewardsInUsd.valueOf());
    return { 'naut-naut-bnb': apy };
  };
  
  const getYearlyRewardsInUsd = async (smartChefAddr, oracle, oracleId, decimals) => {
    const smartChefContract = new web3.eth.Contract(SmartChef, smartChefAddr);
  
    const currentBlock = await getBlockNumber(BSC_CHAIN_ID);
    const bonusEndBlock = await smartChefContract.methods.bonusEndBlock().call();
    const isPoolRunning = currentBlock <= bonusEndBlock;
  
    if (!isPoolRunning) return new BigNumber(0);
  
    const blockRewards = new BigNumber(await smartChefContract.methods.rewardPerBlock().call());
    const secondsPerBlock = 3;
    const secondsPerYear = 31536000;
    const yearlyRewards = blockRewards.dividedBy(secondsPerBlock).times(secondsPerYear);
    const earnedAssetPrice = await fetchPrice({ oracle, id: oracleId });
    const yearlyRewardsInUsd = yearlyRewards.times(earnedAssetPrice).dividedBy(decimals);
    return yearlyRewardsInUsd;
  };
  
  module.exports = getNautApy;
  