	// 5. Convert remaining WETH to SALT and sends it to SaltRewards.
function step5() public onlySameContract
	{
	uint256 wethBalance = weth.balanceOf( address(this) );
	if ( wethBalance == 0 )
		return;

	// Convert remaining WETH to SALT and send it to SaltRewards
	// @audit arbitrage profits sent to saltrewards here, pools contract
	// has allowance to withdrawm unlimited WETH from the upkeep contract
	// but this swap operation can fail, if arbitrage profits are too large
	// and not enough reserves of salt are there
	uint256 amountSALT = pools.depositSwapWithdraw( weth, salt, wethBalance, 0, block.timestamp );
	salt.safeTransfer(address(saltRewards), amountSALT);
	}
	// 7. Distribute SALT from SaltRewards to the stakingRewardsEmitter and liquidityRewardsEmitter.
function step7() public onlySameContract
	{
	// @audit line below can return a list with arbitrage profits assigned for a pool
	uint256[] memory profitsForPools = pools.profitsForWhitelistedPools();

	bytes32[] memory poolIDs = poolsConfig.whitelistedPools();
	// @audit if more than 1 week passed, less rewards passed to the emitters in step 8.
	saltRewards.performUpkeep(poolIDs, profitsForPools );
	// @audit can arbitrage profits be cleared without distributing?
	pools.clearProfitsForPools();
	}
		uint256 saltRewardsToDistribute = salt.balanceOf(address(this));
	if ( saltRewardsToDistribute == 0 ){ // @audit function simply returns, this could be problematic
		return;
	}

	// Determine the total profits so we can calculate proportional share for the liquidity rewards
	uint256 totalProfits = 0;
	for( uint256 i = 0; i < poolIDs.length; i++ ) {
		totalProfits += profitsForPools[i];
	}

	// Make sure that there are some profits to determine the proportional liquidity rewards.
	// Otherwise just handle the SALT balance later so it can be divided between stakingRewardsEmitter and liquidityRewardsEmitter without further accounting.
	if ( totalProfits == 0 ) {
		return;
	}

	// Determine how much of the SALT rewards will be directly awarded to the SALT/USDS pool.
	// This is because SALT/USDS is important, but not included in other arbitrage trades - which would normally yield additional rewards for the pool by being part of arbitrage swaps.
	uint256 directRewardsForSaltUSDS = ( saltRewardsToDistribute * rewardsConfig.percentRewardsSaltUSDS() ) / 100;
	uint256 remainingRewards = saltRewardsToDistribute - directRewardsForSaltUSDS;

	// Divide up the remaining rewards between SALT stakers and liquidity providers
	uint256 stakingRewardsAmount = ( remainingRewards * rewardsConfig.stakingRewardsPercent() ) / 100;
	uint256 liquidityRewardsAmount = remainingRewards - stakingRewardsAmount;
	_sendStakingRewards(stakingRewardsAmount);
	_sendLiquidityRewards(liquidityRewardsAmount, directRewardsForSaltUSDS, poolIDs, profitsForPools, totalProfits); // @audit salt rewards for liquidity will not include amounts related to pool arbitrate profits
