library KeysCalc {

  //引入数学函数
  using SafeMath for *;

  /**
   * 计算收到一定eth时卖出的key数量
   *
   * @param _curEth current amount of eth in contract
   * @param _newEth eth being spent
   * @return amount of ticket purchased
   */
  function keysRec(uint256 _curEth, uint256 _newEth)
  internal
  pure
  returns (uint256)
  {
    return (keys((_curEth).add(_newEth)).sub(keys(_curEth)));
  }

  /**
   * 计算出售一定key时收到的eth数量
   *
   * @param _curKeys current amount of keys that exist
   * @param _sellKeys amount of keys you wish to sell
   * @return amount of eth received
   */
  function ethRec(uint256 _curKeys, uint256 _sellKeys)
  internal
  pure
  returns (uint256)
  {
    return ((eth(_curKeys)).sub(eth(_curKeys.sub(_sellKeys))));
  }

  /**
   * 计算一定数量的eth会兑换多少key
   *
   * @param _eth eth "in contract"
   * @return number of keys that would exist
   */
  function keys(uint256 _eth)
  internal
  pure
  returns (uint256)
  {
    return ((((((_eth).mul(1000000000000000000)).mul(312500000000000000000000000)).add(5624988281256103515625000000000000000000000000000000000000000000)).sqrt()).sub(74999921875000000000000000000000)) / (156250000);
  }

  /**
   * 计算给定key数的情况下eth数量
   *
   * @param _keys number of keys "in contract"
   * @return eth that would exists
   */
  function eth(uint256 _keys)
  internal
  pure
  returns (uint256)
  {
    return ((78125000).mul(_keys.sq()).add(((149999843750000).mul(_keys.mul(1000000000000000000))) / (2))) / ((1000000000000000000).sq());
  }
}
// key计算
function ethRec(uint256 _curKeys, uint256 _sellKeys)
internal
pure
returns (uint256)
{
  return ((eth(_curKeys)).sub(eth(_curKeys.sub(_sellKeys))));
}

/**
 * 计算出售一定key时收到的eth数量
 *
 * @param _curKeys current amount of keys that exist
 * @param _sellKeys amount of keys you wish to sell
 * @return amount of eth received
 */