Skip to content

Commit

Permalink
update astaria v1 testing to use the new refinance flow/remove old flow
Browse files Browse the repository at this point in the history
  • Loading branch information
androolloyd committed Oct 18, 2023
1 parent 1b8aed1 commit c8301f9
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 178 deletions.
126 changes: 65 additions & 61 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,86 +1,90 @@
EnforcerTest:testCollateralEnforcer() (gas: 952711)
EnforcerTest:testFailCollateralEnforcerDifferentCollateral() (gas: 833351)
EnforcerTest:testFailRateEnforcerMaxCarryRate() (gas: 788492)
EnforcerTest:testFailRateEnforcerMaxRate() (gas: 788464)
EnforcerTest:testFailRateEnforcerMaxRateAndMaxCarryRate() (gas: 788329)
EnforcerTest:testRateEnforcerBasic() (gas: 909887)
EnforcerTest:testTermEnforcerBasic() (gas: 982214)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecall() (gas: 964044)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 857161)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 893410)
TestCustodian:testCannotLazyMintTwice() (gas: 76641)
EnforcerTest:testCollateralEnforcer() (gas: 953710)
EnforcerTest:testFailCollateralEnforcerDifferentCollateral() (gas: 834285)
EnforcerTest:testFailRateEnforcerMaxCarryRate() (gas: 789421)
EnforcerTest:testFailRateEnforcerMaxRate() (gas: 789393)
EnforcerTest:testFailRateEnforcerMaxRateAndMaxCarryRate() (gas: 789258)
EnforcerTest:testRateEnforcerBasic() (gas: 910883)
EnforcerTest:testTermEnforcerBasic() (gas: 983211)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallBase() (gas: 1212053)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLender() (gas: 858559)
TestAstariaV1Loan:testNewLoanERC721CollateralDefaultTermsRecallLiquidation() (gas: 894808)
TestCustodian:testCannotLazyMintTwice() (gas: 76619)
TestCustodian:testCannotMintInvalidLoanInvalidCustodian() (gas: 66839)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72378)
TestCustodian:testCannotMintInvalidLoanValidCustodian() (gas: 72356)
TestCustodian:testCustodySelector() (gas: 2822520)
TestCustodian:testDefaultCustodySelectorRevert() (gas: 11673)
TestCustodian:testGenerateOrderInvalidHandlerExecution() (gas: 139369)
TestCustodian:testGenerateOrderRepay() (gas: 179721)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 205339)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 1113500)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 1027370)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 523917)
TestCustodian:testGenerateOrderRepay() (gas: 179699)
TestCustodian:testGenerateOrderRepayAsRepayApprovedBorrower() (gas: 205317)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNative() (gas: 1116698)
TestCustodian:testGenerateOrderRepayERC1155AndERC20AndNativeHandlerAuthorized() (gas: 1030557)
TestCustodian:testGenerateOrderRepayERC1155WithRevert() (gas: 524848)
TestCustodian:testGenerateOrderRepayInvalidHookAddress() (gas: 96726)
TestCustodian:testGenerateOrderRepayInvalidHookReturnType() (gas: 91130)
TestCustodian:testGenerateOrderRepayNotBorrower() (gas: 102852)
TestCustodian:testGenerateOrderSettlement() (gas: 161004)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 170134)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 162510)
TestCustodian:testGenerateOrderSettlement() (gas: 160982)
TestCustodian:testGenerateOrderSettlementHandlerAuthorized() (gas: 170112)
TestCustodian:testGenerateOrderSettlementNoActiveLoan() (gas: 162488)
TestCustodian:testGenerateOrderSettlementUnauthorized() (gas: 100769)
TestCustodian:testGetBorrower() (gas: 76159)
TestCustodian:testInvalidAction() (gas: 124400)
TestCustodian:testInvalidActionRepayInActiveLoan() (gas: 124410)
TestCustodian:testInvalidActionSettleActiveLoan() (gas: 124413)
TestCustodian:testInvalidAction() (gas: 124378)
TestCustodian:testInvalidActionRepayInActiveLoan() (gas: 124388)
TestCustodian:testInvalidActionSettleActiveLoan() (gas: 124391)
TestCustodian:testName() (gas: 7098)
TestCustodian:testNonPayableFunctions() (gas: 247795)
TestCustodian:testOnlySeaport() (gas: 17931)
TestCustodian:testPayableFunctions() (gas: 43468)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 105228)
TestCustodian:testPreviewOrderRepay() (gas: 230713)
TestCustodian:testPreviewOrderSettlement() (gas: 192940)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 107146)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 113233)
TestCustodian:testRatifyOrder() (gas: 185906)
TestCustodian:testPreviewOrderNoActiveLoan() (gas: 105206)
TestCustodian:testPreviewOrderRepay() (gas: 230669)
TestCustodian:testPreviewOrderSettlement() (gas: 192896)
TestCustodian:testPreviewOrderSettlementInvalidFufliller() (gas: 107124)
TestCustodian:testPreviewOrderSettlementInvalidRepayer() (gas: 113211)
TestCustodian:testRatifyOrder() (gas: 185884)
TestCustodian:testSafeTransferReceive() (gas: 159087)
TestCustodian:testSeaportMetadata() (gas: 8501)
TestCustodian:testSetRepayApproval() (gas: 37907)
TestCustodian:testSupportsInterface() (gas: 9406)
TestCustodian:testSymbol() (gas: 7127)
TestCustodian:testTokenURI() (gas: 64839)
TestCustodian:testTokenURIInvalidLoan() (gas: 13196)
TestExoticLoans:testSwap() (gas: 1356857)
TestExoticLoans:testSwap() (gas: 1357792)
TestLoanCombinations:testLoan20For721SimpleInterestDutchFixedRepay() (gas: 164)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 542275)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 747965)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 577463)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 739552)
TestLoanManager:testCannotSettleInvalidLoan() (gas: 72860)
TestLoanCombinations:testLoan20for20SimpleInterestDutchFixedRepay() (gas: 543024)
TestLoanCombinations:testLoan721for20SimpleInterestDutchFixedRepay() (gas: 748703)
TestLoanCombinations:testLoanAstariaSettlementRepay() (gas: 578211)
TestLoanCombinations:testLoanSimpleInterestEnglishFixed() (gas: 740303)
TestLoanManager:testCannotSettleInvalidLoan() (gas: 72816)
TestLoanManager:testCannotSettleUnlessValidCustodian() (gas: 68984)
TestLoanManager:testCaveatEnforcerInvalidOrigination() (gas: 1306498)
TestLoanManager:testDefaultFeeRake() (gas: 403106)
TestLoanManager:testGenerateOrder() (gas: 1003397)
TestLoanManager:testGenerateOrderInvalidAction() (gas: 864365)
TestLoanManager:testGenerateOrderNotSeaport() (gas: 13089)
TestLoanManager:testInitializedFlagSetProperly() (gas: 65061)
TestLoanManager:testInvalidDebt() (gas: 25968)
TestLoanManager:testInvalidMaximumSpentEmpty() (gas: 36066)
TestLoanManager:testIssued() (gas: 67018)
TestLoanManager:testName() (gas: 7206)
TestLoanManager:testNonDefaultCustodianCustodyCallFails() (gas: 23909)
TestLoanManager:testNonDefaultCustodianCustodyCallSuccess() (gas: 25415)
TestLoanManager:testOverrideFeeRake() (gas: 404444)
TestLoanManager:testPreviewOrderInvalidAction() (gas: 864492)
TestLoanManager:testSupportsInterface() (gas: 9579)
TestLoanManager:testSymbol() (gas: 7257)
TestLoanManager:testTokenURI() (gas: 64958)
TestLoanManager:testTokenURIInvalidLoan() (gas: 13265)
TestLoanManager:testTransferFromFailFromSeaport() (gas: 82235)
TestNewLoan:testBuyNowPayLater() (gas: 1149851)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 981209)
TestNewLoan:testNewLoanERC721CollateralDefaultTermsRefinance() (gas: 635641)
TestNewLoan:testNewLoanERC721CollateralLessDebtThanOffered():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 981521)
TestNewLoan:testNewLoanRefinanceNew() (gas: 695966)
TestNewLoan:testSettleLoan() (gas: 1222369)
TestRepayLoan:testRepayLoan() (gas: 682950)
TestLoanManager:testCaveatEnforcerInvalidOrigination() (gas: 1306453)
TestLoanManager:testDefaultFeeRake() (gas: 403541)
TestLoanManager:testGenerateOrder() (gas: 1003241)
TestLoanManager:testGenerateOrderInvalidAction() (gas: 864336)
TestLoanManager:testGenerateOrderNotSeaport() (gas: 13068)
TestLoanManager:testInitializedFlagSetProperly() (gas: 65260)
TestLoanManager:testInvalidDebt() (gas: 37092)
TestLoanManager:testInvalidMaximumSpentEmpty() (gas: 48104)
TestLoanManager:testIssued() (gas: 67041)
TestLoanManager:testName() (gas: 7231)
TestLoanManager:testNonDefaultCustodianCustodyCallFails() (gas: 24012)
TestLoanManager:testNonDefaultCustodianCustodyCallSuccess() (gas: 25377)
TestLoanManager:testOverrideFeeRake() (gas: 404902)
TestLoanManager:testPreviewOrderInvalidAction() (gas: 808721)
TestLoanManager:testPreviewOrderOriginationWithNoCaveatsSetAsBorrowerFeeOn() (gas: 852458)
TestLoanManager:testPreviewOrderOriginationWithNoCaveatsSetAsBorrowerNoFee() (gas: 821119)
TestLoanManager:testPreviewOrderOriginationWithNoCaveatsSetNotBorrowerFeeOn() (gas: 861097)
TestLoanManager:testPreviewOrderOriginationWithNoCaveatsSetNotBorrowerNoFee() (gas: 828894)
TestLoanManager:testSeaportMetadata() (gas: 8622)
TestLoanManager:testSupportsInterface() (gas: 9557)
TestLoanManager:testSymbol() (gas: 7194)
TestLoanManager:testTokenURI() (gas: 64939)
TestLoanManager:testTokenURIInvalidLoan() (gas: 13224)
TestLoanManager:testTransferFromFailFromSeaport() (gas: 82103)
TestNewLoan:testBuyNowPayLater() (gas: 1149725)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 982226)
TestNewLoan:testNewLoanERC721CollateralLessDebtThanOffered():((uint256,address,address,address,address,(uint8,address,uint256,uint256)[],(uint8,address,uint256,uint256)[],(address,bytes,address,bytes,address,bytes))) (gas: 982560)
TestNewLoan:testNewLoanRefinanceNew() (gas: 696898)
TestNewLoan:testSettleLoan() (gas: 1223385)
TestRepayLoan:testRepayLoan() (gas: 683954)
TestStarLiteUtils:testEncodeReceivedWithRecipient() (gas: 17955)
TestStarLiteUtils:testSpentToReceived() (gas: 17708)
TestStarLiteUtils:testValidateSaltOpt(address,bytes32) (runs: 256, μ: 26479, ~: 26479)
Expand Down
49 changes: 0 additions & 49 deletions src/LoanManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -636,54 +636,5 @@ contract LoanManager is ConduitHelper, Ownable, ERC721 {
_issueLoanManager(loan, fulfiller.code.length > 0);
}

