Skip to content

Commit

Permalink
Feat/repay test (#40)
Browse files Browse the repository at this point in the history
* feat: implement repayment tests

* set the consideration on the previewOrder, forge fmt, grammar

* feat: implement repay tests

* fix: format

* format & snapshot

---------

Co-authored-by: Andrew Redden <[email protected]>
Co-authored-by: androolloyd <[email protected]>
  • Loading branch information
3 people authored Nov 1, 2023
1 parent a2d9d81 commit 409be25
Show file tree
Hide file tree
Showing 9 changed files with 535 additions and 267 deletions.
202 changes: 104 additions & 98 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,114 +1,120 @@
DiffFuzzTestStarPortLib:testSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 880501, ~: 882340)
DiffFuzzTestStarPortLib:testUnboundSpentToReceived((uint8,address,uint256,uint256)[]) (runs: 256, μ: 232899, ~: 237832)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallBase() (gas: 1041013)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 693016)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 772585)
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)
TestBorrowerEnforcer:testBERevertAdditionalTransfers() (gas: 73101)
TestBorrowerEnforcer:testBERevertInvalidLoanTerms() (gas: 78338)
TestBorrowerEnforcer:testBEValidLoanTerms() (gas: 69429)
TestBorrowerEnforcer:testBEValidLoanTermsAnyIssuer() (gas: 69581)
TestCustodian:testCannotLazyMintTwice() (gas: 76686)
TestCustodian:testCannotMintInvalidLoanInvalidCustodian() (gas: 66883)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72511)
TestCustodian:testCustodySelector() (gas: 2543980)
TestCustodian:testDefaultCustodySelectorRevert() (gas: 70105)
TestCustodian:testGenerateOrderInvalidHandlerExecution() (gas: 132855)
TestCustodian:testGenerateOrderRepay() (gas: 164353)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 190000)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 847956)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 789588)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 520259)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 90249)
TestCustodian:testCannotLazyMintTwice() (gas: 76698)
TestCustodian:testCannotMintInvalidLoanInvalidCustodian() (gas: 66861)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72501)
TestCustodian:testCustodySelector() (gas: 2625664)
TestCustodian:testDefaultCustodySelectorRevert() (gas: 70083)
TestCustodian:testGenerateOrderInvalidHandlerExecution() (gas: 132842)
TestCustodian:testGenerateOrderRepay() (gas: 173845)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 199470)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 864690)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 790047)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 529819)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 90336)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 84653)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 96449)
TestCustodian:testGenerateOrderSettlement() (gas: 151359)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 160399)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 155598)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 94219)
TestCustodian:testGetBorrower() (gas: 76234)
TestCustodian:testInvalidAction() (gas: 114489)
TestCustodian:testInvalidActionRepayInActiveLoan() (gas: 117416)
TestCustodian:testInvalidActionSettleActiveLoan() (gas: 117376)
TestCustodian:testName() (gas: 7120)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 96427)
TestCustodian:testGenerateOrderSettlement() (gas: 151465)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 160505)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 155682)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 94206)
TestCustodian:testGetBorrower() (gas: 76322)
TestCustodian:testInvalidAction() (gas: 114479)
TestCustodian:testInvalidActionRepayInActiveLoan() (gas: 117428)
TestCustodian:testInvalidActionSettleActiveLoan() (gas: 117388)
TestCustodian:testName() (gas: 7098)
TestCustodian:testNonPayableFunctions() (gas: 225770)
TestCustodian:testOnlySeaport() (gas: 17917)
TestCustodian:testPayableFunctions() (gas: 41689)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 98776)
TestCustodian:testPreviewOrderRepay() (gas: 208195)
TestCustodian:testPreviewOrderSettlement() (gas: 182743)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 100577)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 106735)
TestCustodian:testRatifyOrder() (gas: 170196)
TestCustodian:testSeaportMetadata() (gas: 8567)
TestCustodian:testSetRepayApproval() (gas: 37883)
TestCustodian:testOnlySeaport() (gas: 17895)
TestCustodian:testPayableFunctions() (gas: 41667)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 98766)
TestCustodian:testPreviewOrderRepay() (gas: 225774)
TestCustodian:testPreviewOrderSettlement() (gas: 182835)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 100684)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 106814)
TestCustodian:testRatifyOrder() (gas: 180014)
TestCustodian:testSeaportMetadata() (gas: 8545)
TestCustodian:testSetRepayApproval() (gas: 37861)
TestCustodian:testSupportsInterface() (gas: 9428)
TestCustodian:testSymbol() (gas: 7149)
TestCustodian:testTokenURI() (gas: 64839)
TestCustodian:testTokenURIInvalidLoan() (gas: 13218)
TestLenderEnforcer:testLERevertAdditionalTransfersFromLender() (gas: 73760)
TestLenderEnforcer:testLERevertInvalidLoanTerms() (gas: 78385)
TestLenderEnforcer:testLEValidLoanTerms() (gas: 69429)
TestLenderEnforcer:testLEValidLoanTermsAnyBorrower() (gas: 69516)
TestLenderEnforcer:testLEValidLoanTermsWithAdditionalTransfers() (gas: 70757)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 532861)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 501718)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 551584)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 541442)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 546102)
TestLoanManager:testAdditionalTransfers() (gas: 293082)
TestLoanManager:testCannotIssueSameLoanTwice() (gas: 331285)
TestLoanManager:testCannotOriginateWhilePaused() (gas: 87923)
TestLoanManager:testCannotSettleInvalidLoan() (gas: 72594)
TestLoanManager:testCannotSettleUnlessValidCustodian() (gas: 68750)
TestLoanManager:testCaveatEnforcerRevert() (gas: 119155)
TestLoanManager:testDefaultFeeRake() (gas: 352463)
TestLoanManager:testExoticDebtWithNoCaveatsNotAsBorrower() (gas: 342734)
TestLoanManager:testInitializedFlagSetProperly() (gas: 65262)
TestLoanManager:testInvalidAmountCollateral() (gas: 152833)
TestLoanManager:testInvalidAmountCollateral721() (gas: 152976)
TestLoanManager:testInvalidAmountDebt() (gas: 177144)
TestLoanManager:testInvalidIdentifierDebt() (gas: 197138)
TestLoanManager:testInvalidItemType() (gas: 138881)
TestLoanManager:testInvalidTransferLengthCollateral() (gas: 161050)
TestLoanManager:testInvalidTransferLengthDebt() (gas: 165593)
TestLoanManager:testIssued() (gas: 67144)
TestLoanManager:testName() (gas: 7184)
TestLoanManager:testNonDefaultCustodianCustodyCallFails() (gas: 190220)
TestLoanManager:testNonDefaultCustodianCustodyCallSuccess() (gas: 258469)
TestLoanManager:testNonPayableFunctions() (gas: 175555)
TestLoanManager:testOverrideFeeRake() (gas: 346202)
TestLoanManager:testPause() (gas: 34222)
TestCustodian:testSymbol() (gas: 7127)
TestCustodian:testTokenURI() (gas: 64817)
TestCustodian:testTokenURIInvalidLoan() (gas: 13196)
TestLenderEnforcer:testLERevertAdditionalTransfersFromLender() (gas: 73738)
TestLenderEnforcer:testLERevertInvalidLoanTerms() (gas: 78363)
TestLenderEnforcer:testLEValidLoanTerms() (gas: 69496)
TestLenderEnforcer:testLEValidLoanTermsAnyBorrower() (gas: 69494)
TestLenderEnforcer:testLEValidLoanTermsWithAdditionalTransfers() (gas: 70735)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 552683)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 540288)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 570519)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 560249)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 565723)
TestLoanManager:testActive() (gas: 67135)
TestLoanManager:testAdditionalTransfers() (gas: 293184)
TestLoanManager:testCannotIssueSameLoanTwice() (gas: 331467)
TestLoanManager:testCannotOriginateWhilePaused() (gas: 87814)
TestLoanManager:testCannotSettleInvalidLoan() (gas: 72691)
TestLoanManager:testCannotSettleUnlessValidCustodian() (gas: 68772)
TestLoanManager:testCaveatEnforcerRevert() (gas: 119189)
TestLoanManager:testDefaultFeeRake() (gas: 352643)
TestLoanManager:testExoticDebtWithNoCaveatsNotAsBorrower() (gas: 342836)
TestLoanManager:testInitializedFlagSetProperly() (gas: 65234)
TestLoanManager:testInvalidAmountCollateral() (gas: 152867)
TestLoanManager:testInvalidAmountCollateral721() (gas: 153010)
TestLoanManager:testInvalidAmountDebt() (gas: 177200)
TestLoanManager:testInvalidIdentifierDebt() (gas: 197194)
TestLoanManager:testInvalidItemType() (gas: 138915)
TestLoanManager:testInvalidTransferLengthCollateral() (gas: 161106)
TestLoanManager:testInvalidTransferLengthDebt() (gas: 165627)
TestLoanManager:testIssued() (gas: 67084)
TestLoanManager:testName() (gas: 7251)
TestLoanManager:testNonDefaultCustodianCustodyCallFails() (gas: 190254)
TestLoanManager:testNonDefaultCustodianCustodyCallSuccess() (gas: 258571)
TestLoanManager:testNonPayableFunctions() (gas: 175599)
TestLoanManager:testOverrideFeeRake() (gas: 346404)
TestLoanManager:testPause() (gas: 34091)
TestLoanManager:testSupportsInterface() (gas: 9181)
TestLoanManager:testSymbol() (gas: 7235)
TestLoanManager:testTokenNoCodeCollateral() (gas: 137653)
TestLoanManager:testTokenNoCodeDebt() (gas: 170689)
TestLoanManager:testTokenURI() (gas: 64892)
TestLoanManager:testTokenURIInvalidLoan() (gas: 13244)
TestLoanManager:testTransferFromFail() (gas: 80176)
TestLoanManager:testTokenNoCodeCollateral() (gas: 137687)
TestLoanManager:testTokenNoCodeDebt() (gas: 170831)
TestLoanManager:testTokenURI() (gas: 64945)
TestLoanManager:testTokenURIInvalidLoan() (gas: 13384)
TestLoanManager:testTransferFromFail() (gas: 80088)
TestLoanManager:testUnPause() (gas: 14291)
TestNewLoan:testBuyNowPayLater() (gas: 2831719)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2() (gas: 391890)
TestNewLoan:testNewLoanERC721CollateralLessDebtThanOffered() (gas: 2259)
TestNewLoan:testNewLoanRefinanceNew() (gas: 207)
TestNewLoan:testNewLoanViaOriginatorBorrowerApprovalAndLenderApproval() (gas: 299336)
TestNewLoan:testNewLoanViaOriginatorLenderApproval() (gas: 353952)
TestNewLoan:testSettleLoan() (gas: 163)
TestNewLoan:testBuyNowPayLater() (gas: 2831787)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2() (gas: 391958)
TestNewLoan:testNewLoanERC721CollateralLessDebtThanOffered() (gas: 2348)
TestNewLoan:testNewLoanRefinanceNew() (gas: 185)
TestNewLoan:testNewLoanViaOriginatorBorrowerApprovalAndLenderApproval() (gas: 299404)
TestNewLoan:testNewLoanViaOriginatorLenderApproval() (gas: 354020)
TestNewLoan:testSettleLoan() (gas: 252)
TestRefStarPortLib:testSpentToReceived() (gas: 13315)
TestRefStarPortLib:testValidateSalt(address,bytes32) (runs: 256, μ: 33865, ~: 33865)
TestRepayLoan:testRepayLoan() (gas: 207)
TestRepayLoan:testRepayLoanApprovedRepayer() (gas: 643615)
TestRepayLoan:testRepayLoanBase() (gas: 580019)
TestRepayLoan:testRepayLoanGenerateOrderNotSeaport() (gas: 399854)
TestRepayLoan:testRepayLoanInSettlement() (gas: 544869)
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:testGetStrategistData() (gas: 1473217)
TestStrategistOriginator:testIncrementCounterAsStrategist() (gas: 18676)
TestStrategistOriginator:testIncrementCounterNotAuthorized() (gas: 13467)
TestStrategistOriginator:testInvalidCollateral() (gas: 204416)
TestStrategistOriginator:testInvalidDeadline() (gas: 210145)
TestStrategistOriginator:testInvalidDebt() (gas: 206100)
TestStrategistOriginator:testInvalidDebtAmountAskingMoreThanOffered() (gas: 206548)
TestStrategistOriginator:testInvalidDebtAmountOfferingZero() (gas: 186903)
TestStrategistOriginator:testInvalidDebtAmountRequestingZero() (gas: 206801)
TestStrategistOriginator:testInvalidDebtLength() (gas: 205428)
TestStrategistOriginator:testInvalidOffer() (gas: 396325)
TestStrategistOriginator:testInvalidSigner() (gas: 208639)
TestStrategistOriginator:testSetStrategist() (gas: 17818)
TestStrategistOriginator:testIncrementCounterAsStrategist() (gas: 18654)
TestStrategistOriginator:testIncrementCounterNotAuthorized() (gas: 13445)
TestStrategistOriginator:testInvalidCollateral() (gas: 204438)
TestStrategistOriginator:testInvalidDeadline() (gas: 210167)
TestStrategistOriginator:testInvalidDebt() (gas: 206122)
TestStrategistOriginator:testInvalidDebtAmountAskingMoreThanOffered() (gas: 206570)
TestStrategistOriginator:testInvalidDebtAmountOfferingZero() (gas: 186947)
TestStrategistOriginator:testInvalidDebtAmountRequestingZero() (gas: 206823)
TestStrategistOriginator:testInvalidDebtLength() (gas: 205450)
TestStrategistOriginator:testInvalidOffer() (gas: 396393)
TestStrategistOriginator:testInvalidSigner() (gas: 208683)
TestStrategistOriginator:testSetStrategist() (gas: 17796)
16 changes: 7 additions & 9 deletions src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {SettlementHandler} from "starport-core/handlers/SettlementHandler.sol";
import {Pricing} from "starport-core/pricing/Pricing.sol";
import {LoanManager} from "starport-core/LoanManager.sol";
import {StarPortLib, Actions} from "starport-core/lib/StarPortLib.sol";
import "forge-std/console2.sol";

contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
using {StarPortLib.getId} for LoanManager.Loan;
Expand Down Expand Up @@ -148,7 +149,7 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
function mint(LoanManager.Loan calldata loan) external {
bytes memory encodedLoan = abi.encode(loan);
uint256 loanId = uint256(keccak256(encodedLoan));
if (loan.custodian != address(this) || !LM.issued(loanId)) {
if (loan.custodian != address(this) || !LM.active(loanId)) {
revert InvalidLoan();
}

Expand Down Expand Up @@ -213,11 +214,10 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
_beforeApprovalsSetHook(fulfiller, maximumSpent, context);
_setOfferApprovalsWithSeaport(offer);

(SpentItem[] memory paymentConsiderations, SpentItem[] memory carryFeeConsideration) =
(SpentItem[] memory payment, SpentItem[] memory carry) =
Pricing(loan.terms.pricing).getPaymentConsideration(loan);

// consideration = _mergeConsiderations(paymentConsiderations, carryFeeConsideration, new ReceivedItem[](0));
// consideration = _removeZeroAmounts(consideration);
consideration = StarPortLib.mergeSpentItemsToReceivedItems(payment, loan.issuer, carry, loan.originator);

_settleLoan(loan);
} else if (action == Actions.Settlement && !SettlementHook(loan.terms.hook).isActive(loan)) {
Expand Down Expand Up @@ -298,7 +298,7 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
) public view returns (SpentItem[] memory offer, ReceivedItem[] memory consideration) {
(Actions action, LoanManager.Loan memory loan) = abi.decode(context, (Actions, LoanManager.Loan));

if (!LM.issued(loan.getId())) {
if (!LM.active(loan.getId())) {
revert InvalidLoan();
}
bool loanActive = SettlementHook(loan.terms.hook).isActive(loan);
Expand All @@ -309,11 +309,9 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
}
offer = loan.collateral;

(SpentItem[] memory paymentConsiderations, SpentItem[] memory carryFeeConsideration) =
(SpentItem[] memory payment, SpentItem[] memory carry) =
Pricing(loan.terms.pricing).getPaymentConsideration(loan);

// consideration = _mergeConsiderations(paymentConsiderations, carryFeeConsideration, new ReceivedItem[](0));
// consideration = _removeZeroAmounts(consideration);
consideration = StarPortLib.mergeSpentItemsToReceivedItems(payment, loan.issuer, carry, loan.originator);
} else if (action == Actions.Settlement && !loanActive) {
address authorized;
(consideration, authorized) = SettlementHandler(loan.terms.handler).getSettlement(loan);
Expand Down
36 changes: 11 additions & 25 deletions src/LoanManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ contract LoanManager is Ownable, ERC721 {
LENDER
}
enum FieldFlags {
INITIALIZED,
UNINITIALIZED,
ACTIVE,
INACTIVE
}
Expand Down Expand Up @@ -534,40 +534,18 @@ contract LoanManager is Ownable, ERC721 {
return _getExtraData(loanId) == uint8(FieldFlags.INACTIVE);
}

/**
* @dev helper to check if a loan is initialized(ie. has never been opened)
* @param loanId The id of the loan
* @return True if the loan is initialized
*/
function initialized(uint256 loanId) public view returns (bool) {
return _getExtraData(loanId) == uint8(FieldFlags.INITIALIZED);
}

/**
* @dev erc721 tokenURI override
* @param loanId The id of the loan
* @return the string uri of the loan
*/
function tokenURI(uint256 loanId) public view override returns (string memory) {
if (!_issued(loanId)) {
if (!active(loanId)) {
revert InvalidLoan();
}
return string("");
}

function _issued(uint256 loanId) internal view returns (bool) {
return (_getExtraData(loanId) > uint8(0));
}

/**
* @dev helper to check if a loan was issued ever(getExtraData > 0)
* @param loanId The id of the loan
* @return True if the loan is initialized
*/
function issued(uint256 loanId) external view returns (bool) {
return _issued(loanId);
}

/**
* @dev helper to check if a loan is initialized(ie. has never been opened)
* guarded to ensure only the loan.custodian can call it
Expand All @@ -582,7 +560,7 @@ contract LoanManager is Ownable, ERC721 {

function _settle(Loan memory loan) internal {
uint256 tokenId = loan.getId();
if (!_issued(tokenId)) {
if (!active(tokenId)) {
revert InvalidLoan();
}
if (_exists(tokenId)) {
Expand Down Expand Up @@ -663,6 +641,14 @@ contract LoanManager is Ownable, ERC721 {
}
}

function _issued(uint256 loanId) internal view returns (bool) {
return (_getExtraData(loanId) > uint8(0));
}

function getExtraData(uint256 loanId) public view returns (uint8 extraData) {
return uint8(_getExtraData(loanId));
}

/**
* @dev issues a LM token if needed
* only owner can call
Expand Down
Loading

0 comments on commit 409be25

Please sign in to comment.