function testSuccessStep5NoStep6() public
	{
	_setupLiquidity();
	vm.warp( block.timestamp + 1 days );
	_generateArbitrageProfits(true);

	// stakingRewardsEmitter and liquidityRewardsEmitter have initial bootstrapping rewards
	bytes32[] memory poolIDsB = new bytes32[](1);
	poolIDsB[0] = PoolUtils._poolID(salt, usds);
	uint256 initialRewardsB = liquidityRewardsEmitter.pendingRewardsForPools(poolIDsB)[0];

	bool errorRaised = false;

	//Mimic depositing arbitrage profits.
	vm.prank(DEPLOYER);
	weth.transfer(address(dao), 700 ether);
	

	vm.startPrank(address(dao));
	weth.approve(address(pools), 700 ether );
	pools.deposit( weth, 700 ether);
	vm.stopPrank();

	assertEq( salt.balanceOf(address(saltRewards)), 0 );

    vm.startPrank(address(upkeep));
	ITestUpkeep(address(upkeep)).step2(alice);
	ITestUpkeep(address(upkeep)).step3();
	ITestUpkeep(address(upkeep)).step4();
	try ITestUpkeep(address(upkeep)).step5() {}
	catch (bytes memory error) {errorRaised = true; }
	assertEq( errorRaised, true ); // proves that the swap from WETH to Salt in step 5 failed

	// No SALT was sent to SaltRewards, because swap from WETH to SALT failed due to low reserves.
	// which means arbitrage profits reward wont be sent to pools
	assertEq( salt.balanceOf(address(saltRewards)), 0);

	// Check that the rewards were recorded in storage
	bool poolsHaveProfit = false;
	uint256[] memory profitsForPools = IPoolStats(address(pools)).profitsForWhitelistedPools();
	for( uint256 i = 0; i < profitsForPools.length; i++ ) {
		console.log("pool profit for pool before:", i);
		console.log(profitsForPools[i]);
		if (profitsForPools[i] > 0){
			poolsHaveProfit = true;
		}
		
	}
	assertEq( poolsHaveProfit, true );

	ITestUpkeep(address(upkeep)).step7();
	bool poolsDontHaveProfit = false;
	// Check that the rewards for pools no longer recorded in storage
	uint256[] memory profitsForPools2 = IPoolStats(address(pools)).profitsForWhitelistedPools();
	for( uint256 i = 0; i < profitsForPools2.length; i++ ) {
		console.log("pool profit for pool:", i);
		console.log(profitsForPools2[i]);
		if (profitsForPools2[i] > 0){
			poolsDontHaveProfit = true;
		}
	}
    // test proves that in the case a pool doesnt have enough reserve to swap the arbitrage profits to SALT, the upkeep sequence
	// can lead to arbitrage profits being cleared from storage, effectively meaning they can not be distributed
	// to anyone, hence lost funds. ie  the pools that have taken part in generating recent arbitrage profits so that those pools 
	// will be unable to can receive proportional pending rewards on the next call to performUpkeep(), because their record has been cleared from storage.
	assertEq( poolsDontHaveProfit, false );

	bytes32[] memory poolIDs = new bytes32[](4);
	poolIDs[0] = PoolUtils._poolID(salt,weth);
	poolIDs[1] = PoolUtils._poolID(salt,wbtc);
	poolIDs[2] = PoolUtils._poolID(wbtc,weth);
	poolIDs[3] = PoolUtils._poolID(salt,usds);

	// Check that rewards were not sentto the three pools involved in generating the test arbitrage
	assertEq( liquidityRewardsEmitter.pendingRewardsForPools(poolIDs)[0], initialRewardsB );
	assertEq( liquidityRewardsEmitter.pendingRewardsForPools(poolIDs)[1], initialRewardsB );
	assertEq( liquidityRewardsEmitter.pendingRewardsForPools(poolIDs)[2], initialRewardsB );

	vm.stopPrank();
	}



    function testSuccessStep5Withstep6() public
	{
	_setupLiquidity();
	vm.warp( block.timestamp + 1 days );
	_generateArbitrageProfits(true);

	// stakingRewardsEmitter and liquidityRewardsEmitter have initial bootstrapping rewards
	bytes32[] memory poolIDsB = new bytes32[](1);
	poolIDsB[0] = PoolUtils._poolID(salt, usds);
	uint256 initialRewardsB = liquidityRewardsEmitter.pendingRewardsForPools(poolIDsB)[0];

	bool errorRaised = false;

	//Mimic depositing arbitrage profits.
	vm.prank(DEPLOYER);
	weth.transfer(address(dao), 700 ether);
	

	vm.startPrank(address(dao));
	weth.approve(address(pools), 700 ether );
	pools.deposit( weth, 700 ether);
	vm.stopPrank();

	assertEq( salt.balanceOf(address(saltRewards)), 0 );

    vm.startPrank(address(upkeep));
	ITestUpkeep(address(upkeep)).step2(alice);
	ITestUpkeep(address(upkeep)).step3();
	ITestUpkeep(address(upkeep)).step4();
	try ITestUpkeep(address(upkeep)).step5() {}
	catch (bytes memory error) {errorRaised = true; }
	assertEq( errorRaised, true ); // proves that the swap from WETH to Salt in step 5 failed

	// No SALT was sent to SaltRewards, because swap from WETH to SALT failed due to low reserves.
	// which means salt rewards related to arbitrage profits reward wont be sent to pools
	assertEq( salt.balanceOf(address(saltRewards)), 0);

	// Check that the rewards were recorded in storage
	bool poolsHaveProfit = false;
	uint256[] memory profitsForPools = IPoolStats(address(pools)).profitsForWhitelistedPools();
	for( uint256 i = 0; i < profitsForPools.length; i++ ) {
		console.log("pool profit for pool before:", i);
		console.log(profitsForPools[i]);
		if (profitsForPools[i] > 0){
			poolsHaveProfit = true;
		}
		
	}
	assertEq( poolsHaveProfit, true );


	ITestUpkeep(address(upkeep)).step6();
	ITestUpkeep(address(upkeep)).step7();
	bool poolsDontHaveProfit = false;
	// Check that the rewards for pools no longer recorded in storage
	uint256[] memory profitsForPools2 = IPoolStats(address(pools)).profitsForWhitelistedPools();
	for( uint256 i = 0; i < profitsForPools2.length; i++ ) {
		console.log("pool profit for pool:", i);
		console.log(profitsForPools2[i]);
		if (profitsForPools2[i] > 0){
			poolsDontHaveProfit = true;
		}
	}
    // test proves that in the case a pool doesnt have enough reserve to swap the arbitrage profits to SALT, the upkeep sequence
	// can lead to arbitrage profits being cleared from storage, effectively meaning they can not be distributed
	// to anyone, hence lost funds. ie  the pools that have taken part in generating recent arbitrage profits so that those pools 
	// will be unable to can receive proportional pending rewards on the next call to performUpkeep(), because their record has been cleared from storage.
	assertEq( poolsDontHaveProfit, false );


	vm.stopPrank();
	}

 function _setupLiquidity() internal
	{
	vm.prank(address(collateralAndLiquidity));
	usds.mintTo(DEPLOYER, 100000 ether );

	vm.prank(address(teamVestingWallet));
	salt.transfer(DEPLOYER, 100000 ether );

	vm.startPrank(DEPLOYER);
	weth.approve( address(collateralAndLiquidity), 300000 ether);
	usds.approve( address(collateralAndLiquidity), 100000 ether);
	dai.approve( address(collateralAndLiquidity), 100000 ether);
	salt.approve( address(collateralAndLiquidity), 100000 ether);

	collateralAndLiquidity.depositLiquidityAndIncreaseShare(weth, usds, 100000 ether, 100000 ether, 0, block.timestamp, false);
	collateralAndLiquidity.depositLiquidityAndIncreaseShare(weth, dai, 100000 ether, 100000 ether, 0, block.timestamp, false);
	collateralAndLiquidity.depositLiquidityAndIncreaseShare(weth, salt, 100000000000 wei, 100000000000 wei, 0, block.timestamp, false);

	vm.stopPrank();
	}

 function _generateArbitrageProfits( bool despositSaltUSDS ) internal
	{
	/// Pull some SALT from the daoVestingWallet
	vm.prank(address(daoVestingWallet));
	salt.transfer(DEPLOYER, 100000 ether);

	// Mint some USDS
	vm.prank(address(collateralAndLiquidity));
	usds.mintTo(DEPLOYER, 1000 ether);

	vm.startPrank(DEPLOYER);
	salt.approve(address(collateralAndLiquidity), type(uint256).max);
	wbtc.approve(address(collateralAndLiquidity), type(uint256).max);
	weth.approve(address(collateralAndLiquidity), type(uint256).max);
	wbtc.approve(address(collateralAndLiquidity), type(uint256).max);
	weth.approve(address(collateralAndLiquidity), type(uint256).max);

	if ( despositSaltUSDS )
		//collateralAndLiquidity.depositLiquidityAndIncreaseShare( salt, weth, 1000 ether, 1000 ether, 0, block.timestamp, false );
		collateralAndLiquidity.depositLiquidityAndIncreaseShare( salt, weth, 100000000000 wei, 100000000000 wei, 0, block.timestamp, false );

	collateralAndLiquidity.depositLiquidityAndIncreaseShare( wbtc, salt, 1000 * 10**8, 1000 ether, 0, block.timestamp, false );
	collateralAndLiquidity.depositCollateralAndIncreaseShare( 1000 * 10**8, 1000 ether, 0, block.timestamp, false );

	salt.approve(address(pools), type(uint256).max);
	wbtc.approve(address(pools), type(uint256).max);
	weth.approve(address(pools), type(uint256).max);
	vm.stopPrank();

	// Place some sample trades to create arbitrage profits
	_swapToGenerateProfits();
	}
