package lastcall // Spot cost basis. The desk records what each holder has paid for their current // sStock position so the UI can show an average entry price and unrealized P&L on // spot — the desk itself only needs balances, this is bookkeeping for the trader. // // spotCost[{owner,ticker}] is the total micro-USD paid (sum of price*units/1e6 at each // buy, fee excluded), reduced pro-rata on every sell so the average per share is // preserved. Plain stock transfers are not tracked (received shares carry no recorded // cost); for a testnet desk that is acceptable. type costKey struct { owner address ticker string } var spotCost = map[costKey]int64{} // addCost records the tUSD notional (micro) a buyer paid toward their cost basis. func addCost(owner address, ticker string, paid int64) { spotCost[costKey{owner, ticker}] += paid } // reduceCost removes the cost of `sold` units from a holder whose balance BEFORE the // sale was `heldBefore`, keeping the average per-share cost unchanged. func reduceCost(owner address, ticker string, sold, heldBefore int64) { if heldBefore <= 0 { return } k := costKey{owner, ticker} spotCost[k] -= mulDiv(spotCost[k], sold, heldBefore) if spotCost[k] < 0 { spotCost[k] = 0 } } // AvgEntry returns the owner's average entry price for ticker in micro-USD per share, // or 0 if they hold none. func AvgEntry(ticker string, owner address) int64 { held := mustStock(ticker).token.BalanceOf(owner) if held <= 0 { return 0 } cost := spotCost[costKey{owner, ticker}] return mulDiv(cost, microUSD, held) }