Submitted by berndartmueller, also found by 0x52, hyh, and Ruhum
The JBPayoutRedemptionPaymentTerminal._feeAmount function is used to calculate the fee based on a given _amount, a fee rate _fee and an optional discount _feeDiscount.
However, the current implementation calculates the fee in a way that leads to inaccuracy and to fewer fees being paid than anticipated by the protocol.
JBPayoutRedemptionPaymentTerminal._feeAmount
Example:
Given the following (dont mind the floating point arithmetic, this is only for simplicity. The issues still applies with integer arithmetic and higher decimal precision):
$discountedFee = fee - {{fee \ast feeDiscount} \over MAX_FEE_DISCOUNT}$
$discountedFee = 5 - {{5 \ast 10} \over 100}$
$discountedFee = 4.5$
Calculating the fee amount based on the discounted fee of $4.5$:
$fee\_{Amount} = amount - {{amount \ast MAX_FEE} \over {discountedFee + MAX_FEE}}$
$fee\_{Amount} = 1000 - {{1000 \ast 100} \over {4.5 + 100}}$
$fee\_{Amount} = 1000 - 956.93779904$
$fee\_{Amount} = 43.06220096$
The calculated and wrong fee amount is ~43, instead, it should be 45. The issue comes from dividing by _discountedFee + JBConstants.MAX_FEE.
Now the correct way:
I omitted the discountedFee calculation as this formula is correct.
$fee\_{Amount} = {{amount \ast discountedFee} \over {MAX_FEE}}$
$fee\_{Amount} = {{1000 \ast 4.5} \over {100}}$
$fee\_{Amount} = 45$
Fix the discounted fee calculation by adjusting the formula to:
$fee\_{Amount} = amount \ast {fee - fee \ast {discount \over MAX\_{FEE_DISCOUNT}} \over MAX\_{FEE}}$
In Solidity:
mejango (Juicebox) acknowledged 
jack-the-pug (judge) commented:
