// File: Pina/ApyHelper.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

interface AggregatorV3Interface {

  function decimals() external view returns (uint8);
  function description() external view returns (string memory);
  function version() external view returns (uint256);

  // getRoundData and latestRoundData should both raise "No data present"
  // if they do not have data to report, instead of returning unset values
  // which could be misinterpreted as actual reported values.
  function getRoundData(uint80 _roundId)
    external
    view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );
  function latestRoundData()
    external
    view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );

}

interface IDAO {
    function totalBonded() external view returns (uint256);
    function totalStaged() external view returns (uint256);
    function totalCouponStaged() external view returns (uint256);
    function getPrice() external view returns (uint256);
}

contract APYHelper{
    using SafeMath for uint256;

    address private constant DAO =
        address(0xd50B9219C832a762dd9a6929Dc4FeF988f65175b);
    address private constant POOL =
        address(0x03083F4fE89b899C7887E26bE3E974EbBa11E591);
    address private constant PINA =
        address(0x02814F435dD04e254Be7ae69F61FCa19881a780D);
    address private constant WETH =
        address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
    address private constant PINA_USDC =
        address(0x58624E7a53700cb39772E0267ca0AC70f064078B);
    address private constant PINA_MEME =
        address(0x713afA49478f1A33c3194Ff65dbf3c8058406670);
    address private constant MEME_ETH =
        address(0xb892A4b35F227F27e4B58cc20691B3C671D0beC8);

    // address private constant DAO =
    //     address(0xb87d9E3De5C78F69983F48Da2b810cE817F74a1e);
    // address private constant POOL =
    //     address(0xe8628B108771c2eFCa33525a879394b5ff9aA689);
    // address private constant PINA =
    //     address(0xB8e299BC7370d1cd7e66809Ad89346C97505De70);
    // address private constant PINA_USDC =
    //     address(0x4C655Aa8d9A63bC514F081475Ea1eC8127074A38);

    uint256 private epochsPerDay;
    uint256 private maxExpansionRate;
    uint256 private minExpansionRate;
    uint256 private expansionRateDivisor;
    uint256 private forgeRatio;
    uint256 private pinausdcRatio;
    uint256 private pinamemeRatio;
    uint256 private memeethRatio;

    constructor(){
        epochsPerDay = 4;
        maxExpansionRate = 3e16;
        minExpansionRate = 1e15;
        expansionRateDivisor = 10e18;
        forgeRatio = 40;
        pinausdcRatio = 27;
        pinamemeRatio = 7;
        memeethRatio = 6;
    }

    function setParam(uint256 _epochsPerDay, uint256 _maxExpansionRate, uint256 _minExpansionRate, uint256 _expansionRateDivisor, uint256 _forgeRatio, uint256 _pinausdcRatio, uint256 _pinamemeRatio, uint256 _memeethRatio) public{
        require(msg.sender == address(0x6E26579BB9b04eA358524F2AAefAf3a82dEFDaE5), "error");
        epochsPerDay = _epochsPerDay;
        maxExpansionRate = _maxExpansionRate;
        minExpansionRate = _minExpansionRate;
        expansionRateDivisor = _expansionRateDivisor;
        forgeRatio = _forgeRatio;
        pinausdcRatio = _pinausdcRatio;
        pinamemeRatio = _pinamemeRatio;
        memeethRatio = _memeethRatio;
    }

    function getParam() public view returns(uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256){
        return (epochsPerDay, maxExpansionRate, minExpansionRate, expansionRateDivisor, forgeRatio, pinausdcRatio, pinamemeRatio, memeethRatio);
    }

    function getOraclePrice() public view returns (uint256) {
        return IDAO(DAO).getPrice();
    }

    function pinaExpansionPerEpoch() public view returns (uint256) {
        uint256 totalSupply = IERC20(PINA).totalSupply();
        uint256 price = getOraclePrice();
        uint256 expansionRatePerEpoch = 0;
        if(price >= 15e17){  //1.5$
            expansionRatePerEpoch = maxExpansionRate;
        }
        else if(price >= 1003e15){
            expansionRatePerEpoch = (price.sub(1e18)).div(10);
        }
        else{
            expansionRatePerEpoch = minExpansionRate;
        }

        return totalSupply.mul(expansionRatePerEpoch).div(1e18);
    }

    //forge
    function getForgeEpochYield() public view returns (uint256) {
        uint256 pinaExpansionPerEpoch_Forge = pinaExpansionPerEpoch().mul(forgeRatio).div(100);
        uint256 totalBonded = IDAO(DAO).totalBonded();
        return pinaExpansionPerEpoch_Forge.mul(1e8).div(totalBonded);
    }

    function getForgeAPR() public view returns (uint256) {
        return getForgeEpochYield().mul(epochsPerDay).mul(365);
    }

    function getForgeTVL() public view returns (uint256) {
        uint256 totalBonded = IDAO(DAO).totalBonded();
        uint256 totalStaged = IDAO(DAO).totalStaged();
        uint256 price = getOraclePrice();
        return price.mul(totalBonded.add(totalStaged)).div(1e18);
    }
    
    // pina-usdc
    function getPinaUSDCTVL() public view returns (uint256) {
        uint256 totalSupply = IERC20(PINA_USDC).totalSupply();
        uint256 balanceOfPool = IERC20(PINA_USDC).balanceOf(POOL);
        uint256 totalPina = IERC20(PINA).balanceOf(PINA_USDC);
        uint256 balanceOfPina = balanceOfPool.mul(totalPina).div(totalSupply);
        return balanceOfPina.mul(getOraclePrice()).mul(2).div(1e18);
    }

    function getPinaUSDCEpochYield() public view returns (uint256) {
        uint256 pinaExpansionPerEpoch_PINAUSDC = pinaExpansionPerEpoch().mul(pinausdcRatio).div(100);
        return pinaExpansionPerEpoch_PINAUSDC.mul(1e8).mul(getOraclePrice()).div(getPinaUSDCTVL()).div(1e18);
    }

    function getPinaUSDCAPR() public view returns (uint256) {
        return getPinaUSDCEpochYield().mul(epochsPerDay).mul(365);
    } 

    //pina-meme
    function getPinaMemeTVL() public view returns (uint256) {
        uint256 totalSupply = IERC20(PINA_MEME).totalSupply();
        uint256 balanceOfPool = IERC20(PINA_MEME).balanceOf(POOL);
        uint256 totalPina = IERC20(PINA).balanceOf(PINA_MEME);
        uint256 balanceOfPina = balanceOfPool.mul(totalPina).div(totalSupply);
        return balanceOfPina.mul(getOraclePrice()).mul(2).div(1e18);
    }

    function getPinaMEMEEpochYield() public view returns (uint256) {
        uint256 pinaExpansionPerEpoch_PINAMEME = pinaExpansionPerEpoch().mul(pinamemeRatio).div(100);
        return pinaExpansionPerEpoch_PINAMEME.mul(1e8).mul(getOraclePrice()).div(getPinaMemeTVL()).div(1e18);
    }

    function getPinaMEMEAPR() public view returns (uint256) {
        return getPinaMEMEEpochYield().mul(epochsPerDay).mul(365);
    } 

    //meme-eth
    function getMemeETHTVL() public view returns (uint256) {
        uint256 totalSupply = IERC20(MEME_ETH).totalSupply();
        uint256 balanceOfPool = IERC20(MEME_ETH).balanceOf(POOL);
        uint256 totalWETH = IERC20(WETH).balanceOf(MEME_ETH);
        uint256 balanceOfWETH = balanceOfPool.mul(totalWETH).div(totalSupply);
        (, int256 ethPrice, , , ) = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419).latestRoundData();
        return balanceOfWETH.mul(uint256(ethPrice).mul(1e10)).mul(2).div(1e18);
    }

    function getMEMEETHEpochYield() public view returns (uint256) {
        uint256 pinaExpansionPerEpoch_MEMEETH = pinaExpansionPerEpoch().mul(memeethRatio).div(100);
        return pinaExpansionPerEpoch_MEMEETH.mul(1e8).mul(getOraclePrice()).div(getMemeETHTVL()).div(1e18);
    }

    function getMEMEETHAPR() public view returns (uint256) {
        return getMEMEETHEpochYield().mul(epochsPerDay).mul(365);
    } 

    function getAPR() public view returns (uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256){
        return (getForgeEpochYield(), getForgeAPR(), getForgeTVL(), getPinaMEMEEpochYield(), getPinaMEMEAPR(), getPinaMemeTVL(), getMEMEETHEpochYield(), getMEMEETHAPR(), getMemeETHTVL());
    }
}
// File: @openzeppelin/contracts/utils/math/SafeMath.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (utils/math/SafeMath.sol)

pragma solidity ^0.8.0;

// CAUTION
// This version of SafeMath should only be used with Solidity 0.8 or later,
// because it relies on the compiler's built in overflow checks.

/**
 * @dev Wrappers over Solidity's arithmetic operations.
 *
 * NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
 * now has built in overflow checking.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            uint256 c = a + b;
            if (c < a) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b > a) return (false, 0);
            return (true, a - b);
        }
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
            // benefit is lost if 'b' is also tested.
            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
            if (a == 0) return (true, 0);
            uint256 c = a * b;
            if (c / a != b) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the division of two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a % b);
        }
    }

    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     *
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        return a + b;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return a - b;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     *
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        return a * b;
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator.
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return a / b;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return a % b;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {trySub}.
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b <= a, errorMessage);
            return a - b;
        }
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b > 0, errorMessage);
            return a / b;
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting with custom message when dividing by zero.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryMod}.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b > 0, errorMessage);
            return a % b;
        }
    }
}

// File: @openzeppelin/contracts/token/ERC20/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}