August 14, 2024

DeFi Protocol Data – Exploring the Easiest Way to Get a Wallet’s DeFi Positions

Table of Contents

Are you looking to access decentralized finance (DeFi) protocol data easily? If so, you’re in the right place. This guide introduces Moralis’ DeFi API – your go-to solution for querying DeFi data. With the DeFi API, you can effortlessly fetch protocol data for any wallet with just a single API call. Eager to get going? Here’s how to quickly retrieve a summary of the DeFi protocols a wallet interacts with:

import fetch from 'node-fetch';

const options = {
 method: 'GET',
 headers: {
   accept: 'application/json',
   'X-API-Key': 'YOUR_API_KEY'
 },
};

fetch('https://deep-index.developers.moralis.com/api/v2.2/wallets/0xcB1C1FdE09f811B294172696404e88E658659905/defi/summary?chain=eth', options)
 .then(response => response.json())
 .then(response => console.log(response))
 .catch(err => console.error(err));

Running the code above provides a summary of all DeFi protocols linked to a wallet, including the number of positions, total USD value, unclaimed rewards, protocol names, logos, and more. Here’s an example of the output:

{
  active_protocols: 8,
  total_positions: 12,
  total_usd_value: 871.6550972487607,
  total_unclaimed_usd_value: 26.31655680118138,
  protocols: [
    {
      protocol_name: 'Uniswap v3',
      protocol_id: 'uniswap-v3',
      protocol_url: 'https://app.uniswap.org',
      protocol_logo: 'https://cdn.developers.moralis.com/defi/uniswap.png',
      total_usd_value: 433.6700616074799,
      total_unclaimed_usd_value: 26.31655680118138,
      positions: 3
    },
    //...
  ]
}

That’s it! Fetching DeFi protocol data is easy with Moralis. For a detailed breakdown, check out the official DeFi API documentation or follow this guide to learn more.

Ready to start querying DeFi protocol data? Sign up for a free Moralis account to access our industry-leading development tools instantly!

Want to Use the Industry's
Leading Web3 APIs?

Overview

Wallet holders often have significant portions of their portfolios locked in DeFi protocols to, for instance, earn yield. However, tracking these positions can be challenging, complicating the development of tools like portfolio trackers, wallets, and tax platforms. That’s why we introduced our DeFi Positions feature.

Moralis DeFi Positions announcement.

With Moralis’ DeFi Positions, you can query DeFi protocol data with just a few lines of code. Are you curious about how it works? If so, join us in this guide, and we’ll show you. Let’s dive in!

What is DeFi Protocol Data?


DeFi protocol data encompasses detailed information about a wallet’s positions across various DeFi platforms, such as Uniswap and Aave. This data includes liquidity positions, yield farming activities and other interactions within the DeFi ecosystem. Given that many wallet holders lock assets in DeFi protocols, this data is crucial for building portfolio trackers, crypto tax platforms, and other Web3 projects, along with ensuring overall accurate financial reporting.

DeFi protocol data.

Traditionally, retrieving DeFi protocol data has been complex and time-consuming. However, Moralis’ DeFi API simplifies this process, offering a streamlined way to access comprehensive DeFi protocol data.

So, what exactly is Moralis’ DeFi API, and how does it work? Let’s explore its features and benefits!

Moralis’ DeFi API – The Easiest Way to Get DeFi Protocol Data

Moralis’ DeFi API is the simplest and most powerful way to access DeFi protocol data. This leading API provides a unified interface to query liquidity pairs, ERC-20 allowances, liquidity provider tokens, DeFi positions, and more – all in one place.

Moralis' DeFi API.

With robust cross-chain support, Moralis’ DeFi API enables you to retrieve data across major blockchain networks and protocols, including Ethereum, Polygon, BNB Smart Chain (BSC), and others.

As such, whether you’re developing lending platforms, portfolio trackers, or similar applications, Moralis’ DeFi API equips you with the necessary tools.

So, how can you use Moralis’ DeFi API to get protocol data? This is where our DeFi Positions feature comes into play!

Introducing Moralis’ DeFi Positions

