Skip to content

Commit

Permalink
fix: custodian tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerousfood committed Oct 27, 2023
1 parent 956b298 commit 67da5a8
Show file tree
Hide file tree
Showing 20 changed files with 1,543 additions and 1,490 deletions.
7 changes: 4 additions & 3 deletions src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,10 @@ contract Custodian is ERC721, ContractOffererInterface, ConduitHelper {
* revert with NotEnteredViaSeaport()
*/
function onERC1155Received(address, address, uint256, uint256, bytes calldata) public virtual returns (bytes4) {
try seaport.incrementCounter() {
revert NotEnteredViaSeaport();
} catch {}
// commenting out because, we are not entering this flow via Seaport after teh new origiantion changes
// try seaport.incrementCounter() {
// revert NotEnteredViaSeaport();
// } catch {}
return this.onERC1155Received.selector;
}

Expand Down
21 changes: 0 additions & 21 deletions src/Enforcer.sol

This file was deleted.

59 changes: 22 additions & 37 deletions src/LoanManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ import {SignatureCheckerLib} from "solady/src/utils/SignatureCheckerLib.sol";
import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";
import {Ownable} from "solady/src/auth/Ownable.sol";
import {ConduitHelper} from "starport-core/ConduitHelper.sol";
import {Enforcer} from "starport-core/Enforcer.sol";
import "forge-std/console.sol";
import "forge-std/console2.sol";

interface LoanSettledCallback {
function onLoanSettled(LoanManager.Loan calldata loan) external;
Expand All @@ -69,7 +68,7 @@ contract LoanManager is ConduitHelper, Ownable, ERC721 {
bytes32 public constant EIP_DOMAIN =
keccak256("EIP712Domain(string version,uint256 chainId,address verifyingContract)");
bytes32 public constant INTENT_ORIGINATION_TYPEHASH =
keccak256("IntentOrigination(bytes32 hash,bytes32 salt,uint256 nonce)");
keccak256("Origination(bytes32 hash,address enforcer,bytes32 salt,uint256 nonce,uint256 deadline,bytes data)");
bytes32 public constant VERSION = keccak256("0");
address public feeTo;
uint96 public defaultFeeRake;
Expand Down Expand Up @@ -173,40 +172,40 @@ contract LoanManager is ConduitHelper, Ownable, ERC721 {

mapping(address => mapping(bytes32 => bool)) invalidHashes;
mapping(address => mapping(address => bool)) approvals;
mapping(address => uint256) caveatNonces;

function originate(
ConduitTransfer[] calldata additionalTransfers,
Enforcer.Caveat calldata borrowerCaveat,
Enforcer.Caveat calldata lenderCaveat,
LoanManager.Loan memory loan) external payable returns (LoanManager.Loan memory){
CaveatEnforcer.CaveatWithApproval calldata borrowerCaveat,
CaveatEnforcer.CaveatWithApproval calldata lenderCaveat,
LoanManager.Loan memory loan) external payable {

if(msg.sender != loan.borrower){
_validateAndEnforceCaveats(borrowerCaveat, loan.borrower, additionalTransfers, loan);
}

if(msg.sender != loan.issuer && !approvals[loan.issuer][msg.sender]){
_validateAndEnforceCaveats(lenderCaveat, loan.issuer, additionalTransfers, loan);
_validateAndEnforceCaveats(lenderCaveat, loan.issuer, additionalTransfers, loan);
}

_transferSpentItems(loan.debt, loan.issuer, loan.borrower);
_transferSpentItems(loan.collateral, loan.borrower, loan.custodian);


if(additionalTransfers.length > 0){
_validateAdditionalTransfers(loan.borrower, loan.issuer, msg.sender, additionalTransfers);
_transferConduitTransfers(additionalTransfers);
_validateAdditionalTransfers(loan.borrower, loan.issuer, msg.sender, additionalTransfers);
_transferConduitTransfers(additionalTransfers);
}

loan.start = block.timestamp;
loan.originator = msg.sender;
//mint LM
_issueLoanManager(loan, true);
return loan;
}

function refinance(
address lender,
Enforcer.Caveat calldata lenderCaveat,
CaveatEnforcer.CaveatWithApproval calldata lenderCaveat,
LoanManager.Loan memory loan,
bytes memory pricingData
) external
Expand Down Expand Up @@ -278,23 +277,17 @@ contract LoanManager is ConduitHelper, Ownable, ERC721 {
}
}
}
function _validateAndEnforceCaveats(Enforcer.Caveat memory caveat, address validator, ConduitTransfer[] memory additionalTransfers, LoanManager.Loan memory loan) internal {
bytes32 salt = caveat.salt;
if(salt != bytes32(0)){
if(invalidHashes[validator][salt]){
revert HashAlreadyInvalidated();
}
else{
if(salt != bytes32(0)) invalidHashes[validator][salt] = true;
}
}
function _validateAndEnforceCaveats(CaveatEnforcer.CaveatWithApproval memory caveatApproval, address validator, ConduitTransfer[] memory additionalTransfers, LoanManager.Loan memory loan) internal {
invalidHashes.validateSalt(validator, caveatApproval.caveat.salt);

bytes32 hash = hashCaveatWithSaltAndNonce(validator, caveatApproval.caveat);
address signer = ecrecover(hash, caveatApproval.v, caveatApproval.r, caveatApproval.s);

bytes32 hash = keccak256(abi.encode(caveat.enforcer, caveat.caveat, salt));
address signer = ecrecover(hash, caveat.approval.v, caveat.approval.r, caveat.approval.s);
console2.log("bark", validator, msg.sender);
if(signer != validator) revert InvalidCaveatSigner();

// will revert if invalid
Enforcer(caveat.enforcer).validate(additionalTransfers, loan, caveat.caveat);
CaveatEnforcer(caveatApproval.caveat.enforcer).validate(additionalTransfers, loan, caveatApproval.caveat.data);
}

function _transferConduitTransfers(ConduitTransfer[] memory transfers) internal {
Expand Down Expand Up @@ -346,26 +339,18 @@ contract LoanManager is ConduitHelper, Ownable, ERC721 {
}
}

/**
* @dev previews the order for this contract offerer.
*
* @param borrower The address of the borrower
* @param salt The salt of the borrower's obligation
* @param caveatHash The hash of the abi.encoded obligation caveats
* @return The abi encode packed bytes that include the intent typehash with the salt and nonce and caveatHash
*/
function encodeWithSaltAndBorrowerCounter(address borrower, bytes32 salt, bytes32 caveatHash)
function hashCaveatWithSaltAndNonce(address validator, CaveatEnforcer.Caveat memory caveat)
public
view
virtual
returns (bytes memory)
returns (bytes32)
{
return abi.encodePacked(
return keccak256(abi.encodePacked(
bytes1(0x19),
bytes1(0x01),
_DOMAIN_SEPARATOR,
keccak256(abi.encode(INTENT_ORIGINATION_TYPEHASH, salt, borrowerNonce[borrower], caveatHash))
);
keccak256(abi.encode(INTENT_ORIGINATION_TYPEHASH, caveat.enforcer, caveatNonces[validator], caveat.salt, caveat.deadline, caveat.data))
));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/BaseEnforcer.sol → src/enforcers/BaseEnforcer.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pragma solidity =0.8.17;

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

error InvalidLoanTerms();
error InvalidAdditionalTransfer();
Expand All @@ -12,10 +12,10 @@ contract BaseEnforcer is Enforcer{
LoanManager.Loan loan;
}

function validate(ConduitTransfer[] calldata additionalTransfers, LoanManager.Loan calldata loan, bytes calldata caveat) public view virtual override {
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(caveat, (Details));
Details memory details = abi.decode(caveatData, (Details));
details.loan.start = block.timestamp;

if(loanHash != keccak256(abi.encode(details.loan))) revert InvalidLoanTerms();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
pragma solidity =0.8.17;

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

error BorrowerOnlyEnforcer();

function validate(ConduitTransfer[] calldata additionalTransfers, LoanManager.Loan calldata loan, bytes calldata caveat) public view virtual override {
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(caveat, (Details));
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;
Expand Down
23 changes: 19 additions & 4 deletions src/enforcers/CaveatEnforcer.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
pragma solidity =0.8.17;

// import {ReceivedItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {ConduitTransfer} from "seaport-types/src/conduit/lib/ConduitStructs.sol";
import {LoanManager} from "starport-core/LoanManager.sol";

abstract contract CaveatEnforcer {
function enforceCaveat(bytes calldata terms, LoanManager.Loan memory loan) public virtual returns (bool);
}

struct Caveat {
address enforcer;
bytes32 salt;
uint256 deadline;
bytes data;
}

struct CaveatWithApproval {
uint8 v;
bytes32 r;
bytes32 s;
Caveat caveat;
}

function validate(ConduitTransfer[] calldata solution, LoanManager.Loan calldata loan, bytes calldata caveatData) public view virtual;
}
25 changes: 0 additions & 25 deletions src/enforcers/CollateralEnforcer.sol

This file was deleted.

6 changes: 3 additions & 3 deletions src/LenderEnforcer.sol → src/enforcers/LenderEnforcer.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
pragma solidity =0.8.17;

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

error LenderOnlyEnforcer();

function validate(ConduitTransfer[] calldata additionalTransfers, LoanManager.Loan calldata loan, bytes calldata caveat) public view virtual override {
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(caveat, (Details));
Details memory details = abi.decode(caveatData, (Details));
if(details.loan.issuer != loan.issuer) revert LenderOnlyEnforcer();
details.loan.borrower = loan.borrower;

Expand Down
23 changes: 0 additions & 23 deletions src/enforcers/RateEnforcer.sol

This file was deleted.

34 changes: 0 additions & 34 deletions src/enforcers/TermEnforcer.sol

This file was deleted.

Loading

0 comments on commit 67da5a8

Please sign in to comment.