# Incorrect calculation of new price while adding position

Tigris Trading.addToPosition used a wrong weighted-average price formula, so users adding margin lost part of their PnL payout on close.

- Payout: Contest reward
- Severity: high
- Program: Tigris Trade
- Credited: KingNFT
- Disclosed: 2023-02-17
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2022-12-tigris-findings/issues/236
- Library page: https://bugbountylibrary.com/r/c4%3A2022-12-tigris%3AH-06
- Tags: domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error

**Target:** Trading.sol addToPosition (perpetual leverage trading contract)
**Root cause:** New entry price computed as a margin-weighted average of prices instead of a position-size-weighted average of base-asset amounts, so the blended price is wrong whenever old and new prices differ.
**Steps:**
1. Open position: margin $500, leverage 2x, price $1000 (position $1000).
2. Call `addToPosition(orderId, 500e18, priceData@2000, sig, vault, DAI, permit, owner)`.
3. Observe stored `trade.price` from `_trade.price*_trade.margin/_newMargin + _price*_addMargin/_newMargin`.
4. Compute `tradinglibrary.pnl(...)` at price $2000: payout ~$1666 instead of expected $2000.
5. Repeat with new price $750: payout ~$714 instead of $750.
**How found:** Code-reading of addToPosition's `_newPrice` formula; recognized it averaged prices weighted by margin rather than computing total notional over total base-asset exposure, then derived the correct formula algebraically and confirmed with Hardhat PoC.
**Exploitation:** Any user adding to an existing position at a price different from entry gets a mispriced blended entry, systematically losing part of margin+profit on close; no attacker capital needed, loss accrues to the honest user (and mispricing could be gamed by an attacker opening then adding at favorable prices).
**Insight:** When a protocol blends an existing position with a new one, verify the average is weighted by the quantity that actually matters (base-asset size = notional/price), not by margin; derive P = U/E and compare to the code's formula.
