Skip to content

Commit

Permalink
Merge branch 'main' into feat/pricing-unit-tests
Browse files Browse the repository at this point in the history
* main:
  Implement all new origination flow (#36)
  • Loading branch information
justingreenberg committed Oct 31, 2023
2 parents 6d04f32 + cc94101 commit 249b971
Show file tree
Hide file tree
Showing 28 changed files with 2,438 additions and 3,186 deletions.
174 changes: 76 additions & 98 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,103 +1,81 @@
EnforcerTest:testCollateralEnforcer() (gas: 954073)
EnforcerTest:testFailCollateralEnforcerDifferentCollateral() (gas: 834092)
EnforcerTest:testFailRateEnforcerMaxCarryRate() (gas: 789228)
EnforcerTest:testFailRateEnforcerMaxRate() (gas: 789200)
EnforcerTest:testFailRateEnforcerMaxRateAndMaxCarryRate() (gas: 789065)
EnforcerTest:testRateEnforcerBasic() (gas: 911246)
EnforcerTest:testTermEnforcerBasic() (gas: 983574)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallBase() (gas: 1218188)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 858353)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 894602)
TestCustodian:testCannotLazyMintTwice() (gas: 76663)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallBase() (gas: 1039520)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 691168)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 770737)
TestCustodian:testCannotLazyMintTwice() (gas: 76597)
TestCustodian:testCannotMintInvalidLoanInvalidCustodian() (gas: 66883)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72400)
TestCustodian:testCustodySelector() (gas: 2818119)
TestCustodian:testDefaultCustodySelectorRevert() (gas: 11672)
TestCustodian:testGenerateOrderInvalidHandlerExecution() (gas: 139368)
TestCustodian:testGenerateOrderRepay() (gas: 176877)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 202524)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 1115040)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 1028737)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 524739)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 96790)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 91194)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 102990)
TestCustodian:testGenerateOrderSettlement() (gas: 158129)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 167257)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 162422)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 100769)
TestCustodian:testGetBorrower() (gas: 76157)
TestCustodian:testInvalidAction() (gas: 121504)
TestCustodian:testInvalidActionRepayInActiveLoan() (gas: 124409)
TestCustodian:testInvalidActionSettleActiveLoan() (gas: 124413)
TestCustodian:testName() (gas: 7098)
TestCustodian:testNonPayableFunctions() (gas: 219009)
TestCustodian:testOnlySeaport() (gas: 17975)
TestCustodian:testPayableFunctions() (gas: 43468)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 105228)
TestCustodian:testPreviewOrderRepay() (gas: 227912)
TestCustodian:testPreviewOrderSettlement() (gas: 190065)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 107146)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 113276)
TestCustodian:testRatifyOrder() (gas: 183062)
TestCustodian:testSafeTransfer1155Receive() (gas: 115587)
TestCustodian:testSeaportMetadata() (gas: 8589)
TestCustodian:testSetRepayApproval() (gas: 37839)
TestCustodian:testSupportsInterface() (gas: 9406)
TestCustodian:testSymbol() (gas: 7171)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72422)
TestCustodian:testCustodySelector() (gas: 2543980)
TestCustodian:testDefaultCustodySelectorRevert() (gas: 70105)
TestCustodian:testGenerateOrderInvalidHandlerExecution() (gas: 132855)
TestCustodian:testGenerateOrderRepay() (gas: 164375)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 190022)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 843726)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 785358)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 518154)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 90249)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 84653)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 96449)
TestCustodian:testGenerateOrderSettlement() (gas: 151381)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 160421)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 155620)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 94219)
TestCustodian:testGetBorrower() (gas: 76234)
TestCustodian:testInvalidAction() (gas: 114400)
TestCustodian:testInvalidActionRepayInActiveLoan() (gas: 117327)
TestCustodian:testInvalidActionSettleActiveLoan() (gas: 117287)
TestCustodian:testName() (gas: 7120)
TestCustodian:testNonPayableFunctions() (gas: 225770)
TestCustodian:testOnlySeaport() (gas: 17917)
TestCustodian:testPayableFunctions() (gas: 41689)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 98687)
TestCustodian:testPreviewOrderRepay() (gas: 208128)
TestCustodian:testPreviewOrderSettlement() (gas: 182676)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 100488)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 106646)
TestCustodian:testRatifyOrder() (gas: 170218)
TestCustodian:testSeaportMetadata() (gas: 8567)
TestCustodian:testSetRepayApproval() (gas: 37883)
TestCustodian:testSupportsInterface() (gas: 9428)
TestCustodian:testSymbol() (gas: 7149)
TestCustodian:testTokenURI() (gas: 64839)
TestCustodian:testTokenURIInvalidLoan() (gas: 13196)
TestExoticLoans:testSwap() (gas: 1353169)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 164)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 517422)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 711981)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 541506)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 701482)
TestLoanManager:testCannotIssueSameLoanTwice() (gas: 1529536)
TestLoanManager:testCannotSettleInvalidLoan() (gas: 72844)
TestLoanManager:testCannotSettleUnlessValidCustodian() (gas: 68990)
TestLoanManager:testCaveatEnforcerInvalidOrigination() (gas: 1797025)
TestLoanManager:testDefaultFeeRake() (gas: 403187)
TestLoanManager:testExoticDebtWithNoCaveatsAsBorrower() (gas: 1587178)
TestLoanManager:testExoticDebtWithNoCaveatsNotAsBorrower() (gas: 1677088)
TestLoanManager:testGenerateOrder() (gas: 1495062)
TestLoanManager:testGenerateOrderInvalidAction() (gas: 1354698)
TestLoanManager:testGenerateOrderNotSeaport() (gas: 13023)
TestLoanManager:testInitializedFlagSetProperly() (gas: 65194)
TestLoanManager:testInvalidDebtLength() (gas: 39181)
TestLoanManager:testInvalidDebtType() (gas: 1364311)
TestLoanManager:testInvalidMaximumSpentEmpty() (gas: 50192)
TestLoanManager:testIssued() (gas: 67041)
TestLoanManager:testName() (gas: 7209)
TestLoanManager:testNativeDebtWithNoCaveatsAsBorrower() (gas: 1500521)
TestLoanManager:testNativeDebtWithNoCaveatsNotAsBorrower() (gas: 1541426)
TestLoanManager:testNativeDebtWithNoCaveatsNotAsBorrowerFeesOn() (gas: 1604818)
TestLoanManager:testNonDefaultCustodianCustodyCallFails() (gas: 23953)
TestLoanManager:testNonDefaultCustodianCustodyCallSuccess() (gas: 25327)
TestLoanManager:testNonPayableFunctions() (gas: 109562)
TestLoanManager:testOverrideFeeRake() (gas: 404503)
TestLoanManager:testPayableFunctions() (gas: 55281)
TestLoanManager:testPreviewOrderInvalidAction() (gas: 1299017)
TestLoanManager:testPreviewOrderOriginationWithNoCaveatsSetAsBorrowerFeeOn() (gas: 1345487)
TestLoanManager:testPreviewOrderOriginationWithNoCaveatsSetAsBorrowerNoFee() (gas: 1313690)
TestLoanManager:testPreviewOrderOriginationWithNoCaveatsSetNotBorrowerFeeOn() (gas: 1354173)
TestLoanManager:testPreviewOrderOriginationWithNoCaveatsSetNotBorrowerNoFee() (gas: 1322064)
TestLoanManager:testPreviewOrderRefinanceAsRefinancerFeeOff() (gas: 1373981)
TestLoanManager:testPreviewOrderRefinanceAsRefinancerFeeOn() (gas: 1399308)
TestLoanManager:testRefinanceNoRefinanceConsideration() (gas: 1386463)
TestLoanManager:testSafeTransfer1155Receive() (gas: 54338)
TestLoanManager:testSeaportMetadata() (gas: 8667)
TestLoanManager:testSupportsInterface() (gas: 9580)
TestLoanManager:testSymbol() (gas: 7238)
TestLoanManager:testTokenURI() (gas: 64967)
TestLoanManager:testTokenURIInvalidLoan() (gas: 13290)
TestLoanManager:testTransferFromFailFromSeaport() (gas: 82187)
TestNewLoan:testBuyNowPayLater() (gas: 1143307)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 982374)
TestNewLoan:testNewLoanERC721CollateralLessDebtThanOffered():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 982708)
TestNewLoan:testNewLoanRefinanceNew() (gas: 702267)
TestNewLoan:testSettleLoan() (gas: 1223453)
TestRepayLoan:testRepayLoan() (gas: 683942)
TestCustodian:testTokenURIInvalidLoan() (gas: 13218)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 530977)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 499814)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 549734)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 539558)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 544218)
TestLoanManager:testCannotIssueSameLoanTwice() (gas: 331333)
TestLoanManager:testCannotSettleInvalidLoan() (gas: 72594)
TestLoanManager:testCannotSettleUnlessValidCustodian() (gas: 68772)
TestLoanManager:testCaveatEnforcerRevert() (gas: 116699)
TestLoanManager:testDefaultFeeRake() (gas: 350119)
TestLoanManager:testExoticDebtWithNoCaveatsNotAsBorrower() (gas: 342776)
TestLoanManager:testInitializedFlagSetProperly() (gas: 65230)
TestLoanManager:testInvalidAmountCollateral() (gas: 152875)
TestLoanManager:testInvalidAmountCollateral721() (gas: 152995)
TestLoanManager:testInvalidAmountDebt() (gas: 177208)
TestLoanManager:testInvalidItemType() (gas: 138923)
TestLoanManager:testInvalidTransferLengthCollateral() (gas: 161136)
TestLoanManager:testInvalidTransferLengthDebt() (gas: 165669)
TestLoanManager:testIssued() (gas: 67055)
TestLoanManager:testName() (gas: 7184)
TestLoanManager:testNonDefaultCustodianCustodyCallFails() (gas: 190240)
TestLoanManager:testNonDefaultCustodianCustodyCallSuccess() (gas: 258491)
TestLoanManager:testNonPayableFunctions() (gas: 173071)
TestLoanManager:testOverrideFeeRake() (gas: 343912)
TestLoanManager:testSupportsInterface() (gas: 9159)
TestLoanManager:testSymbol() (gas: 7127)
TestLoanManager:testTokenNoCodeCollateral() (gas: 137717)
TestLoanManager:testTokenNoCodeDebt() (gas: 170775)
TestLoanManager:testTokenURI() (gas: 64914)
TestLoanManager:testTokenURIInvalidLoan() (gas: 13309)
TestLoanManager:testTransferFromFail() (gas: 80088)
TestNewLoan:testBuyNowPayLater() (gas: 208)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 389501)
TestNewLoan:testNewLoanERC721CollateralLessDebtThanOffered():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 2259)
TestNewLoan:testNewLoanRefinanceNew() (gas: 207)
TestNewLoan:testSettleLoan() (gas: 163)
TestRepayLoan:testRepayLoan() (gas: 207)
TestStarLiteUtils:testEncodeReceivedWithRecipient() (gas: 17955)
TestStarLiteUtils:testSpentToReceived() (gas: 17708)
TestStarLiteUtils:testValidateSaltOpt(address,bytes32) (runs: 256, μ: 26479, ~: 26479)
Expand Down
43 changes: 15 additions & 28 deletions src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
_beforeApprovalsSetHook(fulfiller, maximumSpent, context);
_setOfferApprovalsWithSeaport(offer);

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

