# Precision loss allows a mallicious user to drain amount received from orders

Integer division in AutomationMaster._getExchangeRate rounds to zero for low-priced tokens, letting a malicious keeper forge txData and steal pending order funds.

- Payout: Audit contest
- Severity: medium
- Program: Oku
- Found by: NHristov
- Disclosed: 2025-07-25
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2025-07-oku-trade-order-types-judging/issues/171
- Library page: https://bugbountylibrary.com/r/sherlock%3A2025-07-oku-trade-order-types%3AM-1
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:precision-loss

**Target:** AutomationMaster / Bracket order contracts
**Root cause:** _getExchangeRate computes (priceIn*1e8)/priceOut with integer division; when priceIn*1e8 < priceOut the rate truncates to 0, making minAmountReceived zero and bypassing the require check in Bracket.execute.
**Steps:**
1. Create a Bracket order for a cheap token vs expensive token when exchange rate is nonzero (e.g. 10).
2. Drop the cheap token price so (priceIn*1e8)/priceOut == 0 (e.g. WETH 1 vs USDC 120000).
3. Call checkUpkeep to obtain performData with minAmountReceived == 0.
4. Forge encodedTxData via generateUniTx setting swap recipient to attacker address and minAmountReceived 0.
5. Call Master.performUpkeep(encodedTxData); require passes and output tokens are sent to attacker.
**How found:** Noticed integer division in _getExchangeRate could round to zero for extreme price ratios, then traced how minAmountReceived feeds the require in Bracket.execute and that performUpkeep txData is attacker-controlled.
**Exploitation:** Malicious keeper waits for exchange rate to truncate to zero, then crafts performUpkeep txData redirecting the swap recipient to their own address; the zero minAmountReceived check passes and the full order output (WBTC) is transferred to the attacker.
**Insight:** Audit fixed-point math for truncation-to-zero at extreme ratios; any minAmountReceived derived from a rate that can be 0 silently disables slippage protection, and keeper-supplied txData is attacker-controlled.

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

**Test creating order with low-priced token**

```
await s.Bracket.connect(s.Bob).createOrder(
            "0x",
            currentPrice + 10n, // 18
            currentPrice - 1n, //7
            parseEther("25"), // 250 weth
            await s.WETH.getAddress(),
            await s.USDC.getAddress(),
            await s.Bob.getAddress(),
            0,//5 bips fee
            500,
            500,
            "0x",
            { value: s.fee }
        )
        console.log("Order created")
```

**Test lowering oracle price to zero exchange rate**

```
await s.wethOracle.setPrice(ethers.parseUnits("0.001", 8))
        const currentPrice = await s.Master.getExchangeRate(await s.WETH.getAddress(), await s.USDC.getAddress())
        //should be 0
        console.log("Current Price: ", currentPrice.toString())
```

**Malicious performUpkeep with forged recipient**

```
const encodedTxData = await generateUniTx(
            s.router02,
            s.UniPool,
            malicious.address,
            minAmountReceived,
            data
        )

        console.log("Gas to performUpkeep: ", await getGas(await s.Master.performUpkeep(encodedTxData)))
```

