How to get all the portfolios owned by an address in the protocol

Having the portfolios of a wallet address can be useful for auditing purposes or for building applications that interact with the protocol. If you want to get all the portfolios owned by an address in the protocol, Chainbase offers an API called getAccountPortfolios, through which you can access to Chainbase datasets through one single call.

Overview - Tools you need to work with Chainbase

  1. A free account at Chainbase with an API key.
  2. An IDE. Our examples are shown in JavaScript, you can use VS Code as your IDE for example.
  3. A wallet address as your input.

Step 1: Set up a free account at Chainbase

To better leverage the ability that Chainbase provides, you can register here for a free account and access to different APIs and data cloud.

After logging into Chainbase, visit the dashboard to get an overview. Create a new project in the console and get an API key.

Step 2: Write script using Chainbase API

  1. Using fetch in JavaScript.
wallet_addr = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; // Take Vitalik's wallet address as an example.

fetch(`https://api.chainbase.online/v1/account/portfolios?address=${wallet_addr}`, {
    method: 'GET',
    headers: {
        'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
        'accept': 'application/json'
    }
}).then(response => response.json())
    .then(data => console.log(data.data))
    .catch(error => console.error(error));
  1. Using axios in JavaScript. You need to install axios using npm install axios --save in the terminal first.
wallet_addr = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; // Take Vitalik's wallet address as an example.

const axios = require('axios');
const options = {
    url: `https://api.chainbase.online/v1/account/portfolios?address=${wallet_addr}`,
    method: 'GET',
    headers: {
        'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
        'accept': 'application/json'
    }
};
axios(options)
    .then(response => console.log(response.data.data))
    .catch(error => console.log(error));

Step 3: Print DeFi portfolios

Chainbase API getAccountPortfolios takes the chain id and wallet address as parameters, and returns the DeFi portfolios of the wallet.

To get data printed, run command node <filename>.js in the terminal. In this case, the returned data looks like as below.