consideration = _mergeConsiderations(paymentConsiderations, carryFeeConsideration, new ReceivedItem[](0));
consideration = _removeZeroAmounts(consideration);
// consideration = _mergeConsiderations(paymentConsiderations, carryFeeConsideration, new ReceivedItem[](0));
// consideration = _removeZeroAmounts(consideration);

_settleLoan(loan);
} else if (action == Actions.Settlement && !SettlementHook(loan.terms.hook).isActive(loan)) {
Expand Down Expand Up @@ -252,20 +252,12 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
}

/**
* @dev previews the order for this contract offerer.
* @dev If any additional state updates are needed when taking custody of a loan
*
* @param consideration The items received from the order completing
* @param orderHashes The hashes of the orders completed
* @param contractNonce The nonce of the contract in seaport
* @param context The abi encoded bytes passed with the order
* @param loan The loan that was just placed into custody
* @return selector The function selector of the custody method
*/
function custody(
ReceivedItem[] calldata consideration,
bytes32[] calldata orderHashes,
uint256 contractNonce,
bytes calldata context
) external virtual onlyLoanManager returns (bytes4 selector) {
function custody(LoanManager.Loan memory loan) external virtual onlyLoanManager returns (bytes4 selector) {
revert ImplementInChild();
}

Expand Down Expand Up @@ -317,11 +309,11 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
}
offer = loan.collateral;

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

