# Fee Precision Loss Disrupts Liquidations and Causes Loss of Funds

Two integer precision-loss bugs in Velar Artha's fee math undercharge borrow/funding fees, delaying liquidations and shortchanging LPs.

- Payout: Audit contest
- Severity: high
- Program: Velar Artha
- Found by: 0xbranded
- Disclosed: 2024-08-22
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-velar-artha-judging/issues/66
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-velar-artha%3AH-1
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:liquidation vuln:precision-loss

**Target:** Vyper perpetuals fee math (math.vy apply/DENOM, params.vy utilization/scale)
**Root cause:** apply() divides by a coarse DENOM=1e9, and utilization() divides reserves by 100 before dividing interest, truncating utilization to whole percent; both round fees down.
**Steps:**
1. Read math.vy apply(): `fee = (x * numerator) / DENOM` with DENOM=1e9, capping smallest representable per-block fee.
2. Read params.vy utilization(): `interest / (reserves / 100)` truncates utilization to 2-digit percent.
3. Compute example: reserves=10_000_000e18, interest=199_999e18 -> utilization rounds 1.9999% to 1%.
4. With max_fee=65, scale() yields borrow rate r=0 instead of ~1.3; combined with DENOM truncation, 2.051% rounds to 1.578%.
5. Run Foundry PoC (Denom.t.sol) testRounding/testCombined asserting util1==util2 and fee1*1000 == fee2-999.
6. Compare annualized fee intervals: any annual fee below 1.578% rounds to zero.
**How found:** Code review of fee math constants and integer division; noticed DENOM granularity sets a 1.578% annual fee floor and utilization() truncates to whole percent, then quantified combined error with worked examples and a Foundry PoC.
**Exploitation:** Not an attacker-driven exploit; the flaw causes systematic underpayment of borrow/funding fees across all pools, delaying liquidations and reducing LP/funding-recipient income.
**Insight:** When auditing fee/interest math, compute the smallest representable rate from the denominator constant and the block-time annualization; check whether intermediate divisions (e.g. reserves/100) truncate before the meaningful ratio, and test boundary values just below a rounding interval.

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

**Foundry test showing utilization rounding collapses distinct interests**

```
function testRounding() public {
        uint max_fee = 100; // set max 8-hour rate to 0.288% (157.8% annually)

        uint256 reserves = 10_000_000 ether;
        uint256 interest1 = 1_000_000 ether;
        uint256 interest2 = 1_099_999 ether;

        uint256 util1 = denom.utilization(reserves, interest1);
        uint256 util2 = denom.utilization(reserves, interest2);

        assertEq(util1, util2); // current calculation yields same utilization for both interests

        // borrow rate
        uint fee1 = denom.scale(max_fee, util1); 
        uint fee2 = denom.scale(max_fee, util2);

        assertEq(fee1, fee2); // results in the same fee also. fee2 should be ~1% higher
```

**Test showing combined DENOM and utilization precision loss**

```
function testCombined() public {
        // now let's see what would happen if we raised the precision of both fees and percents
        uint max_fee = 65;
        uint max_fee2 = 65_000; // 3 extra digits of precision lowers error by 3 orders of magnitude

        uint256 reserves = 10_000_000 ether;
        uint256 interest = 199_999 ether; // interest & reserves same in both, only differ in precision.

        uint256 util1 = denom.utilization(reserves, interest); 
        uint256 util2 = denom.utilization2(reserves, interest); // 3 extra digits of precision here also
        
        // borrow rate
        uint fee1 = denom.scale(max_fee, util1); 
        uint fee2 = denom.scale2(max_fee2, util2);

        assertEq(fee1 * 1_000, fee2 - 999); // fee 1 is 1.000, fee 2 is 1.999 (~50% error)
```

**Vyper utilization and scale functions with low precision**

```
def utilization(reserves: uint256, interest: uint256) -> uint256:
    return 0 if (reserves == 0 or interest == 0) else (interest / (reserves / 100))

This function is in turn used to calculate borrowing (and funding rates, following a slightly different approach that similarly combines the use of `utilization` and `scale`), in `[dynamic_fees](https://github.com/sherlock-audit/2024-08-velar-artha/blob/main/gl-sherlock/contracts/params.vy#L33-L55)` of `params.vy`:

def dynamic_fees(pool: PoolState) -> DynFees:
    long_utilization : uint256 = self.utilization(pool.base_reserves, pool.base_interest)
    short_utilization: uint256 = self.utilization(pool.quote_reserves, pool.quote_interest)
    borrowing_long   : uint256 = self.check_fee(
      self.scale(self.PARAMS.MAX_FEE, long_utilization))
    borrowing_short  : uint256 = self.check_fee(
      self.scale(self.PARAMS.MAX_FEE, short_utilization))
...
def scale(fee: uint256, utilization: uint256) -> uint256:
    return (fee * utilization) / 100
```

