Tracking profit and loss (PnL) is essential for understanding trading and investing performance onchain. While centralized exchanges provide PnL reporting, onchain traders traders, and builders lack an easy way to calculate it. This guide explains how PnL works, why tracking it onchain is challenging, and how the Zerion API PnL endpoint simplifies the process for developers.

Why PnL Matters 

Profit and loss (PnL) is a key metric for traders and investors. It gives a clear picture of portfolio performance by showing gains and losses. 

PnL is measured in USD (or another base currency) and is calculated by subtracting sales from buys: PnL = amount * price bought - amount * price sold  

For onchain wallets, tracking PnL helps with:

  • Understanding the overall profitability of trades and strategies
  • Managing risk by taking profits and cutting losses 
  • Simplifying reporting for taxes and financial analysis

Without an accurate PnL calculation, it’s difficult to measure actual returns. This is especially true in DeFi, where transactions involve swaps, staking, and liquidity provision.

PnL Explained 

PnL can be broken down into three key components: realized, unrealized, and total PnL. 

Realized PnL

Represents gains or losses from completed transactions. It is calculated when an asset is sold, sent to another address, or otherwise exited.

Unrealized PnL

Also known as paper gains, this measures the potential profit or loss of open positions. It’s based on the current market price and fluctuates with it. 

Total PnL

The sum of realized and unrealized PnL, reflects the overall performance.

Accurate tracking of these metrics requires precise historical price data and an understanding of how tokens move across wallets and protocols.

Why Tracking Onchain PnL is Hard

On centralized exchanges, PnL is straightforward: you have amounts, prices at which they were bought or sold, and fees. 

Calculating onchain PnL for a wallet comes with several challenges:  

  • Tracking prices — actual prices for executed swaps and market prices for open positions  
  • Not only swaps — send, receive, depositing into DeFi protocols and withdrawing
  • Multiple chains — bridging and fees add another layer of complexity  

How Zerion API Calculates PnL 

Zerion API can calculate onchain PnL for any wallet address across 23 supported EVM chains.

Zerion uses the FIFO (First In, First Out) standard to calculate the PnL. This covers all types of wallet transactions, including: 

  • Receiving and sending tokens to other addresses 
  • Swaps 
  • Buying and selling NFTs 
  • Depositing and withdrawing from DeFi protocols 

To include transactions in PnL, Zerion API uses: 

  • When a transaction is happening, then the current price of the assets is attached and saved within the transaction,
  • Prices for assets come from different sources, including Coingecko, and directly from tracked liquidity pools.

Here is an example of an API call:

curl --request GET \
     --url 'https://api.zerion.io/v1/wallets/address/pnl/?currency=usd' \
     --header 'accept: application/json'

Here is an example of a 200 response for an address:

{
  "links": {
    "self": "https://api.zerion.io/v1/wallets/0x42b9df65b219b3dd36ff330a4dd8f327a6ada990/pnl/"
  },
    "data": {
    "type": "pnl",
    "id": "0x42b9df65b219b3dd36ff330a4dd8f327a6ada990",
    "attributes": {
      "realized_gain": -655.3618983,
      "unrealized_gain": 17.5445466,
      "total_fee": 281.9088917,
      "net_invested": 45.84218703,
      "received_external": 133971.2931,
      "sent_external": 133270.089,
      "sent_for_nfts": 133971.2931,
      "received_for_nfts": 133971.2931
    }
  }
}

As you can see, the PnL API includes realized PnL, unrealized PnL, net invested amount, and sent/received for tokens and NFTs. 

You can filter the API response to get PnL for a specific chain and/or token(s). The profit or loss can be calculated and displayed in ETH, BTC, USD, and other major world currencies. 

Track PnL for Onchain Wallets with Zerion 

Zerion initially built PnL API endpoint for the Zerion portfolio tracking app and the Zerion Wallet. 

The PnL for tokens in Zerion is a premium feature and one of the main reasons why users get an annual subscription ($99). It shows PnL for any address and any token on any of the 40+ supported chains.   

If you get Zerion Premium, you’ll be able to see, for example, the profit for ETH for Vitalik's address

Showing PnL for ETH in Zerion
Showing PnL for ETH in Zerion

How to Integrate the Zerion API PnL Endpoint

It’s very easy to get started with Zerion API: 

  1. Get free dev API key by filling out this Typeform
  2. Wait for the activation email with your key
  3. Use the key to query PnL data
  4. Display insights in your app 

Try PnL in Zerion API Today

You’ve explored what PnL is, why it’s difficult to calculate it onchain, and how you can solve this with Zerion API. 

Now it’s time to put this into practice. Get your free API key.