Skip to content

Commit

Permalink
make the newLoan method more dynamic so it can be reused to light up …
Browse files Browse the repository at this point in the history
…more code paths
  • Loading branch information
androolloyd committed Jun 17, 2023
1 parent 41b2e6b commit fe5bd1f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 39 deletions.
12 changes: 0 additions & 12 deletions script/Counter.s.sol

This file was deleted.

15 changes: 5 additions & 10 deletions src/LoanManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ contract LoanManager is ERC721, ContractOffererInterface {
using {StarLiteLib.toReceivedItems} for SpentItem[];

// address public feeRecipient;
bytes32 public immutable KNOWN_CUSTODIAN_CODE_HASH;
address public constant seaport = address(0x2e234DAe75C793f67A35089C9d99245E1C58470b);
address public immutable defaultCustodian;
// uint256 public fee;
// uint256 private constant ONE_WORD = 0x20;

Expand Down Expand Up @@ -99,10 +99,12 @@ contract LoanManager is ERC721, ContractOffererInterface {
}

constructor() {
KNOWN_CUSTODIAN_CODE_HASH = keccak256(type(Custodian).creationCode);
defaultCustodian = address(new Custodian(this, seaport));
emit SeaportCompatibleContractDeployed();
}

event log(bytes32);

function name() public pure override returns (string memory) {
return "Astaria Loan Manager";
}
Expand Down Expand Up @@ -161,16 +163,9 @@ contract LoanManager is ERC721, ContractOffererInterface {
assembly {
custodian := calldataload(add(data.offset, 0x20)) // 0x20 offset for the first address 'custodian'
}

bytes32 codeHash;

assembly {
codeHash := extcodehash(custodian)
}

// Comparing the retrieved code hash with a known hash (placeholder here)

if (codeHash != KNOWN_CUSTODIAN_CODE_HASH) {
if (custodian != defaultCustodian) {
bytes4 functionSelector = bytes4(keccak256("custody(bytes)"));
bytes memory callData = abi.encodeWithSelector(functionSelector, data);

Expand Down
5 changes: 5 additions & 0 deletions src/handlers/DutchAuctionHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ contract DutchAuctionHandler is SettlementHandler, AmountDeriver {
recipient: payable(LM.ownerOf(uint256(keccak256(abi.encode(loan)))))
});
}

function validate(LoanManager.Loan calldata loan) external view override returns (bool) {
Details memory details = abi.decode(loan.terms.handlerData, (Details));
return details.startingPrice > details.endingPrice;
}
}
15 changes: 8 additions & 7 deletions src/handlers/EnglishAuctionHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {Pricing} from "src/pricing/Pricing.sol";

contract EnglishAuctionHandler is SettlementHandler {
struct Details {
uint256 reservePrice;
uint256[] reservePrice; // per debt item
uint256 window;
}

Expand All @@ -41,6 +41,12 @@ contract EnglishAuctionHandler is SettlementHandler {
consideration = consideration_;
}

//use when building offers to ensure the data works with the handler
function validate(LoanManager.Loan calldata loan) external view override returns (bool) {
Details memory details = abi.decode(loan.terms.handlerData, (Details));
return details.reservePrice.length == loan.debt.length;
}

function execute(LoanManager.Loan calldata loan) external virtual override returns (bytes4 selector) {
selector = SettlementHandler.execute.selector;
}
Expand Down Expand Up @@ -80,11 +86,6 @@ contract EnglishAuctionHandler is SettlementHandler {
});

Details memory details = abi.decode(loan.terms.handlerData, (Details));
uint256[] memory owing = Pricing(loan.terms.pricing).getOwed(loan);

if (owing[0] < details.reservePrice) {
owing[0] = details.reservePrice;
}
//check the loan debt type and set the order type based on that

OfferItem[] memory offerItems = new OfferItem[](loan.collateral.length);
Expand All @@ -110,7 +111,7 @@ contract EnglishAuctionHandler is SettlementHandler {
itemType: loan.debt[i].itemType,
token: loan.debt[i].token,
identifierOrCriteria: loan.debt[i].identifier,
startAmount: loan.debt[i].amount,
startAmount: details.reservePrice[i],
endAmount: loan.debt[i].amount,
recipient: payable(LM.ownerOf(uint256(keccak256(abi.encode(loan)))))
});
Expand Down
4 changes: 4 additions & 0 deletions src/handlers/LenderRestrictedHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ contract LenderRestrictedHandler is SettlementHandler {
{
return (new ReceivedItem[](0), LM.ownerOf(uint256(keccak256(abi.encode(loan)))));
}

function validate(LoanManager.Loan calldata loan) external view override returns (bool) {
return true;
}
}
2 changes: 2 additions & 0 deletions src/handlers/SettlementHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ abstract contract SettlementHandler {
return SettlementHandler.execute.selector;
}

