Looking for the easiest way to track wallet profit and loss (PnL)? You’ve come to the right place! In this guide, we’ll introduce Moralis’ crypto PnL feature, providing comprehensive insights into wallet and token profitability. With this feature, you can seamlessly get the overall profitability of a given address, gain insight into the PnL status of individual tokens, and fetch the top profitable wallets for a given ERC-20 token!
Are you eager to dive into the code? Here’s our Wallet PnL Summary endpoint in action:
import fetch from 'node-fetch'; const options = { method: 'GET', headers: { accept: 'application/json', 'X-API-Key': 'YOUR_API_KEY' }, }; fetch('https://deep-index.moralis.io/api/v2.2/wallets/0xd8da6bf26964af9d7eed9e03e53415d37aa96045/profitability/summary?chain=eth', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
Calling this endpoint returns the specified wallet’s total trading volume, total PnL, PnL percentage, and other key metrics. Here’s an example of what it looks like:
{ total_count_of_trades: 12, total_trade_volume: '3793782.5812942344', total_realized_profit_usd: '-20653.121064896484', total_realized_profit_percentage: -1.350177388165031, total_buys: 8, total_sells: 20, total_sold_volume_usd: '1509006.703335308', total_bought_volume_usd: '2284775.8779589264' }
That’s it; tracking wallet profit and loss doesn’t have to be more challenging than this when using Moralis. For a more in-depth tutorial on how this works and further information on our other endpoints, join us in this article or check out the Wallet API documentation page!
Ready to use our crypto PnL feature? Sign up for free with Moralis and gain immediate access to our industry-leading development tools!
Overview
Crypto PnL is a crucial financial metric used to determine a portfolio’s net profit or loss. Traders, investors, and analysts rely on this metric to assess asset performance over specific periods. As such, it is fundamental for creating tools such as cryptocurrency wallets and trading platforms since it provides users with a clear overview of their portfolios’ performance.
Traditionally, obtaining this information requires extensive manual data aggregation, including tracking trades and cryptocurrency prices. However, this process can now be streamlined using a Web3 data provider like Moralis.
![](https://moralis.io/wp-content/uploads/2024/05/Blog-Moralis-Logo-1.png)
With Moralis’ crypto PnL feature, you can easily determine the overall profitability of a given address, analyze the PnL status of ERC-20s, and identify the top profitable wallets for specific tokens. To learn more about how this feature works and how it can benefit you, follow our comprehensive guide. Let’s dive in!
What is PnL in Crypto?
Crypto PnL, short for “profit and loss,” refers to the financial outcome of your trading activities. It is calculated based on the difference between the buying and selling prices of your cryptocurrency tokens. As such, crypto PnL provides an overview of how your assets are performing over a given period.
![Illustration of crypto PnL.](https://moralis.io/wp-content/uploads/2024/07/Blog-Crypto-PnL.png)
Crypto PnL is an essential metric for traders and analysts alike, offering a comprehensive overview of portfolio performance. Traders rely on it to assess the effectiveness of their strategies, while analysts use it for various purposes, including tax reporting.
If you’re building decentralized finance (DeFi) platforms, cryptocurrency wallets, decentralized exchanges (DEXs), portfolio trackers, or other Web3 platforms, incorporating crypto PnL is crucial. This feature will give your users a clear indication of how their assets are performing directly within your platform, boosting both engagement and retention.
However, calculating crypto PnL from scratch can be complex and time-consuming. It requires tracking all trades a user makes and monitoring prices at specific times, a process that can be tedious if done manually.
Fortunately, a Web3 data provider like Moralis can streamline this process. It eliminates the hassle of manual calculations, allowing you to focus on enhancing your platform’s features and user experience!
Introducing Moralis – The Industry’s Leading Crypto PnL API for Tracking Wallet Profit & Loss
With Moralis’ realized crypto PnL feature, you can seamlessly query the profit and loss status of any wallet. As such, this feature offers comprehensive insights into wallet and token profitability, making it easier to track and analyze profits/losses across tokens and identify the most profitable wallets for any ERC-20 token!
The realized crypto PnL feature includes three endpoints:
Wallet PnL Summary
Fetch the overall profitability of a given address over a specific time period, including total trading volume, total profit/loss, and other key metrics:
import fetch from 'node-fetch'; const options = { method: 'GET', headers: { accept: 'application/json', 'X-API-Key': 'YOUR_API_KEY' }, }; fetch('https://deep-index.moralis.io/api/v2.2/wallets/0xd8da6bf26964af9d7eed9e03e53415d37aa96045/profitability/summary?chain=eth', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
Example Response:
{ total_count_of_trades: 12, total_trade_volume: '3793782.5812942344', total_realized_profit_usd: '-20653.121064896484', total_realized_profit_percentage: -1.350177388165031, total_buys: 8, total_sells: 20, total_sold_volume_usd: '1509006.703335308', total_bought_volume_usd: '2284775.8779589264' }
Wallet PnL Breakdown
Get a detailed breakdown of buys, sells, and profit/loss for each ERC-20 token traded by a wallet. This endpoint helps you understand the performance of individual tokens in a portfolio:
import fetch from 'node-fetch'; const options = { method: 'GET', headers: { accept: 'application/json', 'X-API-Key': 'YOUR_API_KEY' }, }; fetch('https://deep-index.moralis.io/api/v2.2/wallets/0xd8da6bf26964af9d7eed9e03e53415d37aa96045/profitability?chain=eth', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
Example Response:
{ result: [ { token_address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', avg_buy_price_usd: '1250.89117636677242138858', avg_sell_price_usd: '1217.57108487456170834445', total_usd_invested: '765334.18465647742708110434311858', total_tokens_bought: '611.831148157427433472', total_tokens_sold: '611.831148157427433472', total_sold_usd: '744947.914822087616974748507047918740485538176519383', avg_cost_of_quantity_sold: '1250.89117636677242138858', count_of_trades: 5, realized_profit_usd: '-20386.26983438981010635958779520045817511936', realized_profit_percentage: -2.663708252302914, total_buys: 3, total_sells: 18, name: 'Wrapped Ether', symbol: 'WETH', decimals: 'WETH', logo: 'https://logo.moralis.io/0x1_0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_018112a9229b4bf1bf0d042beb7c2c55', logo_hash: '0a7fc292596820fe066ce8ce3fd6e2ad9d479c2993f905e410ef74f2062a83ec', thumbnail: 'https://logo.moralis.io/0x1_0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_018112a9229b4bf1bf0d042beb7c2c55', possible_spam: false }, //... ] }
Top Profitable Wallets by Token
Fetch the top profitable wallets that have traded a certain token, providing insights into the most successful traders for that asset:
import fetch from 'node-fetch'; const options = { method: 'GET', headers: { accept: 'application/json', 'X-API-Key': 'YOUR_API_KEY' }, }; fetch('https://deep-index.moralis.io/api/v2.2/erc20/0x7c9f4c87d911613fe9ca58b579f737911aad2d43/top-gainers?chain=eth', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
Example Response:
{ name: 'Wrapped Matic', symbol: 'WMATIC', decimals: '18', logo: 'https://logo.moralis.io/0x1_0x7c9f4c87d911613fe9ca58b579f737911aad2d43_128bc82fe3d945e59c5e67b540eb7ab5', possible_spam: false, result: [ { address: '0x202bb2fab1e35d940fde99b214ba49dafbcef62a', avg_buy_price_usd: '0.9183155718922578201779330700749389942841', avg_sell_price_usd: '1.59952463158282389246', total_tokens_bought: '50.425564410689794636', total_usd_invested: '46.30658101979248190652763127006057463808', total_tokens_sold: '50.423563438810080581', total_sold_usd: '80.6537317325558427356221252383435353256', avg_cost_of_quantity_sold: '0.91831557189225782928', count_of_trades: 2, realized_profit_usd: '34.34898823639942076774324344951973710758', realized_profit_percentage: 74.18027969262069 }, //... ] }
This overview covers our crypto PnL feature. Next, we’ll walk you through a tutorial on how to use these endpoints to track wallet profit and loss!
3-Step Tutorial: How to Track Wallet Profit & Loss
We’ll now show you how to track the profit and loss status of any wallet. And thanks to the accessibility of our premier API, you can get the data you need in three simple steps:
- Get a Moralis API Key
- Write a Script Calling the Wallet PnL Summary Endpoint
- Run the Code
However, before we get going, you’ll need to take care of a couple of prerequisites!
Prerequisites
Before moving on, make sure you have the following installed and set up:
Step 1: Get a Moralis API Key
Click the “Start for Free” button at the top right to sign up for an account with Moralis:
![Red arrow pointing at "Start for Free" button when getting crypto PnL](https://moralis.io/wp-content/uploads/2024/07/Blog-Crypto-PnL-Sign-Up-With-Moralis.png)
From there, you’ll find your API key directly under the “Home” tab:
![Red box highlighting the API key on Moralis' admin panel.](https://moralis.io/wp-content/uploads/2024/07/Blog-Crypto-PnL-Get-API-Key.png)
Copy and keep the key for the moment, as you’ll need it during the next step!
Step 2: Write a Script Calling the Wallet PnL Summary Endpoint
Open your preferred IDE, set up a folder, launch a terminal, and initialize a project with this command:
npm init
Install the needed dependencies with this terminal command:
npm install node-fetch --save npm install moralis @moralisweb3/common-evm-utils
From here, open your “package.json” file and add “type”: “module”
to the list:
!["type": "module" highlighted in code editor.](https://moralis.io/wp-content/uploads/2024/07/Blog-Crypto-PnL-Type-Module.png)
Create a new “index.js” file and add the following code:
import fetch from 'node-fetch'; const options = { method: 'GET', headers: { accept: 'application/json', 'X-API-Key': 'YOUR_API_KEY' }, }; fetch('https://deep-index.moralis.io/api/v2.2/wallets/0xd8da6bf26964af9d7eed9e03e53415d37aa96045/profitability/summary?chain=eth', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
Next, you need to configure the code slightly. Firstly, add your Moralis API key by replacing YOUR_API_KEY
. Secondly, configure the address
and chain
parameters to fit your query:
![Code configurations highlighted when getting crypto PnL](https://moralis.io/wp-content/uploads/2024/07/Blog-Crypto-PnL-Code-Config.png)
That’s it for the code. All that remains now is to execute the script!
Step 3: Run the Code
Open your terminal, go to the root folder of your project, and run this command:
node index.js
In return, you’ll receive a response showcasing the wallet’s total trading volume, total realized profit/loss, realized profit/loss percentage, and more. Here’s an example of what it might look like:
{ total_count_of_trades: 12, total_trade_volume: '3793782.5812942344', total_realized_profit_usd: '-20653.121064896484', total_realized_profit_percentage: -1.350177388165031, total_buys: 8, total_sells: 20, total_sold_volume_usd: '1509006.703335308', total_bought_volume_usd: '2284775.8779589264' }
That’s it! Tracking wallet profit and loss doesn’t have to be more challenging than this when using Moralis!
Use Case – Build a Crypto PnL Tracker
Now that you know how to track wallet profit and loss, you can leverage this data to build tools like a crypto PnL tracker. A crypto PnL tracker is a platform or tool that helps users monitor their portfolio performance effectively!
![Crypto PnL tracker.](https://moralis.io/wp-content/uploads/2024/07/Blog-Crypto-PnL-Use-Case.png)
Key features of a crypto PnL tracker include:
- Real-Time Portfolio Tracking: Monitor the performance of a portfolio in real-time, including the profit/loss status for individual ERC-20 tokens.
- Historical Data Analysis: Access historical performance data to analyze how investments have performed over specific periods.
- Tax Calculations: Utilize PnL data to assist users with their cryptocurrency taxes.
A crypto PnL tracker can be integrated into various platforms, such as portfolio trackers, cryptocurrency wallets, DEXs, and more, to improve the overall user experience of your dapps!
Beyond Crypto PnL & Tracking Wallet Profit & Loss – Diving Deeper Into Moralis
Moralis stands as the industry’s leading Web3 data provider, offering an extensive suite of Web3 APIs and RPC nodes beyond the crypto PnL feature. With Moralis, you can access all crypto data in one place, making it easy to develop sophisticated platforms like cryptocurrency wallets, portfolio trackers, and more.
![](https://moralis.io/wp-content/uploads/2024/06/Blog-Moralis-Logo-2.png)
But why choose Moralis’ Web3 APIs and RPC nodes?
- Comprehensive: Moralis delivers the most detailed API responses in the industry, providing more data with fewer calls. This efficiency enables you to build dapps faster and more effectively.
- Cross-Chain Compatibility: Moralis supports over 30 chains, including Ethereum, Polygon, BSC, and Optimism, offering full feature parity across all networks. This means you only need one provider for all your crypto needs.
- Secure: Benefit from enterprise-grade data security as Moralis is Web3’s first SOC 2 Type 2 certified data provider.
To fully grasp the capabilities of Moralis, let’s delve deeper into our suite of development tools, starting with our Web3 APIs!
Web3 APIs
In our suite of Web3 APIs, you’ll find ten use-case-specific interfaces. Below, we’ll introduce three prominent examples:
![](https://moralis.io/wp-content/uploads/2024/07/Blog-Crypto-PnL-Moralis-1024x475.jpg)
- Wallet API: With the Wallet API, you can effortlessly retrieve any wallet’s crypto PnL, token balances, transaction history, net worth, and much more. By leveraging this tool, you can seamlessly integrate wallet functionality into your dapps.
- Token API: The Token API allows you to access token balances, metadata, prices, and more with just a few lines of code. This makes the Token API an excellent tool for dapps requiring ERC-20 data, including DEXs, portfolio trackers, and more.
- NFT API: With the NFT API, you can effortlessly query NFT balances, metadata, prices, and more. If you’re looking to build NFT marketplaces, Web3 games, or similar platforms, be sure to check out the NFT API.
- Streams API: With the Streams API, you can effortlessly set up Web3 data pipelines at the click of a few buttons to stream data directly into your projects. As such, this powerful tool is ideal for setting up real-time alerts, populating databases, or enhancing dapps with up-to-date insights on tokens, wallets, and smart contracts.
- Price API: The Price API enables seamless retrieval of prices for any ERC-20 token or NFT. This advanced tool supports batch requests, allowing you to query the prices of multiple tokens simultaneously. It’s perfect for building DEXs, token trackers, or any platform requiring accurate pricing data.
Visit our official Web3 API page to learn more about the examples above and our other interfaces!
RPC Nodes
Moralis provides RPC nodes for over 30 blockchains, including Ethereum, Polygon, BSC, and many others. And our user-friendly, point-and-click interface makes setting up nodes a breeze!
![](https://moralis.io/wp-content/uploads/2024/07/Blog-Moralis-RPC-Nodes-1024x576.jpg)
But what sets our RPC nodes apart?
- Speed: Enjoy lightning-fast response times starting at just 70 ms, ensuring you receive the data you need without delay.
- Reliability: Our nodes are designed for maximum dependability, with an impressive 99.9% uptime.
- Security: Benefit from top-notch data security as the industry’s first SOC 2 Type 2 certified data provider.
To learn more, please visit our official RPC nodes page or check out one of the following guides, where we explore Fantom RPC nodes and Blast RPC nodes!
Summary: Crypto PnL API – How to Track Wallet Profit & Loss
Crypto PnL is a critical financial metric used to determine the net profit or loss of a cryptocurrency portfolio. Traders, analysts, and investors rely on this metric to track asset performance over specific time periods, making it essential for building everything from wallets to portfolio trackers.
![Crypto PnL summary.](https://moralis.io/wp-content/uploads/2024/07/Blog-Crypto-PnL-Summary.png)
However, calculating the profit and loss of a wallet from scratch can be a tedious and complex task, requiring constant monitoring of user transactions, token prices, and more. Thankfully, you can now seamlessly query the crypto PnL of any portfolio with a single line of code using a Web3 data provider like Moralis.
Moralis’ crypto PnL feature includes three core endpoints:
- Wallet PnL Summary: Obtain the overall profitability of a given wallet over a specified period. This includes total trading volume, total profit/loss, profit/loss percentage, and other key metrics.
- Wallet PnL Breakdown: Fetch a detailed breakdown of buys, sells, and profit/loss for each token traded by a wallet. This endpoint provides an in-depth understanding of the performance of individual tokens in a portfolio.
- Top Profitable Wallets by Token: Identify the top profitable wallets that have traded a specific token, offering insights into the most successful traders for that particular asset.
As such, when using Moralis, you can seamlessly track wallet profit and loss without breaking a sweat!
If you found this crypto PnL tutorial helpful, consider exploring more content on our blog. For example, check out our article on the DeBank API for information on our DeFi positions feature, or read about our Alchemy Webhooks guide for more information on streams.
Additionally, if you wish to track wallet profit and loss yourself, sign up with Moralis. You can create an account free of charge and gain immediate access to our suite of industry-leading tools.