function refinance(LoanManager.Loan memory loan, bytes memory newPricingData, address conduit) external {
(,, address conduitController) = seaport.information();

if (ConduitControllerInterface(conduitController).ownerOf(conduit) != msg.sender) {
revert InvalidConduit();
}
(
// used to update the new loan amount
ReceivedItem[] memory considerationPayment,
// used to pay the carry amount
ReceivedItem[] memory carryPayment,
// note: considerationPayment - carryPayment = amount to pay lender

// used for any additional payments beyond consideration and carry
ReceivedItem[] memory additionalPayment
) = Pricing(loan.terms.pricing).isValidRefinance(loan, newPricingData, msg.sender);

ReceivedItem[] memory refinanceConsideration =
_mergeConsiderations(considerationPayment, carryPayment, additionalPayment);
refinanceConsideration = _removeZeroAmounts(refinanceConsideration);

// if for malicious or non-malicious the refinanceConsideration is zero
if (refinanceConsideration.length == 0) {
revert InvalidNoRefinanceConsideration();
}

_settle(loan);

for (uint256 i; i < loan.debt.length;) {
loan.debt[i].amount = considerationPayment[i].amount;
unchecked {
++i;
}
}

if (
ConduitInterface(conduit).execute(_packageTransfers(refinanceConsideration, msg.sender))
!= ConduitInterface.execute.selector
) {
revert ConduitTransferError();
}

loan.terms.pricingData = newPricingData;
loan.originator = msg.sender;
loan.issuer = msg.sender;
loan.start = block.timestamp;
_issueLoanManager(loan, msg.sender.code.length > 0);
}

