pragma solidity ^0.5.16;

interface IERC20 {
    function totalSupply() external view returns (uint);
    function balanceOf(address account) external view returns (uint);
    function transfer(address recipient, uint amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint);
    function approve(address spender, uint amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);
}

library SafeMath {
    function add(uint a, uint b) internal pure returns (uint) {
        uint c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }
    function sub(uint a, uint b) internal pure returns (uint) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }
    function sub(uint a, uint b, string memory errorMessage) internal pure returns (uint) {
        require(b <= a, errorMessage);
        uint c = a - b;

        return c;
    }
    function mul(uint a, uint b) internal pure returns (uint) {
        if (a == 0) {
            return 0;
        }

        uint c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }
    function div(uint a, uint b) internal pure returns (uint) {
        return div(a, b, "SafeMath: division by zero");
    }
    function div(uint a, uint b, string memory errorMessage) internal pure returns (uint) {
        // Solidity only automatically asserts when dividing by 0
        require(b > 0, errorMessage);
        uint c = a / b;

        return c;
    }
}

library Address {
    function isContract(address account) internal view returns (bool) {
        bytes32 codehash;
        bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
        // solhint-disable-next-line no-inline-assembly
        assembly { codehash := extcodehash(account) }
        return (codehash != 0x0 && codehash != accountHash);
    }
}

library SafeERC20 {
    using SafeMath for uint;
    using Address for address;

    function safeTransfer(IERC20 token, address to, uint value) internal {
        callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(IERC20 token, address from, address to, uint value) internal {
        callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    function safeApprove(IERC20 token, address spender, uint value) internal {
        require((value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }
    function callOptionalReturn(IERC20 token, bytes memory data) private {
        require(address(token).isContract(), "SafeERC20: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = address(token).call(data);
        require(success, "SafeERC20: low-level call failed");

        if (returndata.length > 0) { // Return data is optional
            // solhint-disable-next-line max-line-length
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }
}

contract YearnRewards {
  using SafeERC20 for IERC20;
  using Address for address;
  using SafeMath for uint;
  
  
  IERC20 constant public yfi = IERC20(0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e);
  IERC20 constant public adai = IERC20(0xfC1E690f61EFd961294b3e1Ce3313fBD8aa4f85d);
  
  address public governance;
  
  constructor() public {
      governance = msg.sender;
  }
  
  function claimable(address _claimer) public view returns (uint) {
      uint _amount = yfi.balanceOf(_claimer);
      uint _adai = adai.balanceOf(address(this));
      uint _totalSupply = yfi.totalSupply();
      uint _balance = yfi.balanceOf(address(this));
      uint _adjTotalSupply = _totalSupply.sub(_balance);
      uint _share = _adai.mul(_amount).div(_adjTotalSupply);
      return _share;
  }
  
  function claim(uint _amount) public {
      uint _adai = adai.balanceOf(address(this));
      uint _totalSupply = yfi.totalSupply();
      uint _balance = yfi.balanceOf(address(this));
      uint _adjTotalSupply = _totalSupply.sub(_balance);
      uint _share = _adai.mul(_amount).div(_adjTotalSupply);
      
      yfi.safeTransferFrom(msg.sender, address(this), _amount);
      adai.safeTransfer(msg.sender, _share);
  }
  
  function seize(address _token, uint _amount) public {
      require(msg.sender == governance, "!governance");
      IERC20(_token).safeTransfer(governance, _amount);
  }
}