One of the DeFi API’s most prominent features is DeFi Positions, which provides comprehensive insight into any wallet’s DeFi activities. But what does this actually entail? Our DeFi Positions feature comprises three key endpoints:

Summary DeFi Protocols by Wallet

Retrieve a comprehensive summary of all DeFi protocols associated with a specific wallet address. Below is an example of the 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.developers.moralis.com/api/v2.2/wallets/0xcB1C1FdE09f811B294172696404e88E658659905/defi/summary?chain=eth', options)
 .then(response => response.json())
 .then(response => console.log(response))
 .catch(err => console.error(err));

When you call this endpoint, you’ll receive a detailed summary of the DeFi protocols the wallet interacts with, including the total USD value of all positions, unclaimed rewards, protocol names, and additional relevant data. Here’s an example of the response:

{
  active_protocols: 8,
  total_positions: 12,
  total_usd_value: 871.6550972487607,
  total_unclaimed_usd_value: 26.31655680118138,
  protocols: [
    {
      protocol_name: 'Uniswap v3',
      protocol_id: 'uniswap-v3',
      protocol_url: 'https://app.uniswap.org',
      protocol_logo: 'https://cdn.developers.moralis.com/defi/uniswap.png',
      total_usd_value: 433.6700616074799,
      total_unclaimed_usd_value: 26.31655680118138,
      positions: 3
    },
    //...
  ]
}

Summary DeFi Positions by Wallet

Fetch a summary of all DeFi positions associated with a wallet, including unclaimed rewards for each position. Down below, you’ll find an example of what it might look like when calling this endpoint: 

import fetch from 'node-fetch';

const options = {
  method: 'GET',
  headers: {
    accept: 'application/json',
    'X-API-Key': 'YOUR_API_KEY'
  },
};

