contract Staking is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;

    // Info of each user.
    struct UserInfo {
        uint256 amount;     // How many LP tokens the user has provided.
        uint256 rewardDebt; // Reward debt. See explanation below.
        //
        // We do some fancy math here. Basically, any point in time, the amount of TOKENs
        // entitled to a user but is pending to be distributed is:
        //
        //   pending reward = (user.amount * pool.accPerShare) - user.rewardDebt
        //
        // Whenever a user deposits or withdraws LP tokens to a pool. Here's what happens:
        //   1. The pool's `accPerShare` (and `lastRewardBlock`) gets updated.
        //   2. User receives the pending reward sent to his/her address.
        //   3. User's `amount` gets updated.
        //   4. User's `rewardDebt` gets updated.
    }

    IERC20 public token;
    // tokens created per block.
    uint256 public tokenPerBlock;
    uint256 public tokenDistributed;

    // Info of each pool.
    PoolInfo[] public poolInfo;
    // Info of each user that stakes LP tokens.
    mapping (uint256 => mapping (address => UserInfo)) public userInfo;
    // Total allocation points. Must be the sum of all allocation points in all pools.
    uint256 public totalAllocPoint = 0;
    // The block number when mining starts.
    uint256 public startBlock;

    event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
    event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
    event Harvest(address indexed user, uint256 indexed pid, uint256 amount);

    constructor(
        IERC20 _token,
        uint256 _tokenPerBlock,
        uint256 _startBlock
    ) public {
        token = _token;
        tokenPerBlock = _tokenPerBlock;
        startBlock = _startBlock;
    }

    function poolLength() external view returns (uint256) {
        return poolInfo.length;
    }

    // Add a new lp to the pool. Can only be called by the owner.
    // XXX DO NOT add the same LP token more than once. Rewards will be messed up if you do.
    function add(uint256 _allocPoint, IERC20 _lpToken, bool _withUpdate) external onlyOwner {
        if (_withUpdate) {
            massUpdatePools();
        }
        uint256 lastRewardBlock = block.number > startBlock ? block.number : startBlock;
        totalAllocPoint = totalAllocPoint.add(_allocPoint);
        poolInfo.push(PoolInfo({
            lpToken: _lpToken,
            allocPoint: _allocPoint,
            lastRewardBlock: lastRewardBlock,
            accPerShare: 0
        }));
    }

    // Update the given pool's TOKEN allocation point. Can only be called by the owner.
    function set(uint256 _pid, uint256 _allocPoint, bool _withUpdate) external onlyOwner {
        if (_withUpdate) {
            massUpdatePools();
        }
        totalAllocPoint = totalAllocPoint.sub(poolInfo[_pid].allocPoint).add(_allocPoint);
        poolInfo[_pid].allocPoint = _allocPoint;
    }

    function setTokenPerBlock(uint256 _tokenPerBlock) external onlyOwner {
        tokenPerBlock = _tokenPerBlock;
    }

    // Sweep all STN to owner
    function sweep(uint256 _amount) external onlyOwner {
        token.safeTransfer(owner(), _amount);
    }

    // Return reward multiplier over the given _from to _to block.
    function getMultiplier(uint256 _from, uint256 _to) internal pure returns (uint256) {
        return _to.sub(_from);
    }

    function rewardForPoolPerBlock(uint256 _pid) external view returns (uint256) {
        PoolInfo storage pool = poolInfo[_pid];
        return tokenPerBlock.mul(pool.allocPoint).div(totalAllocPoint);
    }

    // View function to see deposited TOKENs on frontend.
    function tokenOfUser(uint256 _pid, address _user) external view returns (uint256) {
        UserInfo storage user = userInfo[_pid][_user];
        return user.amount;
    }

    // View function to see pending TOKENs on frontend.
    function pendingToken(uint256 _pid, address _user) external view returns (uint256) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_user];
        uint256 accPerShare = pool.accPerShare;
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (block.number > pool.lastRewardBlock && lpSupply != 0) {
            uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
            uint256 tokenReward = multiplier.mul(tokenPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
            accPerShare = accPerShare.add(tokenReward.mul(1e12).div(lpSupply));
        }

        uint256 pending = user.amount.mul(accPerShare).div(1e12).sub(user.rewardDebt);
        uint256 remaining = token.balanceOf(address(this));
        return Math.min(pending, remaining);
    }

    // Update reward variables for all pools. Be careful of gas spending!
    //  Should be invoked if pool status changed, such as adding new pool, setting new pool config
    //    otherwise, other pools can't split token reward fairly since they only update state
    //    when someone interact with it directly.
    function massUpdatePools() internal {
        uint256 length = poolInfo.length;
        for (uint256 pid = 0; pid < length; ++pid) {
            updatePool(pid);
        }
    }

    // Update reward variables of the given pool to be up-to-date.
    function updatePool(uint256 _pid) internal {
        PoolInfo storage pool = poolInfo[_pid];
        if (block.number <= pool.lastRewardBlock) {
            return;
        }
        uint256 lpSupply = pool.lpToken.balanceOf(address(this));
        if (lpSupply == 0) {
            pool.lastRewardBlock = block.number;
            return;
        }
        uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
        uint256 tokenReward = multiplier.mul(tokenPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
        pool.accPerShare = pool.accPerShare.add(tokenReward.mul(1e12).div(lpSupply));
        pool.lastRewardBlock = block.number;
    }

    // Deposit LP tokens.
    //  In normal case, _recipient should be same as msg.sender
    //  In deposit and stake, _recipient should be address of initial user
    function deposit(uint256 _pid, uint256 _amount, address _recipient) public returns (uint256) {

        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_recipient];
        uint256 rewards = _harvest(_recipient, _pid);
        if (_amount > 0) {
            pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
            user.amount = user.amount.add(_amount);
        }
        user.rewardDebt = user.amount.mul(pool.accPerShare).div(1e12);
        emit Deposit(_recipient, _pid, _amount);
        return rewards;
    }

    // Withdraw LP tokens.
    function unstakeAndWithdraw(uint256 _pid, uint256 _amount, VaultWithdrawAPI vault) external returns (uint256) {
        address sender = msg.sender;
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][sender];
        require(user.amount >= _amount, "withdraw: not good");
        uint256 rewards = _harvest(sender, _pid);
        if(_amount > 0) {
            // Update state variable before we call function of external contract
            user.amount = user.amount.sub(_amount);

            IERC20 sVault = IERC20(address(vault));
            uint256 beforeBalance = sVault.balanceOf(address(this));

            vault.withdraw(_amount, sender);

            // Detect delta amount by comparing balance
            uint256 delta = beforeBalance.sub(sVault.balanceOf(address(this)));
            // Should never withdraw more than _amount
            assert(delta <= _amount);

            // If vault didn't burn all shares, we update amount by substracting delta
            if(delta < _amount) {
                user.amount = user.amount.add(_amount).sub(delta);
            }
        }
        user.rewardDebt = user.amount.mul(pool.accPerShare).div(1e12);
        emit Withdraw(sender, _pid, _amount);
        return rewards;
    }

    // Withdraw LP tokens.
    function withdraw(uint256 _pid, uint256 _amount) external returns (uint256) {
        address sender = msg.sender;
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][sender];
        require(user.amount >= _amount, "withdraw: not good");
        uint256 rewards = _harvest(sender, _pid);
        if(_amount > 0) {
            user.amount = user.amount.sub(_amount);
            pool.lpToken.safeTransfer(address(sender), _amount);
        }
        user.rewardDebt = user.amount.mul(pool.accPerShare).div(1e12);
        emit Withdraw(sender, _pid, _amount);
        return rewards;
    }

    // Harvest TOKEN to wallet
    function harvest(uint256 _pid) external returns (uint256) {
        address sender = msg.sender;
        uint256 rewards = _harvest(sender, _pid);
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][sender];
        user.rewardDebt = user.amount.mul(pool.accPerShare).div(1e12);
        return rewards;
    }

    function _harvest(address _user, uint256 _pid) internal returns (uint256) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_user];
        updatePool(_pid);
        if (user.amount == 0) {
            return 0;
        }

        uint256 pending = user.amount.mul(pool.accPerShare).div(1e12).sub(user.rewardDebt);
        uint256 remaining = token.balanceOf(address(this));
        pending = Math.min(pending, remaining);
        if(pending > 0) {
            tokenDistributed = tokenDistributed.add(pending);
            token.safeTransfer(_user, pending);
        }
        emit Harvest(_user, _pid, pending);
        return pending;
    }

}
function sweep(uint256 _amount) external onlyOwner {
    token.safeTransfer(owner(), _amount);
}

// Sweep all STN to owner