- Deploy Uniswap factory once before all runs (saves ~1.16M gas per run) - Fix CSV buffer accumulation bug by clearing buffer between runs - Add clearCSV() function to CSVManager for proper buffer management - Each fuzzing run now gets its own clean CSV with correct token0isWeth values - Comment out failing console.log in Optimizer.t.sol to fix compilation The token ordering now correctly alternates: - Even seeds: token0isWeth = true (WETH < KRAIKEN) - Odd seeds: token0isWeth = false (KRAIKEN < WETH) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
245 lines
No EOL
9.9 KiB
Solidity
245 lines
No EOL
9.9 KiB
Solidity
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
pragma solidity ^0.8.19;
|
|
|
|
import "forge-std/Test.sol";
|
|
import "forge-std/console.sol";
|
|
import "../src/Optimizer.sol";
|
|
import "./mocks/MockStake.sol";
|
|
import "./mocks/MockKraiken.sol";
|
|
|
|
contract OptimizerTest is Test {
|
|
Optimizer optimizer;
|
|
MockStake mockStake;
|
|
MockKraiken mockKraiken;
|
|
|
|
function setUp() public {
|
|
// Deploy mocks
|
|
mockKraiken = new MockKraiken();
|
|
mockStake = new MockStake();
|
|
|
|
// Deploy Optimizer implementation
|
|
Optimizer implementation = new Optimizer();
|
|
|
|
// Deploy proxy and initialize
|
|
bytes memory initData = abi.encodeWithSelector(
|
|
Optimizer.initialize.selector,
|
|
address(mockKraiken),
|
|
address(mockStake)
|
|
);
|
|
|
|
// For simplicity, we'll test the implementation directly
|
|
// In production, you'd use a proper proxy setup
|
|
optimizer = implementation;
|
|
optimizer.initialize(address(mockKraiken), address(mockStake));
|
|
}
|
|
|
|
/**
|
|
* @notice Test that anchorWidth adjusts correctly for bull market conditions
|
|
* @dev High staking, low tax → narrow anchor (30-35%)
|
|
*/
|
|
function testBullMarketAnchorWidth() public {
|
|
// Set bull market conditions: high staking (80%), low tax (10%)
|
|
mockStake.setPercentageStaked(0.8e18);
|
|
mockStake.setAverageTaxRate(0.1e18);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("Bull Market - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 32 = -12) + tax_adj(4 - 10 = -6) = 22
|
|
assertEq(anchorWidth, 22, "Bull market should have narrow anchor width");
|
|
assertTrue(anchorWidth >= 20 && anchorWidth <= 35, "Bull market width should be 20-35%");
|
|
}
|
|
|
|
/**
|
|
* @notice Test that anchorWidth adjusts correctly for bear market conditions
|
|
* @dev Low staking, high tax → wide anchor (60-80%)
|
|
*/
|
|
function testBearMarketAnchorWidth() public {
|
|
// Set bear market conditions: low staking (20%), high tax (70%)
|
|
mockStake.setPercentageStaked(0.2e18);
|
|
mockStake.setAverageTaxRate(0.7e18);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("Bear Market - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 8 = 12) + tax_adj(28 - 10 = 18) = 70
|
|
assertEq(anchorWidth, 70, "Bear market should have wide anchor width");
|
|
assertTrue(anchorWidth >= 60 && anchorWidth <= 80, "Bear market width should be 60-80%");
|
|
}
|
|
|
|
/**
|
|
* @notice Test neutral market conditions
|
|
* @dev Medium staking, medium tax → balanced anchor (35-50%)
|
|
*/
|
|
function testNeutralMarketAnchorWidth() public {
|
|
// Set neutral conditions: medium staking (50%), medium tax (30%)
|
|
mockStake.setPercentageStaked(0.5e18);
|
|
mockStake.setAverageTaxRate(0.3e18);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("Neutral Market - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 20 = 0) + tax_adj(12 - 10 = 2) = 42
|
|
assertEq(anchorWidth, 42, "Neutral market should have balanced anchor width");
|
|
assertTrue(anchorWidth >= 35 && anchorWidth <= 50, "Neutral width should be 35-50%");
|
|
}
|
|
|
|
/**
|
|
* @notice Test high volatility scenario
|
|
* @dev High staking with high tax (speculative frenzy) → moderate-wide anchor
|
|
*/
|
|
function testHighVolatilityAnchorWidth() public {
|
|
// High staking (70%) but also high tax (80%) - speculative market
|
|
mockStake.setPercentageStaked(0.7e18);
|
|
mockStake.setAverageTaxRate(0.8e18);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("High Volatility - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 28 = -8) + tax_adj(32 - 10 = 22) = 54
|
|
assertEq(anchorWidth, 54, "High volatility should have moderate-wide anchor");
|
|
assertTrue(anchorWidth >= 50 && anchorWidth <= 60, "Volatile width should be 50-60%");
|
|
}
|
|
|
|
/**
|
|
* @notice Test stable market conditions
|
|
* @dev Medium staking with very low tax → narrow anchor for fee optimization
|
|
*/
|
|
function testStableMarketAnchorWidth() public {
|
|
// Medium staking (50%), very low tax (5%) - stable conditions
|
|
mockStake.setPercentageStaked(0.5e18);
|
|
mockStake.setAverageTaxRate(0.05e18);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("Stable Market - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 20 = 0) + tax_adj(2 - 10 = -8) = 32
|
|
assertEq(anchorWidth, 32, "Stable market should have narrower anchor");
|
|
assertTrue(anchorWidth >= 30 && anchorWidth <= 40, "Stable width should be 30-40%");
|
|
}
|
|
|
|
/**
|
|
* @notice Test minimum bound enforcement
|
|
* @dev Extreme conditions that would result in width < 10 should clamp to 10
|
|
*/
|
|
function testMinimumWidthBound() public {
|
|
// Extreme bull: very high staking (95%), zero tax
|
|
mockStake.setPercentageStaked(0.95e18);
|
|
mockStake.setAverageTaxRate(0);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("Minimum Bound Test - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 38 = -18) + tax_adj(0 - 10 = -10) = 12
|
|
// But should be at least 10
|
|
assertEq(anchorWidth, 12, "Should not go below calculated value if above 10");
|
|
assertTrue(anchorWidth >= 10, "Width should never be less than 10");
|
|
}
|
|
|
|
/**
|
|
* @notice Test maximum bound enforcement
|
|
* @dev Extreme conditions that would result in width > 80 should clamp to 80
|
|
*/
|
|
function testMaximumWidthBound() public {
|
|
// Extreme bear: zero staking, maximum tax
|
|
mockStake.setPercentageStaked(0);
|
|
mockStake.setAverageTaxRate(1e18);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("Maximum Bound Test - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 0 = 20) + tax_adj(40 - 10 = 30) = 90
|
|
// But should be clamped to 80
|
|
assertEq(anchorWidth, 80, "Should clamp to maximum of 80");
|
|
assertTrue(anchorWidth <= 80, "Width should never exceed 80");
|
|
}
|
|
|
|
/**
|
|
* @notice Test edge case with exactly minimum staking and tax
|
|
*/
|
|
function testEdgeCaseMinimumInputs() public {
|
|
mockStake.setPercentageStaked(0);
|
|
mockStake.setAverageTaxRate(0);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("Minimum Inputs - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 0 = 20) + tax_adj(0 - 10 = -10) = 50
|
|
assertEq(anchorWidth, 50, "Zero inputs should give moderate width");
|
|
}
|
|
|
|
/**
|
|
* @notice Test edge case with exactly maximum staking and tax
|
|
*/
|
|
function testEdgeCaseMaximumInputs() public {
|
|
mockStake.setPercentageStaked(1e18);
|
|
mockStake.setAverageTaxRate(1e18);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
console.log("Maximum Inputs - Anchor Width:", anchorWidth);
|
|
|
|
// Expected: base(40) + staking_adj(20 - 40 = -20) + tax_adj(40 - 10 = 30) = 50
|
|
assertEq(anchorWidth, 50, "Maximum inputs should balance out to moderate width");
|
|
}
|
|
|
|
/**
|
|
* @notice Fuzz test to ensure anchorWidth always stays within bounds
|
|
*/
|
|
function testFuzzAnchorWidthBounds(uint256 percentageStaked, uint256 averageTaxRate) public {
|
|
// Bound inputs to valid ranges
|
|
percentageStaked = bound(percentageStaked, 0, 1e18);
|
|
averageTaxRate = bound(averageTaxRate, 0, 1e18);
|
|
|
|
mockStake.setPercentageStaked(percentageStaked);
|
|
mockStake.setAverageTaxRate(averageTaxRate);
|
|
|
|
(,, uint24 anchorWidth,) = optimizer.getLiquidityParams();
|
|
|
|
// Assert bounds are always respected
|
|
assertTrue(anchorWidth >= 10, "Width should never be less than 10");
|
|
assertTrue(anchorWidth <= 80, "Width should never exceed 80");
|
|
|
|
// Log some interesting cases
|
|
if (anchorWidth == 10 || anchorWidth == 80) {
|
|
// Commented out due to console.log compilation issue
|
|
// console.log("Bound hit - Staking:", percentageStaked / 1e16, "%, Tax:", averageTaxRate / 1e16, "%, Width:", anchorWidth);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notice Test that other liquidity params are still calculated correctly
|
|
*/
|
|
function testOtherLiquidityParams() public {
|
|
mockStake.setPercentageStaked(0.6e18);
|
|
mockStake.setAverageTaxRate(0.4e18);
|
|
|
|
(uint256 capitalInefficiency, uint256 anchorShare, uint24 anchorWidth, uint256 discoveryDepth) =
|
|
optimizer.getLiquidityParams();
|
|
|
|
uint256 sentiment = optimizer.getSentiment();
|
|
|
|
console.log("Sentiment:", sentiment / 1e16, "%");
|
|
console.log("Capital Inefficiency:", capitalInefficiency / 1e16, "%");
|
|
console.log("Anchor Share:", anchorShare / 1e16, "%");
|
|
console.log("Anchor Width:", anchorWidth, "%");
|
|
console.log("Discovery Depth:", discoveryDepth / 1e16, "%");
|
|
|
|
// Verify relationships
|
|
assertEq(capitalInefficiency, 1e18 - sentiment, "Capital inefficiency should be 1 - sentiment");
|
|
assertEq(anchorShare, sentiment, "Anchor share should equal sentiment");
|
|
assertEq(discoveryDepth, sentiment, "Discovery depth should equal sentiment");
|
|
|
|
// Verify anchor width is calculated independently
|
|
// Expected: base(40) + staking_adj(20 - 24 = -4) + tax_adj(16 - 10 = 6) = 42
|
|
assertEq(anchorWidth, 42, "Anchor width should be independently calculated");
|
|
}
|
|
} |