Skip to content

Commit

Permalink
remove lazy mint functionality from custodian
Browse files Browse the repository at this point in the history
  • Loading branch information
0xgregthedev committed Feb 14, 2024
1 parent 0fae7dd commit e89da9a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 280 deletions.
101 changes: 44 additions & 57 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -26,75 +26,62 @@ TestBorrowerEnforcer:testBERevertAdditionalTransfersFromBorrower() (gas: 76462)
TestBorrowerEnforcer:testBERevertInvalidLoanTerms() (gas: 81160)
TestBorrowerEnforcer:testBEValidLoanTerms() (gas: 72257)
TestBorrowerEnforcer:testBEValidLoanTermsAnyIssuer() (gas: 72343)
TestCustodian:testCannotLazyMintTwice() (gas: 82105)
TestCustodian:testCannotMintInvalidLoanInvalidCustodian() (gas: 72477)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 77943)
TestCustodian:testCustodianCannotBeAuthorized() (gas: 142026)
TestCustodian:testCustodySelector() (gas: 2732377)
TestCustodian:testDefaultCustodySelectorRevert() (gas: 72478)
TestCustodian:testGenerateOrderInvalidPostRepayment() (gas: 172939)
TestCustodian:testGenerateOrderInvalidPostSettlement() (gas: 163178)
TestCustodian:testGenerateOrderRepay() (gas: 177099)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 193567)
TestCustodian:testGenerateOrderRepayERC1155AndERC20() (gas: 876545)
TestCustodian:testGenerateOrderRepayERC1155AndERC20HandlerAuthorized() (gas: 804495)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 97556)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 91917)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 106827)
TestCustodian:testGenerateOrderSettlement() (gas: 154891)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 160288)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 163374)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 101809)
TestCustodian:testGenerateOrdersWithLoanStartAtBlockTimestampInvalidLoan() (gas: 461511)
TestCustodian:testGetBorrower() (gas: 78603)
TestCustodian:testInvalidAction() (gas: 179808)
TestCustodian:testInvalidActionRepayInActiveLoan() (gas: 130095)
TestCustodian:testInvalidActionSettleActiveLoan() (gas: 130032)
TestCustodian:testInvalidEncodedData() (gas: 26219)
TestCustodian:testMintWithApprovalSetAsBorrower() (gas: 366740)
TestCustodian:testMintWithApprovalSetAsBorrowerInvalidLoan() (gas: 64505)
TestCustodian:testMintWithApprovalSetNotAuthorized() (gas: 66842)
TestCustodian:testName() (gas: 7077)
TestCustodian:testNonPayableFunctions() (gas: 215289)
TestCustodian:testOnlySeaport() (gas: 17915)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 105729)
TestCustodian:testPreviewOrderRepay() (gas: 232018)
TestCustodian:testPreviewOrderSettlement() (gas: 191813)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 110326)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 116902)
TestCustodian:testRatifyOrder() (gas: 183917)
TestCustodian:testSeaportMetadata() (gas: 8588)
TestCustodian:testSupportsInterface() (gas: 9428)
TestCustodian:testSymbol() (gas: 7194)
TestCustodian:testTokenURI() (gas: 85244)
TestCustodian:testTokenURIInvalidLoan() (gas: 13179)
TestCustodian:testCustodianCannotBeAuthorized() (gas: 141762)
TestCustodian:testCustodySelector() (gas: 1975305)
TestCustodian:testDefaultCustodySelectorRevert() (gas: 72301)
TestCustodian:testGenerateOrderInvalidPostRepayment() (gas: 161196)
TestCustodian:testGenerateOrderInvalidPostSettlement() (gas: 154883)
TestCustodian:testGenerateOrderRepay() (gas: 165356)
TestCustodian:testGenerateOrderRepayERC1155AndERC20() (gas: 860554)
TestCustodian:testGenerateOrderRepayERC1155AndERC20HandlerAuthorized() (gas: 794955)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 97362)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 91788)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 96209)
TestCustodian:testGenerateOrderSettlement() (gas: 146585)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 151993)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 158096)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 101735)
TestCustodian:testGenerateOrdersWithLoanStartAtBlockTimestampInvalidLoan() (gas: 461954)
TestCustodian:testInvalidAction() (gas: 179384)
TestCustodian:testInvalidActionRepayInActiveLoan() (gas: 129808)
TestCustodian:testInvalidActionSettleActiveLoan() (gas: 129790)
TestCustodian:testInvalidEncodedData() (gas: 26064)
TestCustodian:testNonPayableFunctions() (gas: 145115)
TestCustodian:testOnlySeaport() (gas: 17738)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 105721)
TestCustodian:testPreviewOrderRepay() (gas: 216678)
TestCustodian:testPreviewOrderSettlement() (gas: 183427)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 110202)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 106296)
TestCustodian:testRatifyOrder() (gas: 172130)
TestCustodian:testSeaportMetadata() (gas: 8496)
TestCustodian:testSupportsInterface() (gas: 5823)
TestLenderEnforcer:testLERevertAdditionalTransfersFromLender() (gas: 76455)
TestLenderEnforcer:testLERevertInvalidLoanTerms() (gas: 81096)
TestLenderEnforcer:testLEValidLoanTerms() (gas: 72169)
TestLenderEnforcer:testLEValidLoanTermsAnyBorrower() (gas: 72234)
TestLenderEnforcer:testLEValidLoanTermsWithAdditionalTransfers() (gas: 73525)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 593400)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 599835)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 590053)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 579870)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 581478)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 587913)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 578143)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 567960)
TestNewLoan:testBuyNowPayLater() (gas: 3018543)
TestNewLoan:testNewLoanAs1271ProxyAccountSender() (gas: 874195)
TestNewLoan:testNewLoanAs1271ProxyAccountThirdPartyFiller() (gas: 885220)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2() (gas: 429635)
TestNewLoan:testNewLoanRefinance() (gas: 593205)
TestNewLoan:testNewLoanViaOriginatorBorrowerApprovalAndLenderApproval() (gas: 324707)
TestNewLoan:testNewLoanViaOriginatorLenderApproval() (gas: 383503)
TestNewLoan:testSettleLoan() (gas: 642162)
TestNewLoan:testSettleLoan() (gas: 636847)
TestPausableNonReentrant:testNotOwner() (gas: 21276)
TestPausableNonReentrant:testPauseAndUnpause() (gas: 22643)
TestPausableNonReentrant:testReentrancy() (gas: 15404)
TestPausableNonReentrant:testUnpauseWhenNotPaused() (gas: 12604)
TestRepayLoan:testRepayLoanApprovedRepayer() (gas: 666799)
TestRepayLoan:testRepayLoanBase() (gas: 599665)
TestRepayLoan:testRepayLoanGenerateOrderNotSeaport() (gas: 438777)
TestRepayLoan:testRepayLoanInSettlement() (gas: 585692)
TestRepayLoan:testRepayLoanInvalidRepayer() (gas: 603729)
TestRepayLoan:testRepayLoanThatDoesNotExist() (gas: 857990)
TestRepayLoan:testRepayLoanBase() (gas: 587731)
TestRepayLoan:testRepayLoanGenerateOrderNotSeaport() (gas: 438666)
TestRepayLoan:testRepayLoanInSettlement() (gas: 585332)
TestRepayLoan:testRepayLoanInvalidRepayer() (gas: 579691)
TestRepayLoan:testRepayLoanThatDoesNotExist() (gas: 829691)
TestSimpleInterestPricing:test_calculateInterest() (gas: 887709)
TestSimpleInterestPricing:test_getPaymentConsideration() (gas: 935199)
TestSimpleInterestPricing:test_getRefinanceConsideration() (gas: 926059)
Expand All @@ -115,8 +102,8 @@ TestStarport:testDefaultFeeRake2() (gas: 445805)
TestStarport:testDefaultFeeRakeExoticDebt() (gas: 394352)
TestStarport:testEIP712Signing() (gas: 83093)
TestStarport:testExcessiveFeeRake() (gas: 19992)
TestStarport:testExoticDebtWithCustomPricingAndRepayment() (gas: 1237813)
TestStarport:testExoticDebtWithCustomPricingAndSettlement() (gas: 1695865)
TestStarport:testExoticDebtWithCustomPricingAndRepayment() (gas: 1225352)
TestStarport:testExoticDebtWithCustomPricingAndSettlement() (gas: 1690270)
TestStarport:testExoticDebtWithNoCaveatsNotAsBorrower() (gas: 376631)
TestStarport:testIncrementCaveatNonce() (gas: 35117)
TestStarport:testInitializedFlagSetProperly() (gas: 67372)
Expand All @@ -128,8 +115,8 @@ TestStarport:testInvalidItemType() (gas: 151908)
TestStarport:testInvalidTransferLengthCollateral() (gas: 154177)
TestStarport:testInvalidTransferLengthDebt() (gas: 175976)
TestStarport:testInvalidateCaveatSalt() (gas: 33428)
TestStarport:testNonDefaultCustodianCustodyCallFails() (gas: 264259)
TestStarport:testNonDefaultCustodianCustodyCallSuccess() (gas: 290384)
TestStarport:testNonDefaultCustodianCustodyCallFails() (gas: 264236)
TestStarport:testNonDefaultCustodianCustodyCallSuccess() (gas: 290428)
TestStarport:testNonPayableFunctions() (gas: 114479)
TestStarport:testOverrideFeeRake() (gas: 379857)
TestStarport:testPause() (gas: 18127)
Expand Down
96 changes: 8 additions & 88 deletions src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {SafeTransferLib} from "solady/src/utils/SafeTransferLib.sol";
import {LibString} from "solady/src/utils/LibString.sol";

