Skip to content

Commit

Permalink
Feat/custodian testing (#26)
Browse files Browse the repository at this point in the history
* custodian coverage moving on up

* update snapshot

* coverage at 75%, lines at 100%, funcs at 96(fallback and receive not being picked up)

* update snapshot

* update snapshot
  • Loading branch information
androolloyd authored Oct 10, 2023
1 parent 7d75510 commit 0efb307
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 102 deletions.
100 changes: 54 additions & 46 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,52 +1,60 @@
EnforcerTest:testCollateralEnforcer() (gas: 962290)
EnforcerTest:testFailCollateralEnforcerDifferentCollateral() (gas: 844283)
EnforcerTest:testFailRateEnforcerMaxCarryRate() (gas: 799437)
EnforcerTest:testFailRateEnforcerMaxRate() (gas: 799387)
EnforcerTest:testFailRateEnforcerMaxRateAndMaxCarryRate() (gas: 799252)
EnforcerTest:testRateEnforcerBasic() (gas: 919547)
EnforcerTest:testTermEnforcerBasic() (gas: 991809)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecall() (gas: 981121)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 883818)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 930628)
TestCustodian:testCannotLazyMintTwice() (gas: 76547)
TestCustodian:testCannotMintInvalidLoanInvalidCustodian() (gas: 66855)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72372)
TestCustodian:testCustodySelector() (gas: 7174)
TestCustodian:testGenerateOrderRepay() (gas: 194926)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 109195)
TestCustodian:testGenerateOrderSettlement() (gas: 135403)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 105827)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 106951)
TestCustodian:testGetBorrower() (gas: 76068)
EnforcerTest:testCollateralEnforcer() (gas: 946634)
EnforcerTest:testFailCollateralEnforcerDifferentCollateral() (gas: 828668)
EnforcerTest:testFailRateEnforcerMaxCarryRate() (gas: 783830)
EnforcerTest:testFailRateEnforcerMaxRate() (gas: 783780)
EnforcerTest:testFailRateEnforcerMaxRateAndMaxCarryRate() (gas: 783645)
EnforcerTest:testRateEnforcerBasic() (gas: 903898)
EnforcerTest:testTermEnforcerBasic() (gas: 976160)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecall() (gas: 964589)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 867112)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 913906)
TestCustodian:testCannotLazyMintTwice() (gas: 76569)
TestCustodian:testCannotMintInvalidLoanInvalidCustodian() (gas: 66833)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72328)
TestCustodian:testCustodySelector() (gas: 7219)
TestCustodian:testGenerateOrderRepay() (gas: 189138)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 214734)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 1116860)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 526314)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 104802)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 99227)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 110778)
TestCustodian:testGenerateOrderSettlement() (gas: 135471)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 107403)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 108565)
TestCustodian:testGetBorrower() (gas: 76016)
TestCustodian:testName() (gas: 7142)
TestCustodian:testOnlySeaport() (gas: 15165)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 103386)
TestCustodian:testPreviewOrderRepay() (gas: 249187)
TestCustodian:testPreviewOrderSettlement() (gas: 166701)
TestCustodian:testPreviewOrderSettlementInvalidFufiller() (gas: 104885)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 111090)
TestCustodian:testRatifyOrder() (gas: 209374)
TestCustodian:testSafeTransferReceive() (gas: 159017)
TestCustodian:testSeaportMetadata() (gas: 8478)
TestCustodian:testSetRepayApproval() (gas: 37885)
TestCustodian:testSupportsInterface() (gas: 5880)
TestCustodian:testSymbol() (gas: 7150)
TestCustodian:testTokenURI() (gas: 64833)
TestCustodian:testTokenURIInvalidLoan() (gas: 11643)
TestExoticLoans:testSwap() (gas: 1161209)
TestCustodian:testNonPayableFunctions() (gas: 247767)
TestCustodian:testOnlySeaport() (gas: 17865)
TestCustodian:testPayableFunctions() (gas: 45528)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 104915)
TestCustodian:testPreviewOrderRepay() (gas: 243607)
TestCustodian:testPreviewOrderSettlement() (gas: 166861)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 106410)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 112636)
TestCustodian:testRatifyOrder() (gas: 203683)
TestCustodian:testRatifyOrderInvalidHandlerExecution() (gas: 89630)
TestCustodian:testSafeTransferReceive() (gas: 159087)
TestCustodian:testSeaportMetadata() (gas: 8512)
TestCustodian:testSetRepayApproval() (gas: 37929)
TestCustodian:testSupportsInterface() (gas: 9428)
TestCustodian:testSymbol() (gas: 7149)
TestCustodian:testTokenURI() (gas: 64867)
TestCustodian:testTokenURIInvalidLoan() (gas: 13196)
TestExoticLoans:testSwap() (gas: 1147092)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 252)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 515139)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 730997)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 513054)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 717518)
TestLoanManager:testGenerateOrder() (gas: 999606)
TestLoanManager:testGenerateOrderInvalidSender() (gas: 11408)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 499598)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 715578)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 498937)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 701464)
TestLoanManager:testGenerateOrder() (gas: 998199)
TestLoanManager:testGenerateOrderInvalidSender() (gas: 12986)
TestLoanManager:testSupportsInterface() (gas: 7003)
TestNewLoan:testBuyNowPayLater() (gas: 1144555)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 990663)
TestNewLoan:testNewLoanERC721CollateralDefaultTermsRefinance() (gas: 651211)
TestNewLoan:testSettleLoan() (gas: 1256463)
TestRepayLoan:testRepayLoan() (gas: 714153)
TestNewLoan:testBuyNowPayLater() (gas: 1144531)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 974985)
TestNewLoan:testNewLoanERC721CollateralDefaultTermsRefinance() (gas: 635547)
TestNewLoan:testSettleLoan() (gas: 1240937)
TestRepayLoan:testRepayLoan() (gas: 698757)
TestStarLiteUtils:testEncodeReceivedWithRecipient() (gas: 17955)
TestStarLiteUtils:testSpentToReceived() (gas: 17708)
TestStarLiteUtils:testValidateSaltOpt(address,bytes32) (runs: 256, μ: 26479, ~: 26479)
Expand Down
20 changes: 10 additions & 10 deletions src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ contract Custodian is ContractOffererInterface, TokenReceiverInterface, ConduitH
error NotSeaport();
error InvalidRepayer();
error InvalidFulfiller();
error InvalidHandler();
error InvalidHandlerExecution();
error InvalidLoan();
error InvalidAsset();

