_refundHeldFees
Contract: JBPayoutRedemptionPaymentTerminal
- Step by step
- Code
- Events
- Bug bounty
Refund fees based on the specified amount.
Definition
function _refundHeldFees(uint256 _projectId, uint256 _amount)
private
returns (uint256 refundedFees) { ... }
- Arguments:
_projectId
is the project for which fees are being refunded._amount
is the amount to base the refund on, as a fixed point number with the same amount of decimals as this terminal.
- The function is private to this contract.
- The function doesn't return anything.
Body
-
Get a reference to any held
JBFee
's for the project.// Get a reference to the project's held fees.
JBFee[] memory _heldFees = _heldFeesOf[_projectId];Internal references:
-
Delete all of the project's held fees. These will be repopulated if they were not refunded.
// Delete the current held fees.
delete _heldFeesOf[_projectId];Internal references:
-
Get a reference to how much of the amount is left to refund fees for.
// Get a reference to the leftover amount once all fees have been settled.
uint256 leftoverAmount = _amount; -
Loop through each held fee, decrementing the amount as held fees are refunded and incrementing the amount of refunded fees. If the entire refund amount has been refunded, add the fee structure back into the project's held fees so that they can be processed or refunded later. If the amount left is greater than the fee structure's amount, decrement the refunded amount and leave the fee structure out of the project's held fees. If only some of the fee structure's amount is needed to cover the rest of the remaining amount, set the amount to 0 after adding the fee structure back into the project's held fees having subtracted the remaining refund amount.
// Push length in stack
uint256 _heldFeesLength = _heldFees.length;
// Process each fee.
for (uint256 _i; _i < _heldFeesLength; ) {
if (leftoverAmount == 0) _heldFeesOf[_projectId].push(_heldFees[_i]);
else if (leftoverAmount >= _heldFees[_i].amount) {
unchecked {
leftoverAmount = leftoverAmount - _heldFees[_i].amount;
refundedFees += _feeAmount(
_heldFees[_i].amount,
_heldFees[_i].fee,
_heldFees[_i].feeDiscount
);
}
} else {
unchecked {
_heldFeesOf[_projectId].push(
JBFee(
_heldFees[_i].amount - leftoverAmount,
_heldFees[_i].fee,
_heldFees[_i].feeDiscount,
_heldFees[_i].beneficiary
)
);
refundedFees += _feeAmount(leftoverAmount, _heldFees[_i].fee, _heldFees[_i].feeDiscount);
}
leftoverAmount = 0;
}
unchecked {
++_i;
}
}Internal references:
-
Emit a
RefundHeldFees
event with the relevant parameters.emit RefundHeldFees(_projectId, _amount, refundedFees, leftoverAmount, msg.sender);
Event references:
/**
@notice
Refund fees based on the specified amount.
@param _projectId The project for which fees are being refunded.
@param _amount The amount to base the refund on, as a fixed point number with the same amount of decimals as this terminal.
@return refundedFees How much fees were refunded, as a fixed point number with the same number of decimals as this terminal.
*/
function _refundHeldFees(uint256 _projectId, uint256 _amount)
private
returns (uint256 refundedFees) {
// Get a reference to the project's held fees.
JBFee[] memory _heldFees = _heldFeesOf[_projectId];
// Delete the current held fees.
delete _heldFeesOf[_projectId];
// Get a reference to the leftover amount once all fees have been settled.
uint256 leftoverAmount = _amount;
// Push length in stack
uint256 _heldFeesLength = _heldFees.length;
// Process each fee.
for (uint256 _i; _i < _heldFeesLength) {
if (leftoverAmount == 0) _heldFeesOf[_projectId].push(_heldFees[_i]);
else if (leftoverAmount >= _heldFees[_i].amount) {
unchecked {
leftoverAmount = leftoverAmount - _heldFees[_i].amount;
refundedFees += _feeAmount(
_heldFees[_i].amount,
_heldFees[_i].fee,
_heldFees[_i].feeDiscount
);
}
} else {
unchecked {
_heldFeesOf[_projectId].push(
JBFee(
_heldFees[_i].amount - leftoverAmount,
_heldFees[_i].fee,
_heldFees[_i].feeDiscount,
_heldFees[_i].beneficiary
)
);
refundedFees += _feeAmount(leftoverAmount, _heldFees[_i].fee, _heldFees[_i].feeDiscount);
}
leftoverAmount = 0;
}
unchecked {
++_i;
}
}
emit RefundHeldFees(_projectId, _amount, refundedFees, leftoverAmount, msg.sender);
}
Name | Data |
---|---|
RefundHeldFees |
|
Category | Description | Reward |
---|---|---|
Optimization | Help make this operation more efficient. | 0.5ETH |
Low severity | Identify a vulnerability in this operation that could lead to an inconvenience for a user of the protocol or for a protocol developer. | 1ETH |
High severity | Identify a vulnerability in this operation that could lead to data corruption or loss of funds. | 5+ETH |