Skip to content

Commit

Permalink
Run prettier on tests and organize a few things (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenshively authored Oct 14, 2023
1 parent 4751303 commit c986bdd
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 160 deletions.
2 changes: 1 addition & 1 deletion core/src/Lender.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 22 additions & 18 deletions core/test/Liquidator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));

Expand Down
86 changes: 43 additions & 43 deletions core/test/RateModel.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -59,62 +96,25 @@ 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(
model.getYieldPerSecond(utilization, address(0)),
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
}
}
20 changes: 10 additions & 10 deletions core/test/VolatilityOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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();
Expand All @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions core/test/libraries/BalanceSheet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion core/test/libraries/Constants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions core/test/libraries/Exp.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion core/test/libraries/LiquidityAmounts.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract LiquidityAmountsTest is Test {
assertEq(amount1, 499487993);
}

function test_getAmountsForLiquidity(
function test_fuzz_getAmountsForLiquidity(
uint160 sqrtPrice,
uint160 sqrtLower,
uint160 sqrtUpper,
Expand Down
Loading

0 comments on commit c986bdd

Please sign in to comment.