Querying the balance of assets in your users blockchain wallet is one of the most basic and critical building blocks of DeFi. Given the exponential growth in the number of users and new tokens, this operation has become prohibitively expensive for dApp developers.

Every time a DeFi user logs in or refreshes a popular app, in the backend, there's hundreds of requests happening. On average 30-40% are wallet balance lookups for the various tokens in their portfolio. This is one of the biggest chokepoints in usability for products that well funded startups have setup internal infrastructure to alleviate.

As you can see below this issue has been raised multiple times in the past:

Others have built subgraphs to get data for single tokens, but not for multiple tokens or a way to batch requests:


This was exactly the motivaton for us to build a fast multi-token wallet balance tool to do batch token balance lookups for the most common tokens in DeFi. Current infrastructure, Infura, Etherscan & others, restrict lookups to one token per wallet per request and cause latency bottlenecks in UX for DeFi products.

See the deployed subgraph here: https://thegraph.com/explorer/subgraph/valve-finance/defi-balance

We believe this data is extremely useful for analytics, portfolio trackers, aggregators, DEXs, wallets, and every other type of DeFi products that would require fast wallet balance lookups for a large set of users. With our new fast multi-token balance updater, there should be at least 10x performance improvement due to the substantial decrease in API calls needed to update user wallet balances.

Given up to a 1000 wallet addresses, and a block ID, we will return the token balances of the DeFi pulse index for those wallets. You can also cut it by category: lending, DEXs, Derivatives, Assets.

These subgraph(s) would enable developers to query in the following ways:

  • Token wallet balances of all DEX, Lending, Derivatives, Asset tokens in 1-2 requests vs 9-12 requests traditionally
  • Top wallet balances of all DEX type DeFi tokens
  • DEX type token balance for the specified 1000 wallet addresses
  • Greater/Less/Equal to specified wallet balance for DEX type DeFi tokens
  • Asset balances at a particular block ID for airdrops

We believe our fast multi-token wallet balance tool will have an impact on multiple parties:

  • Applications: ability to lookup multiple tokens, wallets at once and thus easily 10x improvement in speed
  • End-Users: snappier performance that is akin to using a centralized infrastructure app
  • Developers: no longer need to maintain wallet balances locally since the subgraph will have the latest information ready for them

Features & Capabilities

Our subgraph tracks the token balances of the most popular DeFi tokens and presents them in a easily queriable manner. The assets we currently track are:

  • Stable Coin Tokens: DAI, USDC, USDT, TUSD, BUSD
  • Lending Tokens: MKR, COMP, AAVE, LQTY, ALPHA, ALCX
  • DEX Tokens: UNI, CRV, BAL, SUSHI, BNT, LRC
  • Derivatives Tokens: SNX, NXM, BOND, HEGIC, FST, DDX
  • Asset Manager Tokens: YFI, VSP, BADGER, REN, FARM
  • Payment Tokens: MATIC, TORN, STAKE

Below are a few examples of types of queries that can be run on our subgraph https://thegraph.com/explorer/subgraph/valve-finance/defi-balance:

Get all supported assets

{
  assets(first: 100) {
    id
    name
    symbol
    decimals
    category
  }
}

Get Top 100 holders of a given asset

{
  accountTokens (
    first: 100
    orderBy: balance
    orderDirection: desc
    where: {
      balance_gt: 0
      symbol: "DAI"
    }
  ) {
    userID
    balance
  }
}

Get balance of DPI assets for given list of wallets (up to 1000 wallets)

{
  accounts (
    where: {
      id_in: [
        "0x0000000000007f150bd6f54c40a34d7c3d5e9f56",
        "0x00000000b7ca7e12dcc72290d1fe47b2ef14c607",
        "0x0000006daea1723962647b7e189d311d757fb793"
      ]
    }
  ) {
    id
    tokens (
      where: {
        balance_gt: 0
      }
    ) {
      symbol
      balance
    }
  }
}

Get balance for a category of assets

{
  accounts (
    where: {
      id_in: [
        "0x0000000000007f150bd6f54c40a34d7c3d5e9f56",
        "0x00000000b7ca7e12dcc72290d1fe47b2ef14c607",
        "0x0000006daea1723962647b7e189d311d757fb793"
      ]
    }
  ) {
    id
    tokens (
      where: {
        category: "Lending"
        balance_gt: 0
      }
    ) {
      symbol
      balance
    }
  }
}

Learnings & Useful Tips

Our overall experience working with the Graph software & community has been overwhelmingly positive. We had a lot of questions regarding setup, debugging, and organizing the subgraph entities that were answered by the helpful community members in the Graph discord.

Here are a few things we learned on the way that might help you with your subgraph development.

GRAFTING

  • It is one of the most useful features we found while developing this subgraph. If for some reason your subgraph fails during deployment, you can use this feature to re-deploy a newer version of a subgraph without having to index from genesis block. All you need to do is specify the previous graph id and the block you'd like to copy till in your subgraph.yaml file.
specVersion: 0.0.2
description: Batch or categorically lookup DeFi token balances
repository: https://github.com/valve-finance/defi-balance
schema:
  file: ./schema.graphql
graft:
  base: Qmav5pEiXBKfa8ycG1sGZfp7PVfkEuGGmLyQ6VmEaHTMyK
  block: 7006712

TIME TRAVEL QUERIES

  • This was a super useful feature that enable you to query data at a certain block id without us having to store the information. A great use case for this query is to determine which addresses should recieve airdrops. See below for how to get a token balance at specified block number:
{
  accounts(
    where: {
      id_in: [
        "0x0000000000007f150bd6f54c40a34d7c3d5e9f56",
        "0x00000000b7ca7e12dcc72290d1fe47b2ef14c607",
        "0x0000006daea1723962647b7e189d311d757fb793"
      ]
    }
    block: {number: 12459565}
  ) {
    id
    tokens {
      symbol
      balance
    }
  }
}

You can see the working subgraph here: https://thegraph.com/explorer/subgraph/valve-finance/defi-balance

If you have any questions or features you'd like to see, please contact us at hello@simpleid.xyz.