Skip to content

Commit

Permalink
Feat/ast 1952 v1 handler testing (#41)
Browse files Browse the repository at this point in the history
* update getAuctionStart to be a public helper, make an internal version that is more optimized(less calls/decoding), add tests

* coverage testing for AstariaV1Settlement handler complete

* coverage testing for AstariaV1Settlement handler complete

* fix test name
  • Loading branch information
androolloyd authored Nov 2, 2023
1 parent 409be25 commit 2364ed7
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 24 deletions.
28 changes: 19 additions & 9 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
DiffFuzzTestStarPortLib:testSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 880483, ~: 882762)
DiffFuzzTestStarPortLib:testUnboundSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 232905, ~: 237832)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallBase() (gas: 1028726)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 693092)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 763444)
DiffFuzzTestStarPortLib:testSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 880529, ~: 882914)
DiffFuzzTestStarPortLib:testUnboundSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 232899, ~: 237832)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallBase() (gas: 1011545)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 693342)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 746648)
TestBorrowerEnforcer:testBERevertAdditionalTransfers() (gas: 73101)
TestBorrowerEnforcer:testBERevertInvalidLoanTerms() (gas: 78338)
TestBorrowerEnforcer:testBEValidLoanTerms() (gas: 69429)
Expand All @@ -16,13 +16,13 @@ TestCustodian:testGenerateOrderInvalidHandlerExecution() (gas: 132842)
TestCustodian:testGenerateOrderRepay() (gas: 173845)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 199470)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 864690)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 790047)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 789937)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 529819)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 90336)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 84653)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 96427)
TestCustodian:testGenerateOrderSettlement() (gas: 151465)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 160505)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 160461)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 155682)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 94206)
TestCustodian:testGetBorrower() (gas: 76322)
Expand Down Expand Up @@ -99,12 +99,19 @@ TestRefStarPortLib:testValidateSalt(address,bytes32) (runs: 256, μ: 33865, ~: 3
TestRepayLoan:testRepayLoanApprovedRepayer() (gas: 643615)
TestRepayLoan:testRepayLoanBase() (gas: 580019)
TestRepayLoan:testRepayLoanGenerateOrderNotSeaport() (gas: 399854)
TestRepayLoan:testRepayLoanInSettlement() (gas: 544869)
TestRepayLoan:testRepayLoanInSettlement() (gas: 540930)
TestRepayLoan:testRepayLoanInvalidRepayer() (gas: 560285)
TestRepayLoan:testRepayLoanThatDoesNotExist() (gas: 848820)
TestStarPortLib:testSpentToReceived() (gas: 13315)
TestStarPortLib:testValidateSalt(address,bytes32) (runs: 256, μ: 33865, ~: 33865)
TestStrategistOriginator:testEncodeWithAccountCounter() (gas: 12307)
TestStrategistOriginator:testGetAuctionStart() (gas: 424899)
TestStrategistOriginator:testGetAuctionStartNotStarted() (gas: 424491)
TestStrategistOriginator:testGetCurrentAuctionPrice() (gas: 439776)
TestStrategistOriginator:testGetCurrentAuctionPriceNoAuction() (gas: 428100)
TestStrategistOriginator:testGetSettlementDutchAuctionSettlementAbove() (gas: 482308)
TestStrategistOriginator:testGetSettlementFailedDutchAuction() (gas: 440099)
TestStrategistOriginator:testGetSettlementLoanNotRecalled() (gas: 430598)
TestStrategistOriginator:testGetStrategistData() (gas: 1473217)
TestStrategistOriginator:testIncrementCounterAsStrategist() (gas: 18654)
TestStrategistOriginator:testIncrementCounterNotAuthorized() (gas: 13445)
Expand All @@ -117,4 +124,7 @@ TestStrategistOriginator:testInvalidDebtAmountRequestingZero() (gas: 206823)
TestStrategistOriginator:testInvalidDebtLength() (gas: 205450)
TestStrategistOriginator:testInvalidOffer() (gas: 396393)
TestStrategistOriginator:testInvalidSigner() (gas: 208683)
TestStrategistOriginator:testSetStrategist() (gas: 17796)
TestStrategistOriginator:testSetStrategist() (gas: 17796)
TestStrategistOriginator:testV1SettlementHandlerExecute() (gas: 411108)
TestStrategistOriginator:testV1SettlementHandlerValidate() (gas: 411220)
TestStrategistOriginator:testV1SettlementHandlerValidateInvalidHandler() (gas: 411344)
52 changes: 46 additions & 6 deletions src/handlers/AstariaV1SettlementHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {BaseRecall} from "starport-core/hooks/BaseRecall.sol";
import {DutchAuctionHandler} from "starport-core/handlers/DutchAuctionHandler.sol";
import {StarPortLib} from "starport-core/lib/StarPortLib.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import "forge-std/console2.sol";

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

Expand All @@ -16,26 +16,62 @@ contract AstariaV1SettlementHandler is DutchAuctionHandler {

constructor(LoanManager LM_) DutchAuctionHandler(LM_) {}

function _getAuctionStart(LoanManager.Loan memory loan) internal view virtual override returns (uint256) {
error NoAuction();
error LoanNotRecalled();
error ExecuteHandlerNotImplemented();
error InvalidHandler();

function getCurrentAuctionPrice(LoanManager.Loan calldata loan) public view virtual returns (uint256) {
(address recaller, uint64 recallStart) = BaseRecall(loan.terms.hook).recalls(loan.getId());
if (recaller == loan.issuer || recallStart == uint256(0) || recaller == address(0)) {
revert NoAuction();
}

uint256 start = _getAuctionStart(loan, recallStart);

Details memory details = abi.decode(loan.terms.handlerData, (Details));

return _locateCurrentAmount({
startAmount: details.startingPrice,
endAmount: details.endingPrice,
startTime: start,
endTime: start + details.window,
roundUp: true
});
}

function getAuctionStart(LoanManager.Loan calldata loan) public view virtual override returns (uint256) {
(, uint64 start) = BaseRecall(loan.terms.hook).recalls(loan.getId());
if (start == 0) {
revert LoanNotRecalled();
}
BaseRecall.Details memory details = abi.decode(loan.terms.hookData, (BaseRecall.Details));
return start + details.recallWindow + 1;
}

function _getAuctionStart(LoanManager.Loan calldata loan, uint64 start) internal view virtual returns (uint256) {
BaseRecall.Details memory details = abi.decode(loan.terms.hookData, (BaseRecall.Details));

return start + details.recallWindow + 1;
}

function getSettlement(LoanManager.Loan calldata loan)
public
view
virtual
override
returns (ReceivedItem[] memory consideration, address restricted)
{
(address recaller,) = BaseRecall(loan.terms.hook).recalls(loan.getId());
(address recaller, uint64 recallStart) = BaseRecall(loan.terms.hook).recalls(loan.getId());

if (recaller == address(0) || recallStart == uint256(0)) {
revert LoanNotRecalled();
}
if (recaller == loan.issuer) {
return (new ReceivedItem[](0), recaller);
}

uint256 start = _getAuctionStart(loan);
uint256 start = _getAuctionStart(loan, recallStart);
Details memory details = abi.decode(loan.terms.handlerData, (Details));

// DutchAuction has failed, give the NFT back to the lender (if they want it 😐)
Expand Down Expand Up @@ -121,10 +157,14 @@ contract AstariaV1SettlementHandler is DutchAuctionHandler {
}

function execute(LoanManager.Loan calldata loan, address fulfiller) external virtual override returns (bytes4) {
return SettlementHandler.execute.selector;
revert ExecuteHandlerNotImplemented();
}

function validate(LoanManager.Loan calldata loan) external view virtual override returns (bool) {
return true;
if (loan.terms.handler != address(this)) {
revert InvalidHandler();
}
Details memory details = abi.decode(loan.terms.handlerData, (Details)); //will revert if this fails
return (details.startingPrice > details.endingPrice);
}
}
8 changes: 3 additions & 5 deletions src/handlers/DutchAuctionHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import {AmountDeriver} from "seaport-core/src/lib/AmountDeriver.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {LoanManager, SettlementHandler} from "starport-core/handlers/SettlementHandler.sol";

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

abstract contract DutchAuctionHandler is SettlementHandler, AmountDeriver, ConduitHelper {
abstract contract DutchAuctionHandler is SettlementHandler, AmountDeriver {
constructor(LoanManager LM_) SettlementHandler(LM_) {
LM = LM_;
}
Expand All @@ -36,7 +34,7 @@ abstract contract DutchAuctionHandler is SettlementHandler, AmountDeriver, Condu
return SettlementHandler.execute.selector;
}

function _getAuctionStart(LoanManager.Loan memory loan) internal view virtual returns (uint256);
function getAuctionStart(LoanManager.Loan calldata loan) public view virtual returns (uint256);

function getSettlement(LoanManager.Loan calldata loan)
public
Expand All @@ -47,7 +45,7 @@ abstract contract DutchAuctionHandler is SettlementHandler, AmountDeriver, Condu
{
Details memory details = abi.decode(loan.terms.handlerData, (Details));

uint256 start = _getAuctionStart(loan);
uint256 start = getAuctionStart(loan);

// DutchAuction has failed, allow lender to redeem
if (start + details.window < block.timestamp) {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/FixedTermDutchAuctionHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract FixedTermDutchAuctionHandler is DutchAuctionHandler {

constructor(LoanManager LM_) DutchAuctionHandler(LM_) {}

function _getAuctionStart(LoanManager.Loan memory loan) internal view virtual override returns (uint256) {
function getAuctionStart(LoanManager.Loan calldata loan) public view virtual override returns (uint256) {
FixedTermHook.Details memory details = abi.decode(loan.terms.hookData, (FixedTermHook.Details));
return loan.start + details.loanDuration;
}
Expand Down
4 changes: 1 addition & 3 deletions test/integration-testing/TestAstariaV1Loan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ contract TestAstariaV1Loan is AstariaV1Test {
console.logBytes32(
LM.hashCaveatWithSaltAndNonce(refinancer.addr, bytes32(uint256(1)), refinancerCaveat.caveat)
);
emit log_caveatapproval(refinancerCaveat);

vm.startPrank(refinancer.addr);
erc20s[0].approve(address(LM), refinanceDetails.loan.debt[0].amount);
vm.stopPrank();
Expand Down Expand Up @@ -221,8 +221,6 @@ contract TestAstariaV1Loan is AstariaV1Test {
}
}

event log_caveatapproval(CaveatEnforcer.CaveatWithApproval caveatApproval);

// lender is recaller, liquidation amount is 0
function testNewLoanERC721CollateralDefaultTermsRecallLender() public {
LoanManager.Terms memory terms = LoanManager.Terms({
Expand Down
Loading

0 comments on commit 2364ed7

Please sign in to comment.