From 63b24c7a2ca4dae05c87d41f0071ffae52490851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Fingen?= Date: Tue, 23 Apr 2024 15:08:52 +0100 Subject: [PATCH] chore: forge fmt --- contracts/src/BoldToken.sol | 19 +++-- contracts/src/CollateralRegistry.sol | 71 ++++++++++--------- contracts/src/Dependencies/LiquityBase.sol | 4 +- .../src/Interfaces/ICollateralRegistry.sol | 1 - contracts/src/Interfaces/ITroveManager.sol | 8 +-- contracts/src/SortedTroves.sol | 2 +- contracts/src/TroveManager.sol | 17 +---- contracts/src/deployment.sol | 12 +++- .../src/test/TestContracts/DevTestSetup.sol | 7 +- contracts/src/test/basicOps.t.sol | 6 +- contracts/src/test/multicollateral.t.sol | 70 ++++++++++++------ contracts/src/test/troveManager.t.sol | 6 +- 12 files changed, 119 insertions(+), 104 deletions(-) diff --git a/contracts/src/BoldToken.sol b/contracts/src/BoldToken.sol index 3eaa290fd..6757ec934 100644 --- a/contracts/src/BoldToken.sol +++ b/contracts/src/BoldToken.sol @@ -71,9 +71,7 @@ contract BoldToken is CheckContract, IBoldToken { event BorrowerOperationsAddressAdded(address _newBorrowerOperationsAddress); event ActivePoolAddressAdded(address _newActivePoolAddress); - constructor - ( - ) { + constructor() { bytes32 hashedName = keccak256(bytes(_NAME)); bytes32 hashedVersion = keccak256(bytes(_VERSION)); @@ -264,16 +262,16 @@ contract BoldToken is CheckContract, IBoldToken { } function _requireCallerIsBOorAP() internal view { - require(borrowerOperationsAddresses[msg.sender] || - activePoolAddresses[msg.sender], - "BoldToken: Caller is not BO or AP"); + require( + borrowerOperationsAddresses[msg.sender] || activePoolAddresses[msg.sender], + "BoldToken: Caller is not BO or AP" + ); } function _requireCallerIsBOorTroveMorSP() internal view { require( - borrowerOperationsAddresses[msg.sender] || - troveManagerAddresses[msg.sender] || - stabilityPoolAddresses[msg.sender], + borrowerOperationsAddresses[msg.sender] || troveManagerAddresses[msg.sender] + || stabilityPoolAddresses[msg.sender], "Bold: Caller is neither BorrowerOperations nor TroveManager nor StabilityPool" ); } @@ -285,7 +283,8 @@ contract BoldToken is CheckContract, IBoldToken { function _requireCallerIsTroveMorSP() internal view { require( troveManagerAddresses[msg.sender] || stabilityPoolAddresses[msg.sender], - "Bold: Caller is neither TroveManager nor StabilityPool"); + "Bold: Caller is neither TroveManager nor StabilityPool" + ); } // --- Optional functions --- diff --git a/contracts/src/CollateralRegistry.sol b/contracts/src/CollateralRegistry.sol index 7f061c709..c879b89b7 100644 --- a/contracts/src/CollateralRegistry.sol +++ b/contracts/src/CollateralRegistry.sol @@ -12,7 +12,6 @@ import "./Interfaces/ICollateralRegistry.sol"; //import "forge-std/console.sol"; - contract CollateralRegistry is LiquityBase, ICollateralRegistry { // mapping from Collateral token address to the corresponding TroveManagers //mapping(address => address) troveManagers; @@ -111,7 +110,7 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry { for (uint256 index = 0; index < numCollaterals; index++) { uint256 unbackedPortion = unbackedPortions[index]; if (unbackedPortion > 0) { - uint256 redeemAmount = _boldAmount * unbackedPortion / totalUnbacked; + uint256 redeemAmount = _boldAmount * unbackedPortion / totalUnbacked; if (redeemAmount > 0) { ITroveManager troveManager = getTroveManager(index); uint256 feePercentage = troveManager.redeemCollateral(msg.sender, redeemAmount, _maxIterations); @@ -127,50 +126,54 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry { } function getTroveManager(uint256 _index) public view returns (ITroveManager) { - if (_index == 0) { return _troveManager0; } - else if (_index == 1) { return _troveManager1; } - else if (_index == 2) { return _troveManager2; } - else if (_index == 3) { return _troveManager3; } - else if (_index == 4) { return _troveManager4; } - else if (_index == 5) { return _troveManager5; } - else if (_index == 6) { return _troveManager6; } - else if (_index == 7) { return _troveManager7; } - else if (_index == 8) { return _troveManager8; } - else if (_index == 9) { return _troveManager9; } - else { - revert("Invalid index"); - } + if (_index == 0) return _troveManager0; + else if (_index == 1) return _troveManager1; + else if (_index == 2) return _troveManager2; + else if (_index == 3) return _troveManager3; + else if (_index == 4) return _troveManager4; + else if (_index == 5) return _troveManager5; + else if (_index == 6) return _troveManager6; + else if (_index == 7) return _troveManager7; + else if (_index == 8) return _troveManager8; + else if (_index == 9) return _troveManager9; + else revert("Invalid index"); } function getToken(uint256 _index) external view returns (IERC20) { - if (_index == 0) { return _token0; } - else if (_index == 1) { return _token1; } - else if (_index == 2) { return _token2; } - else if (_index == 3) { return _token3; } - else if (_index == 4) { return _token4; } - else if (_index == 5) { return _token5; } - else if (_index == 6) { return _token6; } - else if (_index == 7) { return _token7; } - else if (_index == 8) { return _token8; } - else if (_index == 9) { return _token9; } - else { - revert("Invalid index"); - } + if (_index == 0) return _token0; + else if (_index == 1) return _token1; + else if (_index == 2) return _token2; + else if (_index == 3) return _token3; + else if (_index == 4) return _token4; + else if (_index == 5) return _token5; + else if (_index == 6) return _token6; + else if (_index == 7) return _token7; + else if (_index == 8) return _token8; + else if (_index == 9) return _token9; + else revert("Invalid index"); } // require functions - function _requireValidMaxFeePercentage(uint _maxFeePercentage) internal pure { - require(_maxFeePercentage >= REDEMPTION_FEE_FLOOR && _maxFeePercentage <= DECIMAL_PRECISION, - "Max fee percentage must be between 0.5% and 100%"); + function _requireValidMaxFeePercentage(uint256 _maxFeePercentage) internal pure { + require( + _maxFeePercentage >= REDEMPTION_FEE_FLOOR && _maxFeePercentage <= DECIMAL_PRECISION, + "Max fee percentage must be between 0.5% and 100%" + ); } - function _requireAmountGreaterThanZero(uint _amount) internal pure { + function _requireAmountGreaterThanZero(uint256 _amount) internal pure { require(_amount > 0, "TroveManager: Amount must be greater than zero"); } - function _requireBoldBalanceCoversRedemption(IBoldToken _boldToken, address _redeemer, uint _amount) internal view { - require(_boldToken.balanceOf(_redeemer) >= _amount, "TroveManager: Requested redemption amount must be <= user's Bold token balance"); + function _requireBoldBalanceCoversRedemption(IBoldToken _boldToken, address _redeemer, uint256 _amount) + internal + view + { + require( + _boldToken.balanceOf(_redeemer) >= _amount, + "TroveManager: Requested redemption amount must be <= user's Bold token balance" + ); } /* diff --git a/contracts/src/Dependencies/LiquityBase.sol b/contracts/src/Dependencies/LiquityBase.sol index 98d26179d..fbb6b88a0 100644 --- a/contracts/src/Dependencies/LiquityBase.sol +++ b/contracts/src/Dependencies/LiquityBase.sol @@ -38,8 +38,8 @@ contract LiquityBase is BaseMath, ILiquityBase { uint256 public constant PERCENT_DIVISOR = 200; // dividing by 200 yields 0.5% - uint constant public BORROWING_FEE_FLOOR = DECIMAL_PRECISION / 1000 * 5; // 0.5% - uint constant public REDEMPTION_FEE_FLOOR = DECIMAL_PRECISION / 1000 * 5; // 0.5% + uint256 public constant BORROWING_FEE_FLOOR = DECIMAL_PRECISION / 1000 * 5; // 0.5% + uint256 public constant REDEMPTION_FEE_FLOOR = DECIMAL_PRECISION / 1000 * 5; // 0.5% IActivePool public activePool; diff --git a/contracts/src/Interfaces/ICollateralRegistry.sol b/contracts/src/Interfaces/ICollateralRegistry.sol index 017a183cb..239fac33e 100644 --- a/contracts/src/Interfaces/ICollateralRegistry.sol +++ b/contracts/src/Interfaces/ICollateralRegistry.sol @@ -3,7 +3,6 @@ pragma solidity 0.8.18; import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; import "./ITroveManager.sol"; - interface ICollateralRegistry { function redeemCollateral(uint256 _boldamount, uint256 _maxIterations, uint256 _maxFeePercentage) external; // getters diff --git a/contracts/src/Interfaces/ITroveManager.sol b/contracts/src/Interfaces/ITroveManager.sol index c6e38b597..a7a135d9e 100644 --- a/contracts/src/Interfaces/ITroveManager.sol +++ b/contracts/src/Interfaces/ITroveManager.sol @@ -43,11 +43,9 @@ interface ITroveManager is IERC721, ILiquityBase { function batchLiquidateTroves(uint256[] calldata _troveArray) external; - function redeemCollateral( - address _sender, - uint _boldAmount, - uint _maxIterations - ) external returns (uint256 _feePercentage); + function redeemCollateral(address _sender, uint256 _boldAmount, uint256 _maxIterations) + external + returns (uint256 _feePercentage); function updateStakeAndTotalStakes(uint256 _troveId) external returns (uint256); diff --git a/contracts/src/SortedTroves.sol b/contracts/src/SortedTroves.sol index 663bfac5e..3510d9e04 100644 --- a/contracts/src/SortedTroves.sol +++ b/contracts/src/SortedTroves.sol @@ -22,7 +22,7 @@ uint256 constant ROOT_NODE_ID = 0; * * The annual interest rate is stored on the Trove struct in TroveManager, not directly on the Node. * -* A node need only be re-inserted when the borrower adjusts their interest rate. Interest rate order is preserved +* A node need only be re-inserted when the borrower adjusts their interest rate. Interest rate order is preserved * under all other system operations. * * The list is a modification of the following audited SortedDoublyLinkedList: diff --git a/contracts/src/TroveManager.sol b/contracts/src/TroveManager.sol index 924335a5a..989b10f21 100644 --- a/contracts/src/TroveManager.sol +++ b/contracts/src/TroveManager.sol @@ -948,25 +948,15 @@ contract TroveManager is ERC721, LiquityBase, Ownable, CheckContract, ITroveMana * redemption will stop after the last completely redeemed Trove and the sender will keep the remaining Bold amount, which they can attempt * to redeem later. */ - function redeemCollateral( - address _sender, - uint _boldamount, - uint _maxIterations - ) + function redeemCollateral(address _sender, uint256 _boldamount, uint256 _maxIterations) external override returns (uint256 _feePercentage) { _requireIsCollateralRegistry(); - ContractsCache memory contractsCache = ContractsCache( - activePool, - defaultPool, - boldToken, - sortedTroves, - collSurplusPool, - gasPoolAddress - ); + ContractsCache memory contractsCache = + ContractsCache(activePool, defaultPool, boldToken, sortedTroves, collSurplusPool, gasPoolAddress); RedemptionTotals memory totals; totals.price = priceFeed.fetchPrice(); @@ -1519,7 +1509,6 @@ contract TroveManager is ERC721, LiquityBase, Ownable, CheckContract, ITroveMana require(checkTroveIsActive(_troveId), "TroveManager: Trove does not exist or is closed"); } - function _requireMoreThanOneTroveInSystem(uint256 TroveIdsArrayLength) internal view { require(TroveIdsArrayLength > 1 && sortedTroves.getSize() > 1, "TroveManager: Only one trove in the system"); } diff --git a/contracts/src/deployment.sol b/contracts/src/deployment.sol index b32549236..2bb48bea1 100644 --- a/contracts/src/deployment.sol +++ b/contracts/src/deployment.sol @@ -34,13 +34,17 @@ struct LiquityContracts { IERC20 WETH; } -function _deployAndConnectContracts() returns (LiquityContracts memory contracts, ICollateralRegistry collateralRegistry, IBoldToken boldToken) { +function _deployAndConnectContracts() + returns (LiquityContracts memory contracts, ICollateralRegistry collateralRegistry, IBoldToken boldToken) +{ LiquityContracts[] memory contractsArray; (contractsArray, collateralRegistry, boldToken) = _deployAndConnectContracts(1); contracts = contractsArray[0]; } -function _deployAndConnectContracts(uint256 _numCollaterals) returns (LiquityContracts[] memory contractsArray, ICollateralRegistry collateralRegistry, IBoldToken boldToken) { +function _deployAndConnectContracts(uint256 _numCollaterals) + returns (LiquityContracts[] memory contractsArray, ICollateralRegistry collateralRegistry, IBoldToken boldToken) +{ boldToken = new BoldToken(); contractsArray = new LiquityContracts[](_numCollaterals); @@ -81,7 +85,9 @@ function _deployAndConnectContracts(uint256 _numCollaterals) returns (LiquityCon } } -function _deployAndConnectCollateralContracts(IERC20 _collateralToken, IBoldToken _boldToken) returns (LiquityContracts memory contracts) { +function _deployAndConnectCollateralContracts(IERC20 _collateralToken, IBoldToken _boldToken) + returns (LiquityContracts memory contracts) +{ // TODO: optimize deployment order & constructor args & connector functions contracts.WETH = _collateralToken; diff --git a/contracts/src/test/TestContracts/DevTestSetup.sol b/contracts/src/test/TestContracts/DevTestSetup.sol index 34822f800..8d2d40d64 100644 --- a/contracts/src/test/TestContracts/DevTestSetup.sol +++ b/contracts/src/test/TestContracts/DevTestSetup.sol @@ -11,7 +11,12 @@ contract DevTestSetup is BaseTest { return giveAndApproveCollateral(WETH, _account, _amount, address(borrowerOperations)); } - function giveAndApproveCollateral(IERC20 _token, address _account, uint256 _amount, address _borrowerOperationsAddress) public { + function giveAndApproveCollateral( + IERC20 _token, + address _account, + uint256 _amount, + address _borrowerOperationsAddress + ) public { // Give some Collateral to test accounts deal(address(_token), _account, _amount); diff --git a/contracts/src/test/basicOps.t.sol b/contracts/src/test/basicOps.t.sol index 4c2327a61..bd8ed004c 100644 --- a/contracts/src/test/basicOps.t.sol +++ b/contracts/src/test/basicOps.t.sol @@ -98,11 +98,7 @@ contract BasicOps is DevTestSetup { // A redeems 1k BOLD vm.startPrank(A); - collateralRegistry.redeemCollateral( - redemptionAmount, - 10, - 1e18 - ); + collateralRegistry.redeemCollateral(redemptionAmount, 10, 1e18); // Check B's coll and debt reduced uint256 debt_2 = troveManager.getTroveDebt(B_Id); diff --git a/contracts/src/test/multicollateral.t.sol b/contracts/src/test/multicollateral.t.sol index 0a1df1d2e..b234df1cd 100644 --- a/contracts/src/test/multicollateral.t.sol +++ b/contracts/src/test/multicollateral.t.sol @@ -2,7 +2,6 @@ pragma solidity ^0.8.18; import "./TestContracts/DevTestSetup.sol"; - contract MulticollateralTest is DevTestSetup { uint256 constant NUM_COLLATERALS = 4; LiquityContracts[] public contractsArray; @@ -14,20 +13,19 @@ contract MulticollateralTest is DevTestSetup { uint256 _coll, uint256 _boldAmount, uint256 _annualInterestRate - ) - public - returns (uint256) - { + ) public returns (uint256) { // TODO: remove when we switch to new gas compensation - if (_boldAmount >= 2000e18) { _boldAmount -= 200e18; } + if (_boldAmount >= 2000e18) _boldAmount -= 200e18; vm.startPrank(_account); - uint256 troveId = contractsArray[_collIndex].borrowerOperations.openTrove(_account, _index, 1e18, _coll, _boldAmount, 0, 0, _annualInterestRate); + uint256 troveId = contractsArray[_collIndex].borrowerOperations.openTrove( + _account, _index, 1e18, _coll, _boldAmount, 0, 0, _annualInterestRate + ); vm.stopPrank(); return troveId; } - function makeMulticollateralSPDeposit(uint256 _collIndex,address _account, uint256 _amount) public { + function makeMulticollateralSPDeposit(uint256 _collIndex, address _account, uint256 _amount) public { vm.startPrank(_account); contractsArray[_collIndex].stabilityPool.provideToSP(_amount); vm.stopPrank(); @@ -40,26 +38,39 @@ contract MulticollateralTest is DevTestSetup { accounts = new Accounts(); createAccounts(); - (A, B, C, D, E, F, G) = - (accountsList[0], accountsList[1], accountsList[2], accountsList[3], accountsList[4], accountsList[5], accountsList[6]); + (A, B, C, D, E, F, G) = ( + accountsList[0], + accountsList[1], + accountsList[2], + accountsList[3], + accountsList[4], + accountsList[5], + accountsList[6] + ); LiquityContracts[] memory _contractsArray; (_contractsArray, collateralRegistry, boldToken) = _deployAndConnectContracts(NUM_COLLATERALS); // Unimplemented feature (...):Copying of type struct LiquityContracts memory[] memory to storage not yet supported. - for(uint256 c = 0; c < NUM_COLLATERALS; c++) { + for (uint256 c = 0; c < NUM_COLLATERALS; c++) { contractsArray.push(_contractsArray[c]); } // Set all price feeds to 2k - for(uint256 c = 0; c < NUM_COLLATERALS; c++) { + for (uint256 c = 0; c < NUM_COLLATERALS; c++) { contractsArray[c].priceFeed.setPrice(2000e18); } // Give some Collateral to test accounts, and approve it to BorrowerOperations uint256 initialCollateralAmount = 10_000e18; - for(uint256 c = 0; c < NUM_COLLATERALS; c++) { - for(uint256 i = 0; i < 6; i++) { // A to F - giveAndApproveCollateral(contractsArray[c].WETH, accountsList[i], initialCollateralAmount, address(contractsArray[c].borrowerOperations)); + for (uint256 c = 0; c < NUM_COLLATERALS; c++) { + for (uint256 i = 0; i < 6; i++) { + // A to F + giveAndApproveCollateral( + contractsArray[c].WETH, + accountsList[i], + initialCollateralAmount, + address(contractsArray[c].borrowerOperations) + ); } } } @@ -133,7 +144,13 @@ contract MulticollateralTest is DevTestSetup { uint256 unbackedPortion; } - function testMultiCollateralRedemptionFuzz(uint256 _spBoldAmount1, uint256 _spBoldAmount2, uint256 _spBoldAmount3, uint256 _spBoldAmount4, uint256 _redemptionFraction) public { + function testMultiCollateralRedemptionFuzz( + uint256 _spBoldAmount1, + uint256 _spBoldAmount2, + uint256 _spBoldAmount3, + uint256 _spBoldAmount4, + uint256 _redemptionFraction + ) public { TestValues memory testValues1; TestValues memory testValues2; TestValues memory testValues3; @@ -150,19 +167,19 @@ contract MulticollateralTest is DevTestSetup { // First collateral openMulticollateralTroveNoHints100pctMaxFeeWithIndex(0, A, 0, 10e18, boldAmount, 5e16); - if (_spBoldAmount1 > 0) { makeMulticollateralSPDeposit(0, A, _spBoldAmount1); } + if (_spBoldAmount1 > 0) makeMulticollateralSPDeposit(0, A, _spBoldAmount1); // Second collateral openMulticollateralTroveNoHints100pctMaxFeeWithIndex(1, A, 0, 10e18, boldAmount, 5e16); - if (_spBoldAmount2 > 0) { makeMulticollateralSPDeposit(1, A, _spBoldAmount2); } + if (_spBoldAmount2 > 0) makeMulticollateralSPDeposit(1, A, _spBoldAmount2); // Third collateral openMulticollateralTroveNoHints100pctMaxFeeWithIndex(2, A, 0, 10e18, boldAmount, 5e16); - if (_spBoldAmount3 > 0) { makeMulticollateralSPDeposit(2, A, _spBoldAmount3); } + if (_spBoldAmount3 > 0) makeMulticollateralSPDeposit(2, A, _spBoldAmount3); // Fourth collateral openMulticollateralTroveNoHints100pctMaxFeeWithIndex(3, A, 0, 10e18, boldAmount, 5e16); - if (_spBoldAmount4 > 0) { makeMulticollateralSPDeposit(3, A, _spBoldAmount4); } + if (_spBoldAmount4 > 0) makeMulticollateralSPDeposit(3, A, _spBoldAmount4); // Check A’s final bal uint256 boldBalance = boldToken.balanceOf(A); @@ -187,7 +204,9 @@ contract MulticollateralTest is DevTestSetup { vm.stopPrank(); // Check bold balance - assertApproximatelyEqual(boldToken.balanceOf(A), boldBalance - redeemAmount, 10, "Wrong Bold balance after redemption"); + assertApproximatelyEqual( + boldToken.balanceOf(A), boldBalance - redeemAmount, 10, "Wrong Bold balance after redemption" + ); // Check collateral balances // final balances @@ -200,13 +219,18 @@ contract MulticollateralTest is DevTestSetup { testValues2.unbackedPortion = boldAmount - _spBoldAmount2; testValues3.unbackedPortion = boldAmount - _spBoldAmount3; testValues4.unbackedPortion = boldAmount - _spBoldAmount4; - uint256 totalUnbacked = testValues1.unbackedPortion + testValues2.unbackedPortion + testValues3.unbackedPortion + testValues4.unbackedPortion; + uint256 totalUnbacked = testValues1.unbackedPortion + testValues2.unbackedPortion + testValues3.unbackedPortion + + testValues4.unbackedPortion; console.log(redeemAmount, "redeemAmount"); console.log(testValues1.unbackedPortion, "testValues1.unbackedPortion"); console.log(totalUnbacked, "totalUnbacked"); console.log(redeemAmount * testValues1.unbackedPortion / totalUnbacked, "var"); - assertEq(testValues1.collFinalBalance - testValues1.collInitialBalance, redeemAmount * testValues1.unbackedPortion / totalUnbacked / 2000, "Wrong Collateral 1 balance"); + assertEq( + testValues1.collFinalBalance - testValues1.collInitialBalance, + redeemAmount * testValues1.unbackedPortion / totalUnbacked / 2000, + "Wrong Collateral 1 balance" + ); //assertEq(coll2FinalBalance - coll2InitialBalance, 25e16, "Wrong Collateral 2 balance"); //assertEq(coll3FinalBalance - coll3InitialBalance, 5e16, "Wrong Collateral 3 balance"); //assertEq(coll4FinalBalance - coll4InitialBalance, 0, "Wrong Collateral 4 balance"); diff --git a/contracts/src/test/troveManager.t.sol b/contracts/src/test/troveManager.t.sol index e4bbbf727..cdb573314 100644 --- a/contracts/src/test/troveManager.t.sol +++ b/contracts/src/test/troveManager.t.sol @@ -28,11 +28,7 @@ contract TroveManagerTest is DevTestSetup { // C redeems 1k BOLD vm.startPrank(C); - collateralRegistry.redeemCollateral( - redemptionAmount, - 10, - 1e18 - ); + collateralRegistry.redeemCollateral(redemptionAmount, 10, 1e18); vm.stopPrank(); // Check A's coll and debt are the same