Skip to content

Commit

Permalink
AstariaV1Pricing tests, coverage, and cleanup, new snapshot (#50)
Browse files Browse the repository at this point in the history
* AstariaV1Pricing tests, coverage, and cleanup, new snapshot

* clean up if else to adopt {} braces

* remove console imports from core and modules

* newest snapshot

* clean up commended code
  • Loading branch information
androolloyd authored Nov 4, 2023
1 parent b4723b2 commit bb80142
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 62 deletions.
69 changes: 38 additions & 31 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DiffFuzzTestStarportLib:testSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 880420, ~: 883377)
DiffFuzzTestStarportLib:testUnboundSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 232899, ~: 237832)
DiffFuzzTestStarportLib:testSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 880466, ~: 882693)
DiffFuzzTestStarportLib:testUnboundSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 232918, ~: 237832)
IntegrationTestCaveats:testOriginateUnapprovedFulfiller() (gas: 341293)
IntegrationTestCaveats:testOriginateWBorrowerApproval() (gas: 298091)
IntegrationTestCaveats:testOriginateWCaveats() (gas: 272268)
Expand All @@ -15,9 +15,16 @@ IntegrationTestCaveats:testRefinanceWCaveatsInvalidSalt() (gas: 381151)
IntegrationTestCaveats:testRefinanceWLenderApproval() (gas: 405713)
PausableNonReentrantImpl:test() (gas: 2442)
PausableNonReentrantImpl:testReentrancy() (gas: 2735)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallBase() (gas: 1024388)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallBase() (gas: 1023396)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 708447)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 747164)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 747230)
TestAstariaV1Pricing:testGetRefiannceConsiderationValidEqualRate() (gas: 146499)
TestAstariaV1Pricing:testGetRefiannceConsiderationValidHigherRate() (gas: 158898)
TestAstariaV1Pricing:testGetRefiannceConsiderationValidLowerRate() (gas: 153927)
TestAstariaV1Pricing:testGetRefinanceConsiderationAsBorrowerZeroRate() (gas: 81159)
TestAstariaV1Pricing:testGetRefinanceConsiderationInsufficientRefinance() (gas: 108392)
TestAstariaV1Pricing:testGetRefinanceConsiderationInvalidRefinance() (gas: 88455)
TestAstariaV1Pricing:testGetRefinanceConsiderationZeroRate() (gas: 139117)
TestAstariaV1Settlement:testGetAuctionStart() (gas: 433653)
TestAstariaV1Settlement:testGetAuctionStartNotStarted() (gas: 433226)
TestAstariaV1Settlement:testGetCurrentAuctionPrice() (gas: 448508)
Expand All @@ -28,16 +35,16 @@ TestAstariaV1Settlement:testGetSettlementLoanNotRecalled() (gas: 439333)
TestAstariaV1Settlement:testV1SettlementHandlerExecute() (gas: 419840)
TestAstariaV1Settlement:testV1SettlementHandlerValidate() (gas: 419952)
TestAstariaV1Settlement:testV1SettlementHandlerValidateInvalidHandler() (gas: 420076)
TestAstariaV1Status:testCannotRecallTwice() (gas: 580716)
TestAstariaV1Status:testCannotRecallTwice() (gas: 580760)
TestAstariaV1Status:testCannotWithdrawLoanHasNotBeenRefinanced() (gas: 425689)
TestAstariaV1Status:testCannotWithdrawWithdrawDoesNotExist() (gas: 432118)
TestAstariaV1Status:testGenerateRecallConsideration() (gas: 481554)
TestAstariaV1Status:testInvalidRecallInvalidStakeType() (gas: 507879)
TestAstariaV1Status:testInvalidRecallLoanDoesNotExist() (gas: 523886)
TestAstariaV1Status:testGenerateRecallConsideration() (gas: 481620)
TestAstariaV1Status:testInvalidRecallInvalidStakeType() (gas: 507923)
TestAstariaV1Status:testInvalidRecallLoanDoesNotExist() (gas: 523908)
TestAstariaV1Status:testIsActive() (gas: 432838)
TestAstariaV1Status:testIsRecalledInsideWindow() (gas: 571509)
TestAstariaV1Status:testIsRecalledOutsideWindow() (gas: 569410)
TestAstariaV1Status:testRecallRateActiveRecall() (gas: 557048)
TestAstariaV1Status:testIsRecalledInsideWindow() (gas: 571531)
TestAstariaV1Status:testIsRecalledOutsideWindow() (gas: 569432)
TestAstariaV1Status:testRecallRateActiveRecall() (gas: 557070)
TestAstariaV1Status:testRecallRateEmptyRecall() (gas: 430492)
TestBorrowerEnforcer:testBERevertAdditionalTransfers() (gas: 73195)
TestBorrowerEnforcer:testBERevertInvalidLoanTerms() (gas: 78316)
Expand All @@ -53,11 +60,11 @@ TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72489)
TestCustodian:testCustodySelector() (gas: 2728398)
TestCustodian:testDefaultCustodySelectorRevert() (gas: 70083)
TestCustodian:testGenerateOrderInvalidHandlerExecution() (gas: 133050)
TestCustodian:testGenerateOrderRepay() (gas: 171915)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 197518)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 833690)
TestCustodian:testGenerateOrderRepay() (gas: 171937)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 197540)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 833734)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 757868)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 516666)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 516688)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 90335)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 84652)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 96426)
Expand All @@ -75,11 +82,11 @@ TestCustodian:testNonPayableFunctions() (gas: 225792)
TestCustodian:testOnlySeaport() (gas: 17895)
TestCustodian:testPayableFunctions() (gas: 41667)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 98786)
TestCustodian:testPreviewOrderRepay() (gas: 227648)
TestCustodian:testPreviewOrderRepay() (gas: 227692)
TestCustodian:testPreviewOrderSettlement() (gas: 178524)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 100823)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 106856)
TestCustodian:testRatifyOrder() (gas: 178410)
TestCustodian:testRatifyOrder() (gas: 178432)
TestCustodian:testSeaportMetadata() (gas: 8567)
TestCustodian:testSetRepayApproval() (gas: 37861)
TestCustodian:testSupportsInterface() (gas: 9450)
Expand All @@ -91,33 +98,33 @@ TestLenderEnforcer:testLERevertInvalidLoanTerms() (gas: 78341)
TestLenderEnforcer:testLEValidLoanTerms() (gas: 69496)
TestLenderEnforcer:testLEValidLoanTermsAnyBorrower() (gas: 69494)
TestLenderEnforcer:testLEValidLoanTermsWithAdditionalTransfers() (gas: 70740)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 537008)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 524637)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 554832)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 544574)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 550050)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 537052)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 524681)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 554876)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 544618)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 550094)
TestNewLoan:testBuyNowPayLater() (gas: 2832923)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2() (gas: 394541)
TestNewLoan:testNewLoanERC721CollateralLessDebtThanOffered() (gas: 2348)
TestNewLoan:testNewLoanRefinanceNew() (gas: 207)
TestNewLoan:testNewLoanViaOriginatorBorrowerApprovalAndLenderApproval() (gas: 305741)
TestNewLoan:testNewLoanViaOriginatorLenderApproval() (gas: 358465)
TestNewLoan:testSettleLoan() (gas: 608050)
TestNewLoan:testSettleLoan() (gas: 608094)
TestPausableNonReentrant:testNotOwner() (gas: 21254)
TestPausableNonReentrant:testPauseAndUnpause() (gas: 22555)
TestPausableNonReentrant:testReentrancy() (gas: 15360)
TestPausableNonReentrant:testUnpauseWhenNotPaused() (gas: 12582)
TestRefStarportLib:testSpentToReceived() (gas: 13315)
TestRefStarportLib:testValidateSalt(address,bytes32) (runs: 256, μ: 33865, ~: 33865)
TestRepayLoan:testRepayLoanApprovedRepayer() (gas: 629456)
TestRepayLoan:testRepayLoanBase() (gas: 565750)
TestRepayLoan:testRepayLoanApprovedRepayer() (gas: 629500)
TestRepayLoan:testRepayLoanBase() (gas: 565794)
TestRepayLoan:testRepayLoanGenerateOrderNotSeaport() (gas: 402462)
TestRepayLoan:testRepayLoanInSettlement() (gas: 550056)
TestRepayLoan:testRepayLoanInvalidRepayer() (gas: 564465)
TestRepayLoan:testRepayLoanThatDoesNotExist() (gas: 837042)
TestSimpleInterestPricing:test_calculateInterest() (gas: 808192)
TestSimpleInterestPricing:test_getPaymentConsideration() (gas: 889476)
TestSimpleInterestPricing:test_getRefinanceConsideration() (gas: 883676)
TestRepayLoan:testRepayLoanInSettlement() (gas: 550078)
TestRepayLoan:testRepayLoanInvalidRepayer() (gas: 564487)
TestRepayLoan:testRepayLoanThatDoesNotExist() (gas: 837152)
TestSimpleInterestPricing:test_calculateInterest() (gas: 800891)
TestSimpleInterestPricing:test_getPaymentConsideration() (gas: 882219)
TestSimpleInterestPricing:test_getRefinanceConsideration() (gas: 876412)
TestStarport:testActive() (gas: 67100)
TestStarport:testAdditionalTransfers() (gas: 295920)
TestStarport:testCannotIssueSameLoanTwice() (gas: 336999)
Expand Down
1 change: 0 additions & 1 deletion src/BNPLHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {AdditionalTransfer} from "starport-core/lib/StarportLib.sol";
import {Seaport} from "seaport/contracts/Seaport.sol";
import {Starport} from "./Starport.sol";
import {CaveatEnforcer} from "./enforcers/CaveatEnforcer.sol";
import "forge-std/console.sol";

