From 0e6a228a120b0a0d4bb924554c987e39c00befa6 Mon Sep 17 00:00:00 2001 From: Joseph Delong Date: Mon, 5 Feb 2024 20:48:24 -0600 Subject: [PATCH] fix: rename enforcer structs to Details (#47) --- src/enforcers/AstariaV1BorrowerEnforcer.sol | 29 +++++++++------------ src/enforcers/AstariaV1LenderEnforcer.sol | 14 +++++----- test/AstariaV1Test.sol | 14 ++++------ test/TestV1BorrowerEnforcer.sol | 16 ++++++------ test/TestV1LenderEnforcer.sol | 17 +++++------- test/fuzz-testing/TestFuzzV1.sol | 2 +- 6 files changed, 41 insertions(+), 51 deletions(-) diff --git a/src/enforcers/AstariaV1BorrowerEnforcer.sol b/src/enforcers/AstariaV1BorrowerEnforcer.sol index 3755714..51478c9 100644 --- a/src/enforcers/AstariaV1BorrowerEnforcer.sol +++ b/src/enforcers/AstariaV1BorrowerEnforcer.sol @@ -36,7 +36,7 @@ contract AstariaV1BorrowerEnforcer is CaveatEnforcer { /* STRUCTS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ - struct V1BorrowerDetails { + struct Details { uint256 startTime; uint256 endTime; uint256 startRate; @@ -51,8 +51,8 @@ contract AstariaV1BorrowerEnforcer is CaveatEnforcer { /// @notice Calculates the current maximum valid rate of a caveat function locateCurrentRate(bytes calldata caveatData) external view returns (uint256 currentRate) { - V1BorrowerDetails memory v1Details = abi.decode(caveatData, (V1BorrowerDetails)); - return _locateCurrentRate(v1Details); + Details memory details = abi.decode(caveatData, (Details)); + return _locateCurrentRate(details); } /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ @@ -84,21 +84,21 @@ contract AstariaV1BorrowerEnforcer is CaveatEnforcer { AstariaV1Lib.getBasePricingDecimals(loanTerms.pricingData) // decimals ); - V1BorrowerDetails memory v1Details = abi.decode(caveatData, (V1BorrowerDetails)); + Details memory details = abi.decode(caveatData, (Details)); - if (loanAmount < v1Details.minAmount || loanAmount > v1Details.maxAmount) { + if (loanAmount < details.minAmount || loanAmount > details.maxAmount) { // Debt amount is less than the current caveat amount revert LoanAmountOutOfBounds(); } - uint256 currentRate = _locateCurrentRate(v1Details); + uint256 currentRate = _locateCurrentRate(details); if (loanRate > currentRate) { // Loan rate is greater than the current caveat rate revert LoanRateExceedsCurrentRate(); } // Update the caveat loan rate and amount - Starport.Loan memory caveatLoan = v1Details.loan; + Starport.Loan memory caveatLoan = details.loan; uint256 i = 0; for (; i < caveatLoan.collateral.length;) { if (caveatLoan.collateral[i].amount < loan.collateral[i].amount) { @@ -122,20 +122,17 @@ contract AstariaV1BorrowerEnforcer is CaveatEnforcer { /* INTERNAL FUNCTIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ - function _locateCurrentRate(V1BorrowerDetails memory v1Details) internal view returns (uint256 currentRate) { - uint256 endRate = AstariaV1Lib.getBasePricingRate(v1Details.loan.terms.pricingData); + function _locateCurrentRate(Details memory details) internal view returns (uint256 currentRate) { + uint256 endRate = AstariaV1Lib.getBasePricingRate(details.loan.terms.pricingData); // if endRate == startRate, or startTime == endTime, or block.timestamp > endTime - if ( - endRate == v1Details.startRate || v1Details.startTime == v1Details.endTime - || block.timestamp > v1Details.endTime - ) { + if (endRate == details.startRate || details.startTime == details.endTime || block.timestamp > details.endTime) { return endRate; } // Will revert if startTime > endTime - uint256 duration = v1Details.endTime - v1Details.startTime; - uint256 elapsed = block.timestamp - v1Details.startTime; + uint256 duration = details.endTime - details.startTime; + uint256 elapsed = block.timestamp - details.startTime; uint256 remaining; assembly ("memory-safe") { // block.timestamp <= endTime, can't underflow @@ -144,7 +141,7 @@ contract AstariaV1BorrowerEnforcer is CaveatEnforcer { // Calculate rate with a linear growth // Weight startRate by the remaining time, and endRate by the elapsed time - uint256 totalBeforeDivision = (v1Details.startRate * remaining) + (endRate * elapsed); + uint256 totalBeforeDivision = (details.startRate * remaining) + (endRate * elapsed); assembly ("memory-safe") { // duration > 0, as startTime != endTime and endTime - startTime did not underflow currentRate := div(totalBeforeDivision, duration) diff --git a/src/enforcers/AstariaV1LenderEnforcer.sol b/src/enforcers/AstariaV1LenderEnforcer.sol index 05b6c6b..5919f09 100644 --- a/src/enforcers/AstariaV1LenderEnforcer.sol +++ b/src/enforcers/AstariaV1LenderEnforcer.sol @@ -43,7 +43,7 @@ contract AstariaV1LenderEnforcer is CaveatEnforcer { /* STRUCTS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ - struct V1LenderDetails { + struct Details { bool matchIdentifier; uint256 minDebtAmount; Starport.Loan loan; @@ -76,16 +76,16 @@ contract AstariaV1LenderEnforcer is CaveatEnforcer { AstariaV1Lib.getBasePricingDecimals(loanTerms.pricingData) ); - V1LenderDetails memory v1Details = abi.decode(caveatData, (V1LenderDetails)); - Starport.Loan memory caveatLoan = v1Details.loan; + Details memory details = abi.decode(caveatData, (Details)); + Starport.Loan memory caveatLoan = details.loan; SpentItem memory caveatDebt = caveatLoan.debt[0]; - if (v1Details.minDebtAmount > caveatDebt.amount) { + if (details.minDebtAmount > caveatDebt.amount) { revert MinDebtAmountExceedsMax(); } - if (loanAmount > caveatDebt.amount || loanAmount < v1Details.minDebtAmount) { + if (loanAmount > caveatDebt.amount || loanAmount < details.minDebtAmount) { // Debt amount is greater than the max amount or the caveatDebt amount - revert DebtAmountOOB(v1Details.minDebtAmount, caveatDebt.amount, loanAmount); + revert DebtAmountOOB(details.minDebtAmount, caveatDebt.amount, loanAmount); } bytes memory caveatPricingData = caveatLoan.terms.pricingData; @@ -99,7 +99,7 @@ contract AstariaV1LenderEnforcer is CaveatEnforcer { // Update the caveat loan amount caveatDebt.amount = loanAmount; - if (!v1Details.matchIdentifier) { + if (!details.matchIdentifier) { // Update the caveat loan identifier uint256 i = 0; for (; i < caveatLoan.collateral.length;) { diff --git a/test/AstariaV1Test.sol b/test/AstariaV1Test.sol index 33e4952..e05d39b 100644 --- a/test/AstariaV1Test.sol +++ b/test/AstariaV1Test.sol @@ -99,8 +99,7 @@ contract AstariaV1Test is StarportTest { { loan = loanCopy(loan); loan.issuer = address(0); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory v1BorrowerDetails = AstariaV1BorrowerEnforcer - .V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory Details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp, endTime: block.timestamp, startRate: AstariaV1Lib.getBasePricingRate(loan.terms.pricingData), @@ -109,7 +108,7 @@ contract AstariaV1Test is StarportTest { loan: loan }); CaveatEnforcer.Caveat memory caveat = - CaveatEnforcer.Caveat({enforcer: address(borrowerEnforcer), data: abi.encode(v1BorrowerDetails)}); + CaveatEnforcer.Caveat({enforcer: address(borrowerEnforcer), data: abi.encode(Details)}); return signCaveatForAccount(caveat, salt, signer, true); } @@ -123,14 +122,11 @@ contract AstariaV1Test is StarportTest { loan = loanCopy(loan); loan.borrower = address(0); - AstariaV1LenderEnforcer.V1LenderDetails memory v1LenderDetails = AstariaV1LenderEnforcer.V1LenderDetails({ - matchIdentifier: true, - minDebtAmount: loan.debt[0].amount, - loan: loan - }); + AstariaV1LenderEnforcer.Details memory Details = + AstariaV1LenderEnforcer.Details({matchIdentifier: true, minDebtAmount: loan.debt[0].amount, loan: loan}); CaveatEnforcer.Caveat memory caveat = - CaveatEnforcer.Caveat({enforcer: address(lenderEnforcer), data: abi.encode(v1LenderDetails)}); + CaveatEnforcer.Caveat({enforcer: address(lenderEnforcer), data: abi.encode(Details)}); return signCaveatForAccount(caveat, salt, signer, invalidate); } diff --git a/test/TestV1BorrowerEnforcer.sol b/test/TestV1BorrowerEnforcer.sol index e34cdb9..2e4965b 100644 --- a/test/TestV1BorrowerEnforcer.sol +++ b/test/TestV1BorrowerEnforcer.sol @@ -34,7 +34,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer { function testV1BorrowerEnforcerEnd() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp, endTime: block.timestamp + 10, startRate: endRate / 2, @@ -53,7 +53,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer { function testV1BorrowerEnforcerStart() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp, endTime: block.timestamp + 10, startRate: endRate / 2, @@ -68,7 +68,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer { function testRevertLocateCurrentRateAndAmount() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp + 10, endTime: block.timestamp, startRate: endRate / 2, @@ -91,7 +91,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer { function testV1BorrowerEnforcerHalfway() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp, endTime: block.timestamp + 10, startRate: endRate / 2, @@ -112,7 +112,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer { function testV1BorrowerEnforcerDebtAmountOOB() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp, endTime: block.timestamp + 10, startRate: endRate / 2, @@ -142,7 +142,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer { identifier: 0 // 0 for ERC20 }); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp, endTime: block.timestamp + 10, startRate: endRate / 2, @@ -167,7 +167,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer { function testV1BorrowerEnforcerRateGTCurrent() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp, endTime: block.timestamp + 10, startRate: endRate / 2, @@ -187,7 +187,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer { function testV1BorrowerEnforcerRateLTCurrent() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({ + AstariaV1BorrowerEnforcer.Details memory details = AstariaV1BorrowerEnforcer.Details({ startTime: block.timestamp, endTime: block.timestamp + 10, startRate: endRate / 2, diff --git a/test/TestV1LenderEnforcer.sol b/test/TestV1LenderEnforcer.sol index 59a3d8d..e602ca2 100644 --- a/test/TestV1LenderEnforcer.sol +++ b/test/TestV1LenderEnforcer.sol @@ -26,7 +26,7 @@ contract TestV1LenderEnforcer is AstariaV1Test, AstariaV1LenderEnforcer { function testV1LenderEnforcerAmount() public { Starport.Loan memory loan = generateDefaultLoanTerms(); uint256 max = loan.debt[0].amount; - AstariaV1LenderEnforcer.V1LenderDetails memory details = AstariaV1LenderEnforcer.V1LenderDetails({ + AstariaV1LenderEnforcer.Details memory details = AstariaV1LenderEnforcer.Details({ matchIdentifier: true, minDebtAmount: loan.debt[0].amount / 2, loan: loanCopy(loan) @@ -57,7 +57,7 @@ contract TestV1LenderEnforcer is AstariaV1Test, AstariaV1LenderEnforcer { function testV1LenderEnforcerMinDebtExceedsMax() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1LenderEnforcer.V1LenderDetails memory details = AstariaV1LenderEnforcer.V1LenderDetails({ + AstariaV1LenderEnforcer.Details memory details = AstariaV1LenderEnforcer.Details({ matchIdentifier: true, minDebtAmount: loan.debt[0].amount + 1, loan: loanCopy(loan) @@ -71,7 +71,7 @@ contract TestV1LenderEnforcer is AstariaV1Test, AstariaV1LenderEnforcer { function testV1LenderEnforcerRate() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1LenderEnforcer.V1LenderDetails memory details = AstariaV1LenderEnforcer.V1LenderDetails({ + AstariaV1LenderEnforcer.Details memory details = AstariaV1LenderEnforcer.Details({ matchIdentifier: true, minDebtAmount: loan.debt[0].amount, loan: loanCopy(loan) @@ -95,7 +95,7 @@ contract TestV1LenderEnforcer is AstariaV1Test, AstariaV1LenderEnforcer { function testV1LenderEnforcerMatchIdentifier() public { Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1LenderEnforcer.V1LenderDetails memory details = AstariaV1LenderEnforcer.V1LenderDetails({ + AstariaV1LenderEnforcer.Details memory details = AstariaV1LenderEnforcer.Details({ matchIdentifier: false, minDebtAmount: loan.debt[0].amount, loan: loanCopy(loan) @@ -122,11 +122,8 @@ contract TestV1LenderEnforcer is AstariaV1Test, AstariaV1LenderEnforcer { }); Starport.Loan memory loan = generateDefaultLoanTerms(); - AstariaV1LenderEnforcer.V1LenderDetails memory details = AstariaV1LenderEnforcer.V1LenderDetails({ - matchIdentifier: false, - minDebtAmount: loan.debt[0].amount, - loan: loan - }); + AstariaV1LenderEnforcer.Details memory details = + AstariaV1LenderEnforcer.Details({matchIdentifier: false, minDebtAmount: loan.debt[0].amount, loan: loan}); vm.expectRevert(LenderEnforcer.InvalidAdditionalTransfer.selector); lenderEnforcer.validate(additionalTransfers, loan, abi.encode(details)); @@ -143,7 +140,7 @@ contract TestV1LenderEnforcer is AstariaV1Test, AstariaV1LenderEnforcer { debt[1] = _getERC721SpentItem(TestERC721(loan.debt[0].token), loan.debt[0].identifier + 1); loan.debt = debt; - AstariaV1LenderEnforcer.V1LenderDetails({matchIdentifier: false, minDebtAmount: loan.debt[0].amount, loan: loan}); + AstariaV1LenderEnforcer.Details({matchIdentifier: false, minDebtAmount: loan.debt[0].amount, loan: loan}); vm.expectRevert(DebtBundlesNotSupported.selector); lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(LenderEnforcer.Details({loan: loan}))); diff --git a/test/fuzz-testing/TestFuzzV1.sol b/test/fuzz-testing/TestFuzzV1.sol index fdf02fc..de9cd2e 100644 --- a/test/fuzz-testing/TestFuzzV1.sol +++ b/test/fuzz-testing/TestFuzzV1.sol @@ -251,7 +251,7 @@ contract TestFuzzV1 is AstariaV1Test, TestFuzzStarport { refiLoan.terms.pricingData = pricingData; refiLoan.debt = SP.applyRefinanceConsiderationToLoan(considerationPayment, carryPayment); - AstariaV1LenderEnforcer.V1LenderDetails memory lenderDetails = AstariaV1LenderEnforcer.V1LenderDetails({ + AstariaV1LenderEnforcer.Details memory lenderDetails = AstariaV1LenderEnforcer.Details({ matchIdentifier: true, minDebtAmount: refiLoan.debt[0].amount, loan: refiLoan