contract CentaurStakingV1 is Ownable {

	using SafeMath for uint;

	// Events
	event Deposit(uint256 _timestmap, address indexed _address, uint256 _amount);
	event Withdraw(uint256 _timestamp, address indexed _address, uint256 _amount);

	// CNTR Token Contract & Funding Address
	IERC20 public tokenContract = IERC20(0x03042482d64577A7bdb282260e2eA4c8a89C064B);
	address public fundingAddress = 0x6359EAdBB84C8f7683E26F392A1573Ab6a37B4b4;

	// Current rewardPercentage
	uint256 public currentRewardPercentage;
	
	// Initial & Final Reward Constants (100% => 10000)
	uint256 constant initialRewardPercentage = 1000; // 10%
	uint256 constant finalRewardPercetage = 500; // 5%

	// Rewards % decrement when TVL hits certain volume (100% => 10000)
	uint256 constant rewardDecrementCycle = 10000000 * 1 ether; // Decrement when TVL hits certain volume
	uint256 constant percentageDecrementPerCycle = 50; // 0.5%

	// Stake Lock Constants
	uint256 public constant stakeLockDuration = 30 days;

	// Stake tracking
	uint256 public stakeStartTimestamp;
	uint256 public stakeEndTimestamp;

	mapping(address => StakeInfo[]) stakeHolders;

	struct StakeInfo {
		uint256 startTimestamp;
		uint256 amountStaked;
		uint256 rewardPercentage;
		bool withdrawn;
	}

	// Total Value Locked (TVL) Tracking
	uint256 public totalValueLocked;

	/**
     * @dev Constructor
     */

	constructor() public {
		currentRewardPercentage = initialRewardPercentage;
		stakeStartTimestamp = block.timestamp + 7 days; // Stake event will start 7 days from deployment
		stakeEndTimestamp = stakeStartTimestamp + 30 days; // Stake event is going to run for 30 days
	}

	/**
     * @dev Contract Modifiers
     */

	function updateFundingAddress(address _address) public onlyOwner {
		require(block.timestamp < stakeStartTimestamp);

		fundingAddress = _address;
	}

	function changeStartTimestamp(uint256 _timestamp) public onlyOwner {
		require(block.timestamp < stakeStartTimestamp);

		stakeStartTimestamp = _timestamp;
	}

	function changeEndTimestamp(uint256 _timestamp) public onlyOwner {
		require(block.timestamp < stakeEndTimestamp);
		require(_timestamp > stakeStartTimestamp);

		stakeEndTimestamp = _timestamp;
	}

	/**
     * @dev Stake functions
     */

    function deposit(uint256 _amount) public {
    	require(block.timestamp > stakeStartTimestamp && block.timestamp < stakeEndTimestamp, "Contract is not accepting deposits at the moment");
    	require(_amount > 0, "Amount has to be more than 0");
    	require(stakeHolders[msg.sender].length < 1000, "Prevent Denial of Service");

    	// Transfers amount to contract
    	require(tokenContract.transferFrom(msg.sender, address(this), _amount));
		emit Deposit(block.timestamp, msg.sender, _amount);

    	uint256 stakeAmount = _amount;
		uint256 stakeRewards = 0;

    	// Check if deposit exceeds rewardDecrementCycle
		while(stakeAmount >= amountToNextDecrement()) {

			// Variable cache
			uint256 amountToNextDecrement = amountToNextDecrement();

			// Add new stake
	    	StakeInfo memory newStake;
	    	newStake.startTimestamp = block.timestamp;
	    	newStake.amountStaked = amountToNextDecrement;
	    	newStake.rewardPercentage = currentRewardPercentage;

	    	stakeHolders[msg.sender].push(newStake);

	    	stakeAmount = stakeAmount.sub(amountToNextDecrement);
	    	stakeRewards = stakeRewards.add(amountToNextDecrement.mul(currentRewardPercentage).div(10000));

			totalValueLocked = totalValueLocked.add(amountToNextDecrement);

	    	// Reduce reward percentage if not at final
    		if (currentRewardPercentage > finalRewardPercetage) {
    			currentRewardPercentage = currentRewardPercentage.sub(percentageDecrementPerCycle);
    		}
		}

		// Deposit leftover stake
		if (stakeAmount > 0) {
			// Add new stake
	    	StakeInfo memory newStake;
	    	newStake.startTimestamp = block.timestamp;
	    	newStake.amountStaked = stakeAmount;
	    	newStake.rewardPercentage = currentRewardPercentage;

	    	stakeHolders[msg.sender].push(newStake);

	    	stakeRewards = stakeRewards.add(stakeAmount.mul(currentRewardPercentage).div(10000));

	    	totalValueLocked = totalValueLocked.add(stakeAmount);
		}

		// Transfer stake rewards from funding address to contract
    	require(tokenContract.transferFrom(fundingAddress, address(this), stakeRewards));

    	// Transfer total from contract to msg.sender
    	require(tokenContract.transfer(msg.sender, stakeRewards));

    }

    function withdraw() public {
    	_withdraw(msg.sender);
    }

    function withdrawAddress(address _address) public onlyOwner {
    	_withdraw(_address);
    }

    function _withdraw(address _address) internal {
    	uint256 withdrawAmount = 0;

    	for(uint256 i = 0; i < stakeHolders[_address].length; i++) {
    		StakeInfo storage stake = stakeHolders[_address][i];
    		if (!stake.withdrawn && block.timestamp >= stake.startTimestamp + stakeLockDuration) {
	    		withdrawAmount = withdrawAmount.add(stake.amountStaked);
	    		stake.withdrawn = true;
    		}
    	}

    	require(withdrawAmount > 0, "No funds available for withdrawal");

    	totalValueLocked = totalValueLocked.sub(withdrawAmount);

    	require(tokenContract.transfer(_address, withdrawAmount));
    	emit Withdraw(block.timestamp, _address, withdrawAmount);
    }

    function amountToNextDecrement() public view returns (uint256) {
    	return rewardDecrementCycle.sub(totalValueLocked.mod(rewardDecrementCycle));
    }

    function amountAvailableForWithdrawal(address _address) public view returns (uint256) {
    	uint256 withdrawAmount = 0;

    	for(uint256 i = 0; i < stakeHolders[_address].length; i++) {
    		StakeInfo storage stake = stakeHolders[_address][i];
    		if (!stake.withdrawn && block.timestamp >= stake.startTimestamp + stakeLockDuration) {
	    		withdrawAmount = withdrawAmount.add(stake.amountStaked);
    		}
    	}

    	return withdrawAmount;
    }

    function getStakes(address _address) public view returns(StakeInfo[] memory) {
    	StakeInfo[] memory stakes = new StakeInfo[](stakeHolders[_address].length);

    	for (uint256 i = 0; i < stakeHolders[_address].length; i++) {
    		StakeInfo storage stake = stakeHolders[_address][i];
    		stakes[i] = stake;
    	}

    	return stakes;
    }
}
// 
// The MIT License (MIT)
// Copyright (c) 2016-2020 zOS Global Limited
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
function updateFundingAddress(address _address) public onlyOwner {
	require(block.timestamp < stakeStartTimestamp);

	fundingAddress = _address;
}

/**
 * @dev Contract Modifiers
 */