consideration = _mergeConsiderations(paymentConsiderations, carryFeeConsideration, new ReceivedItem[](0));
consideration = _removeZeroAmounts(consideration);
// consideration = _mergeConsiderations(paymentConsiderations, carryFeeConsideration, new ReceivedItem[](0));
// consideration = _removeZeroAmounts(consideration);
} else if (action == Actions.Settlement && !loanActive) {
address authorized;
(consideration, authorized) = SettlementHandler(loan.terms.handler).getSettlement(loan);
Expand All @@ -343,9 +335,10 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
* revert with NotEnteredViaSeaport()
*/
function onERC1155Received(address, address, uint256, uint256, bytes calldata) public virtual returns (bytes4) {
try seaport.incrementCounter() {
revert NotEnteredViaSeaport();
} catch {}
// commenting out because, we are not entering this flow via Seaport after teh new origiantion changes
// try seaport.incrementCounter() {
// revert NotEnteredViaSeaport();
// } catch {}
return this.onERC1155Received.selector;
}

Expand All @@ -358,9 +351,7 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
*/
function _enableAssetWithSeaport(SpentItem memory offer) internal {
//approve consideration based on item type
if (offer.itemType == ItemType.NATIVE) {
payable(address(seaport)).call{value: offer.amount}("");
} else if (offer.itemType == ItemType.ERC721) {
if (offer.itemType == ItemType.ERC721) {
ERC721(offer.token).approve(address(seaport), offer.identifier);
} else if (offer.itemType == ItemType.ERC1155) {
ERC1155(offer.token).setApprovalForAll(address(seaport), true);
Expand Down Expand Up @@ -388,9 +379,7 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {

function _transferCollateralToHandler(SpentItem memory offer, address handler) internal {
//approve consideration based on item type
if (offer.itemType == ItemType.NATIVE) {
payable(address(handler)).call{value: offer.amount}("");
} else if (offer.itemType == ItemType.ERC721) {
if (offer.itemType == ItemType.ERC721) {
ERC721(offer.token).transferFrom(address(this), handler, offer.identifier);
} else if (offer.itemType == ItemType.ERC1155) {
ERC1155(offer.token).safeTransferFrom(address(this), handler, offer.identifier, offer.amount, "");
Expand Down Expand Up @@ -480,6 +469,4 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
* @param loan The loan being settled
*/
function _afterSettleLoanHook(LoanManager.Loan memory loan) internal virtual {}

receive() external payable onlySeaport {}
}
Loading

0 comments on commit 249b971

Please sign in to comment.