[
    {
      "id": "uniswap_v2",
      "name": "uniswap_v2",
      "blockchain": "ethereum",
      "logo_url": "https://static.chainbase.online/token_icons/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984.png",
      "portfolios": [
        {
          "pool": {
            "address": "0x3d1cb7638d73657f7e554eda1d97703bc29d3c15",
            "created_at": "2020-09-09T21:43:36"
          },
          "type": "liquidity",
          "assets_deposited": [
            {
              "asset": {
                "id": "0xa585cb4e6e833d6fa55a992e75b9ee0743b23d69",
                "logo_url": null,
                "name": "",
                "symbol": "",
                "type": ""
              },
              "amount": -1,
              "usd_value": 0
            },
            {
              "asset": {
                "id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                "logo_url": "https://static.chainbase.online/token_icons/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
                "name": "WETH",
                "symbol": "WETH",
                "type": ""
              },
              "amount": 0.1378100814794963,
              "usd_value": 249.6605333854962
            }
          ],
          "assets_rewarded": null,
          "stats": {
            "usd_value": 249.6605333854962
          }
        },
        {
          "pool": {
            "address": "0x87ffcda0c507117683f0bc0defb683eebe9c223b",
            "created_at": "2021-02-28T18:47:23"
          },
          "type": "liquidity",
          "assets_deposited": [
            {
              "asset": {
                "id": "0x693391144d1e079e20cc64f795e9450c94966171",
                "logo_url": null,
                "name": "",
                "symbol": "",
                "type": ""
              },
              "amount": -1,
              "usd_value": 0
            },
            {
              "asset": {
                "id": "0x6b175474e89094c44da98b954eedeac495271d0f",
                "logo_url": "https://static.chainbase.online/token_icons/0x6b175474e89094c44da98b954eedeac495271d0f.png",
                "name": "Dai",
                "symbol": "DAI",
                "type": ""
              },
              "amount": 6.210360351628101,
              "usd_value": 6.210360351628101
            }
          ],
          "assets_rewarded": null,
          "stats": {
            "usd_value": 6.210360351628101
          }
        },
        {
          "pool": {
            "address": "0xa05dbff666f155dbef69fe0cde810a85225dcb7f",
            "created_at": "2022-11-23T16:20:47"
          },
          "type": "liquidity",
          "assets_deposited": [
            {
              "asset": {
                "id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                "logo_url": "https://static.chainbase.online/token_icons/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
                "name": "WETH",
                "symbol": "WETH",
                "type": ""
              },
              "amount": 1.3979513482467887,
              "usd_value": 2532.5671061459593
            },
            {
              "asset": {
                "id": "0xc6e8e7cd99f8682dfc7641d5c044e5b748e0cce4",
                "logo_url": null,
                "name": "",
                "symbol": "",
                "type": ""
              },
              "amount": -1,
              "usd_value": 0
            }
          ],
          "assets_rewarded": null,
          "stats": {
            "usd_value": 2532.5671061459593
          }
        },
        {
          "pool": {
            "address": "0xa7f8de1f0e6964201d412fd172e5740663b35567",
            "created_at": "2021-12-25T22:48:33"
          },
          "type": "liquidity",
          "assets_deposited": [
            {
              "asset": {
                "id": "0x8df994371e3cfe3d218d2b8f42b5bf414651fcd8",
                "logo_url": null,
                "name": "",
                "symbol": "",
                "type": ""
              },
              "amount": -1,
              "usd_value": 0
            },
            {
              "asset": {
                "id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                "logo_url": "https://static.chainbase.online/token_icons/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
                "name": "WETH",
                "symbol": "WETH",
                "type": ""
              },
              "amount": 0.17474029229976862,
              "usd_value": 316.56431888829906
            }
          ],
          "assets_rewarded": null,
          "stats": {
            "usd_value": 316.56431888829906
          }
        }
      ]
    },
    {
      "id": "shibaswap",
      "name": "shibaswap",
      "blockchain": "ethereum",
      "logo_url": "https://static.chainbase.online/token_icons/0x38e3b07d607def629b4f3f46a0082006fdd6cda2.png",
      "portfolios": [
        {
          "pool": {
            "address": "0x76ec974feaf0293f64cf8643e0f42dea5b71689b",
            "created_at": "2023-03-09T06:30:47"
          },
          "type": "liquidity",
          "assets_deposited": [
            {
              "asset": {
                "id": "0x198065e69a86cb8a9154b333aad8efe7a3c256f8",
                "logo_url": null,
                "name": "",
                "symbol": "",
                "type": ""
              },
              "amount": -1,
              "usd_value": 0
            },
            {
              "asset": {
                "id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                "logo_url": "https://static.chainbase.online/token_icons/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
                "name": "WETH",
                "symbol": "WETH",
                "type": ""
              },
              "amount": 0.0315879559920359,
              "usd_value": 57.22560974396202
            }
          ],
          "assets_rewarded": null,
          "stats": {
            "usd_value": 57.22560974396202
          }
        }
      ]
    },
    {
      "id": "uniswap_v3",
      "name": "uniswap_v3",
      "blockchain": "ethereum",
      "logo_url": "https://static.chainbase.online/token_icons/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984.png",
      "portfolios": [
        {
          "pool": {
            "address": "0x24a3ab2a20f5e903b5bb3efc3b4a60fcda17b496",
            "created_at": "2021-08-16T19:44:59"
          },
          "type": "liquidity",
          "assets_deposited": [
            {
              "asset": {
                "id": "0x6793655c396eaee311fb4d3596b88fd7d195c79e",
                "logo_url": null,
                "name": "",
                "symbol": "",
                "type": ""
              },
              "amount": -1,
              "usd_value": 0
            },
            {
              "asset": {
                "id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                "logo_url": "https://static.chainbase.online/token_icons/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
                "name": "WETH",
                "symbol": "WETH",
                "type": ""
              },
              "amount": 0.05291463555431614,
              "usd_value": 95.86160892267687
            }
          ],
          "assets_rewarded": null,
          "stats": {
            "usd_value": 95.86160892267687
          }
        },
        {
          "pool": {
            "address": "0x21c6f5d2ecea9950e19ab229a7705ac23e43a35e",
            "created_at": "2022-09-27T19:20:59"
          },
          "type": "liquidity",
          "assets_deposited": [
            {
              "asset": {
                "id": "0x2321537fd8ef4644bacdceec54e5f35bf44311fa",
                "logo_url": null,
                "name": "",
                "symbol": "",
                "type": ""
              },
              "amount": -1,
              "usd_value": 0
            },
            {
              "asset": {
                "id": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
                "logo_url": "https://static.chainbase.online/token_icons/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png",
                "name": "USD Coin",
                "symbol": "USDC",
                "type": ""
              },
              "amount": 5693.769978,
              "usd_value": 5683.0941592912495
            }
          ],
          "assets_rewarded": null,
          "stats": {
            "usd_value": 5683.0941592912495
          }
        }
      ]
    }
  ]

API Reference

If you want to know more details on the endpoint and optional parameters, check out:

Support

If you meet any trouble following the tutorial, feel free to reach out to us in our Discord to get 24/7 community support.