constructor(LoanManager LM_, address seaport_) {
seaport = seaport_;
Expand Down Expand Up @@ -127,7 +126,7 @@ contract Custodian is ContractOffererInterface, TokenReceiverInterface, ConduitH
uint256 loanId = loan.getId();
if (LM.active(loanId)) {
if (SettlementHandler(loan.terms.handler).execute(loan) != SettlementHandler.execute.selector) {
revert InvalidHandler();
revert InvalidHandlerExecution();
}
_settleLoan(loan);
}
Expand Down Expand Up @@ -281,12 +280,11 @@ contract Custodian is ContractOffererInterface, TokenReceiverInterface, ConduitH
}
}

if (offer.length > 0) {
_beforeApprovalsSetHook(fulfiller, maximumSpent, context);
_setOfferApprovalsWithSeaport(offer);
}
_beforeApprovalsSetHook(fulfiller, maximumSpent, context);
_setOfferApprovalsWithSeaport(offer);
}

//custodian cant get any other assets deposited aside from what the LM supports
function _enableAssetWithSeaport(SpentItem memory offer) internal {
//approve consideration based on item type
if (offer.itemType == ItemType.NATIVE) {
Expand All @@ -297,8 +295,6 @@ contract Custodian is ContractOffererInterface, TokenReceiverInterface, ConduitH
ERC1155(offer.token).setApprovalForAll(address(seaport), true);
} else if (offer.itemType == ItemType.ERC20) {
ERC20(offer.token).approve(address(seaport), offer.amount);
} else {
revert InvalidAsset();
}
}

Expand Down Expand Up @@ -332,7 +328,11 @@ contract Custodian is ContractOffererInterface, TokenReceiverInterface, ConduitH

function _afterSettleLoanHook(LoanManager.Loan memory loan) internal virtual {
if (loan.issuer.code.length > 0) {
try LoanSettledCallback(loan.issuer).onLoanSettled(loan) {} catch (bytes memory error) {}
loan.issuer.call(abi.encodeWithSelector(LoanSettledCallback.onLoanSettled.selector, loan));
}
}

fallback() external payable onlySeaport {}

receive() external payable onlySeaport {}
}
6 changes: 3 additions & 3 deletions src/LoanManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract LoanManager is ERC721, ContractOffererInterface, ConduitHelper, Ownable
ConsiderationInterface public immutable seaport;
// ConsiderationInterface public constant seaport =
// ConsiderationInterface(0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC); // mainnet
address public immutable defaultCustodian;
address payable public immutable defaultCustodian;
bytes32 public immutable DEFAULT_CUSTODIAN_CODE_HASH;
bytes32 internal immutable _DOMAIN_SEPARATOR;

