Skip to content

Commit

Permalink
names standardized (#49)
Browse files Browse the repository at this point in the history
* names standardized

* update StarportLib name to proper case

* add snapshot

* rename isValidRefinance => getRefinanceConsideration

* remove unitalized field flag, use only inactve/active flags, update mermaid for rename

* update status, settlement names across the codebase

* update snapshot
  • Loading branch information
androolloyd authored Nov 3, 2023
1 parent 01e1c26 commit b4723b2
Show file tree
Hide file tree
Showing 59 changed files with 1,406 additions and 1,423 deletions.
267 changes: 134 additions & 133 deletions .gas-snapshot

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions gas-bench/loan-open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ERC20__factory } from "../typechain-types/factories/lib/solady/src/toke
describe("Loan Open Benchmarks", function () {
it("", async function () {
// one year in the future
// const lm = await ethers.getContractFactoryFromArtifact("LoanManager.sol");
// const lm = await ethers.getContractFactoryFromArtifact("Starport.sol");
});
});

Expand All @@ -29,7 +29,7 @@ async function setUp() {
consideration.address
);
const uniqueOriginator = await new UniqueOriginator__factory(lender).deploy(
loanManager.address,
Starport.address,
conduitController.address,
strategist.address,
0
Expand Down
2 changes: 1 addition & 1 deletion mermaid/refinance.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sequenceDiagram


F->>LoanManager: refinance
LoanManager->>Pricing: isValidRefinance
LoanManager->>Pricing: getRefinanceConsideration
Pricing->>LoanManager: (SpentItem[] ConsiderPayment, SpentItem[] CarryPayment, AdditionalTransfer[] AT)

LoanManager->>LoanManager: settle loan
Expand Down
8 changes: 4 additions & 4 deletions mermaid/settlement.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ sequenceDiagram

F->>Seaport: fulfillAdvancedOrder/matchAdvancedOrder
Seaport->>Custodian: generateOrder
Custodian->>SettlementHook: isActive
SettlementHook->>Custodian: (true/false)
alt Action is Repayment and SettlementHook is Active
Custodian->>Status: isActive
Status->>Custodian: (true/false)
alt Action is Repayment and Status is Active
Custodian->>Custodian: _beforeApprovalsSetHook
Custodian->>Custodian: _setOfferApprovalsWithSeaport
Custodian->>Pricing: getPaymentConsideration
Pricing->>Custodian: (SpentItem[] payment, SpentItem[] carry)
Custodian->>LoanManager: settle loan
else Action Settlement and Custodian->>SettlementHook: isActive is false
else Action Settlement and Custodian->>Status: isActive is false
Custodian->>Custodian: _beforeGetSettlement
Custodian->>SettlementHandler: getSettlement
SettlementHandler->>Custodian: (ReceivedItem[] consideration, address authorized)
Expand Down
8 changes: 4 additions & 4 deletions src/BNPLHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
ItemType,
Fulfillment
} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {AdditionalTransfer} from "starport-core/lib/StarPortLib.sol";
import {AdditionalTransfer} from "starport-core/lib/StarportLib.sol";

import {Seaport} from "seaport/contracts/Seaport.sol";
import {LoanManager} from "./LoanManager.sol";
import {Starport} from "./Starport.sol";
import {CaveatEnforcer} from "./enforcers/CaveatEnforcer.sol";
import "forge-std/console.sol";

Expand Down Expand Up @@ -60,7 +60,7 @@ contract BNPLHelper is IFlashLoanRecipient {
address borrower;
CaveatEnforcer.CaveatWithApproval borrowerCaveat;
CaveatEnforcer.CaveatWithApproval lenderCaveat;
LoanManager.Loan loan;
Starport.Loan loan;
AdvancedOrder[] orders;
CriteriaResolver[] resolvers;
Fulfillment[] fulfillments;
Expand Down Expand Up @@ -127,7 +127,7 @@ contract BNPLHelper is IFlashLoanRecipient {
++i;
}
}
LoanManager(execution.lm).originate(transfers, execution.borrowerCaveat, execution.lenderCaveat, execution.loan);
Starport(execution.lm).originate(transfers, execution.borrowerCaveat, execution.lenderCaveat, execution.loan);
}