receive() external payable {}
}
37 changes: 36 additions & 1 deletion test/StarPortTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ contract StarPortTest is BaseOrderTest {
internal
returns (LoanManager.Loan memory)
{
return newLoan(loanData, originator, collateral, "");
}

function newLoan(
NewLoanData memory loanData,
Originator originator,
ConsiderationItem[] storage collateral,
bytes memory revertMessage
) internal returns (LoanManager.Loan memory) {
bool isTrusted = loanData.caveats.length == 0;
{
bytes32 detailsHash = keccak256(originator.encodeWithAccountCounter(keccak256(loanData.details)));
Expand All @@ -288,8 +297,20 @@ contract StarPortTest is BaseOrderTest {
internal
returns (LoanManager.Loan memory newLoan)
{
return refinanceLoan(loan, newPricingData, asWho, "");
}

function refinanceLoan(
LoanManager.Loan memory loan,
bytes memory newPricingData,
address asWho,
bytes memory revertMessage
) internal returns (LoanManager.Loan memory newLoan) {
if (revertMessage.length > 0) {
vm.expectRevert(revertMessage);
}
(SpentItem[] memory offer, ReceivedItem[] memory requiredConsideration) = LM.previewOrder(
address(LM.seaport()),
address(seaport),
asWho,
new SpentItem[](0),
new SpentItem[](0),
Expand Down Expand Up @@ -332,6 +353,9 @@ contract StarPortTest is BaseOrderTest {
vm.recordLogs();
vm.startPrank(asWho);

if (revertMessage.length > 0) {
vm.expectRevert(); //reverts InvalidContractOfferer with an address an a contract nonce so expect general revert
}
consideration.fulfillAdvancedOrder({
advancedOrder: refinanceOrder,
criteriaResolvers: new CriteriaResolver[](0),
Expand Down Expand Up @@ -688,6 +712,14 @@ contract StarPortTest is BaseOrderTest {
internal
returns (LoanManager.Loan memory loan)
{
return _executeNLR(nlr, collateral, "");
}

function _executeNLR(
LoanManager.Obligation memory nlr,
ConsiderationItem[] memory collateral,
bytes memory revertReason
) internal returns (LoanManager.Loan memory loan) {
bytes32 caveatHash =
keccak256(LM.encodeWithSaltAndBorrowerCounter(nlr.borrower, nlr.salt, keccak256(abi.encode(nlr.caveats))));
OfferItem[] memory offer = new OfferItem[](nlr.debt.length + 1);
Expand Down Expand Up @@ -732,6 +764,9 @@ contract StarPortTest is BaseOrderTest {
}
vm.recordLogs();
vm.startPrank(borrower.addr);
if (revertReason.length > 0) {
vm.expectRevert(revertReason);
}
if (collateral[0].itemType == ItemType.NATIVE) {
consideration.fulfillAdvancedOrder{value: collateral[0].endAmount}({
advancedOrder: x,
Expand Down
24 changes: 9 additions & 15 deletions test/integration-testing/TestAstariaV1Loan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {StarPortLib, Actions} from "starport-core/lib/StarPortLib.sol";
contract TestAstariaV1Loan is AstariaV1Test {
using {StarPortLib.getId} for LoanManager.Loan;

function testNewLoanERC721CollateralDefaultTermsRecall() public {
function testNewLoanERC721CollateralDefaultTermsRecallBase() public {
Custodian custody = Custodian(LM.defaultCustodian());

LoanManager.Terms memory terms = LoanManager.Terms({
Expand Down Expand Up @@ -63,14 +63,12 @@ contract TestAstariaV1Loan is AstariaV1Test {
}
{
// refinance with before recall is initiated
vm.startPrank(refinancer.addr);
vm.expectRevert(Pricing.InvalidRefinance.selector);
LM.refinance(
refinanceLoan(
loan,
abi.encode(BasePricing.Details({rate: (uint256(1e16) * 100) / (365 * 1 days), carryRate: 0})),
refinancerConduit
refinancer.addr,
abi.encodeWithSelector(Pricing.InvalidRefinance.selector)
);
vm.stopPrank();
}
uint256 stake;
{
Expand Down Expand Up @@ -115,27 +113,23 @@ contract TestAstariaV1Loan is AstariaV1Test {
}
{
// refinance with incorrect terms
vm.expectRevert(AstariaV1Pricing.InsufficientRefinance.selector);
vm.startPrank(refinancer.addr);
LM.refinance(
refinanceLoan(
loan,
abi.encode(BasePricing.Details({rate: (uint256(1e16) * 100) / (365 * 1 days), carryRate: 0})),
refinancerConduit
refinancer.addr,
abi.encodeWithSelector(AstariaV1Pricing.InsufficientRefinance.selector)
);
vm.stopPrank();
}
{
// refinance with correct terms
uint256 newLenderBefore = erc20s[0].balanceOf(refinancer.addr);
uint256 oldLenderBefore = erc20s[0].balanceOf(lender.addr);
uint256 recallerBefore = erc20s[0].balanceOf(recaller.addr);
BaseRecall.Details memory details = abi.decode(loan.terms.hookData, (BaseRecall.Details));
vm.startPrank(refinancer.addr);
vm.warp(block.timestamp + (details.recallWindow / 2));
LM.refinance(
loan, abi.encode(BasePricing.Details({rate: details.recallMax / 2, carryRate: 0})), refinancerConduit
refinanceLoan(
loan, abi.encode(BasePricing.Details({rate: details.recallMax / 2, carryRate: 0})), refinancer.addr
);
vm.stopPrank();
uint256 delta_t = block.timestamp - loan.start;
BasePricing.Details memory pricingDetails = abi.decode(loan.terms.pricingData, (BasePricing.Details));
uint256 interest =
Expand Down
52 changes: 0 additions & 52 deletions test/integration-testing/TestNewLoan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,58 +102,6 @@ contract TestNewLoan is StarPortTest {
newLoan(NewLoanData(address(custody), caveats, abi.encode(loanDetails)), Originator(UO), selectedCollateral);
}

function testNewLoanERC721CollateralDefaultTermsRefinance() public {
Custodian custody = Custodian(LM.defaultCustodian());

// pricing = new AstariaV1Pricing(LM);
LoanManager.Terms memory terms = LoanManager.Terms({
hook: address(hook),
handler: address(handler),
pricing: address(pricing),
pricingData: defaultPricingData,
handlerData: defaultHandlerData,
hookData: defaultHookData
});

selectedCollateral.push(
ConsiderationItem({
token: address(erc721s[0]),
startAmount: 1,
endAmount: 1,
identifierOrCriteria: 1,
itemType: ItemType.ERC721,
recipient: payable(address(custody))
})
);

debt.push(SpentItem({itemType: ItemType.ERC20, token: address(erc20s[0]), amount: 100, identifier: 0}));
Originator.Details memory loanDetails = Originator.Details({
conduit: address(lenderConduit),
custodian: address(custody),
issuer: lender.addr,
deadline: block.timestamp + 100,
offer: Originator.Offer({
salt: bytes32(0),
terms: terms,
collateral: ConsiderationItemLib.toSpentItemArray(selectedCollateral),
debt: debt
})
});

LoanManager.Loan memory loan = newLoan(
NewLoanData(address(custody), new LoanManager.Caveat[](0), abi.encode(loanDetails)),
Originator(UO),
selectedCollateral
);
vm.startPrank(refinancer.addr);
LM.refinance(
loan,
abi.encode(BasePricing.Details({rate: (uint256(1e16) * 100) / (365 * 1 days), carryRate: 0})),
refinancerConduit
);
vm.stopPrank();
}

function testNewLoanRefinanceNew() public {
Custodian custody = Custodian(LM.defaultCustodian());

Expand Down
Loading

0 comments on commit c8301f9

Please sign in to comment.