// SPDX-License-Identifier: UNLICENSED

// File: contracts/interfaces/ISaltsToken.sol


pragma solidity ^0.8.9;

//TODO: Remove this interface and use reward wallet.

interface ISaltsToken {

    function transfer( address _to, uint256 _value ) external returns (bool success);

    function balanceOf(address account) external view returns (uint256);

    function registerUser(address _user, address _referer) external;

    function approve( address _spender, uint256 _value ) external returns (bool success);

    function transferFrom( address _from, address _to, uint256 _value ) external returns (bool success);

    function burn(uint256 amount) external;

    function burnFrom(address account, uint256 amount) external;

    function CurrentSupply() external view returns (uint256);

    function setRewardsWallet(address _rewardsContractAddress) external;

    function BurnedTokens() external view returns (uint256);

    function setMasterchef(address _masterchef) external;

    function burnMasterchef(address to, uint256 amount) external;

    function mintMasterchef(address to, uint256 amount) external;

    // sets developer wallet address for receiving fee
    function setDevWallet(address _devWallet) external;

    event Taxes(uint256 burnTax, uint256 devTax, uint256 rewardstax);

    event UserRegistered( address indexed user, address indexed referer, uint256 timestamp );

    event Burn(address account, uint256 amount, uint256 timestamp);

}

// File: contracts/saltzYard.sol


pragma solidity ^0.8;

interface IVault {

function setUpdater(address _updater) external;

function withdraw(uint amount, address _user) external;
}
contract StakingRewards{
    ISaltsToken public immutable rewardsToken;
    IVault Ivault;

    address public owner;

    address vault;

    address updater;

    // Duration of rewards to be paid out (in seconds)
    uint public duration;
    // Timestamp of when the rewards finish
    uint public finishAt;
    // Minimum of last updated time and reward finish time
    uint public updatedAt;
    // Reward to be paid out per second
    uint public rewardRate;
    // Sum of (reward rate * dt * 1e18 / total supply)
    uint public rewardPerTokenStored;
    // User address => rewardPerTokenStored
    mapping(address => uint) public userRewardPerTokenPaid;
    // User address => rewards to be claimed
    mapping(address => uint) public rewards;

    // Total staked
    uint public totalSupply;
    // User address => staked amount
    mapping(address => uint) public balanceOf;

    constructor(address _rewardToken) {
        owner = msg.sender;
       // stakingToken = IERC20(_stakingToken);
        rewardsToken = ISaltsToken(_rewardToken);
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "not authorized");
        _;
    }

    modifier onlyUpdater() {
        require(msg.sender == updater, "not authorized");
        _;
    }

    modifier updateReward(address _account) {
        rewardPerTokenStored = rewardPerToken();
        updatedAt = lastTimeRewardApplicable();

        if (_account != address(0)) {
            rewards[_account] = earned(_account);
            userRewardPerTokenPaid[_account] = rewardPerTokenStored;
        }

        _;
    }

    function lastTimeRewardApplicable() public view returns (uint) {
        return _min(finishAt, block.timestamp);
    }

    function rewardPerToken() public view returns (uint) {
        if (totalSupply == 0) {
            return rewardPerTokenStored;
        }

        return
            rewardPerTokenStored +
            (rewardRate * (lastTimeRewardApplicable() - updatedAt) * 1e18) /
            totalSupply;
    }

    function stake(uint _amount) external updateReward(msg.sender) {
        require(_amount > 0, "amount = 0");
        rewardsToken.transferFrom(msg.sender, address(this), _amount);
        balanceOf[msg.sender] += _amount;
        totalSupply += _amount;
    }

    function withdraw(uint _amount) external updateReward(msg.sender) {
        require(_amount > 0, "amount = 0");
        balanceOf[msg.sender] -= _amount;
        totalSupply -= _amount;
        rewardsToken.transfer(msg.sender, _amount);
    }

    function earned(address _account) public view returns (uint) {
        return
            ((balanceOf[_account] *
                (rewardPerToken() - userRewardPerTokenPaid[_account])) / 1e18) +
            rewards[_account];
    }

    function getReward() external updateReward(msg.sender) {
        uint reward = rewards[msg.sender];
        if (reward > 0) {
            rewards[msg.sender] = 0;
            //rewardsToken.transferFrom(vault,msg.sender, reward);
            Ivault.withdraw(reward, msg.sender);
        }
    }

    function setRewardsDuration(uint _duration) external onlyOwner {
        require(finishAt < block.timestamp, "reward duration not finished");
        duration = _duration;
    }

    function notifyRewardAmount(
        uint _amount
    ) external onlyUpdater updateReward(address(0)) {
        if (block.timestamp >= finishAt) {
            rewardRate = _amount / duration;
        } else {
            uint remainingRewards = (finishAt - block.timestamp) * rewardRate;
            rewardRate = (_amount + remainingRewards) / duration;
        }

        require(rewardRate > 0, "reward rate = 0");
        require(
            rewardRate * duration <= rewardsToken.balanceOf(vault),
            "reward amount > balance"
        );

        finishAt = block.timestamp + duration;
        updatedAt = block.timestamp;
    }

    function addUpdater(address _updater) public onlyOwner {
        updater = _updater;
    }

    function addVault(address _vault) public onlyOwner {
        vault = _vault;
        Ivault = IVault(vault);
    }

    function _min(uint x, uint y) private pure returns (uint) {
        return x <= y ? x : y;
    }
}

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);
}