receive() external payable {
Expand Down
104 changes: 53 additions & 51 deletions src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ import {ConsiderationInterface} from "seaport-types/src/interfaces/Consideration
import {ContractOffererInterface} from "seaport-types/src/interfaces/ContractOffererInterface.sol";

import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {SettlementHook} from "starport-core/hooks/SettlementHook.sol";
import {SettlementHandler} from "starport-core/handlers/SettlementHandler.sol";
import {Status} from "starport-core/status/Status.sol";
import {Settlement} from "starport-core/settlement/Settlement.sol";
import {Pricing} from "starport-core/pricing/Pricing.sol";
import {LoanManager} from "starport-core/LoanManager.sol";
import {StarPortLib, Actions} from "starport-core/lib/StarPortLib.sol";
import {Starport} from "starport-core/Starport.sol";
import {StarportLib, Actions} from "starport-core/lib/StarportLib.sol";
import "forge-std/console2.sol";

contract Custodian is ERC721, ContractOffererInterface {
using {StarPortLib.getId} for LoanManager.Loan;
using {StarportLib.getId} for Starport.Loan;

LoanManager public immutable LM;
Starport public immutable SP;
ConsiderationInterface public immutable seaport;

mapping(address => mapping(address => bool)) public repayApproval;
Expand All @@ -55,11 +55,11 @@ contract Custodian is ERC721, ContractOffererInterface {
error InvalidRepayer();
error NotSeaport();
error NotEnteredViaSeaport();
error NotLoanManager();
error NotStarport();

constructor(LoanManager LM_, ConsiderationInterface seaport_) {
constructor(Starport SP_, ConsiderationInterface seaport_) {
seaport = seaport_;
LM = LM_;
SP = SP_;
emit SeaportCompatibleContractDeployed();
}

Expand All @@ -68,7 +68,7 @@ contract Custodian is ERC721, ContractOffererInterface {
* @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
*/
function getBorrower(LoanManager.Loan memory loan) public view returns (address) {
function getBorrower(Starport.Loan memory loan) public view returns (address) {
uint256 loanId = loan.getId();
return _exists(loanId) ? ownerOf(loanId) : loan.borrower;
}
Expand Down Expand Up @@ -121,11 +121,11 @@ contract Custodian is ERC721, ContractOffererInterface {

//MODIFIERS
/**
* @dev only allows LoanManager to execute the function
* @dev only allows Starport to execute the function
*/
modifier onlyLoanManager() {
if (msg.sender != address(LM)) {
revert NotLoanManager();
modifier onlyStarport() {
if (msg.sender != address(SP)) {
revert NotStarport();
}
_;
}
Expand All @@ -146,10 +146,10 @@ contract Custodian is ERC721, ContractOffererInterface {
*
* @param loan The loan to mint a custody token for
*/
function mint(LoanManager.Loan calldata loan) external {
function mint(Starport.Loan calldata loan) external {
bytes memory encodedLoan = abi.encode(loan);
uint256 loanId = uint256(keccak256(encodedLoan));
if (loan.custodian != address(this) || !LM.active(loanId)) {
if (loan.custodian != address(this) || !SP.active(loanId)) {
revert InvalidLoan();
}

Expand Down Expand Up @@ -202,9 +202,12 @@ contract Custodian is ERC721, ContractOffererInterface {
SpentItem[] calldata maximumSpent,
bytes calldata context // encoded based on the schemaID
) external onlySeaport returns (SpentItem[] memory offer, ReceivedItem[] memory consideration) {
(Actions action, LoanManager.Loan memory loan) = abi.decode(context, (Actions, LoanManager.Loan));
(Actions action, Starport.Loan memory loan) = abi.decode(context, (Actions, Starport.Loan));

if (action == Actions.Repayment && SettlementHook(loan.terms.hook).isActive(loan)) {
if (loan.start == block.timestamp) {
revert InvalidLoan();
}
if (action == Actions.Repayment && Status(loan.terms.status).isActive(loan)) {
address borrower = getBorrower(loan);
if (fulfiller != borrower && !repayApproval[borrower][fulfiller]) {
revert InvalidRepayer();
Expand All @@ -217,28 +220,27 @@ contract Custodian is ERC721, ContractOffererInterface {
(SpentItem[] memory payment, SpentItem[] memory carry) =
Pricing(loan.terms.pricing).getPaymentConsideration(loan);

consideration = StarPortLib.mergeSpentItemsToReceivedItems(payment, loan.issuer, carry, loan.originator);
consideration = StarportLib.mergeSpentItemsToReceivedItems(payment, loan.issuer, carry, loan.originator);

_settleLoan(loan);
} else if (action == Actions.Settlement && !SettlementHook(loan.terms.hook).isActive(loan)) {
} else if (action == Actions.Settlement && !Status(loan.terms.status).isActive(loan)) {
address authorized;
//add in originator fee

_beforeGetSettlement(loan);
(consideration, authorized) = SettlementHandler(loan.terms.handler).getSettlement(loan);
consideration = StarPortLib.removeZeroAmountItems(consideration);
(consideration, authorized) = Settlement(loan.terms.settlement).getSettlement(loan);
consideration = StarportLib.removeZeroAmountItems(consideration);
_afterGetSettlement(loan);
if (authorized == address(0) || fulfiller == authorized) {
offer = loan.collateral;
_beforeApprovalsSetHook(fulfiller, maximumSpent, context);
_setOfferApprovalsWithSeaport(offer);
} else if (authorized == loan.terms.handler || authorized == loan.issuer) {
} else if (authorized == loan.terms.settlement || authorized == loan.issuer) {
_moveCollateralToAuthorized(loan.collateral, authorized);
_beforeSettlementHandlerHook(loan);
if (
authorized == loan.terms.handler
&& SettlementHandler(loan.terms.handler).execute(loan, fulfiller)
!= SettlementHandler.execute.selector
authorized == loan.terms.settlement
&& Settlement(loan.terms.settlement).execute(loan, fulfiller) != Settlement.execute.selector
) {
revert InvalidHandlerExecution();
}
Expand All @@ -258,7 +260,7 @@ contract Custodian is ERC721, ContractOffererInterface {
* @param loan The loan that was just placed into custody
* @return selector The function selector of the custody method
*/
function custody(LoanManager.Loan memory loan) external virtual onlyLoanManager returns (bytes4 selector) {
function custody(Starport.Loan memory loan) external virtual onlyStarport returns (bytes4 selector) {
revert ImplementInChild();
}

Expand All @@ -271,7 +273,7 @@ contract Custodian is ERC721, ContractOffererInterface {
function getSeaportMetadata() external pure returns (string memory, Schema[] memory schemas) {
//adhere to sip data, how to encode the context and what it is
//TODO: add in the context for the loan
//you need to parse LM Open events for the loan and abi encode it
//you need to parse SP Open events for the loan and abi encode it
schemas = new Schema[](1);
schemas[0] = Schema(8, "");
return ("Loans", schemas);
Expand All @@ -297,12 +299,12 @@ contract Custodian is ERC721, ContractOffererInterface {
SpentItem[] calldata maximumSpent,
bytes calldata context // encoded based on the schemaID
) public view returns (SpentItem[] memory offer, ReceivedItem[] memory consideration) {
(Actions action, LoanManager.Loan memory loan) = abi.decode(context, (Actions, LoanManager.Loan));
(Actions action, Starport.Loan memory loan) = abi.decode(context, (Actions, Starport.Loan));

if (!LM.active(loan.getId())) {
if (loan.start == block.timestamp || !SP.active(loan.getId())) {
revert InvalidLoan();
}
bool loanActive = SettlementHook(loan.terms.hook).isActive(loan);
bool loanActive = Status(loan.terms.status).isActive(loan);
if (action == Actions.Repayment && loanActive) {
address borrower = getBorrower(loan);
if (fulfiller != borrower && !repayApproval[borrower][fulfiller]) {
Expand All @@ -312,14 +314,14 @@ contract Custodian is ERC721, ContractOffererInterface {

(SpentItem[] memory payment, SpentItem[] memory carry) =
Pricing(loan.terms.pricing).getPaymentConsideration(loan);
consideration = StarPortLib.mergeSpentItemsToReceivedItems(payment, loan.issuer, carry, loan.originator);
consideration = StarportLib.mergeSpentItemsToReceivedItems(payment, loan.issuer, carry, loan.originator);
} else if (action == Actions.Settlement && !loanActive) {
address authorized;
(consideration, authorized) = SettlementHandler(loan.terms.handler).getSettlement(loan);
consideration = StarPortLib.removeZeroAmountItems(consideration);
(consideration, authorized) = Settlement(loan.terms.settlement).getSettlement(loan);
consideration = StarportLib.removeZeroAmountItems(consideration);
if (authorized == address(0) || fulfiller == authorized) {
offer = loan.collateral;
} else if (authorized == loan.terms.handler || authorized == loan.issuer) {} else {
} else if (authorized == loan.terms.settlement || authorized == loan.issuer) {} else {
revert InvalidFulfiller();
}
} else {
Expand Down Expand Up @@ -373,29 +375,29 @@ contract Custodian is ERC721, ContractOffererInterface {
* @dev transfers out the collateral to the handler address
*
* @param offer The item to send out of the Custodian
* @param handler The address handling the asset further
* @param authorized The address handling the asset further
*/

function _transferCollateralToHandler(SpentItem memory offer, address handler) internal {
function _transferCollateralAuthorized(SpentItem memory offer, address authorized) internal {
//approve consideration based on item type
if (offer.itemType == ItemType.ERC721) {
ERC721(offer.token).transferFrom(address(this), handler, offer.identifier);
ERC721(offer.token).transferFrom(address(this), authorized, offer.identifier);
} else if (offer.itemType == ItemType.ERC1155) {
ERC1155(offer.token).safeTransferFrom(address(this), handler, offer.identifier, offer.amount, "");
ERC1155(offer.token).safeTransferFrom(address(this), authorized, offer.identifier, offer.amount, "");
} else if (offer.itemType == ItemType.ERC20) {
ERC20(offer.token).transfer(handler, offer.amount);
ERC20(offer.token).transfer(authorized, offer.amount);
}
}

/**
* @dev transfers out the collateral of SpentItem to the handler address
*
* @param offer The SpentItem array to send out of the Custodian
* @param handler The address handling the asset further
* @param authorized The address handling the asset further
*/
function _moveCollateralToAuthorized(SpentItem[] memory offer, address handler) internal {
function _moveCollateralToAuthorized(SpentItem[] memory offer, address authorized) internal {
for (uint256 i = 0; i < offer.length; i++) {
_transferCollateralToHandler(offer[i], handler);
_transferCollateralAuthorized(offer[i], authorized);
}
}

Expand All @@ -404,13 +406,13 @@ contract Custodian is ERC721, ContractOffererInterface {
*
* @param loan The the loan to settle
*/
function _settleLoan(LoanManager.Loan memory loan) internal virtual {
function _settleLoan(Starport.Loan memory loan) internal virtual {
_beforeSettleLoanHook(loan);
uint256 loanId = loan.getId();
if (_exists(loanId)) {
_burn(loanId);
}
LM.settle(loan);
SP.settle(loan);
_afterSettleLoanHook(loan);
}

Expand All @@ -431,41 +433,41 @@ contract Custodian is ERC721, ContractOffererInterface {
*
* @param loan The loan being settled
*/
function _beforeGetSettlement(LoanManager.Loan memory loan) internal virtual {}
function _beforeGetSettlement(Starport.Loan memory loan) internal virtual {}

/**
* @dev hook to call after the loan get settlement call
*
*
* @param loan The loan being settled
*/
function _afterGetSettlement(LoanManager.Loan memory loan) internal virtual {}
function _afterGetSettlement(Starport.Loan memory loan) internal virtual {}
/**
* @dev hook to call before the the loan settlement handler execute call
*
* @param loan The loan being settled
*/
function _beforeSettlementHandlerHook(LoanManager.Loan memory loan) internal virtual {}
function _beforeSettlementHandlerHook(Starport.Loan memory loan) internal virtual {}

/**
* @dev hook to call after the the loan settlement handler execute call
*
*
* @param loan The loan being settled
*/
function _afterSettlementHandlerHook(LoanManager.Loan memory loan) internal virtual {}
function _afterSettlementHandlerHook(Starport.Loan memory loan) internal virtual {}

/**
* @dev hook to call before the loan is settled with the LM
*
* @param loan The loan being settled
*/
function _beforeSettleLoanHook(LoanManager.Loan memory loan) internal virtual {}
function _beforeSettleLoanHook(Starport.Loan memory loan) internal virtual {}

/**
* @dev hook to call after the loan is settled with the LM
*
* @param loan The loan being settled
*/
function _afterSettleLoanHook(LoanManager.Loan memory loan) internal virtual {}
function _afterSettleLoanHook(Starport.Loan memory loan) internal virtual {}
}
Loading

0 comments on commit b4723b2

Please sign in to comment.