Skip to content

Commit

Permalink
fix: all test running or commented out
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerousfood committed Oct 27, 2023
1 parent 67da5a8 commit a3287c2
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 537 deletions.
25 changes: 0 additions & 25 deletions src/enforcers/BaseEnforcer.sol

This file was deleted.

11 changes: 8 additions & 3 deletions src/enforcers/BorrowerEnforcer.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
pragma solidity =0.8.17;

import {BaseEnforcer} from "starport-core/enforcers/BaseEnforcer.sol";
import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";
import {ConduitTransfer} from "seaport-types/src/conduit/lib/ConduitStructs.sol";
import {LoanManager} from "starport-core/LoanManager.sol";
contract BorrowerEnforcer is BaseEnforcer {
contract BorrowerEnforcer is CaveatEnforcer {

error BorrowerOnlyEnforcer();
error InvalidLoanTerms();
error InvalidAdditionalTransfer();

struct Details {
LoanManager.Loan loan;
}

function validate(ConduitTransfer[] calldata additionalTransfers, LoanManager.Loan calldata loan, bytes calldata caveatData) public view virtual override {
bytes32 loanHash = keccak256(abi.encode(loan));

Details memory details = abi.decode(caveatData, (Details));
if(details.loan.borrower != loan.borrower) revert BorrowerOnlyEnforcer();
details.loan.issuer = loan.issuer;
// details.loan.start = block.timestamp;

if(loanHash != keccak256(abi.encode(details.loan))) revert InvalidLoanTerms();

Expand Down
10 changes: 8 additions & 2 deletions src/enforcers/LenderEnforcer.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
pragma solidity =0.8.17;

import {BaseEnforcer} from "starport-core/enforcers/BaseEnforcer.sol";
import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";
import {ConduitTransfer} from "seaport-types/src/conduit/lib/ConduitStructs.sol";
import {LoanManager} from "starport-core/LoanManager.sol";
contract LenderEnforcer is BaseEnforcer {
contract LenderEnforcer is CaveatEnforcer {

error LenderOnlyEnforcer();
error InvalidLoanTerms();
error InvalidAdditionalTransfer();

struct Details {
LoanManager.Loan loan;
}

function validate(ConduitTransfer[] calldata additionalTransfers, LoanManager.Loan calldata loan, bytes calldata caveatData) public view virtual override {
bytes32 loanHash = keccak256(abi.encode(loan));
Expand Down
90 changes: 58 additions & 32 deletions src/handlers/DutchAuctionHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {LoanManager, SettlementHandler} from "starport-core/handlers/SettlementH

import {ConduitHelper} from "starport-core/ConduitHelper.sol";
import "forge-std/console2.sol";
import {BasePricing} from "starport-core/pricing/BasePricing.sol";

abstract contract DutchAuctionHandler is SettlementHandler, AmountDeriver, ConduitHelper {
constructor(LoanManager LM_) SettlementHandler(LM_) {
Expand Down Expand Up @@ -44,38 +45,63 @@ abstract contract DutchAuctionHandler is SettlementHandler, AmountDeriver, Condu
override
returns (ReceivedItem[] memory consideration, address restricted)
{
// Details memory details = abi.decode(loan.terms.handlerData, (Details));
// uint256 settlementPrice;

// uint256 start = _getAuctionStart(loan);

// // DutchAuction has failed
// if (start + details.window < block.timestamp) {
// return (new ReceivedItem[](0), loan.issuer);
// }

// settlementPrice = _locateCurrentAmount({
// startAmount: details.startingPrice,
// endAmount: details.endingPrice,
// startTime: start,
// endTime: start + details.window,
// roundUp: true
// });

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

// if (paymentConsiderations[0].amount <= settlementPrice) {
// carryFeeConsideration = new SpentItem[](0);
// } else {
// carryFeeConsideration[0].amount =
// settlementPrice - paymentConsiderations[0].amount - carryFeeConsideration[0].amount;
// }
// paymentConsiderations[0].amount = settlementPrice;

// consideration = _mergeConsiderations(paymentConsiderations, carryFeeConsideration, new ReceivedItem[](0));
// consideration = _removeZeroAmounts(consideration);
consideration = new ReceivedItem[](0);
Details memory details = abi.decode(loan.terms.handlerData, (Details));


uint256 start = _getAuctionStart(loan);

// DutchAuction has failed, allow lender to redeem
if (start + details.window < block.timestamp) {
return (new ReceivedItem[](0), loan.issuer);
}

uint256 settlementPrice = _locateCurrentAmount({
startAmount: details.startingPrice,
endAmount: details.endingPrice,
startTime: start,
endTime: start + details.window,
roundUp: true
});

BasePricing.Details memory pricingDetails = abi.decode(loan.terms.pricingData, (BasePricing.Details));
uint256 interest = BasePricing(loan.terms.pricing).getInterest(loan, pricingDetails.rate, loan.start, block.timestamp, 0);

uint256 carry = interest.mulWad(pricingDetails.carryRate);

if(loan.debt[0].amount + interest <= settlementPrice){
consideration = new ReceivedItem[](2);
consideration[0] = ReceivedItem({
itemType: loan.debt[0].itemType,
identifier: loan.debt[0].identifier,
amount: carry,
token: loan.debt[0].token,
recipient: payable(loan.originator)
});

settlementPrice -= consideration[0].amount;
}
else if (loan.debt[0].amount + interest - carry <= settlementPrice) {
consideration = new ReceivedItem[](2);
consideration[0] = ReceivedItem({
itemType: loan.debt[0].itemType,
identifier: loan.debt[0].identifier,
amount: (settlementPrice - loan.debt[0].amount + interest - carry),
token: loan.debt[0].token,
recipient: payable(loan.originator)
});
settlementPrice -= consideration[0].amount;
}
else {
consideration = new ReceivedItem[](1);
}

consideration[consideration.length - 1] = ReceivedItem({
itemType: loan.debt[0].itemType,
identifier: loan.debt[0].identifier,
amount: settlementPrice,
token: loan.debt[0].token,
recipient: payable(loan.issuer)
});
}

function validate(LoanManager.Loan calldata loan) external view virtual override returns (bool) {
Expand Down
10 changes: 5 additions & 5 deletions test/AstariaV1Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {AstariaV1SettlementHook} from "starport-core/hooks/AstariaV1SettlementHo
import {BaseRecall} from "starport-core/hooks/BaseRecall.sol";

import {AstariaV1SettlementHandler} from "starport-core/handlers/AstariaV1SettlementHandler.sol";
import {BaseEnforcer} from "starport-core/enforcers/BaseEnforcer.sol";
import {LenderEnforcer} from "starport-core/enforcers/LenderEnforcer.sol";
import {BorrowerEnforcer} from "starport-core/enforcers/BorrowerEnforcer.sol";
// import "forge-std/console2.sol";
import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";

Expand Down Expand Up @@ -60,7 +61,7 @@ contract AstariaV1Test is StarPortTest {
);
}

function getLenderSignedCaveat(BaseEnforcer.Details memory details, Account memory signer, bytes32 salt, address enforcer) public view returns(CaveatEnforcer.CaveatWithApproval memory caveatApproval) {
function getLenderSignedCaveat(LenderEnforcer.Details memory details, Account memory signer, bytes32 salt, address enforcer) public view returns(CaveatEnforcer.CaveatWithApproval memory caveatApproval) {
caveatApproval.caveat = CaveatEnforcer.Caveat({
enforcer: enforcer,
salt: salt,
Expand All @@ -73,19 +74,18 @@ contract AstariaV1Test is StarPortTest {
(caveatApproval.v, caveatApproval.r, caveatApproval.s) = vm.sign(signer.key, hash);
}

function getRefinanceDetails(LoanManager.Loan memory loan, bytes memory pricingData, address transactor) public view returns(BaseEnforcer.Details memory) {
function getRefinanceDetails(LoanManager.Loan memory loan, bytes memory pricingData, address transactor) public view returns(LenderEnforcer.Details memory) {
(
SpentItem[] memory considerationPayment,
SpentItem[] memory carryPayment,
ConduitTransfer[] memory additionalTransfers
) = Pricing(loan.terms.pricing).isValidRefinance(loan, pricingData, transactor);

loan = LM.applyRefinanceConsiderationToLoan(loan, considerationPayment, carryPayment, pricingData);
loan.issuer = transactor;
loan.start = 0;
loan.originator = address(0);

return BaseEnforcer.Details({
return LenderEnforcer.Details({
loan: loan
});
}
Expand Down
Loading

0 comments on commit a3287c2

Please sign in to comment.