interface IVault {
function flashLoan(
Expand Down
1 change: 0 additions & 1 deletion src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {Settlement} from "starport-core/settlement/Settlement.sol";
import {Pricing} from "starport-core/pricing/Pricing.sol";
import {Starport} from "starport-core/Starport.sol";
import {StarportLib, Actions} from "starport-core/lib/StarportLib.sol";
import "forge-std/console2.sol";

contract Custodian is ERC721, ContractOffererInterface {
using {StarportLib.getId} for Starport.Loan;
Expand Down
1 change: 0 additions & 1 deletion src/enforcers/AstariaV1LenderEnforcer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Starport} from "starport-core/Starport.sol";
import {BasePricing} from "starport-core/pricing/BasePricing.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {StarportLib} from "starport-core/lib/StarportLib.sol";
import "forge-std/console2.sol";

contract AstariaV1LenderEnforcer is LenderEnforcer {
using FixedPointMathLib for uint256;
Expand Down
1 change: 0 additions & 1 deletion src/lib/RefStarportLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity ^0.8.17;
import {ItemType, ReceivedItem, SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";

import {Starport} from "starport-core/Starport.sol";
import "forge-std/console.sol";

enum Actions {
Nothing,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/StarportLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ pragma solidity ^0.8.17;

import {ItemType, ReceivedItem, SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {Starport} from "starport-core/Starport.sol";
import "forge-std/console.sol";

import {ERC721} from "solady/src/tokens/ERC721.sol";
import {ERC20} from "solady/src/tokens/ERC20.sol";
import {ERC1155} from "solady/src/tokens/ERC1155.sol";
import {SafeTransferLib} from "solady/src/utils/SafeTransferLib.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import "forge-std/console2.sol";

enum Actions {
Nothing,
Expand Down
27 changes: 13 additions & 14 deletions src/pricing/AstariaV1Pricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract AstariaV1Pricing is CompoundInterestPricing {

error InsufficientRefinance();

function getRefinanceConsideration(Starport.Loan memory loan, bytes memory newPricingData, address fulfiller)
function getRefinanceConsideration(Starport.Loan memory loan, bytes calldata newPricingData, address fulfiller)
external
view
virtual
Expand All @@ -35,19 +35,18 @@ contract AstariaV1Pricing is CompoundInterestPricing {
// borrowers can refinance a loan at any time
if (fulfiller != loan.borrower) {
// check if a recall is occuring
AstariaV1Status hook = AstariaV1Status(loan.terms.status);
Details memory newDetails = abi.decode(newPricingData, (Details));
if (hook.isRecalled(loan)) {
uint256 rate = hook.getRecallRate(loan);
// offered loan did not meet the terms of the recall auction
if (newDetails.rate > rate) {
revert InsufficientRefinance();
}
}
// recall is not occuring
else {
AstariaV1Status status = AstariaV1Status(loan.terms.status);

if (!status.isRecalled(loan)) {
revert InvalidRefinance();
}
Details memory newDetails = abi.decode(newPricingData, (Details));
uint256 rate = status.getRecallRate(loan);
// offered loan did not meet the terms of the recall auction
if (newDetails.rate > rate) {
revert InsufficientRefinance();
}

Details memory oldDetails = abi.decode(loan.terms.pricingData, (Details));

uint256 proportion;
Expand All @@ -57,14 +56,14 @@ contract AstariaV1Pricing is CompoundInterestPricing {
// recaller stake is refunded
if (newDetails.rate > oldDetails.rate) {
proportion = 1e18;
(receiver,) = hook.recalls(loanId);
(receiver,) = status.recalls(loanId);
} else {
// scenario where the recaller is penalized
// essentially the old lender and the new lender split the stake of the recaller
// split is proportional to the difference in rate
proportion = 1e18 - (oldDetails.rate - newDetails.rate).divWad(oldDetails.rate);
}
recallConsideration = hook.generateRecallConsideration(loan, proportion, fulfiller, receiver);
recallConsideration = status.generateRecallConsideration(loan, proportion, fulfiller, receiver);
}

(repayConsideration, carryConsideration) = getPaymentConsideration(loan);
Expand Down
8 changes: 5 additions & 3 deletions src/pricing/BasePricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {Pricing} from "starport-core/pricing/Pricing.sol";
import {ReceivedItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {Status} from "starport-core/status/Status.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import "forge-std/console2.sol";

import {BaseStatus} from "starport-core/status/BaseStatus.sol";
import {StarportLib} from "starport-core/lib/StarportLib.sol";
Expand All @@ -48,8 +47,11 @@ abstract contract BasePricing is Pricing {
returns (SpentItem[] memory repayConsideration, SpentItem[] memory carryConsideration)
{
Details memory details = abi.decode(loan.terms.pricingData, (Details));
if (details.carryRate > 0) carryConsideration = new SpentItem[](loan.debt.length);
else carryConsideration = new SpentItem[](0);
if (details.carryRate > 0) {
carryConsideration = new SpentItem[](loan.debt.length);
} else {
carryConsideration = new SpentItem[](0);
}
repayConsideration = new SpentItem[](loan.debt.length);

uint256 i = 0;
Expand Down
1 change: 0 additions & 1 deletion src/pricing/BaseRecallPricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {ReceivedItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {Status} from "starport-core/status/Status.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import "forge-std/console2.sol";

import {BaseStatus} from "starport-core/status/BaseStatus.sol";
import {StarportLib} from "starport-core/lib/StarportLib.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/pricing/Pricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {AdditionalTransfer} from "starport-core/lib/StarportLib.sol";

abstract contract Pricing {
Starport SP;
Starport public immutable SP;

error InvalidRefinance();

Expand Down
1 change: 0 additions & 1 deletion src/settlement/AstariaV1Settlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";

import {Pricing} from "starport-core/pricing/Pricing.sol";
import {BasePricing} from "starport-core/pricing/BasePricing.sol";
import "forge-std/console2.sol";

contract AstariaV1Settlement is DutchAuctionSettlement {
using {StarportLib.getId} for Starport.Loan;
Expand Down
1 change: 0 additions & 1 deletion src/status/AstariaV1Status.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {BaseRecall} from "starport-core/status/BaseRecall.sol";
import "forge-std/console2.sol";
import {BaseStatus} from "starport-core/status/BaseStatus.sol";
import {StarportLib} from "starport-core/lib/StarportLib.sol";

Expand Down
2 changes: 0 additions & 2 deletions src/status/BaseRecall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
pragma solidity ^0.8.17;

import "forge-std/console2.sol";

import {Starport} from "starport-core/Starport.sol";
import {Status} from "starport-core/status/Status.sol";
import {ERC20} from "solady/src/tokens/ERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/AstariaV1Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";
contract AstariaV1Test is StarportTest {
Account recaller;

function setUp() public override {
function setUp() public virtual override {
super.setUp();

recaller = makeAndAllocateAccount("recaller");
Expand Down
2 changes: 1 addition & 1 deletion test/StarportTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ contract StarportTest is BaseOrderTest {
return newLoan(loan, bytes32(msg.sig), bytes32(msg.sig), borrower.addr);
}

function generateDefaultLoanTerms() public view returns (Starport.Loan memory) {
function generateDefaultLoanTerms() public view virtual returns (Starport.Loan memory) {
SpentItem[] memory newCollateral = new SpentItem[](1);
newCollateral[0] = SpentItem({itemType: ItemType.ERC721, token: address(erc721s[0]), identifier: 1, amount: 1});
SpentItem[] memory newDebt = new SpentItem[](1);
Expand Down
Loading

0 comments on commit bb80142

Please sign in to comment.