跳到主要内容

_refundHeldFees

Contract: JBPayoutRedemptionPaymentTerminal​‌

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

  1. 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:

  2. 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:

  3. 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;
  4. 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:

  5. Emit a RefundHeldFees event with the relevant parameters.

    emit RefundHeldFees(_projectId, _amount, refundedFees, leftoverAmount, msg.sender);

    Event references: