Skip to content

Commit

Permalink
merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiagoGregory committed Sep 15, 2023
2 parents 6f66312 + c288281 commit 1a6b45f
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 52 deletions.
4 changes: 3 additions & 1 deletion src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import {Pricing} from "src/pricing/Pricing.sol";
import {LoanManager} from "src/LoanManager.sol";
import "forge-std/console.sol";
import {ConduitHelper} from "src/ConduitHelper.sol";
import {StarPortLib} from "src/lib/StarPortLib.sol";

contract Custodian is
ContractOffererInterface,
TokenReceiverInterface,
ConduitHelper
{
using {StarPortLib.getId} for LoanManager.Loan;
LoanManager public immutable LM;
address public immutable seaport;
event SeaportCompatibleContractDeployed();
Expand Down Expand Up @@ -80,7 +82,7 @@ contract Custodian is
// we burn the loan on repayment in generateOrder, but in ratify order where we would trigger any post settlement actions
// we burn it here so that in the case it was minted and an owner is set for settlement their pointer can still be utilized
// in this case we are not a repayment we have burnt the loan in the generate order for a repayment
uint256 loanId = LM.getLoanIdFromLoan(loan);
uint256 loanId = loan.getId();
if (LM.active(loanId)) {
if (
SettlementHandler(loan.terms.handler).execute(loan) !=
Expand Down
15 changes: 8 additions & 7 deletions src/LoanManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {ConduitHelper} from "src/ConduitHelper.sol";
contract LoanManager is ERC721, ContractOffererInterface, ConduitHelper {
using FixedPointMathLib for uint256;
using {StarPortLib.toReceivedItems} for SpentItem[];
using {StarPortLib.getId} for LoanManager.Loan;

ConsiderationInterface public constant seaport =
ConsiderationInterface(0x2e234DAe75C793f67A35089C9d99245E1C58470b);
Expand Down Expand Up @@ -256,12 +257,8 @@ contract LoanManager is ERC721, ContractOffererInterface, ConduitHelper {
_settle(loan);
}

function getLoanIdFromLoan(Loan memory loan) public pure returns (uint256) {
return uint256(keccak256(abi.encode(loan)));
}

function _settle(Loan memory loan) internal {
uint256 tokenId = getLoanIdFromLoan(loan);
uint256 tokenId = loan.getId();
if (!_issued(tokenId)) {
revert InvalidLoan(tokenId);
}
Expand Down Expand Up @@ -414,7 +411,7 @@ contract LoanManager is ERC721, ContractOffererInterface, ConduitHelper {
function _issueLoanManager(Loan memory loan, bool mint) internal {
bytes memory encodedLoan = abi.encode(loan);

uint256 loanId = uint256(keccak256(encodedLoan));
uint256 loanId = loan.getId();

_setExtraData(loanId, uint8(FieldFlags.ACTIVE));
if (mint) {
Expand Down Expand Up @@ -564,7 +561,11 @@ contract LoanManager is ERC721, ContractOffererInterface, ConduitHelper {

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

ReceivedItem[] memory refinanceConsideration = _mergeConsiderations(
considerationPayment,
Expand Down
7 changes: 4 additions & 3 deletions src/handlers/AstariaV1SettlementHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import {
import {BaseHook} from "src/hooks/BaseHook.sol";
import {BaseRecall} from "src/hooks/BaseRecall.sol";
import {DutchAuctionHandler} from "src/handlers/DutchAuctionHandler.sol";
import {StarPortLib} from "src/lib/StarPortLib.sol";

contract AstariaV1SettlementHandler is DutchAuctionHandler {
using {StarPortLib.getId} for LoanManager.Loan;

constructor(LoanManager LM_) DutchAuctionHandler(LM_) {}

function getSettlement(
Expand All @@ -22,9 +25,7 @@ contract AstariaV1SettlementHandler is DutchAuctionHandler {
override
returns (ReceivedItem[] memory, address restricted)
{
(address recaller, ) = BaseRecall(loan.terms.hook).recalls(
LM.getLoanIdFromLoan(loan)
);
(address recaller, ) = BaseRecall(loan.terms.hook).recalls(loan.getId());

if (recaller == loan.issuer) {
return (new ReceivedItem[](0), recaller);
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/AstariaV1SettlementHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import {LoanManager} from "src/LoanManager.sol";
import {BaseRecall} from "src/hooks/BaseRecall.sol";
import "forge-std/console2.sol";
import {BaseHook} from "src/hooks/BaseHook.sol";
import {StarPortLib} from "src/lib/StarPortLib.sol";

contract AstariaV1SettlementHook is BaseHook, BaseRecall {
using {StarPortLib.getId} for LoanManager.Loan;

constructor(LoanManager LM_) BaseRecall(LM_) {}

function isActive(
LoanManager.Loan calldata loan
) external view override returns (bool) {
Details memory details = abi.decode(loan.terms.hookData, (Details));
uint256 tokenId = LM.getLoanIdFromLoan(loan);
uint256 tokenId = loan.getId();
return
!(uint256(recalls[tokenId].start) + details.recallWindow >
block.timestamp);
Expand All @@ -22,7 +25,7 @@ contract AstariaV1SettlementHook is BaseHook, BaseRecall {
LoanManager.Loan calldata loan
) external view override returns (bool) {
Details memory details = abi.decode(loan.terms.hookData, (Details));
uint256 tokenId = LM.getLoanIdFromLoan(loan);
uint256 tokenId = loan.getId();
Recall memory recall = recalls[tokenId];
return
(recall.start + details.recallWindow > block.timestamp) &&
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/BaseRecall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import {
} from "seaport-types/src/interfaces/ConduitInterface.sol";

import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {StarPortLib} from "src/lib/StarPortLib.sol";

abstract contract BaseRecall is ConduitHelper {
using FixedPointMathLib for uint256;

using {StarPortLib.getId} for LoanManager.Loan;
event Recalled(uint256 loandId, address recaller, uint256 end);
event Withdraw(uint256 loanId, address withdrawer);
LoanManager LM;
Expand Down Expand Up @@ -69,7 +70,7 @@ abstract contract BaseRecall is ConduitHelper {
LoanManager.Loan calldata loan
) external view returns (uint256) {
Details memory details = abi.decode(loan.terms.hookData, (Details));
uint256 loanId = LM.getLoanIdFromLoan(loan);
uint256 loanId = loan.getId();
// calculates the porportion of time elapsed, then multiplies times the max rate
return
details.recallMax.mulWad(
Expand Down
9 changes: 6 additions & 3 deletions src/pricing/AstariaV1Pricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ import {AstariaV1SettlementHook} from "src/hooks/AstariaV1SettlementHook.sol";

import {BaseRecall} from "src/hooks/BaseRecall.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {StarPortLib} from "src/lib/StarPortLib.sol";

contract AstariaV1Pricing is CompoundInterestPricing {
using FixedPointMathLib for uint256;
using {StarPortLib.getId} for LoanManager.Loan;

constructor(LoanManager LM_) Pricing(LM_) {}

error InsufficientRefinance();

function isValidRefinance(
LoanManager.Loan memory loan,
bytes memory newPricingData
bytes memory newPricingData,
address caller
)
external
view
Expand All @@ -31,7 +34,7 @@ contract AstariaV1Pricing is CompoundInterestPricing {
)
{
// borrowers can refinance a loan at any time
if (msg.sender != loan.borrower) {
if (caller != loan.borrower) {
// check if a recall is occuring
AstariaV1SettlementHook hook = AstariaV1SettlementHook(loan.terms.hook);
Details memory newDetails = abi.decode(newPricingData, (Details));
Expand All @@ -46,7 +49,7 @@ contract AstariaV1Pricing is CompoundInterestPricing {

uint256 proportion;
address payable receiver = payable(loan.issuer);
uint256 loanId = LM.getLoanIdFromLoan(loan);
uint256 loanId = loan.getId();
// scenario where the recaller is not penalized
// recaller stake is refunded
if (newDetails.rate > oldDetails.rate) {
Expand Down
2 changes: 1 addition & 1 deletion src/pricing/BasePricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {StarPortLib} from "src/lib/StarPortLib.sol";

abstract contract BasePricing is Pricing {
using FixedPointMathLib for uint256;
using StarPortLib for LoanManager.Loan;
using {StarPortLib.getId} for LoanManager.Loan;
struct Details {
uint256 rate;
uint256 carryRate;
Expand Down
3 changes: 2 additions & 1 deletion src/pricing/BaseRecallPricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {StarPortLib} from "src/lib/StarPortLib.sol";
abstract contract BaseRecallPricing is BasePricing {
function isValidRefinance(
LoanManager.Loan memory loan,
bytes memory newPricingData
bytes memory newPricingData,
address caller
)
external
view
Expand Down
11 changes: 8 additions & 3 deletions src/pricing/Pricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "seaport/lib/seaport-sol/src/lib/ReceivedItemLib.sol";
abstract contract Pricing {
LoanManager LM;
error InvalidRefinance();

constructor(LoanManager LM_) {
LM = LM_;
}
Expand All @@ -19,10 +19,15 @@ abstract contract Pricing {

function isValidRefinance(
LoanManager.Loan memory loan,
bytes memory newPricingData
bytes memory newPricingData,
address caller
)
external
view
virtual
returns (ReceivedItem[] memory, ReceivedItem[] memory, ReceivedItem[] memory);
returns (
ReceivedItem[] memory,
ReceivedItem[] memory,
ReceivedItem[] memory
);
}
3 changes: 2 additions & 1 deletion src/pricing/SimpleInterestPricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ contract SimpleInterestPricing is BasePricing {

function isValidRefinance(
LoanManager.Loan memory loan,
bytes memory newPricingData
bytes memory newPricingData,
address caller
)
external
view
Expand Down
Loading

0 comments on commit 1a6b45f

Please sign in to comment.