Expand Down Expand Up @@ -141,7 +141,7 @@ contract LoanManager is ERC721, ContractOffererInterface, ConduitHelper, Ownable
assembly {
defaultCustodianCodeHash := extcodehash(custodian)
}
defaultCustodian = custodian;
defaultCustodian = payable(custodian);
DEFAULT_CUSTODIAN_CODE_HASH = defaultCustodianCodeHash;
_DOMAIN_SEPARATOR = keccak256(abi.encode(EIP_DOMAIN, VERSION, block.chainid, address(this)));
_initializeOwner(msg.sender);
Expand Down Expand Up @@ -250,7 +250,7 @@ contract LoanManager is ERC721, ContractOffererInterface, ConduitHelper, Ownable
}
if (codeHash != DEFAULT_CUSTODIAN_CODE_HASH) {
if (
Custodian(custodian).custody(consideration, orderHashes, contractNonce, context)
Custodian(payable(custodian)).custody(consideration, orderHashes, contractNonce, context)
!= Custodian.custody.selector
) {
revert InvalidAction();
Expand Down
72 changes: 54 additions & 18 deletions test/StarPortTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {LoanManager} from "starport-core/LoanManager.sol";

import {BaseOrderTest} from "seaport/test/foundry/utils/BaseOrderTest.sol";
import {TestERC721} from "seaport/contracts/test/TestERC721.sol";
import {TestERC1155} from "seaport/contracts/test/TestERC1155.sol";
import {TestERC20} from "seaport/contracts/test/TestERC20.sol";
import {ConsiderationItemLib} from "seaport/lib/seaport-sol/src/lib/ConsiderationItemLib.sol";
import {AAVEPoolCustodian} from "starport-core/custodians/AAVEPoolCustodian.sol";
Expand Down Expand Up @@ -157,15 +158,18 @@ contract StarPortTest is BaseOrderTest {
hook = new FixedTermHook();
vm.label(address(erc721s[0]), "Collateral NFT");
vm.label(address(erc721s[1]), "Collateral2 NFT");
vm.label(address(erc20s[0]), "Debt Token");
vm.label(address(erc20s[1]), "Collateral Token");
vm.label(address(erc20s[0]), "Debt ERC20");
vm.label(address(erc20s[1]), "Collateral ERC20");
vm.label(address(erc1155s[0]), "Collateral 1155");
vm.label(address(erc1155s[1]), "Debt 1155 ");
{
vm.startPrank(borrower.addr);
erc721s[1].mint(seller.addr, 1);
erc721s[0].mint(borrower.addr, 1);
erc721s[0].mint(borrower.addr, 2);
erc721s[0].mint(borrower.addr, 3);
erc20s[1].mint(borrower.addr, 10000);
erc1155s[0].mint(borrower.addr, 1, 1);
vm.stopPrank();
}
conduitKeyOne = bytes32(uint256(uint160(address(lender.addr))) << 96);
Expand Down Expand Up @@ -228,16 +232,6 @@ contract StarPortTest is BaseOrderTest {
{
bytes32 detailsHash = keccak256(originator.encodeWithAccountCounter(keccak256(loanData.details)));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(strategist.key, detailsHash);
LoanManager.Loan memory loan = LoanManager.Loan({
custodian: address(loanData.custodian),
issuer: address(0),
borrower: borrower.addr,
originator: isTrusted ? address(originator) : address(0),
terms: originator.terms(loanData.details),
debt: debt,
collateral: ConsiderationItemLib.toSpentItemArray(collateral),
start: uint256(0)
});
return _executeNLR(
LoanManager.Obligation({
custodian: address(loanData.custodian),
Expand Down Expand Up @@ -561,12 +555,21 @@ contract StarPortTest is BaseOrderTest {
}
vm.recordLogs();
vm.startPrank(borrower.addr);
consideration.fulfillAdvancedOrder({
advancedOrder: x,
criteriaResolvers: new CriteriaResolver[](0),
fulfillerConduitKey: bytes32(0),
recipient: address(borrower.addr)
});
if (collateral[0].itemType == ItemType.NATIVE) {
consideration.fulfillAdvancedOrder{value: collateral[0].endAmount}({
advancedOrder: x,
criteriaResolvers: new CriteriaResolver[](0),
fulfillerConduitKey: bytes32(0),
recipient: address(borrower.addr)
});
} else {
consideration.fulfillAdvancedOrder({
advancedOrder: x,
criteriaResolvers: new CriteriaResolver[](0),
fulfillerConduitKey: bytes32(0),
recipient: address(borrower.addr)
});
}
Vm.Log[] memory logs = vm.getRecordedLogs();
uint256 loanId;

Expand Down Expand Up @@ -761,4 +764,37 @@ contract StarPortTest is BaseOrderTest {
recipient: payable(address(custodian))
});
}

function _getERC1155Consideration(TestERC1155 token) internal view returns (ConsiderationItem memory) {
return ConsiderationItem({
token: address(token),
startAmount: 1,
endAmount: 1,
identifierOrCriteria: 1,
itemType: ItemType.ERC1155,
recipient: payable(address(custodian))
});
}

function _getERC20Consideration(TestERC20 token) internal view returns (ConsiderationItem memory) {
return ConsiderationItem({
token: address(token),
startAmount: 1,
endAmount: 1,
identifierOrCriteria: 0,
itemType: ItemType.ERC20,
recipient: payable(address(custodian))
});
}

function _getNativeConsideration() internal view returns (ConsiderationItem memory) {
return ConsiderationItem({
token: address(0),
startAmount: 100 wei,
endAmount: 100 wei,
identifierOrCriteria: 0,
itemType: ItemType.NATIVE,
recipient: payable(address(custodian))
});
}
}
Loading

0 comments on commit 0efb307

Please sign in to comment.