Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/make-payment-on-behalf-of' into …
Browse files Browse the repository at this point in the history
…deployment
  • Loading branch information
PlayJok3r committed Dec 30, 2024
2 parents 4c76107 + a5fdfbf commit c636963
Show file tree
Hide file tree
Showing 9 changed files with 903 additions and 0 deletions.
Binary file added audit/spearbit-incremental-Nov-2024.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions contracts/common/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ contract Errors {
error PoolOwnerOrHumaOwnerRequired(); // 0x3e984120
error PoolOperatorRequired(); // 0xae7fe070
error PoolOwnerRequired(); // 0x8b506451
error PoolOwnerTreasuryRequired(); // 0xf527eb38
error HumaTreasuryRequired(); // 0x6e0a9ac9
error PoolOwnerOrEARequired(); // 0xe54466f3
error PauserRequired(); // 0xd4a99e4e
Expand Down
4 changes: 4 additions & 0 deletions contracts/credit/Credit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ abstract contract Credit is PoolConfigCache, CreditStorage, ICredit {
revert Errors.SentinelServiceAccountRequired();
}

function _onlyPoolOwnerTreasury(address account) internal view {
if (account != poolConfig.poolOwnerTreasury()) revert Errors.PoolOwnerTreasuryRequired();
}

/**
* @notice Returns from whose account the funds for payment should be extracted.
* @notice This function exists because of Auto-pay:
Expand Down
14 changes: 14 additions & 0 deletions contracts/credit/CreditLine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ contract CreditLine is Credit, ICreditLine {
return _makePayment(borrower, creditHash, amount);
}

/// @inheritdoc ICreditLine
function makePaymentOnBehalfOf(
address borrower,
uint256 amount
) external virtual override returns (uint256 amountPaid, bool paidoff) {
poolConfig.onlyProtocolAndPoolOn();
_onlyPoolOwnerTreasury(msg.sender);

bytes32 creditHash = getCreditHash(borrower);
creditManager.onlyCreditBorrower(creditHash, borrower);

return _makePayment(borrower, creditHash, amount);
}

/// @inheritdoc ICreditLine
function makePrincipalPayment(
uint256 amount
Expand Down
35 changes: 35 additions & 0 deletions contracts/credit/ReceivableBackedCreditLine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ contract ReceivableBackedCreditLine is Credit, IERC721Receiver {
address by
);

/**
* @notice A payment has been made against the credit line by someone on behalf of the borrower.
* @param borrower The address of the borrower.
* @param receivableId The ID of the receivable.
* @param amount The payback amount.
* @param by The address that initiated the payment.
*/
event PaymentMadeOnBehalfOfWithReceivable(
address indexed borrower,
uint256 indexed receivableId,
uint256 amount,
address by
);

/**
* @notice A borrowing event has happened to the credit line.
* @param borrower The address of the borrower.
Expand Down Expand Up @@ -118,6 +132,27 @@ contract ReceivableBackedCreditLine is Credit, IERC721Receiver {
emit PaymentMadeWithReceivable(borrower, receivableId, amount, msg.sender);
}

/**
* @notice Allows the Pool Owner Treasury to pay back on behalf of the borrower with a receivable
*/
function makePaymentOnBehalfOfWithReceivable(
address borrower,
uint256 receivableId,
uint256 amount
) public virtual returns (uint256 amountPaid, bool paidoff) {
poolConfig.onlyProtocolAndPoolOn();
_onlyPoolOwnerTreasury(msg.sender);

bytes32 creditHash = getCreditHash(borrower);
creditManager.onlyCreditBorrower(creditHash, borrower);

_prepareForPayment(borrower, poolConfig.receivableAsset(), receivableId);

(amountPaid, paidoff) = _makePayment(borrower, creditHash, amount);

emit PaymentMadeOnBehalfOfWithReceivable(borrower, receivableId, amount, msg.sender);
}

/**
* @notice Allows the borrower to payback the principal and label it with a receivable
*/
Expand Down
17 changes: 17 additions & 0 deletions contracts/credit/interfaces/ICreditLine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ interface ICreditLine {
uint256 amount
) external returns (uint256 amountPaid, bool paidoff);

/**
* @notice Makes one payment for the credit line by the pool owner treasury on behalf of the borrower.
* If this is the final payment, it automatically triggers the payoff process.
* @notice Warning: payments should be made by calling this function. No token should be transferred directly
* to the contract.
* @param borrower The address of the borrower.
* @param amount The payment amount.
* @return amountPaid The actual amount paid to the contract. When the tendered
* amount is larger than the payoff amount, the contract only accepts the payoff amount.
* @return paidoff A flag indicating whether the account has been paid off.
* @custom:access Only the Pool Owner Treasury can call this function.
*/
function makePaymentOnBehalfOf(
address borrower,
uint256 amount
) external returns (uint256 amountPaid, bool paidoff);

/**
* @notice Makes a payment towards the principal for the credit line. Even if there is additional amount remaining
* after the principal is paid off, this function will only accept the amount up to the total principal due.
Expand Down
1 change: 1 addition & 0 deletions scripts/error-functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"PoolOwnerOrHumaOwnerRequired()": "0x3e984120",
"PoolOperatorRequired()": "0xae7fe070",
"PoolOwnerRequired()": "0x8b506451",
"PoolOwnerTreasuryRequired()": "0xf527eb38",
"HumaTreasuryRequired()": "0x6e0a9ac9",
"PoolOwnerOrEARequired()": "0xe54466f3",
"PauserRequired()": "0xd4a99e4e",
Expand Down
Loading

0 comments on commit c636963

Please sign in to comment.