# If stable tokens depeg, short funding fees will not be accounted properly

Elfi short funding fees are denominated in USD but settled at current token price, so stablecoin depegs misprice realized funding fees for shorters.

- Payout: Contest reward
- Severity: medium
- Program: Elfi Protocol
- Credited: mstpr-brainbot
- Disclosed: 2024-05-09
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/70
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-05-elfi-protocol%3AM-5
- Tags: domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:oracle-manipulation

**Target:** perpetuals funding-fee accounting (MarketQueryProcess/FeeProcess)
**Root cause:** Short funding fee per qty is stored in USD terms without converting to token denomination, while long fees convert USD to token at accrual; settlement later converts the accumulated USD delta using the current token price.
**Steps:**
1. Open short position on a stable-margin market when DAI=1$, recording openFundingFeePerQty in USD.
2. Stablecoin depegs (e.g. DAI=0.8$); funding fee per qty continues accruing in USD only.
3. User updates/closes position: `realizedFundingFee = usdToTokenInt(realizedFundingFeeDelta, decimals, latestPrice)` converts at 0.8$, inflating DAI received vs. the 1:1 period.
4. If peg restores to 1$ before close, the depegged interval is under-compensated instead.
**How found:** Code reading of `getUpdateMarketFundingFeeRate` revealed long branch applies `CalUtils.usdToToken` conversion while short branch stores raw USD per-qty; cross-referenced `updateFundingFee` which converts short deltas at settlement-time price, exposing a timing/price mismatch.
**Insight:** When a per-unit accumulator mixes USD and token denominations across branches, check whether settlement converts at accrual-time or close-time price; asymmetric conversion between long and short branches is the tell.
