From c986bdd8514027fdaa0ddbc31fb4c012a6999ff2 Mon Sep 17 00:00:00 2001 From: Hayden Shively <17186559+haydenshively@users.noreply.github.com> Date: Fri, 13 Oct 2023 19:38:34 -0500 Subject: [PATCH] Run prettier on tests and organize a few things (#191) --- core/src/Lender.sol | 2 +- core/test/Liquidator.t.sol | 40 ++++---- core/test/RateModel.t.sol | 86 ++++++++--------- core/test/VolatilityOracle.t.sol | 20 ++-- core/test/libraries/BalanceSheet.t.sol | 4 +- core/test/libraries/Constants.t.sol | 1 - core/test/libraries/Exp.t.sol | 6 +- core/test/libraries/LiquidityAmounts.t.sol | 2 +- core/test/libraries/Log2.t.sol | 8 +- core/test/libraries/Rewards.t.sol | 27 ++---- core/test/libraries/TickMath.t.sol | 6 +- core/test/libraries/Volatility.t.sol | 103 +++++++++------------ 12 files changed, 145 insertions(+), 160 deletions(-) diff --git a/core/src/Lender.sol b/core/src/Lender.sol index a2e371ae..6f5a482e 100644 --- a/core/src/Lender.sol +++ b/core/src/Lender.sol @@ -516,7 +516,7 @@ contract Lender is Ledger { // Accrue interest (only in memory) uint256 newTotalSupply; - (cache, inventory, newTotalSupply) = _previewInterest(cache); // Includes reentrancy guard + (cache, inventory, newTotalSupply) = _previewInterest(cache); // Reverts if reentrancy guard is active // Update reserves (new `totalSupply` is only in memory, but `balanceOf` is updated in storage) if (newTotalSupply > cache.totalSupply) { diff --git a/core/test/Liquidator.t.sol b/core/test/Liquidator.t.sol index 50dac5f8..2ebdb085 100644 --- a/core/test/Liquidator.t.sol +++ b/core/test/Liquidator.t.sol @@ -49,8 +49,9 @@ contract LiquidatorTest is Test, IManager, ILiquidator { deal(address(account), DEFAULT_ANTE + 1); } - function test_warn(uint8 seed0, uint8 seed1) public { - uint256 margin0 = 1e18 * ((seed0 % 8) + 1); // TODO: Fuzz testing RPC concerns + /// forge-config: default.fuzz.runs = 16 + function test_fuzz_warn(uint8 seed0, uint8 seed1) public { + uint256 margin0 = 1e18 * ((seed0 % 8) + 1); uint256 margin1 = 0.1e18 * ((seed1 % 8) + 1); uint256 borrows0 = margin0 * 200; uint256 borrows1 = margin1 * 200; @@ -75,8 +76,8 @@ contract LiquidatorTest is Test, IManager, ILiquidator { vm.expectRevert(bytes("Aloe: healthy")); account.liquidate(this, bytes(""), 1, (1 << 32)); - setInterest(lender0, 10010); - setInterest(lender1, 10010); + _setInterest(lender0, 10010); + _setInterest(lender1, 10010); assertEq(lender0.borrowBalance(address(account)), (borrows0 * 10010) / 10000); assertEq(lender1.borrowBalance(address(account)), (borrows1 * 10010) / 10000); @@ -112,7 +113,7 @@ contract LiquidatorTest is Test, IManager, ILiquidator { vm.expectRevert(bytes("Aloe: healthy")); account.liquidate(this, bytes(""), strain, (1 << 32)); - setInterest(lender0, 10010); + _setInterest(lender0, 10010); assertEq(lender0.borrowBalance(address(account)), 200.2e18); vm.expectRevert(); @@ -140,7 +141,7 @@ contract LiquidatorTest is Test, IManager, ILiquidator { vm.expectRevert(bytes("Aloe: healthy")); account.liquidate(this, bytes(""), strain, (1 << 32)); - setInterest(lender1, 10010); + _setInterest(lender1, 10010); assertEq(lender1.borrowBalance(address(account)), 20.02e18); vm.expectRevert(); @@ -171,8 +172,8 @@ contract LiquidatorTest is Test, IManager, ILiquidator { vm.expectRevert(bytes("Aloe: healthy")); account.liquidate(this, bytes(""), strain, (1 << 32)); - setInterest(lender0, 10010); - setInterest(lender1, 10010); + _setInterest(lender0, 10010); + _setInterest(lender1, 10010); assertEq(lender0.borrowBalance(address(account)), 200.2e18); assertEq(lender1.borrowBalance(address(account)), 20.02e18); @@ -208,8 +209,8 @@ contract LiquidatorTest is Test, IManager, ILiquidator { vm.expectRevert(bytes("Aloe: healthy")); account.liquidate(this, bytes(""), strain, (1 << 32)); - setInterest(lender0, 10010); - setInterest(lender1, 10010); + _setInterest(lender0, 10010); + _setInterest(lender1, 10010); assertEq(lender0.borrowBalance(address(account)), 200.2e18); assertEq(lender1.borrowBalance(address(account)), 20.02e18); @@ -230,7 +231,8 @@ contract LiquidatorTest is Test, IManager, ILiquidator { assertEq(liquidity, 0); } - function test_spec_interestTriggerRepayDAIUsingSwap(uint8 strain) public { + /// forge-config: default.fuzz.runs = 16 + function test_fuzz_interestTriggerRepayDAIUsingSwap(uint8 strain) public { strain = (strain % 8) + 1; // give the account 1 WETH @@ -255,7 +257,7 @@ contract LiquidatorTest is Test, IManager, ILiquidator { vm.expectRevert(bytes("Aloe: healthy")); account.liquidate(this, bytes(""), 1, (1 << 32)); - setInterest(lender0, 10010); + _setInterest(lender0, 10010); debt = lender0.borrowBalance(address(account)); assertLe(debt - (1595e18 * 10010) / 10000, 1); @@ -278,7 +280,8 @@ contract LiquidatorTest is Test, IManager, ILiquidator { assertGt(asset1.balanceOf(address(this)), 0); } - function test_spec_cannotReenterLiquidate(uint8 strain) public { + /// forge-config: default.fuzz.runs = 16 + function test_fuzz_cannotReenterLiquidate(uint8 strain) public { strain = (strain % 8) + 1; // give the account 1 WETH @@ -294,7 +297,7 @@ contract LiquidatorTest is Test, IManager, ILiquidator { data = abi.encode(Action.WITHDRAW, debt, 0); account.modify(this, data, (1 << 32)); - setInterest(lender0, 10010); + _setInterest(lender0, 10010); debt = (debt * 10010) / 10000; // Disable warn() requirement by setting unleashLiquidationTime=1 @@ -314,7 +317,8 @@ contract LiquidatorTest is Test, IManager, ILiquidator { account.liquidate(this, data, strain, (1 << 32)); } - function test_spec_interestTriggerRepayETHUsingSwap(uint8 scale, uint8 strain) public { + /// forge-config: default.fuzz.runs = 16 + function test_fuzz_interestTriggerRepayETHUsingSwap(uint8 scale, uint8 strain) public { // These tests are forked, so we don't want to spam the RPC with too many fuzzing values strain = (strain % 8) + 1; @@ -343,7 +347,7 @@ contract LiquidatorTest is Test, IManager, ILiquidator { vm.expectRevert(bytes("Aloe: healthy")); account.liquidate(this, bytes(""), 1, (1 << 32)); - setInterest(lender1, 10010); + _setInterest(lender1, 10010); borrow1 = (borrow1 * 10010) / 10000; assertEq(lender1.borrowBalance(address(account)), borrow1); @@ -428,7 +432,7 @@ contract LiquidatorTest is Test, IManager, ILiquidator { assertGt(asset1.balanceOf(address(this)), 0); } - function test_warnDoesProtect() public { + function test_spec_warnDoesProtect() public { uint256 strain = 1; (Prices memory prices, ) = account.getPrices(1 << 32); @@ -554,7 +558,7 @@ contract LiquidatorTest is Test, IManager, ILiquidator { } // (helpers) - function setInterest(Lender lender, uint256 amount) private { + function _setInterest(Lender lender, uint256 amount) private { bytes32 ID = bytes32(uint256(1)); uint256 slot1 = uint256(vm.load(address(lender), ID)); diff --git a/core/test/RateModel.t.sol b/core/test/RateModel.t.sol index 8ebe08e7..1a99ef99 100644 --- a/core/test/RateModel.t.sol +++ b/core/test/RateModel.t.sol @@ -31,12 +31,49 @@ contract RateModelTest is Test { model = new RateModel(); } - function test_accrualFactorRevert(uint256 elapsedTime, uint256 utilization) public { + function test_spec_getYieldPerSecond() public { + assertEq(model.getYieldPerSecond(0.0e18, address(0)), 0); // 0.00% APY + assertEq(model.getYieldPerSecond(0.1e18, address(0)), 67); // 0.21% APY + assertEq(model.getYieldPerSecond(0.2e18, address(0)), 152); // 0.48% APY + assertEq(model.getYieldPerSecond(0.3e18, address(0)), 261); // 0.82% APY + assertEq(model.getYieldPerSecond(0.5e18, address(0)), 610); // 1.94% APY + assertEq(model.getYieldPerSecond(0.6e18, address(0)), 915); // 2.93% APY + assertEq(model.getYieldPerSecond(0.7e18, address(0)), 1423); // 4.59% APY + assertEq(model.getYieldPerSecond(0.8e18, address(0)), 2440); // 8.00% APY + assertEq(model.getYieldPerSecond(0.9e18, address(0)), 5491); // 18.9% APY + assertEq(model.getYieldPerSecond(0.999e18, address(0)), 60400); // 572% APY + } + + function test_spec_getAccrualFactor() public { + assertEq(model.getAccrualFactor(0, 0), 1e12); + assertEq(model.getAccrualFactor(0, 13), 1e12); + assertEq(model.getAccrualFactor(0, 365 days), 1e12); + + assertEq(model.getAccrualFactor(0.1e18, 0), 1e12); + assertEq(model.getAccrualFactor(0.1e18, 13), 1000000000871); + assertEq(model.getAccrualFactor(0.1e18, 1 days), 1000005788813); + assertEq(model.getAccrualFactor(0.1e18, 1 weeks), 1000040522394); + assertEq(model.getAccrualFactor(0.1e18, 365 days), 1000040522394); + + assertEq(model.getAccrualFactor(0.8e18, 0), 1e12); + assertEq(model.getAccrualFactor(0.8e18, 13), 1000000031720); + assertEq(model.getAccrualFactor(0.8e18, 1 days), 1000210838121); + assertEq(model.getAccrualFactor(0.8e18, 1 weeks), 1001476800691); + assertEq(model.getAccrualFactor(0.8e18, 365 days), 1001476800691); + + assertEq(model.getAccrualFactor(0.999e18, 0), 1e12); + assertEq(model.getAccrualFactor(0.999e18, 13), 1000000785200); + assertEq(model.getAccrualFactor(0.999e18, 1 days), 1005232198483); + assertEq(model.getAccrualFactor(0.999e18, 1 weeks), 1037205322874); + assertEq(model.getAccrualFactor(0.999e18, 365 days), 1037205322874); + } + + function test_fuzz_accrualFactorRevert(uint256 elapsedTime, uint256 utilization) public { uint256 result = RateModel(address(0)).getAccrualFactor(utilization, elapsedTime); assertEq(result, 1e12); } - function test_accrualFactorBehavesDespiteEvilModel(uint256 elapsedTime, uint256 utilization) public { + function test_fuzz_accrualFactorBehavesDespiteEvilModel(uint256 elapsedTime, uint256 utilization) public { IRateModel evilModel = new EvilRateModel(); uint256 before = gasleft(); @@ -47,7 +84,7 @@ contract RateModelTest is Test { assertLt(result, 1.533e12); } - function test_accrualFactorIsWithinBounds(uint256 elapsedTime, uint256 utilization) public { + function test_fuzz_accrualFactorIsWithinBounds(uint256 elapsedTime, uint256 utilization) public { uint256 result = model.getAccrualFactor(utilization, elapsedTime); assertGe(result, 1e12); @@ -59,44 +96,20 @@ contract RateModelTest is Test { assertLt(result, 1e12 + 1e12 / MAX_LEVERAGE); } - function test_accrualFactorIncreasesMonotonically(uint256 utilization) public { + function test_fuzz_accrualFactorIncreasesMonotonically(uint256 utilization) public { vm.assume(utilization != 0); assertGe(model.getAccrualFactor(utilization, 13), model.getAccrualFactor(utilization - 1, 13)); } - function test_spec_getAccrualFactor() public { - assertEq(model.getAccrualFactor(0, 0), 1e12); - assertEq(model.getAccrualFactor(0, 13), 1e12); - assertEq(model.getAccrualFactor(0, 365 days), 1e12); - - assertEq(model.getAccrualFactor(0.1e18, 0), 1e12); - assertEq(model.getAccrualFactor(0.1e18, 13), 1000000000871); - assertEq(model.getAccrualFactor(0.1e18, 1 days), 1000005788813); - assertEq(model.getAccrualFactor(0.1e18, 1 weeks), 1000040522394); - assertEq(model.getAccrualFactor(0.1e18, 365 days), 1000040522394); - - assertEq(model.getAccrualFactor(0.8e18, 0), 1e12); - assertEq(model.getAccrualFactor(0.8e18, 13), 1000000031720); - assertEq(model.getAccrualFactor(0.8e18, 1 days), 1000210838121); - assertEq(model.getAccrualFactor(0.8e18, 1 weeks), 1001476800691); - assertEq(model.getAccrualFactor(0.8e18, 365 days), 1001476800691); - - assertEq(model.getAccrualFactor(0.999e18, 0), 1e12); - assertEq(model.getAccrualFactor(0.999e18, 13), 1000000785200); - assertEq(model.getAccrualFactor(0.999e18, 1 days), 1005232198483); - assertEq(model.getAccrualFactor(0.999e18, 1 weeks), 1037205322874); - assertEq(model.getAccrualFactor(0.999e18, 365 days), 1037205322874); - } - - function test_yieldPerSecondIsWithinBounds(uint256 utilization) public { + function test_fuzz_yieldPerSecondIsWithinBounds(uint256 utilization) public { uint256 result = model.getYieldPerSecond(utilization, address(0)); assertGe(result, 0); assertLe(result, 60400); } - function test_yieldPerSecondIncreasesMonotonically(uint256 utilization) public { + function test_fuzz_yieldPerSecondIncreasesMonotonically(uint256 utilization) public { vm.assume(utilization != 0); assertGe( @@ -104,17 +117,4 @@ contract RateModelTest is Test { model.getYieldPerSecond(utilization - 1, address(0)) ); } - - function test_spec_getYieldPerSecond() public { - assertEq(model.getYieldPerSecond(0.0e18, address(0)), 0); // 0.00% APY - assertEq(model.getYieldPerSecond(0.1e18, address(0)), 67); // 0.21% APY - assertEq(model.getYieldPerSecond(0.2e18, address(0)), 152); // 0.48% APY - assertEq(model.getYieldPerSecond(0.3e18, address(0)), 261); // 0.82% APY - assertEq(model.getYieldPerSecond(0.5e18, address(0)), 610); // 1.94% APY - assertEq(model.getYieldPerSecond(0.6e18, address(0)), 915); // 2.93% APY - assertEq(model.getYieldPerSecond(0.7e18, address(0)), 1423); // 4.59% APY - assertEq(model.getYieldPerSecond(0.8e18, address(0)), 2440); // 8.00% APY - assertEq(model.getYieldPerSecond(0.9e18, address(0)), 5491); // 18.9% APY - assertEq(model.getYieldPerSecond(0.999e18, address(0)), 60400); // 572% APY - } } diff --git a/core/test/VolatilityOracle.t.sol b/core/test/VolatilityOracle.t.sol index b04cb8a7..1740c7e3 100644 --- a/core/test/VolatilityOracle.t.sol +++ b/core/test/VolatilityOracle.t.sol @@ -21,7 +21,7 @@ contract VolatilityOracleTest is Test { 0x73B14a78a0D396C521f954532d43fd5fFe385216, // WETH/WBTC 0x1C3140aB59d6cAf9fa7459C6f83D4B52ba881d36, // OP/USDC 0x535541F1aa08416e69Dc4D610131099FA2Ae7222, // WETH/PERP - 0xF334F6104A179207DdaCfb41FA3567FEea8595C2 // WETH/LYRA + 0xF334F6104A179207DdaCfb41FA3567FEea8595C2 // WETH/LYRA ]; function setUp() public { @@ -79,8 +79,8 @@ contract VolatilityOracleTest is Test { oracle.prepare(IUniswapV3Pool(0xbf16ef186e715668AA29ceF57e2fD7f9D48AdFE6)); } - function test_updateTooLate() public { - prepareAllPools(); + function test_spec_updateTooLate() public { + _prepareAllPools(); vm.makePersistent(address(oracle)); vm.rollFork(TWELVE_HOURS_LATER); @@ -107,8 +107,8 @@ contract VolatilityOracleTest is Test { } } - function test_updateTooSoon() public { - prepareAllPools(); + function test_spec_updateTooSoon() public { + _prepareAllPools(); vm.makePersistent(address(oracle)); vm.rollFork(START_BLOCK + 16 seconds / 2 seconds); // roll forward approx. 16 seconds, assuming 2 seconds per block @@ -131,8 +131,8 @@ contract VolatilityOracleTest is Test { } } - function test_updateNormal() public { - prepareAllPools(); + function test_spec_updateNormal() public { + _prepareAllPools(); vm.makePersistent(address(oracle)); vm.rollFork(SIX_HOURS_LATER); @@ -195,7 +195,7 @@ contract VolatilityOracleTest is Test { console2.log("Time Simulated:", currentTime - initialTime, "seconds"); } - function test_historicalETHUSDC() public { + function test_historical_ETHUSDC() public { IUniswapV3Pool pool = IUniswapV3Pool(pools[1]); // WETH/USDC oracle.prepare(pool); vm.makePersistent(address(oracle)); @@ -204,7 +204,7 @@ contract VolatilityOracleTest is Test { uint256 totalGas = 0; for (uint256 i = 0; i < 600; i++) { - currentBlock += (1 + uint256(blockhash(block.number)) % 3) * 7200; + currentBlock += (1 + (uint256(blockhash(block.number)) % 3)) * 7200; vm.createSelectFork("optimism", currentBlock); uint256 g = gasleft(); @@ -217,7 +217,7 @@ contract VolatilityOracleTest is Test { console2.log("avg gas to update oracle:", totalGas / 600); } - function prepareAllPools() private { + function _prepareAllPools() private { uint256 count = pools.length; for (uint256 i = 0; i < count; i++) { IUniswapV3Pool pool = IUniswapV3Pool(pools[i]); diff --git a/core/test/libraries/BalanceSheet.t.sol b/core/test/libraries/BalanceSheet.t.sol index f1c88d56..adf1cf4b 100644 --- a/core/test/libraries/BalanceSheet.t.sol +++ b/core/test/libraries/BalanceSheet.t.sol @@ -32,7 +32,7 @@ contract LibraryWrapper { contract BalanceSheetTest is Test { function setUp() public {} - function test_alwaysHealthyWhenLiabilitiesAre0( + function test_fuzz_alwaysHealthyWhenLiabilitiesAre0( uint128 fixed0, uint128 fixed1, uint128 fluid1A, @@ -160,7 +160,7 @@ contract BalanceSheetTest is Test { assertFalse(seemsLegit, "0.19 false"); } - function test_computeProbePrices(uint160 sqrtMeanPriceX96, uint256 iv, uint8 nSigma, uint8 mtd) public { + function test_fuzz_computeProbePrices(uint160 sqrtMeanPriceX96, uint256 iv, uint8 nSigma, uint8 mtd) public { // The lower bound is related to how precise our assertion is. For prices to be correct within 0.01%, // the sqrtPrice must be >= 2^40 (approximately). Calculations for that are here: // https://www.desmos.com/calculator/suq1f7yswt diff --git a/core/test/libraries/Constants.t.sol b/core/test/libraries/Constants.t.sol index cfd8fc82..d0602f7c 100644 --- a/core/test/libraries/Constants.t.sol +++ b/core/test/libraries/Constants.t.sol @@ -7,7 +7,6 @@ import "src/libraries/constants/Constants.sol"; import {TickMath} from "src/libraries/TickMath.sol"; contract ConstantsTest is Test { - function setUp() public {} function test_spec() external { diff --git a/core/test/libraries/Exp.t.sol b/core/test/libraries/Exp.t.sol index 5c4f02b5..cbde5b7a 100644 --- a/core/test/libraries/Exp.t.sol +++ b/core/test/libraries/Exp.t.sol @@ -8,7 +8,6 @@ import {FixedPointMathLib as SoladyMath} from "solady/utils/FixedPointMathLib.so import {exp1e12} from "src/libraries/Exp.sol"; contract ExpTest is Test { - function setUp() public {} function test_comparative(int256 input) external { @@ -24,7 +23,10 @@ contract ExpTest is Test { function test_bounds() external { assertEq(exp1e12(-28324168296488 + 0), 0); assertEq(exp1e12(-28324168296488 + 1), 0); - assertEq(exp1e12(149121509926857 - 1), 57896044618570924033038090251570834612273709678020728724140821450240425059140); + assertEq( + exp1e12(149121509926857 - 1), + 57896044618570924033038090251570834612273709678020728724140821450240425059140 + ); assertEq(exp1e12(149121509926857 - 0), type(int256).max); assertEq(exp1e12(type(int256).max), type(int256).max); } diff --git a/core/test/libraries/LiquidityAmounts.t.sol b/core/test/libraries/LiquidityAmounts.t.sol index ebb7767b..4f8bd65b 100644 --- a/core/test/libraries/LiquidityAmounts.t.sol +++ b/core/test/libraries/LiquidityAmounts.t.sol @@ -23,7 +23,7 @@ contract LiquidityAmountsTest is Test { assertEq(amount1, 499487993); } - function test_getAmountsForLiquidity( + function test_fuzz_getAmountsForLiquidity( uint160 sqrtPrice, uint160 sqrtLower, uint160 sqrtUpper, diff --git a/core/test/libraries/Log2.t.sol b/core/test/libraries/Log2.t.sol index 248c3c09..f7a680f9 100644 --- a/core/test/libraries/Log2.t.sol +++ b/core/test/libraries/Log2.t.sol @@ -10,7 +10,7 @@ import "src/libraries/Log2.sol"; contract Log2Test is Test { function setUp() public {} - function test_msb() public { + function test_spec_msb() public { assertEq(msb(0), 0); assertEq(msb(1), 0); assertEq(msb(2), 1); @@ -110,7 +110,7 @@ contract Log2Test is Test { assertGe(recovered / 1e4, x / 1e4); } - function test_rewardsUsage(uint56 rate, uint112 totalSupply) public { + function test_rewardsStyleUsage(uint56 rate, uint112 totalSupply) public { vm.assume(totalSupply > 0); int24 log2TotalSupply; @@ -120,8 +120,8 @@ contract Log2Test is Test { } uint256 recoveredTotalSupply = exp2(log2TotalSupply); - uint256 a = 1e16 * uint256(rate) / recoveredTotalSupply; - uint256 b = 1e16 * uint256(rate) / totalSupply; + uint256 a = (1e16 * uint256(rate)) / recoveredTotalSupply; + uint256 b = (1e16 * uint256(rate)) / totalSupply; assertLe(a, b); if (a > 1e3) assertApproxEqRel(a, b, 0.002e18); diff --git a/core/test/libraries/Rewards.t.sol b/core/test/libraries/Rewards.t.sol index 1384338b..eaaeaf59 100644 --- a/core/test/libraries/Rewards.t.sol +++ b/core/test/libraries/Rewards.t.sol @@ -31,11 +31,7 @@ contract MockERC20Rewards is MockERC20 { return super.transfer(to, amount); } - function transferFrom( - address from, - address to, - uint256 amount - ) public virtual override returns (bool) { + function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) { (Rewards.Storage storage s, uint144 a) = Rewards.load(); Rewards.updateUserState(s, a, from, balanceOf[from]); Rewards.updateUserState(s, a, to, balanceOf[to]); @@ -61,10 +57,7 @@ contract MockERC20Rewards is MockERC20 { function claim() external { (Rewards.Storage storage s, uint144 a) = Rewards.load(); - REWARDS_TOKEN.transfer( - msg.sender, - Rewards.claim(s, a, msg.sender, balanceOf[msg.sender]) - ); + REWARDS_TOKEN.transfer(msg.sender, Rewards.claim(s, a, msg.sender, balanceOf[msg.sender])); } function rewards(address user) external view returns (uint112) { @@ -143,12 +136,12 @@ contract RewardsTest is Test { } function test_mock_minRateMaxSupply() public { - uint256 minRate = uint256(10) * 1e18 / 365 days; + uint256 minRate = (uint256(10) * 1e18) / 365 days; uint256 maxSupply = uint256(1e9) * 1e18; pool.setRate(uint56(minRate)); assertEq(pool.rewardsRate(), minRate); - + address alice = address(12345); pool.mint(alice, maxSupply); @@ -160,7 +153,7 @@ contract RewardsTest is Test { } function test_mock_maxRateMinSupply() public { - uint256 maxRate = uint256(1e6) * 1e18 / 365 days; + uint256 maxRate = (uint256(1e6) * 1e18) / 365 days; uint256 minSupply = 1; pool.setRate(uint56(maxRate)); @@ -177,12 +170,12 @@ contract RewardsTest is Test { } function test_mock_minRateMaxSupplyLongTime() public { - uint256 minRate = uint256(10) * 1e18 / 365 days; + uint256 minRate = (uint256(10) * 1e18) / 365 days; uint256 maxSupply = uint256(1e9) * 1e18; pool.setRate(uint56(minRate)); assertEq(pool.rewardsRate(), minRate); - + address alice = address(12345); pool.mint(alice, maxSupply); @@ -194,7 +187,7 @@ contract RewardsTest is Test { } function test_mock_maxRateMinSupplyLongTime() public { - uint256 maxRate = uint256(1e6) * 1e18 / 365 days; + uint256 maxRate = (uint256(1e6) * 1e18) / 365 days; uint256 minSupply = 1; pool.setRate(uint56(maxRate)); @@ -224,7 +217,7 @@ contract RewardsTest is Test { skip(deltaT); uint256 actual = pool.rewardsAccumulator() - before; - uint256 expected = 1e16 * uint256(deltaT) * rate / totalSupply; + uint256 expected = (1e16 * uint256(deltaT) * rate) / totalSupply; assertLe(actual, expected); if (actual / deltaT > 1e3) assertApproxEqRel(actual, expected, 0.002e18); @@ -291,7 +284,7 @@ contract RewardsTest is Test { assertEq(pool.rewards(alice), 6000); rewardsToken.mint(address(pool), 6000); - + vm.prank(alice); pool.claim(); diff --git a/core/test/libraries/TickMath.t.sol b/core/test/libraries/TickMath.t.sol index b01172d3..50ec3bf2 100644 --- a/core/test/libraries/TickMath.t.sol +++ b/core/test/libraries/TickMath.t.sol @@ -8,7 +8,7 @@ import {TickMath} from "src/libraries/TickMath.sol"; contract TickMathTest is Test { function setUp() public {} - function test_memoryGetTickAtSqrtRatio(uint160 sqrtPriceX96) public { + function test_fuzz_memoryGetTickAtSqrtRatio(uint160 sqrtPriceX96) public { if (sqrtPriceX96 < TickMath.MIN_SQRT_RATIO) sqrtPriceX96 = TickMath.MIN_SQRT_RATIO; else if (sqrtPriceX96 >= TickMath.MAX_SQRT_RATIO) sqrtPriceX96 = TickMath.MAX_SQRT_RATIO - 1; @@ -16,7 +16,7 @@ contract TickMathTest is Test { TickMath.getTickAtSqrtRatio(sqrtPriceX96); } - function test_floor(int24 tick, uint8 tickSpacing) public { + function test_fuzz_floor(int24 tick, uint8 tickSpacing) public { if (tick > TickMath.MAX_TICK || tick < TickMath.MIN_TICK) return; if (tickSpacing == 0) return; int24 _tickSpacing = int24(uint24(tickSpacing)); @@ -41,7 +41,7 @@ contract TickMathTest is Test { assertEq(TickMath.floor(-3, 1), -3); } - function test_ceil(int24 tick, uint8 tickSpacing) public { + function test_fuzz_ceil(int24 tick, uint8 tickSpacing) public { if (tick > TickMath.MAX_TICK || tick < TickMath.MIN_TICK) return; if (tickSpacing == 0) return; int24 _tickSpacing = int24(uint24(tickSpacing)); diff --git a/core/test/libraries/Volatility.t.sol b/core/test/libraries/Volatility.t.sol index 28731571..8642cdde 100644 --- a/core/test/libraries/Volatility.t.sol +++ b/core/test/libraries/Volatility.t.sol @@ -102,33 +102,6 @@ contract VolatilityTest is Test { assertEq(dailyIV, 0); // 0% } - function test_estimate( - uint128 tickLiquidity, - int16 tick, - int8 tickMeanOffset, - uint192 a, - uint192 b, - uint48 c, - uint48 d - ) public pure { - Volatility.PoolMetadata memory metadata = Volatility.PoolMetadata(3000, 3000, 60); - Oracle.PoolData memory data = Oracle.PoolData( - TickMath.getSqrtRatioAtTick(tick), // sqrtPriceX96 - tick, // currentTick - TickMath.getSqrtRatioAtTick(tick + int24(tickMeanOffset)), // arithmeticMeanTick - 44521837137365694357186, // secondsPerLiquidityX128 - 3600, // oracleLookback - tickLiquidity // tickLiquidity - ); - Volatility.estimate( - metadata, - data, - Volatility.FeeGrowthGlobals(a, b, 0), - Volatility.FeeGrowthGlobals(uint256(a) + uint256(c), uint256(b) + uint256(d), 7777), - 1 days - ); - } - function test_spec_amount0ToAmount1() public { uint256 amount1; @@ -144,7 +117,7 @@ contract VolatilityTest is Test { assertEq(amount1, 994576722964113793); // ~ 1 ETH } - function test_amount0ToAmount1(uint128 amount0, int24 tick) public { + function test_fuzz_amount0ToAmount1(uint128 amount0, int24 tick) public { tick = int24(bound(tick, -100000, 100000)); uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(tick); uint256 amount1 = Volatility.amount0ToAmount1(amount0, sqrtPriceX96); @@ -166,15 +139,6 @@ contract VolatilityTest is Test { assertEq(revenueGamma, 26); } - function test_computeRevenueGamma( - uint256 feeGrowthGlobalAX128, - uint256 feeGrowthGlobalBX128, - uint160 secondsPerLiquidityX128 - ) public pure { - if (secondsPerLiquidityX128 == 0) return; - Volatility.computeRevenueGamma(feeGrowthGlobalAX128, feeGrowthGlobalBX128, secondsPerLiquidityX128, 5000, 100); - } - function test_spec_computeTickTvl() public { uint256 tickTvl; tickTvl = Volatility.computeTickTvl(1, 19000, TickMath.getSqrtRatioAtTick(19000), 100000000000); @@ -187,11 +151,7 @@ contract VolatilityTest is Test { assertEq(tickTvl, 2578145); } - function test_computeTickTvl( - int24 currentTick, - uint8 tickSpacing, - uint128 tickLiquidity - ) public { + function test_fuzz_computeTickTvl(int24 currentTick, uint8 tickSpacing, uint128 tickLiquidity) public { if (tickSpacing == 0) return; // Always true in the real world int24 _tickSpacing = int24(uint24(tickSpacing)); @@ -208,11 +168,7 @@ contract VolatilityTest is Test { if (tickLiquidity > 1_000_000 && currentTick < lowerBound && currentTick > upperBound) assertGt(tickTvl, 0); } - function test_noRevert_computeTickTvl( - uint8 tier, - int24 tick, - uint128 liquidity - ) public pure { + function test_fuzz_computeTickTvlDoesNotRevert(uint8 tier, int24 tick, uint128 liquidity) public pure { int24 tickSpacing; tier = tier % 4; if (tier == 0) tickSpacing = 1; @@ -225,15 +181,19 @@ contract VolatilityTest is Test { uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(tick); - Volatility.computeTickTvl( - tickSpacing, - tick, - sqrtPriceX96, - liquidity - ); + Volatility.computeTickTvl(tickSpacing, tick, sqrtPriceX96, liquidity); + } + + function test_fuzz_computeRevenueGammaDoesNotRevertA( + uint256 feeGrowthGlobalAX128, + uint256 feeGrowthGlobalBX128, + uint160 secondsPerLiquidityX128 + ) public pure { + if (secondsPerLiquidityX128 == 0) return; + Volatility.computeRevenueGamma(feeGrowthGlobalAX128, feeGrowthGlobalBX128, secondsPerLiquidityX128, 5000, 100); } - function test_noRevert_computeRevenueGamma( + function test_fuzz_computeRevenueGammaDoesNotRevertB( uint256 feeGrowthGlobalAX128, uint224 feeGrowthGlobalDeltaX128, uint160 secondsPerLiquidityX128, @@ -259,7 +219,34 @@ contract VolatilityTest is Test { ); } - function test_noRevert_estimate( + function test_fuzz_estimateDoesNotRevertA( + uint128 tickLiquidity, + int16 tick, + int8 tickMeanOffset, + uint192 a, + uint192 b, + uint48 c, + uint48 d + ) public pure { + Volatility.PoolMetadata memory metadata = Volatility.PoolMetadata(3000, 3000, 60); + Oracle.PoolData memory data = Oracle.PoolData( + TickMath.getSqrtRatioAtTick(tick), // sqrtPriceX96 + tick, // currentTick + TickMath.getSqrtRatioAtTick(tick + int24(tickMeanOffset)), // arithmeticMeanTick + 44521837137365694357186, // secondsPerLiquidityX128 + 3600, // oracleLookback + tickLiquidity // tickLiquidity + ); + Volatility.estimate( + metadata, + data, + Volatility.FeeGrowthGlobals(a, b, 0), + Volatility.FeeGrowthGlobals(uint256(a) + uint256(c), uint256(b) + uint256(d), 7777), + 1 days + ); + } + + function test_fuzz_estimateDoesNotRevertB( uint32 feeGrowthSampleAge, uint256 feeGrowthGlobalA0X128, uint256 feeGrowthGlobalA1X128, @@ -291,8 +278,8 @@ contract VolatilityTest is Test { else if (tier == 3) metadata.tickSpacing = 100; } - tick = boundTick(tick, metadata.tickSpacing); - arithmeticMeanTick = boundTick(arithmeticMeanTick, metadata.tickSpacing); + tick = _boundTick(tick, metadata.tickSpacing); + arithmeticMeanTick = _boundTick(arithmeticMeanTick, metadata.tickSpacing); Oracle.PoolData memory data = Oracle.PoolData( TickMath.getSqrtRatioAtTick(tick), tick, @@ -316,7 +303,7 @@ contract VolatilityTest is Test { assertLt(Volatility.estimate(metadata, data, a, b, scale), 1 << 128); } - function boundTick(int24 tick, int24 tickSpacing) private pure returns (int24) { + function _boundTick(int24 tick, int24 tickSpacing) private pure returns (int24) { if (tick < TickMath.MIN_TICK + tickSpacing) return TickMath.MIN_TICK + tickSpacing; else if (tick > TickMath.MAX_TICK - tickSpacing) return TickMath.MAX_TICK - tickSpacing; else return tick;