filterWallets, filterTokenWallets, getDetailedWalletStats, and walletChart. This page covers how those PnL numbers are calculated so you can interpret them correctly and layer on additional adjustments where needed.
Cost basis methodology
Codex calculates realized PnL using weighted average cost basis. Each buy adds to a cumulative acquisition cost, and each sell reduces that cost proportionally based on the share of holdings sold. The formula forrealizedPnl:
Worked example
A wallet trades a token called PENGU:| Action | Running acquisition cost | Tokens held | Realized PnL |
|---|---|---|---|
| Buy 10 PENGU for $10 | $10 | 10 | $0 |
| Sell 4 PENGU for $8 | $6 | 6 | +$4 |
| Buy 10 PENGU for $20 | $26 | 16 | +$4 (unchanged) |
| Sell 16 PENGU for $32 | $0 | 0 | +$10 |
Transferred and received tokens
Codex only calculates PnL for tokens it can attribute a cost basis to. If a wallet acquires tokens through a swap, that swap establishes the acquisition cost used in the weighted average cost basis calculation. If a wallet receives tokens through a transfer (with no corresponding buy), Codex has no cost basis for those tokens and excludes them from PnL. Concretely, when a wallet later sells tokens it received via transfer:- The sale is not counted in
realizedPnlor other PnL figures, because the profit or loss can’t be known without a cost basis. - The sale is still tracked in
all-suffixed stats such asvolumeUsdAll1w,swapsAll1w, andamountSoldUsdAll1w. Any field withallin its name reflects total activity, including sells of transferred tokens.
Fee handling
Wallet PnL and volume figures handle fees in two different ways depending on the fee type.Pool and swap fees
Pool fees are already reflected inrealizedProfitUsd and volumeUsd. Codex reads swap values post-execution, so the amounts and prices recorded are net of what the pool took. No additional adjustment is needed on your end.
Gas fees
Gas fees (base fees, priority fees, L1 data fees, and builder tips) are tracked separately from wallet PnL. Gas is paid outside the swap itself, so wallet aggregation does not fold it intorealizedProfitUsd or volumeUsd.
If you want PnL net of gas, subtract gas fees yourself using the Global Fees Paid (GFP) fields. GFP exposes per-event and per-window fee components across most endpoints that return swap or bar data.
Data freshness
Wallet stats are indexed and updated on a rolling basis. To keep frequently viewed wallets fresh, queryingfilterWallets or filterTokenWallets with one or more specific wallet addresses triggers a background refresh of the 500 most recently traded tokens for each wallet. Refreshes are capped at once every 6 hours per wallet.
The triggering request returns the current indexed data immediately. Refreshed values become available shortly after, so a follow-up query will reflect the updated stats.
Related
- Global Fees Paid — fee components available across endpoints
- Wallets recipe — practical examples of querying wallet data
filterWallets,filterTokenWallets,getDetailedWalletStats,walletChart