contract Custodian is ERC721, ContractOffererInterface {
contract Custodian is ContractOffererInterface {
using {StarportLib.getId} for Starport.Loan;
using {LibString.concat} for string;

Expand Down Expand Up @@ -100,49 +100,6 @@ contract Custodian is ERC721, ContractOffererInterface {
/* FUNCTION OVERRIDES */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/**
* @dev The name of the ERC721 contract
* @return string The name of the contract
*/
function name() public pure override returns (string memory) {
return "Starport Custodian";
}

/**
* @dev The symbol of the ERC721 contract
* @return string The symbol of the contract
*/
function symbol() public pure override returns (string memory) {
return "SC";
}

/**
* @dev ERC-721 tokenURI override
* @param loanId The id of the custody token/loan
* @return string URI of the custody token/loan
*/
function tokenURI(uint256 loanId) public view override returns (string memory) {
if (!_exists(loanId)) {
revert InvalidLoan();
}
return string("https://astaria.xyz/metadata/loan/").concat(LibString.toString(loanId));
}

/**
* @dev Helper to determine if an interface is supported by this contract
* @param interfaceId The interface to check
* @return bool Returns true if the interface is supported
*/
function supportsInterface(bytes4 interfaceId)
public
view
override (ERC721, ContractOffererInterface)
returns (bool)
{
return interfaceId == type(ERC721).interfaceId || interfaceId == type(ContractOffererInterface).interfaceId
|| super.supportsInterface(interfaceId);
}

/**
* @dev onERC1155Received handler, if we are able to increment the counter
* in seaport that means we have not entered into seaport we dont add for
Expand All @@ -157,38 +114,6 @@ contract Custodian is ERC721, ContractOffererInterface {
/* EXTERNAL FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/**
* @dev Mints a custody token for a loan.
* @param loan The loan to mint a custody token for
*/
function mint(Starport.Loan calldata loan) external {
_validateAndMint(loan);
}

/**
* @dev Mints a custody token for a loan.
* @param loan The loan to mint a custody token for
* @param approvedTo The address with pre approvals set
*/
function mintWithApprovalSet(Starport.Loan calldata loan, address approvedTo) external {
if (msg.sender != loan.borrower) {
revert NotAuthorized();
}
_approve(loan.borrower, approvedTo, _validateAndMint(loan));
}

/**
* @dev internal helper that validates and mints a custody token for a loan.
* @param loan The loan to mint a custody token for
*/
function _validateAndMint(Starport.Loan calldata loan) internal returns (uint256 loanId) {
loanId = loan.getId();
if (loan.custodian != address(this) || SP.closed(loanId)) {
revert InvalidLoan();
}
_safeMint(loan.borrower, loanId);
}

/**
* @dev Generates the order for this contract offerer
* @return ratifyOrderMagicValue The magic value returned by the ratify
Expand Down Expand Up @@ -222,7 +147,7 @@ contract Custodian is ERC721, ContractOffererInterface {
}
bool loanActive = Status(loan.terms.status).isActive(loan, close.extraData);
if (close.action == Actions.Repayment && loanActive) {
if (fulfiller != getBorrower(loan) && fulfiller != _getApproved(loan.getId())) {
if (fulfiller != loan.borrower) {
revert InvalidRepayer();
}

Expand Down Expand Up @@ -292,13 +217,12 @@ contract Custodian is ERC721, ContractOffererInterface {
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/**
* @dev Fetches the borrower of the loan, first checks to see if we've minted the token for the loan
* @param loan Loan to get the borrower of
* @return address The address of the loan borrower(returns the ownerOf the token if any) defaults to loan.borrower
* @dev Helper to determine if an interface is supported by this contract
* @param interfaceId The interface to check
* @return bool Returns true if the interface is supported
*/
function getBorrower(Starport.Loan memory loan) public view returns (address) {
uint256 loanId = loan.getId();
return _exists(loanId) ? ownerOf(loanId) : loan.borrower;
function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
return interfaceId == type(ContractOffererInterface).interfaceId;
}

/**
Expand All @@ -324,7 +248,7 @@ contract Custodian is ERC721, ContractOffererInterface {
}
bool loanActive = Status(loan.terms.status).isActive(loan, close.extraData);
if (close.action == Actions.Repayment && loanActive) {
if (fulfiller != getBorrower(loan) && fulfiller != _getApproved(loan.getId())) {
if (fulfiller != loan.borrower) {
revert InvalidRepayer();
}
offer = loan.collateral;
Expand Down Expand Up @@ -453,10 +377,6 @@ contract Custodian is ERC721, ContractOffererInterface {
*/
function _settleLoan(Starport.Loan memory loan) internal virtual {
_beforeSettleLoanHook(loan);
uint256 loanId = loan.getId();
if (_exists(loanId)) {
_burn(loanId);
}
SP.settle(loan);
_afterSettleLoanHook(loan);
}
Expand Down
64 changes: 64 additions & 0 deletions test/integration-testing/TestPOC.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//pragma solidity ^0.8.17;
//
//import "starport-test/StarportTest.sol";
//import {StarportLib, Actions} from "starport-core/lib/StarportLib.sol";
//import {BNPLHelper, IFlashLoanRecipient} from "starport-test/mocks/BNPLHelper.sol";
//import {Originator} from "starport-core/originators/Originator.sol";
//import {SignatureCheckerLib} from "solady/src/utils/SignatureCheckerLib.sol";
//
//contract TestNewLoan is StarportTest {
// using {StarportLib.getId} for Starport.Loan;
//
// function testLazyMintCustodian() public {
// Starport.Terms memory terms = Starport.Terms({
// status: address(status),
// settlement: address(settlement),
// pricing: address(pricing),
// pricingData: defaultPricingData,
// settlementData: defaultSettlementData,
// statusData: defaultStatusData
// });
//
// Starport.Loan memory loan =
// _createLoan721Collateral20Debt({lender: lender.addr, borrowAmount: 100, terms: terms});
//
// uint256 loanId = loan.getId();
//
// //Borrower mints and sells custodian token to sucker
// Custodian(loan.custodian).mint(loan);
// address sucker = vm.createWallet("sucker").addr;
// vm.prank(loan.borrower);
// Custodian(loan.custodian).transferFrom(loan.borrower, sucker, loanId);
//
// Starport.Loan memory refiLoan = loanCopy(loan);
// bytes memory newPricingData = abi.encode(
// SimpleInterestPricing.Details({carryRate: (uint256(1e16) * 10), rate: uint256(1e16) * 100, decimals: 18})
// );
// refiLoan.terms.pricingData = newPricingData;
// refiLoan.debt = SP.applyRefinanceConsiderationToLoan(loan.debt, new SpentItem[](0));
// LenderEnforcer.Details memory details = LenderEnforcer.Details({loan: refiLoan});
//
// details.loan.issuer = refinancer.addr;
// details.loan.originator = address(0);
// details.loan.start = 0;
// CaveatEnforcer.SignedCaveats memory refiCaveat = getLenderSignedCaveat({
// details: details,
// signer: refinancer,
// salt: bytes32(0),
// enforcer: address(lenderEnforcer)
// });
// _setApprovalsForSpentItems(refinancer.addr, loan.debt);
//
// skip(1);
// Starport.Loan memory newLoan = refinanceLoan(loan, newPricingData, refinancer.addr, refiCaveat, refinancer.addr);
//
// bool oldLoanOpen = SP.open(loanId);
// bool newLoanOpen = SP.open(newLoan.getId());
// bool borrowerOfOldLoanIsSucker = Custodian(loan.custodian).getBorrower(loan) == sucker;
// bool suckerHasCT = Custodian(loan.custodian).ownerOf(loanId) == sucker;
//
// if (!oldLoanOpen && newLoanOpen && suckerHasCT && borrowerOfOldLoanIsSucker) {
// revert("Sucker has been scammed");
// }
// }
//}
28 changes: 0 additions & 28 deletions test/integration-testing/TestRepayLoan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,34 +108,6 @@ contract TestRepayLoan is StarportTest {
});
}

function testRepayLoanApprovedRepayer() public {
uint256 borrowAmount = 1e18;
Starport.Terms memory terms = Starport.Terms({
status: address(status),
settlement: address(settlement),
pricing: address(pricing),
pricingData: defaultPricingData,
settlementData: defaultSettlementData,
statusData: defaultStatusData
});

Starport.Loan memory loan =
_createLoan721Collateral20Debt({lender: lender.addr, borrowAmount: borrowAmount, terms: terms});

vm.startPrank(borrower.addr);
skip(10 days);
SimpleInterestPricing.Details memory details =
abi.decode(loan.terms.pricingData, (SimpleInterestPricing.Details));
uint256 interest = SimpleInterestPricing(loan.terms.pricing).calculateInterest(
10 days, loan.debt[0].amount, details.rate, details.decimals
);
erc20s[0].approve(address(consideration), loan.debt[0].amount + interest);
custodian.mintWithApprovalSet(loan, address(this));
vm.stopPrank();

_repayLoan(loan, address(this));
}

// calling generateOrder on the Custodian to test the onlySeaport modifier
function testRepayLoanGenerateOrderNotSeaport() public {
Starport.Terms memory terms = Starport.Terms({
Expand Down
Loading

0 comments on commit e89da9a

Please sign in to comment.