contract StabilizeStrategyETHArb is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;
    using Address for address;
    
    address public treasuryAddress; // Address of the treasury
    address public stakingAddress; // Address to the STBZ staking pool
    address public zsTokenAddress; // The address of the controlling zs-Token

    uint256 constant DIVISION_FACTOR = 100000;

    uint256 public lastTradeTime;
    uint256 public lastActionBalance; // Balance before last deposit, withdraw or trade
    uint256 public percentTradeTrigger = 500; // 0.5% change in value will trigger a trade
    uint256 public percentSell = 50000; // 50% of the tokens are sold to the cheapest token
    uint256 public percentDepositor = 50000; // 1000 = 1%, depositors earn 50% of all gains
    uint256 public percentExecutor = 10000; // 10000 = 10% of WETH goes to executor
    uint256 public percentStakers = 50000; // 50% of non-executor WETH goes to stakers, can be changed, rest goes to treasury
    uint256 public maxPercentStipend = 30000; // The maximum amount of WETH profit that can be allocated to the executor for gas in percent
    uint256 public gasStipend = 690000; // This is the gas units that are covered by executing a trade taken from the WETH profit
    uint256 public minTradeSplit = 2e18; // If the balance of ETH is less than or equal to this, it trades the entire balance
    uint256 constant minGain = 1e13; // Minimum amount of eth gain (0.00001) before buying WETH and splitting it
    
    // Token information
    // This strategy accepts multiple stablecoins
    // WETH, AETH, STETH
    struct TokenInfo {
        IERC20 token; // Reference of token
        uint256 decimals; // Decimals of token
        int128 curveID; // ID on curve
    }
    
    TokenInfo[] private tokenList; // An array of tokens accepted as deposits
    
    // Strategy specific variables
    // These curve pools are unique because they hold eth directly
    address constant CURVE_STETH_POOL = address(0xDC24316b9AE028F1497c275EB9192a3Ea0f67022); // Curve pool for 2 tokens, eth and steth
    address constant CURVE_ANKRETH_POOL = address(0xA96A65c051bF88B4095Ee1f2451C2A9d43F53Ae2); // Curve pool for 2 tokens, eth and aeth
    address constant WETH_ADDRESS = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
    address constant GAS_ORACLE_ADDRESS = address(0x169E633A2D1E6c10dD91238Ba11c4A708dfEF37C); // Chainlink address for fast gas oracle
    
    constructor(
        address _treasury,
        address _staking,
        address _zsToken
    ) public {
        treasuryAddress = _treasury;
        stakingAddress = _staking;
        zsTokenAddress = _zsToken;
        setupWithdrawTokens();
    }

    // Initialization functions
    
    function setupWithdrawTokens() internal {
        // Start with WETH
        IERC20 _token = IERC20(address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2));
        tokenList.push(
            TokenInfo({
                token: _token,
                decimals: _token.decimals(),
                curveID: 0
            })
        );   
        
        // AETH from Ankr
        _token = IERC20(address(0xE95A203B1a91a908F9B9CE46459d101078c2c3cb));
        tokenList.push(
            TokenInfo({
                token: _token,
                decimals: _token.decimals(),
                curveID: 1
            })
        );
        
        // STETH from Lido
        _token = IERC20(address(0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84));
        tokenList.push(
            TokenInfo({
                token: _token,
                decimals: _token.decimals(),
                curveID: 1
            })
        );
    }
    
    // Modifier
    modifier onlyZSToken() {
        require(zsTokenAddress == _msgSender(), "Call not sent from the zs-Token");
        _;
    }
    
    // Read functions
    
    function rewardTokensCount() external view returns (uint256) {
        return tokenList.length;
    }
    
    function rewardTokenAddress(uint256 _pos) external view returns (address) {
        require(_pos < tokenList.length,"No token at that position");
        return address(tokenList[_pos].token);
    }
    
    function balance() public view returns (uint256) {
        return getNormalizedTotalBalance(address(this));
    }
    
    function getNormalizedTotalBalance(address _address) public view returns (uint256) {
        // Get the balance of the atokens+tokens at this address
        uint256 _balance = 0;
        uint256 _length = tokenList.length;
        for(uint256 i = 0; i < _length; i++){
            uint256 _bal = tokenList[i].token.balanceOf(_address);
            _bal = _bal.mul(1e18).div(10**tokenList[i].decimals);
            _balance = _balance.add(_bal); // This has been normalized to 1e18 decimals
        }
        return _balance;
    }
    
    function withdrawTokenReserves() public view returns (address, uint256) {
        // This function will return the address and amount of the token with the highest balance
        uint256 length = tokenList.length;
        uint256 targetID = 0;
        uint256 targetNormBalance = 0;
        for(uint256 i = 0; i < length; i++){
            uint256 _normBal = tokenList[i].token.balanceOf(address(this)).mul(1e18).div(10**tokenList[i].decimals);
            if(_normBal > 0){
                if(targetNormBalance == 0 || _normBal >= targetNormBalance){
                    targetNormBalance = _normBal;
                    targetID = i;
                }
            }
        }
        if(targetNormBalance > 0){
            return (address(tokenList[targetID].token), tokenList[targetID].token.balanceOf(address(this)));
        }else{
            return (address(0), 0); // No balance
        }
    }
    
    // Write functions
    
    receive() external payable {
        // We need an anonymous fallback function to accept ether into this contract
    }
    
    function enter() external onlyZSToken {
        deposit(false);
    }
    
    function exit() external onlyZSToken {
        // The ZS token vault is removing all tokens from this strategy
        withdraw(_msgSender(),1,1, false);
    }
    
    function deposit(bool nonContract) public onlyZSToken {
        // Only the ZS token can call the function
        
        // No trading is performed on deposit
        if(nonContract == true){}
        lastActionBalance = balance(); // This action balance represents pool post stablecoin deposit
    }
    
    function withdraw(address _depositor, uint256 _share, uint256 _total, bool nonContract) public onlyZSToken returns (uint256) {
        require(balance() > 0, "There are no tokens in this strategy");
        if(nonContract == true){
            if(_share > _total.mul(percentTradeTrigger).div(DIVISION_FACTOR)){
                checkAndSwapTokens(address(0));
            }
        }
        
        uint256 withdrawAmount = 0;
        uint256 _balance = balance();
        if(_share < _total){
            uint256 _myBalance = _balance.mul(_share).div(_total);
            withdrawPerBalance(_depositor, _myBalance, false); // This will withdraw based on token balance
            withdrawAmount = _myBalance;
        }else{
            // We are all shares, transfer all
            withdrawPerBalance(_depositor, _balance, true);
            withdrawAmount = _balance;
        }       
        lastActionBalance = balance();
        
        return withdrawAmount;
    }
    
    // This will withdraw the tokens from the contract based on their balance, from highest balance to lowest
    function withdrawPerBalance(address _receiver, uint256 _withdrawAmount, bool _takeAll) internal {
        uint256 length = tokenList.length;
        if(_takeAll == true){
            // Send the entire balance
            for(uint256 i = 0; i < length; i++){
                uint256 _bal = tokenList[i].token.balanceOf(address(this));
                if(_bal > 0){
                    tokenList[i].token.safeTransfer(_receiver, _bal);
                }
            }
            return;
        }
        bool[4] memory done;
        uint256 targetID = 0;
        uint256 targetNormBalance = 0;
        for(uint256 i = 0; i < length; i++){
            
            targetNormBalance = 0; // Reset the target balance
            // Find the highest balanced token to withdraw
            for(uint256 i2 = 0; i2 < length; i2++){
                if(done[i2] == false){
                    uint256 _normBal = tokenList[i2].token.balanceOf(address(this)).mul(1e18).div(10**tokenList[i2].decimals);
                    if(targetNormBalance == 0 || _normBal >= targetNormBalance){
                        targetNormBalance = _normBal;
                        targetID = i2;
                    }
                }
            }
            done[targetID] = true;
            
            // Determine the balance left
            uint256 _normalizedBalance = tokenList[targetID].token.balanceOf(address(this)).mul(1e18).div(10**tokenList[targetID].decimals);
            if(_normalizedBalance <= _withdrawAmount){
                // Withdraw the entire balance of this token
                if(_normalizedBalance > 0){
                    _withdrawAmount = _withdrawAmount.sub(_normalizedBalance);
                    tokenList[targetID].token.safeTransfer(_receiver, tokenList[targetID].token.balanceOf(address(this)));                    
                }
            }else{
                // Withdraw a partial amount of this token
                if(_withdrawAmount > 0){
                    // Convert the withdraw amount to the token's decimal amount
                    uint256 _balance = _withdrawAmount.mul(10**tokenList[targetID].decimals).div(1e18);
                    _withdrawAmount = 0;
                    tokenList[targetID].token.safeTransfer(_receiver, _balance);
                }
                break; // Nothing more to withdraw
            }
        }
    }
    
    function simulateExchange(uint256 _idIn, uint256 _idOut, uint256 _amount) internal view returns (uint256) {
        CurvePool pool;
        if(_idIn != 0 && _idOut != 0){
            // Must use 2 pools
            if(_idIn == 1){
                // AETH to STETH
                pool = CurvePool(CURVE_ANKRETH_POOL);
                // Must first go to WETH
                _amount = pool.get_dy(tokenList[_idIn].curveID, 0, _amount);
                // Then STETH
                pool = CurvePool(CURVE_STETH_POOL);
                _amount = pool.get_dy(0, tokenList[_idOut].curveID, _amount);
            }else{
                // STETH to AETH
                pool = CurvePool(CURVE_STETH_POOL);
                // Must first go to WETH
                _amount = pool.get_dy(tokenList[_idIn].curveID, 0, _amount);
                // Then AETH
                pool = CurvePool(CURVE_ANKRETH_POOL);
                _amount = pool.get_dy(0, tokenList[_idOut].curveID, _amount);
            }
        }else{
            if(_idIn == 1 || _idOut == 1){
                // We must use the AETH pool
                pool = CurvePool(CURVE_ANKRETH_POOL);
            }else{
                // We are using the STETH pool
                pool = CurvePool(CURVE_STETH_POOL);
            }
            _amount = pool.get_dy(tokenList[_idIn].curveID, tokenList[_idOut].curveID, _amount);
        }
        return _amount;
    }
    
    function exchange(uint256 _idIn, uint256 _idOut, uint256 _amount) internal {
        CurvePool pool;
        uint256 _bal;
        if(_idIn != 0 && _idOut != 0){
            // Must use 2 pools
            if(_idIn == 1){
                // AETH to STETH
                pool = CurvePool(CURVE_ANKRETH_POOL);
                // Must first go to WETH
                IERC20(tokenList[_idIn].token).safeApprove(address(pool), 0);
                IERC20(tokenList[_idIn].token).safeApprove(address(pool), _amount);
                _bal = address(this).balance;
                pool.exchange(tokenList[_idIn].curveID, 0, _amount, 1); // Returns ETH
                _amount = address(this).balance.sub(_bal);
                // Then STETH
                pool = CurvePool(CURVE_STETH_POOL);
                pool.exchange{value: _amount}(0, tokenList[_idOut].curveID, _amount, 1);
            }else{
                // STETH to AETH
                pool = CurvePool(CURVE_STETH_POOL);
                // Must first go to WETH
                IERC20(tokenList[_idIn].token).safeApprove(address(pool), 0);
                IERC20(tokenList[_idIn].token).safeApprove(address(pool), _amount);
                _bal = address(this).balance;
                pool.exchange(tokenList[_idIn].curveID, 0, _amount, 1); // Returns ETH
                _amount = address(this).balance.sub(_bal);
                // Then AETH
                pool = CurvePool(CURVE_ANKRETH_POOL);
                pool.exchange{value: _amount}(0, tokenList[_idOut].curveID, _amount, 1);
            }
        }else{
            if(_idIn == 1 || _idOut == 1){
                // We must use the AETH pool
                pool = CurvePool(CURVE_ANKRETH_POOL);
            }else{
                // We are using the STETH pool
                pool = CurvePool(CURVE_STETH_POOL);
            }
            if(_idIn == 0){
                // We have WETH, we must convert it to ETH to use curve
                WrappedEther(WETH_ADDRESS).withdraw(_amount);
                pool.exchange{value: _amount}(tokenList[_idIn].curveID, tokenList[_idOut].curveID, _amount, 1);
            }else{
                // We are exchange something for ETH, must convert to WETH
                IERC20(tokenList[_idIn].token).safeApprove(address(pool), 0);
                IERC20(tokenList[_idIn].token).safeApprove(address(pool), _amount);
                _bal = address(this).balance;
                pool.exchange(tokenList[_idIn].curveID, tokenList[_idOut].curveID, _amount, 1);
                _bal = address(this).balance.sub(_bal); // Get the balance increase
                WrappedEther(WETH_ADDRESS).deposit{value: _bal}();
            }
        }
    }
    
    function getCheapestCurveToken() internal view returns (uint256) {
        // This will give us the ID of the cheapest token in the pool
        // We will estimate the return for trading 10 WETH
        // The higher the return, the lower the price of the other token
        uint256 targetID = 0; // Our target ID is WETH first
        uint256 wethAmount = uint256(10).mul(10**tokenList[0].decimals);
        uint256 highAmount = wethAmount;
        uint256 length = tokenList.length;
        for(uint256 i = 1; i < length; i++){
            uint256 estimate = simulateExchange(0, i, wethAmount);
            // Normalize the estimate into weth decimals
            estimate = estimate.mul(10**tokenList[0].decimals).div(10**tokenList[i].decimals);
            if(estimate > highAmount){
                // This token is worth less than the WETH
                highAmount = estimate;
                targetID = i;
            }
        }
        return targetID;
    }
    
    function getFastGasPrice() internal view returns (uint256) {
        AggregatorV3Interface gasOracle = AggregatorV3Interface(GAS_ORACLE_ADDRESS);
        ( , int intGasPrice, , , ) = gasOracle.latestRoundData(); // We only want the answer 
        return uint256(intGasPrice);
    }
    
    function checkAndSwapTokens(address _executor) internal {
        lastTradeTime = now;

        // Now find our target token to sell into
        uint256 targetID = getCheapestCurveToken(); // Curve may have a slightly different cheap token than Chainlink
        uint256 length = tokenList.length;
        
        // Now sell all the other tokens into this token
        uint256 _totalBalance = balance(); // Get the token balance at this contract, should increase
        bool _expectIncrease = false;
        for(uint256 i = 0; i < length; i++){
            if(i != targetID){
                uint256 localTarget = targetID;
                uint256 sellBalance = 0;
                uint256 _normBal = tokenList[i].token.balanceOf(address(this)).mul(1e18).div(10**tokenList[i].decimals);
                if(_normBal <= minTradeSplit){
                    // If balance is too small,sell all tokens at once
                    sellBalance = tokenList[i].token.balanceOf(address(this));
                }else{
                    sellBalance = tokenList[i].token.balanceOf(address(this)).mul(percentSell).div(DIVISION_FACTOR);
                }
                uint256 minReceiveBalance = sellBalance.mul(10**tokenList[localTarget].decimals).div(10**tokenList[i].decimals); // Change to match decimals of destination
                if(sellBalance > 0){
                    uint256 estimate = simulateExchange(i, localTarget, sellBalance);
                    if(estimate > minReceiveBalance){
                        _expectIncrease = true;
                        // We are getting a greater number of tokens, complete the exchange
                        exchange(i, localTarget, sellBalance);
                    }         
                }
            }
        }
        uint256 _newBalance = balance();
        if(_expectIncrease == true){
            // There may be rare scenarios where we don't gain any by calling this function
            require(_newBalance > _totalBalance, "Failed to gain in balance from selling tokens");
        }
        uint256 gain = _newBalance.sub(_totalBalance);
        IERC20 weth = IERC20(WETH_ADDRESS);
        uint256 _wethGain = 0;
        if(gain >= minGain){
            uint256 sellBalance = gain.mul(10**tokenList[targetID].decimals).div(1e18);
            uint256 holdBalance = sellBalance.mul(percentDepositor).div(DIVISION_FACTOR);
            sellBalance = sellBalance.sub(holdBalance); // We will buy WETH with this amount
            if(sellBalance <= tokenList[targetID].token.balanceOf(address(this))){
                // Buy more WETH
                if(targetID != 0){
                    _wethGain = weth.balanceOf(address(this));
                    exchange(targetID, 0, sellBalance);
                    _wethGain = weth.balanceOf(address(this)).sub(_wethGain);                    
                }else{
                    // Don't need to exchange
                    _wethGain = sellBalance;
                }
            }
        }
        if(_wethGain > 0){
            // This is pure profit, figure out allocation
            // Split the amount sent to the treasury, stakers and executor if one exists
            if(_executor != address(0)){
                // Executors will get a gas reimbursement in WETH and a percent of the remaining
                uint256 maxGasFee = getFastGasPrice().mul(gasStipend); // This is gas stipend in wei
                uint256 gasFee = tx.gasprice.mul(gasStipend); // This is gas fee requested
                if(gasFee > maxGasFee){
                    gasFee = maxGasFee; // Gas fee cannot be greater than the maximum
                }
                uint256 executorAmount = gasFee;
                if(gasFee >= _wethGain.mul(maxPercentStipend).div(DIVISION_FACTOR)){
                    executorAmount = _wethGain.mul(maxPercentStipend).div(DIVISION_FACTOR); // The executor will get the entire amount up to point
                }else{
                    // Add the executor percent on top of gas fee
                    executorAmount = _wethGain.sub(gasFee).mul(percentExecutor).div(DIVISION_FACTOR).add(gasFee);
                }
                if(executorAmount > 0){
                    weth.safeTransfer(_executor, executorAmount);
                    _wethGain = _wethGain.sub(executorAmount);         
                }
            }
            if(_wethGain > 0){
                uint256 stakersAmount = _wethGain.mul(percentStakers).div(DIVISION_FACTOR);
                uint256 treasuryAmount = _wethGain.sub(stakersAmount);
                if(treasuryAmount > 0){
                    weth.safeTransfer(treasuryAddress, treasuryAmount);
                }
                if(stakersAmount > 0){
                    if(stakingAddress != address(0)){
                        weth.safeTransfer(stakingAddress, stakersAmount);
                        StabilizeStakingPool(stakingAddress).notifyRewardAmount(stakersAmount);                                
                    }else{
                        // No staking pool selected, just send to the treasury
                        weth.safeTransfer(treasuryAddress, stakersAmount);
                    }
                }
            }
        }
    }
    
    function expectedProfit(bool inWETHForExecutor) external view returns (uint256) {
        // This view will return the expected profit in wei units that a trading activity will have on the pool

        // Now find our target token to sell into
        uint256 targetID = getCheapestCurveToken(); // Curve may have a slightly different cheap token than Chainlink
        uint256 length = tokenList.length;
        
        // Now sell all the other tokens into this token
        uint256 _normalizedGain = 0;
        for(uint256 i = 0; i < length; i++){
            if(i != targetID){
                uint256 localTarget = targetID;
                uint256 sellBalance = 0;
                uint256 _normBal = tokenList[i].token.balanceOf(address(this)).mul(1e18).div(10**tokenList[i].decimals);
                if(_normBal <= minTradeSplit){
                    // If balance is too small,sell all tokens at once
                    sellBalance = tokenList[i].token.balanceOf(address(this));
                }else{
                    sellBalance = tokenList[i].token.balanceOf(address(this)).mul(percentSell).div(DIVISION_FACTOR);
                }
                uint256 minReceiveBalance = sellBalance.mul(10**tokenList[localTarget].decimals).div(10**tokenList[i].decimals); // Change to match decimals of destination
                if(sellBalance > 0){
                    uint256 estimate = simulateExchange(i, localTarget, sellBalance);
                    if(estimate > minReceiveBalance){
                        uint256 _gain = estimate.sub(minReceiveBalance).mul(1e18).div(10**tokenList[localTarget].decimals); // Normalized gain
                        _normalizedGain = _normalizedGain.add(_gain);
                    }                        
                }
            }
        }
        if(inWETHForExecutor == false){
            return _normalizedGain;
        }else{
            // Calculate WETH profit
            if(_normalizedGain == 0){
                return 0;
            }
            // Calculate how much WETH the executor would make as profit
            uint256 estimate = 0;
            if(_normalizedGain > 0){
                uint256 sellBalance = _normalizedGain.mul(10**tokenList[targetID].decimals).div(1e18); // Convert to target decimals
                uint256 holdBalance = sellBalance.mul(percentDepositor).div(DIVISION_FACTOR);
                sellBalance = sellBalance.sub(holdBalance); // We will buy WETH with this amount
                // Estimate output
                if(targetID != 0){
                    estimate = simulateExchange(targetID, 0, sellBalance); 
                }else{
                    estimate = sellBalance;
                }
            }
            // Now calculate the amount going to the executor
            uint256 gasFee = getFastGasPrice().mul(gasStipend); // This is gas stipend in wei
            if(gasFee >= estimate.mul(maxPercentStipend).div(DIVISION_FACTOR)){ // Max percent of total
                return estimate.mul(maxPercentStipend).div(DIVISION_FACTOR); // The executor will get max percent of total
            }else{
                estimate = estimate.sub(gasFee); // Subtract fee from remaining balance
                return estimate.mul(percentExecutor).div(DIVISION_FACTOR).add(gasFee); // Executor amount with fee added
            }
        }
    }
    
    function executorSwapTokens(address _executor, uint256 _minSecSinceLastTrade) external {
        // Function designed to promote trading with incentive. Users get percentage of WETH from profitable trades
        require(now.sub(lastTradeTime) > _minSecSinceLastTrade, "The last trade was too recent");
        require(_msgSender() == tx.origin, "Contracts cannot interact with this function");
        checkAndSwapTokens(_executor);
        lastActionBalance = balance();
    }
    
    // Governance functions
    function governanceSwapTokens() external onlyGovernance {
        // This is function that force trade tokens at anytime. It can only be called by governance
        checkAndSwapTokens(_msgSender());
        lastActionBalance = balance();
    }
    
    // Timelock variables
    
    uint256 private _timelockStart; // The start of the timelock to change governance variables
    uint256 private _timelockType; // The function that needs to be changed
    uint256 constant TIMELOCK_DURATION = 86400; // Timelock is 24 hours
    
    // Reusable timelock variables
    address private _timelock_address;
    uint256[5] private _timelock_data;
    
    modifier timelockConditionsMet(uint256 _type) {
        require(_timelockType == _type, "Timelock not acquired for this function");
        _timelockType = 0; // Reset the type once the timelock is used
        if(balance() > 0){ // Timelock only applies when balance exists
            require(now >= _timelockStart + TIMELOCK_DURATION, "Timelock time not met");
        }
        _;
    }
    
    // Change the owner of the token contract
    // --------------------
    function startGovernanceChange(address _address) external onlyGovernance {
        _timelockStart = now;
        _timelockType = 1;
        _timelock_address = _address;       
    }
    
    function finishGovernanceChange() external onlyGovernance timelockConditionsMet(1) {
        transferGovernance(_timelock_address);
    }
    // --------------------
    
    // Change the treasury address
    // --------------------
    function startChangeTreasury(address _address) external onlyGovernance {
        _timelockStart = now;
        _timelockType = 2;
        _timelock_address = _address;
    }
    
    function finishChangeTreasury() external onlyGovernance timelockConditionsMet(2) {
        treasuryAddress = _timelock_address;
    }
    // --------------------
    
    // Change the staking address
    // --------------------
    function startChangeStakingPool(address _address) external onlyGovernance {
        _timelockStart = now;
        _timelockType = 3;
        _timelock_address = _address;
    }
    
    function finishChangeStakingPool() external onlyGovernance timelockConditionsMet(3) {
        stakingAddress = _timelock_address;
    }
    // --------------------
    
    // Change the zsToken address
    // --------------------
    function startChangeZSToken(address _address) external onlyGovernance {
        _timelockStart = now;
        _timelockType = 4;
        _timelock_address = _address;
    }
    
    function finishChangeZSToken() external onlyGovernance timelockConditionsMet(4) {
        zsTokenAddress = _timelock_address;
    }
    // --------------------
    
    // Change the trading conditions used by the strategy
    // --------------------
    
    function startChangeTradingConditions(uint256 _pTradeTrigger, uint256 _pSellPercent,  uint256 _minSplit, uint256 _maxStipend, uint256 _pMaxStipend) external onlyGovernance {
        // Changes a lot of trading parameters in one call
        require(_pTradeTrigger <= 100000 && _pSellPercent <= 100000 && _pMaxStipend <= 100000,"Percent cannot be greater than 100%");
        _timelockStart = now;
        _timelockType = 5;
        _timelock_data[0] = _pTradeTrigger;
        _timelock_data[1] = _pSellPercent;
        _timelock_data[2] = _minSplit;
        _timelock_data[3] = _maxStipend;
        _timelock_data[4] = _pMaxStipend;
    }
    
    function finishChangeTradingConditions() external onlyGovernance timelockConditionsMet(5) {
        percentTradeTrigger = _timelock_data[0];
        percentSell = _timelock_data[1];
        minTradeSplit = _timelock_data[2];
        gasStipend = _timelock_data[3];
        maxPercentStipend = _timelock_data[4];
    }
    // --------------------
    
    // Change the strategy allocations between the parties
    // --------------------
    
    function startChangeStrategyAllocations(uint256 _pDepositors, uint256 _pExecutor, uint256 _pStakers) external onlyGovernance {
        // Changes strategy allocations in one call
        require(_pDepositors <= 100000 && _pExecutor <= 100000 && _pStakers <= 100000,"Percent cannot be greater than 100%");
        _timelockStart = now;
        _timelockType = 6;
        _timelock_data[0] = _pDepositors;
        _timelock_data[1] = _pExecutor;
        _timelock_data[2] = _pStakers;
    }
    
    function finishChangeStrategyAllocations() external onlyGovernance timelockConditionsMet(6) {
        percentDepositor = _timelock_data[0];
        percentExecutor = _timelock_data[1];
        percentStakers = _timelock_data[2];
    }
    // --------------------
    
}
function startChangeZSToken(address _address) external onlyGovernance {
    _timelockStart = now;
    _timelockType = 4;
    _timelock_address = _address;
}

// --------------------
// Change the zsToken address
// --------------------