function validate(LoanManager.Loan calldata loan) external view virtual returns (bool);

function getSettlement(LoanManager.Loan memory loan, SpentItem[] calldata maximumSpent)
external
view
Expand Down
23 changes: 13 additions & 10 deletions test/TestStarLite.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ contract TestStarLite is BaseOrderTest {
bytes memory pricingData =
abi.encode(FixedTermPricing.Details({rate: uint256((uint256(1e16) / 365) * 1 days), loanDuration: 10 days}));

address custody = LM.defaultCustodian();
bytes memory handlerData = abi.encode(
DutchAuctionHandler.Details({startingPrice: uint256(500 ether), endingPrice: 100 wei, window: 7 days})
);
Expand All @@ -142,14 +143,14 @@ contract TestStarLite is BaseOrderTest {

UniqueOriginator.Details memory loanDetails = UniqueOriginator.Details({
conduit: address(lenderConduit),
custodian: address(custodian),
custodian: address(custody),
issuer: lender.addr,
deadline: block.timestamp + 100,
terms: terms,
collateral: ConsiderationItemLib.toSpentItemArray(collateral721),
debt: debt
});
bool isTrusted = false;
bool isTrusted = true;

collateral721.push(
ConsiderationItem({
Expand All @@ -158,7 +159,7 @@ contract TestStarLite is BaseOrderTest {
endAmount: 1,
identifierOrCriteria: 1,
itemType: ItemType.ERC721,
recipient: payable(address(custodian))
recipient: payable(address(custody))
})
);

Expand All @@ -169,12 +170,12 @@ contract TestStarLite is BaseOrderTest {
endAmount: 100,
identifierOrCriteria: 0,
itemType: ItemType.ERC20,
recipient: payable(address(custodian))
recipient: payable(address(custody))
})
);
debt.push(SpentItem({itemType: ItemType.ERC20, token: address(erc20s[0]), amount: 100, identifier: 0}));
LoanManager.Loan memory activeLoan = newLoan(
NewLoanData(isTrusted, abi.encode(loanDetails)),
NewLoanData(custody, isTrusted, abi.encode(loanDetails)),
ExternalCall(address(pricing), pricingData),
ExternalCall(address(handler), handlerData),
ExternalCall(address(hook), hookData)
Expand All @@ -191,8 +192,9 @@ contract TestStarLite is BaseOrderTest {
bytes memory pricingData =
abi.encode(FixedTermPricing.Details({rate: uint256((uint256(1e16) / 365) * 1 days), loanDuration: 10 days}));

bytes memory handlerData =
abi.encode(EnglishAuctionHandler.Details({reservePrice: uint256(1 ether), window: 7 days}));
uint256[] memory reserve = new uint256[](1);
reserve[0] = 1 ether;
bytes memory handlerData = abi.encode(EnglishAuctionHandler.Details({reservePrice: reserve, window: 7 days}));
bytes memory hookData = abi.encode(FixedTermHook.Details({loanDuration: 10 days}));

LoanManager.Terms memory terms = LoanManager.Terms({
Expand Down Expand Up @@ -229,7 +231,7 @@ contract TestStarLite is BaseOrderTest {
bool isTrusted = false;

LoanManager.Loan memory activeLoan = newLoan(
NewLoanData(isTrusted, abi.encode(loanDetails)),
NewLoanData(address(custodian), isTrusted, abi.encode(loanDetails)),
ExternalCall(address(pricing), pricingData),
ExternalCall(address(handler), handlerData),
ExternalCall(address(hook), hookData)
Expand All @@ -253,6 +255,7 @@ contract TestStarLite is BaseOrderTest {
}

struct NewLoanData {
address custodian;
bool isTrusted;
bytes details;
}
Expand All @@ -270,7 +273,7 @@ contract TestStarLite is BaseOrderTest {
);

LoanManager.Loan memory loan = LoanManager.Loan({
custodian: address(custodian),
custodian: address(loanData.custodian),
issuer: address(0),
borrower: borrower.addr,
originator: isTrusted ? address(UO) : address(0),
Expand All @@ -283,7 +286,7 @@ contract TestStarLite is BaseOrderTest {
loan,
LoanManager.Obligation({
isTrusted: isTrusted,
custodian: address(custodian),
custodian: address(loanData.custodian),
borrower: borrower.addr,
debt: debt,
details: loanData.details,
Expand Down

0 comments on commit fe5bd1f

Please sign in to comment.