fetch('https://deep-index.developers.moralis.com/api/v2.2/wallets/0xcB1C1FdE09f811B294172696404e88E658659905/defi/positions?chain=eth', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

Running the code above returns a detailed summary of all DeFi positions for a wallet. This includes position labels, associated tokens, USD values for each position, and other relevant details. Here’s an example output:

[
  {
    "protocol_name": "Uniswap v2",
    "protocol_id": "uniswap-v2",
    "protocol_url": "https://app.uniswap.org/pools/v2",
    "protocol_logo": "https://cdn.developers.moralis.com/defi/uniswap.png",
    "position": {
      "label": "liquidity",
      "tokens": [
        {
          "token_type": "supplied",
          "name": "USD Coin",
          "symbol": "USDC",
          "contract_address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
          "decimals": "6",
          "logo": "https://logo.developers.moralis.com/0x1_0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48_ac01a87aaf98ddb0f349ee4ebe97f0d8",
          "thumbnail": "https://logo.developers.moralis.com/0x1_0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48_ac01a87aaf98ddb0f349ee4ebe97f0d8",
          "balance": "9762775.56142477145245169357",
          "balance_formatted": "9.76277556142477145245169357",
          "usd_price": 0.9999274263552301,
          "usd_value": 9.76206704121921
        },
        //...
      ],
      "address": "0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc",
      "balance_usd": 19.509109037404016,
      "total_unclaimed_usd_value": 5.127740503730312,
      "position_details": {
        "reserve0": "9.76277556142477145245169357",
        "reserve1": "0.00356975548622332765688138345279600856",
        "factory": "0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f",
        "pair": "0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc",
        "share_of_pool": 0.000021077221080781
      }
    }
  },
  //...
]

Detailed DeFi Positions by Wallet & Protocol

Query detailed data on positions for a specific wallet and protocol. Here’s an example of what it might look like:

import fetch from 'node-fetch';

const options = {
 method: 'GET',
 headers: {
   accept: 'application/json',
   'X-API-Key': 'YOUR_API_KEY'
 },
};

fetch('https://deep-index.developers.moralis.com/api/v2.2/wallets/0xcB1C1FdE09f811B294172696404e88E658659905/defi/uniswap-v3/positions?chain=eth', options)
 .then(response => response.json())
 .then(response => console.log(response))
 .catch(err => console.error(err));

Running the code returns comprehensive DeFi positions for the specified wallet and protocol, including position labels, tokens, USD values for each position, unclaimed rewards, and more. See an example response below:

{
  "protocol_name": "Uniswap v3",
  "protocol_id": "uniswap-v3",
  "protocol_url": "https://app.uniswap.org",
  "protocol_logo": "https://cdn.developers.moralis.com/defi/uniswap.png",
  "total_usd_value": 433.28689509249983,
  "total_unclaimed_usd_value": 26.291817301924766,
  "positions": [
    {
      "label": "liquidity",
      "tokens": [
        {
          "token_type": "supplied",
          "name": "Coinbase Wrapped Staked ETH",
          "symbol": "cbETH",
          "contract_address": "0xbe9895146f7af43049ca1c1ae358b0541ea49704",
          "decimals": "18",
          "logo": "https://logo.developers.moralis.com/0x1_0xbe9895146f7af43049ca1c1ae358b0541ea49704_22de8d667f6e46b09b33c805e7973049",
          "thumbnail": "https://logo.developers.moralis.com/0x1_0xbe9895146f7af43049ca1c1ae358b0541ea49704_22de8d667f6e46b09b33c805e7973049",
          "balance": "0",
          "balance_formatted": "0",
          "usd_price": 2937.5037898342443,
          "usd_value": 0
        },
        //...
        {
          "token_type": "reward",
          "name": "Coinbase Wrapped Staked ETH",
          "symbol": "cbETH",
          "contract_address": "0xbe9895146f7af43049ca1c1ae358b0541ea49704",
          "decimals": "18",
          "logo": "https://logo.developers.moralis.com/0x1_0xbe9895146f7af43049ca1c1ae358b0541ea49704_22de8d667f6e46b09b33c805e7973049",
          "thumbnail": "https://logo.developers.moralis.com/0x1_0xbe9895146f7af43049ca1c1ae358b0541ea49704_22de8d667f6e46b09b33c805e7973049",
          "balance": "880092663147703",
          "balance_formatted": "0.000880092663147703",
          "usd_price": 2937.5037898342443,
          "usd_value": 2.5852755334016906
        },
        //...
      ],
      "address": "0x840deeef2f115cf50da625f7368c24af6fe74410",
      "balance_usd": 78.95335940631567,
      "total_unclaimed_usd_value": 5.121540153915798,
      "position_details": {
        "nft_metadata": {
          "name": "Uniswap - 0.05% - cbETH/WETH - 0.95028<>0.95218",
          "description": "This NFT represents a liquidity position in a Uniswap V3 cbETH-WETH pool.//...",
          "image": "//..."
        },
        //...
    },
  ]
  //...
}

With these three endpoints, you can gain unparalleled insight into the DeFi positions of any wallet – all with a single API call!

Supported Protocols

The DeFi Positions feature currently supports 13 major protocols, including:

  • Uniswap-v3
  • Uniswap-v2
  • Pancakeswap-v3
  • Pancakeswap-v2
  • Quickswap-v2
  • Sushiswap-v2
  • Aave-v3
  • Aave-v2
  • Fraxswap-v2
  • Fraxswap-v1
  • Lido
  • Makerdao
  • Eigenlayer

This is just the beginning – our team at Moralis is continuously expanding support by adding new protocols frequently!

Tutorial: How to Get DeFi Protocol Data in 3 Steps

In this tutorial, we’ll guide you through the process of retrieving DeFi protocol data using our DeFi API. And thanks to our user-friendly interface, you can achieve this in just three simple steps:

  1. Get a Moralis API Key
  2. Write a Script Calling the getDefiSummary() Endpoint
  3. Run the Code

But before you get going, you need to deal with a few prerequisites! 

Prerequisites

If you haven’t already, make sure you have the following ready before continuing: 

  • Node v14+
  • npm/yarn

Step 1: Get a Moralis API Key

To get your key, first sign up with Moralis by clicking the ”Start for Free” button at the top right:

Next, log in, and you’ll find your key directly under the ”Home” tab:

Red arrow pointing at Moralis API Key.

Copy and keep the key for now, as you’ll need it during the second step.

Step 2: Write a Script Calling the getDefiSummary() Endpoint

Open your preferred integrated development environment (IDE), set up a folder, and initialize a new project with the following terminal command:

npm init

Next, run the command below to install the required dependencies:

npm install node-fetch --save
npm install moralis @moralisweb3/common-evm-utils

Then, open your ”package.json” file and add "type": "module" to the list:

Adding type: module to package.json file.

Create a new ”index.js” file and add the following script:

import fetch from 'node-fetch';

const options = {
 method: 'GET',
 headers: {
   accept: 'application/json',
   'X-API-Key': 'YOUR_API_KEY'
 },
};

fetch('https://deep-index.developers.moralis.com/api/v2.2/wallets/0xcB1C1FdE09f811B294172696404e88E658659905/defi/summary?chain=eth', options)
 .then(response => response.json())
 .then(response => console.log(response))
 .catch(err => console.error(err));

Now, you need to configure the code. Start by replacing YOUR_API_KEY with the key you copied during the first step. Then, configure the address parameter to fit your query:

Red arrows pointing at "YOUR_API_KEY" and address parameter.

That’s it! All that remains is to execute the script.

Step 3: Run the Code

To execute the script, open a new terminal, navigate to your project’s root folder, and run the following command:

node index.js

In return, you’ll receive a summary of the wallet’s DeFi positions. The response will include the total USD value of all positions, unclaimed rewards, protocol names, and more. Here’s an example of what it might look like:

{
  active_protocols: 8,
  total_positions: 12,
  total_usd_value: 871.6550972487607,
  total_unclaimed_usd_value: 26.31655680118138,
  protocols: [
    {
      protocol_name: 'Uniswap v3',
      protocol_id: 'uniswap-v3',
      protocol_url: 'https://app.uniswap.org',
      protocol_logo: 'https://cdn.developers.moralis.com/defi/uniswap.png',
      total_usd_value: 433.6700616074799,
      total_unclaimed_usd_value: 26.31655680118138,
      positions: 3
    },
    //...
  ]
}

Congratulations! You now know how to seamlessly retrieve protocol data using the Moralis DeFi API. You can follow the same steps to call any of our other endpoints!

Benefits & Use Cases for DeFi Protocol Data

Moralis’ DeFi Positions feature offers versatile applications across various sectors. Here are four key examples:

  • Enhanced Portfolio Management: Developers can elevate portfolio management by integrating detailed DeFi activity insights, driving higher user engagement and retention.
  • Comprehensive Wallet Insight: Portfolio trackers can deliver deeper visibility into users’ DeFi activities, providing a more complete financial overview.
  • Precise Tax Reporting: Crypto tax companies can improve reporting accuracy by leveraging comprehensive DeFi protocol data.
  • Informed Investment Decisions: DeFi-centric investors and analysts can make smarter trading choices with granular DeFi protocol and position data.
DeFi protocol use cases.

In conclusion, our top-tier DeFi Positions feature can come in handy when building a wide range of dapps across multiple industries! 

Beyond DeFi Positions – Exploring Moralis Further

Moralis is the industry’s leading Web3 data provider, consolidating all crypto data in one place. With our robust Web3 APIs and premium RPC nodes, accessing the data you need requires just a few lines of code. As such, whether you’re building a portfolio tracker, crypto tax tool, or any other platform, Moralis has you covered.

Moralis Logo.

What sets our development tools apart?

  • Comprehensive: Designed for efficiency, Moralis’ tools give you more data with less effort, allowing you to build Web3 projects faster and more effectively.
  • Cross-Chain Compatibility: Our Web3 APIs offer full feature parity across 30+ blockchain networks, so you can rely on a single provider for all your data needs.
  • Enterprise-Grade Security: As SOC 2 Type 2 compliant, Moralis ensures enterprise-level data security across all our products.

Now that you have an overview of Moralis, let’s dive into the specifics of our development tools below!

Web3 APIs

The Moralis Web3 API suite offers a comprehensive set of tools designed to streamline your developer experience with seamless access to blockchain data. Here are five key APIs:

Moralis logo.
  • Wallet API: Simplify wallet data integration with the Wallet API. Retrieve a wallet’s history, token balances, net worth, profitability, and more with just a few lines of code.
  • Token API: Get ERC-20 token data effortlessly with the Moralis Token API. This interface provides seamless access to token balances, metadata, prices, transactions, and more, making it ideal for integrating ERC-20 data into wallets, portfolio trackers, and similar platforms.
  • NFT API: The NFT API is your go-to solution for all things NFT. Whether you need NFT balances, prices, or up-to-date metadata, this API delivers. It’s perfect for building NFT marketplaces, Web3 games, and other NFT-centric applications.
  • Price API: Fetch real-time and historical price data with Moralis’ Price API. This powerful tool supports ERC-20 tokens and NFTs, making it easy to integrate accurate price data into your dapps.
  • Streams API: Stay ahead with the Streams API, the industry-leading solution for real-time blockchain data. Our intuitive point-and-click interface lets you set up Web3 data pipelines effortlessly, so you can stream on-chain events directly into your dapp as they happen.

Discover more about our cutting-edge APIs on the Moralis Web3 API page.

RPC Nodes

Moralis’ node solution allows you to set up RPC nodes for 30+ chains seamlessly with just a few clicks. Supported chains include Ethereum, BSC, Base, Optimism, Polygon, and many more. So, if you need nodes, be sure to check out Moralis’ RPC nodes!

Text: "Ultimate PRC Nodes for Web3"

Here are three key reasons to choose our nodes:

  • Reliable: Our nodes boast an impressive 99.9% uptime, ensuring you get the data you need even if your dapp experiences downtime.
  • Fast: Moralis’ nodes set the benchmark for speed with response times as low as 70 ms.
  • Extended RPC Methods: With our Extended RPC Methods, you can query decoded data directly from your nodes – fetch ERC-20 token balances, metadata, native transactions, and much more with single requests.

To learn more about how this works, please visit our RPC nodes page!

Summary: DeFi Protocol Data – Exploring the Easiest Way to Get a Wallet’s DeFi Positions

Cryptocurrency wallet holders often lock up large amounts of their assets in DeFi protocols to earn yield. This makes tracking DeFi positions crucial for the financial accuracy of crypto tax platforms, portfolio trackers, and similar services. However, fetching a wallet’s DeFi positions has traditionally been a cumbersome task. Fortunately, this is where Moralis’ DeFi API comes into play!

DeFi protocol data.

The DeFi API is your go-to tool for DeFi data. This premier interface offers a DeFi Positions feature, allowing you to effortlessly query DeFi protocol data for any wallet. But what exactly does this entail? The DeFi Positions feature provides three main endpoints:

  • Summary of DeFi Protocols by Wallet: Get a summary of all DeFi protocols a wallet interacts with, including the total USD value locked across protocols, the number of DeFi positions, and other protocol-specific details.
  • Summary of DeFi Positions by Wallet: Get a summary of all DeFi positions of a wallet, including position labels, token types, the value of each position, unclaimed rewards, and more.
  • Detailed DeFi Positions by Wallet & Protocol: Fetch detailed position data for a given wallet and protocol, including position labels, token types, prices, unclaimed rewards, etc.

If you found this article on DeFi protocol data helpful, check out more content on the Moralis blog. For instance, explore our guide on the best Alchemy Custom Webhooks alternative or learn how to use our Crypto PnL API.

Ready to dive into DeFi protocol data? Sign up with Moralis for free and get instant access to our APIs and RPC nodes!

Market Data API
Build amazing trading and portfolio dapps with this Market Data API. Keep users engaged with up-to-date data!
Market Data API
Related Articles
December 26, 2022

Exploring Verse – A New Metaverse Programming Language from Epic Games

December 19, 2022

How to Use a Web3 JS Call Contract Function

November 29, 2023

List of Smart Contract Ideas and Examples for Developers

August 3, 2022

Full 2022 Guide to Different Types of DAOs

February 20, 2023

Oasis Testnet Faucet – Get Testnet Funds for the Oasis Network

March 3, 2023

Optimism Goerli Faucet – How to Get Free Optimism Testnet Funds

September 12, 2022

How to Build a Cronos Dapp in 5 Steps

November 27, 2022

Blockchain Infrastructure Companies – How to Choose the Right Provider

October 20, 2022

How to Create a Decentralized Website on Ethereum