# Frequency-dependent `TaxCalculator.sol::calculateCompoundedFactor` leads to interest loss either for users or for protocol

TaxCalculator's discrete compounding formula makes interest frequency-dependent, letting attackers inflate interest up to 71% or causing protocol to lose up to 42% of interest.

- Payout: Audit contest
- Severity: high
- Program: Flayer
- Found by: kuprum
- Disclosed: 2024-08-06
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/117
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-flayer%3AH-1
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** TaxCalculator.sol calculateCompoundedFactor (Solidity interest accrual)
**Root cause:** calculateCompoundedFactor applies a discrete per-checkpoint compounding formula whose result depends on how often checkpoints are created, rather than on elapsed time alone; combined with a wrong divisor (1000 vs 10_000) it amplifies error.
**Steps:**
1. Set collection utilization to 90% (0.9 ether).
2. Compute expected factor via `taxCalculator.calculateCompoundedFactor(1 ether, utilization, 365 days)`.
3. Loop 365 times calling `calculateCompoundedFactor(compoundedFactor, utilization, 1 days)` to simulate daily checkpoints.
4. Assert compoundedFactor ≈ 33x expectedFactor, showing 3200% more interest than expected.
5. With divisor fixed to 10_000 and 100% utilization, daily checkpoints yield ~71% more interest than non-compound expectation.
**How found:** Code review of the compounding formula; noticed result depends on checkpoint frequency (triggered by any listing create/cancel/fill) rather than pure elapsed time, then quantified divergence with a forge test comparing single-shot vs daily-loop compounding.
**Exploitation:** Attacker repeatedly calls createListings/cancelListings on an NFT from a target collection, forcing frequent checkpoints and inflating the compounded factor so all users of that collection pay up to 71% more interest than users of another collection; cost is only gas.
**Escalation:** Per-collection granularity means attacker can target a specific collection's users while leaving others unaffected, and low-activity collections suffer protocol-side interest loss without any attack.
**Insight:** When auditing interest/accrual math, check whether the compounding formula is applied per-checkpoint vs per-time; any user-triggerable checkpoint (listing create/cancel/fill) becomes a griefing lever to inflate or deflate rates for all holders of that asset.

## Proof of concept (copied exactly from the original report)

**Foundry test showing frequency-dependent interest loss**

```
function test_WrongInterestCalculation() public view {
        // We fix collection utilization to 90%
        uint utilization = 0.9 ether;

        // New checkpoints with the updated compoundedFactor are created 
        // and stored whenever there is activity wrt. the collection

        // The expected interest multiplier after 1 year
        uint expectedFactor = 
            taxCalculator.calculateCompoundedFactor(1 ether, utilization, 365 days);

        // The resulting interest multiplier if some activity happens every day
        uint compoundedFactor = 1 ether;
        for (uint time = 0; time < 365 days; time += 1 days) {
            compoundedFactor = 
                taxCalculator.calculateCompoundedFactor(compoundedFactor, utilization, 1 days);
        }

        // The user loss due to the activity which doesn't concern them is 3200%
        assertApproxEqRel(
            33 ether * expectedFactor / 1 ether,
            compoundedFactor,
            0.01 ether);
   }
```

**Updated PoC with fixed divisor, 100% utilization**

```
function test\_WrongInterestCalculation() public {
  // We fix the utilization to 100\%
  uint utilization = 1 ether;

  // New checkpoints with the updated compoundedFactor are created 
  // and stored whenever there is activity wrt. the collection

  // The expected interest multiplier after 1 year
  uint expectedFactor = 
      taxCalculator.calculateCompoundedFactor(1 ether, utilization, 365 days);

  // The resulting interest multiplier if some activity happens every day
  uint compoundedFactor = 1 ether;
  for (uint time = 0; time < 365 days; time += 1 days) {
      compoundedFactor = 
          taxCalculator.calculateCompoundedFactor(compoundedFactor, utilization, 1 days);
  }

  // User interest loss due to the activity which doesn't concern them is 71\%
  // We subtract the principal (1 ether) as only the interests must to be compared.
  assertApproxEqRel(
      1.71 ether * (expectedFactor - 1 ether) / 1 ether,
      compoundedFactor - 1 ether,
      0.01 ether);
 }
```

