Skip to content

Commit

Permalink
Remove ERC721 from Starport, caveat naming/deadline refactor, lots of…
Browse files Browse the repository at this point in the history
… cleanup (#63)

* natspec and some import cleanup, removing unsued modules

* remove repayApproval mapping as its not used, update previewOrder to fetch ERC721 approval
update fuzz testing to be more dynamic

* coverage boost, dead code cleanup

* fixed up the non default custody call success test

* remove commented out code

* remove ERC721 from Starport, cleanup state

* update mermaid, cleanup useless tests/helpers, update salts to not invalidate if they are bytes32(0), fix tests, add snapshot

* rename CaveatWithApproval => SignedCaveats, move deadline out of Caveat into signed caveats, add deadline into the intent origination typehash, update tests, move to packed signatures from explicit vrs values

* add test for caveat deadline, snapshot updated

* cleanup the counter example, add an assume for testing invalid salts that the salt is not bytes32(0), foundry toml update to not include storage in the fuzz runs

* update the reference starport lib validate salt to match the optimized

* chore: update snapshot

* update the signed caveats to take an invalidate bit to identify if the salt is re usable

* chore snapshot

* remove on open hook

* chore: update snapshot
  • Loading branch information
androolloyd authored Nov 13, 2023
1 parent 012be7a commit 91e848c
Show file tree
Hide file tree
Showing 42 changed files with 1,073 additions and 1,186 deletions.
257 changes: 134 additions & 123 deletions .gas-snapshot

Large diffs are not rendered by default.

46 changes: 28 additions & 18 deletions docs/mermaid/origination.mmd
Original file line number Diff line number Diff line change
@@ -1,57 +1,67 @@
sequenceDiagram
title Starport Origination Sequence Diagram
participant F as Fulfiller
participant L as Lender
participant B as Borrower


F->>LoanManager: originate
Box green Orignation
participant Fulfiller
participant Starport
participant CaveatEnforcer
end

Box blue Assets
participant Lender
participant Borrower
participant Custodian
participant FeeRecipient
participant AdditionalTransferRecipient
end

Fulfiller->>Starport: originate

opt F is not Borrower
opt Fulfiller is not Borrower
loop 1->n
LoanManager->>CaveatEnforcer: validate
Starport->>CaveatEnforcer: validate
end
end
opt F is not Lender
opt Fulfiller is not Lender
loop 1->n
LoanManager->>CaveatEnforcer: validate
Starport->>CaveatEnforcer: validate
end
end
loop Transfer 1->n collateral items
B->>Custodian: Move Collateral to Custodian
Borrower->>Custodian: transfer collateral
end
opt Custodian is not default
LoanManager->>Custodian: custody (optional)
Starport->>Custodian: custody (optional)
end

alt Fees Disabled
loop Transfer 1->n debt items
L->>B: Move debt to borrower
Lender->>Borrower: transfer debt
end
else Fees Enabled
LoanManager->>LoanManager: compute feeRake
opt FeeItems length > 0
loop Transfer 1->n fee items
L->>FeeRecipient: Move Fee to FeeRecipient
Lender->>FeeRecipient: Move Fee to FeeRecipient
end
end
loop Transfer 1->n debt items
L->>B: Move debt to Borrower
Lender->>Borrower: Move debt to Borrower - Fee
end
end

opt AdditionalTransferItems length > 0
loop 1->n
alt From is Borrower
B->>AdditionalTransferRecipient: AdditionalTransferItem from Borrower => AdditionalTransferRecipient
Borrower->>AdditionalTransferRecipient: AdditionalTransferItem from Borrower => AdditionalTransferRecipient
else From is Lender
L->>AdditionalTransferRecipient: AdditionalTransferItem from Lender => AdditionalTransferRecipient
Lender->>AdditionalTransferRecipient: AdditionalTransferItem from Lender => AdditionalTransferRecipient
else From is F
F->>AdditionalTransferRecipient: AdditionalTransferItem from Fulfiller => AdditionalTransferRecipient
Fulfiller->>AdditionalTransferRecipient: AdditionalTransferItem from Fulfiller => AdditionalTransferRecipient
end
end
end

opt Lender is contract
LoanManager->>L: onERC721Received
Starport->>Lender: onLoanOpened
end
14 changes: 7 additions & 7 deletions docs/mermaid/refinance.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ sequenceDiagram
participant R as Recaller


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

LoanManager->>LoanManager: settle loan
Starport->>Starport: settle loan

loop Transfer 1->n ConsiderPayment items
R->>L: Move ConsiderPayment to Lender
Expand All @@ -21,11 +21,11 @@ sequenceDiagram

opt F is not Refinancer and F is not approved
loop Validate 1->n Caveats
LoanManager->>CaveatEnforcer: validate
Starport->>CaveatEnforcer: validate
end
end
opt AdditionalTransferItems length > 0
LoanManager->>LoanManager: validateAdditionalTransfers
Starport->>Starport: validateAdditionalTransfers
loop 1->n
alt From is Borrower
B->>AdditionalTransferRecipient: AdditionalTransferItem from Borrower -> AdditionalTransferRecipient
Expand All @@ -38,5 +38,5 @@ sequenceDiagram
end

opt Lender is contract
LoanManager->>L: onERC721Received
Starport->>L: onERC721Received
end
6 changes: 3 additions & 3 deletions docs/mermaid/settlement.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sequenceDiagram
Custodian->>Custodian: _setOfferApprovalsWithSeaport
Custodian->>Pricing: getPaymentConsideration
Pricing->>Custodian: (SpentItem[] payment, SpentItem[] carry)
Custodian->>LoanManager: settle loan
Custodian->>Starport: settle loan
else Action Settlement and Custodian->>Status: isActive is false
Custodian->>Custodian: _beforeGetSettlement
Custodian->>SettlementHandler: getSettlement
Expand All @@ -28,8 +28,8 @@ sequenceDiagram
Custodian->>Custodian: _afterSettlementHandlerHook
end
end
Custodian->>LoanManager: settle loan
Custodian->>Starport: settle loan
opt Lender is contract
LoanManager->>Lender: onLoanSettledHook
Starport->>Lender: onLoanSettledHook
end
end
13 changes: 12 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,16 @@ src = "src"
out = "out"
libs = ["lib"]
ignored_error_codes = [2018, 9302, 5574]

[profile.default.fuzz]
include_storage = false
#solc = "./bin/solc"
# See more config options https://github.com/foundry-rs/foundry/tree/master/config
# See more config options https://github.com/foundry-rs/foundry/tree/master/config
#
#
#[profile.default.model_checker]
#contracts = {'src/Starport.sol' = ['Starport'],'src/Custodian.sol' = ['Custodian']}
#engine = 'chc'
#timeout = 10000
#ignored_error_codes = [2018, 9302, 5574, 7737]
#targets = ['assert', 'constantCondition', 'overflow', 'underflow', 'outOfBounds', 'divByZero']
35 changes: 3 additions & 32 deletions src/BNPLHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,20 @@ interface IFlashLoanRecipient {
) external;
}

interface IWETH9 {
function withdraw(uint256) external;
}

interface ERC20 {
function transfer(address, uint256) external returns (bool);
}

// fulfiller

contract BNPLHelper is IFlashLoanRecipient {
address private constant vault = address(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
IWETH9 private immutable WETH;
bytes32 private activeUserDataHash;

constructor(address WETH_) {
WETH = IWETH9(WETH_);
}

struct Execution {
address lm;
address seaport;
address borrower;
CaveatEnforcer.CaveatWithApproval borrowerCaveat;
CaveatEnforcer.CaveatWithApproval lenderCaveat;
CaveatEnforcer.SignedCaveats borrowerCaveat;
CaveatEnforcer.SignedCaveats lenderCaveat;
Starport.Loan loan;
AdvancedOrder[] orders;
CriteriaResolver[] resolvers;
Expand All @@ -69,19 +58,7 @@ contract BNPLHelper is IFlashLoanRecipient {
error DoNotSendETH();
error InvalidUserDataProvided();

function makeFlashLoan(
address[] calldata tokens,
uint256[] calldata amounts,
// uint256[] calldata userProvidedAmounts,
bytes calldata userData
) external {
// assembly {
// // Compute the hash of userData
// let dataHash := keccak256(userData.offset, calldatasize())
//
// // Store the hash in the activeUserDataHash state variable
// tstore(activeUserDataHash.slot, dataHash)
// }
function makeFlashLoan(address[] calldata tokens, uint256[] calldata amounts, bytes calldata userData) external {
activeUserDataHash = keccak256(userData);

IVault(vault).flashLoan(this, tokens, amounts, userData);
Expand Down Expand Up @@ -128,10 +105,4 @@ contract BNPLHelper is IFlashLoanRecipient {
}
Starport(execution.lm).originate(transfers, execution.borrowerCaveat, execution.lenderCaveat, execution.loan);
}

receive() external payable {
if (msg.sender != address(WETH)) {
revert DoNotSendETH();
}
}
}
10 changes: 2 additions & 8 deletions src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ contract Custodian is ERC721, ContractOffererInterface {
Starport public immutable SP;
ConsiderationInterface public immutable seaport;

mapping(address => mapping(address => bool)) public repayApproval;

event RepayApproval(address borrower, address repayer, bool approved);
event SeaportCompatibleContractDeployed();

error ImplementInChild();
Expand Down Expand Up @@ -315,7 +312,7 @@ contract Custodian is ERC721, ContractOffererInterface {
bool loanActive = Status(loan.terms.status).isActive(loan, close.extraData);
if (close.action == Actions.Repayment && loanActive) {
address borrower = getBorrower(loan);
if (fulfiller != borrower && !repayApproval[borrower][fulfiller]) {
if (fulfiller != borrower && fulfiller != _getApproved(loan.getId())) {
revert InvalidRepayer();
}
offer = loan.collateral;
Expand Down Expand Up @@ -344,10 +341,6 @@ contract Custodian is ERC721, ContractOffererInterface {
* revert with NotEnteredViaSeaport()
*/
function onERC1155Received(address, address, uint256, uint256, bytes calldata) public virtual returns (bytes4) {
// 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 Expand Up @@ -379,6 +372,7 @@ contract Custodian is ERC721, ContractOffererInterface {
_enableAssetWithSeaport(offer[i]);
}
}

/**
* @dev transfers out the collateral to the handler address
*
Expand Down
Loading

0 comments on commit 91e848c

Please sign in to comment.