🌲Traverse ERC6551 Tree

Learn how to use Airstack to traverse the ERC6551 tree, either upwards or downwards.

With ERC6551 accounts, NFT now has the capability to have an account on their own and perform writing actions to the blockchain as any other accounts on EVM. Thus, this also allows NFTs for the first time to own other assets, e.g. ERC20, ERC721, and ERC1155s.

Table Of Contents

Using Airstack, you can efficiently traverse the ERC6551 ownership tree, both upwards and downwards in just a single API call.

There are various use cases for traversing the ERC6551 ownership tree, which include but is not limited to:

  1. ERC6551 Ownership Visualization

  2. ERC6551-Compatible Wallet

  3. etc.

In this guide you will learn how to use Airstack to:

Pre-requisites

Get Started

JavaScript/TypeScript/Python

If you are using JavaScript/TypeScript or Python, Install the Airstack SDK:

React

npm install @airstack/airstack-react

Node

npm install @airstack/node

Then, add the following snippets to your code:

import { init, useQuery } from "@airstack/airstack-react";

init("YOUR_AIRSTACK_API_KEY");

const query = `YOUR_QUERY`; // Replace with GraphQL Query

const Component = () => {
  const { data, loading, error } = useQuery(query);

  if (data) {
    return <p>Data: {JSON.stringify(data)}</p>;
  }

  if (loading) {
    return <p>Loading...</p>;
  }

  if (error) {
    return <p>Error: {error.message}</p>;
  }
};

Other Programming Languages

To access the Airstack APIs in other languages, you can use https://api.airstack.xyz/gql as your GraphQL endpoint.

πŸ€– AI Natural Language​

Airstack provides an AI solution for you to build GraphQL queries to fulfill your use case easily. You can find the AI prompt of each query in the demo's caption or title for yourself to try.

Traverse Up ERC6551 Tree By ERC6551 Account Address

Suppose that you know the ERC6551 account address X and would like to get the NFT that own the given ERC6551 account and the ERC6551 account that hold the mentioned NFT as shown in the chart below.

Then, you can use the Accounts API to do so with the following query by providing the ERC6551 account address X to the address filter input:

Try Demo

Code

To query the upper-level NFTs & ERC6551 accounts in the ERC6551 ownership tree, simply add more tokenBalances nesting inside the most nested nft field.

query MyQuery {
  Accounts(
    input: {
      filter: { address: { _eq: "0xd8dc5794dd43aa9d7495f05bf110614ed32e950f" } }
      blockchain: base
    }
  ) {
    Account {
      nft {
        address
        tokenId
        tokenBalances {
          owner {
            addresses
            accounts {
              nft {
                address
                tokenId
                tokenBalances {
                  owner {
                    addresses
                    accounts {
                      nft {
                        address
                        tokenId
                        tokenBalances {
                          owner {
                            addresses
                            accounts {
                              nft {
                                address
                                tokenId
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Traverse Up ERC6551 Tree By NFT

Suppose that you know the NFT X and would like to get the ERC6551 account that own the given NFT and the NFT that hold by the mentioned ERC6551 account, and the higher-level layer ERC6551 accounts & NFTs and so on, as shown in the chart below.

Then, you can use the TokenBalances API to do so with the following query by providing the NFT X's token address and token ID to the tokenAddress and tokenId filter input, respectively:

Try Demo

Code

To query the upper-level NFTs & ERC6551 accounts in the ERC6551 ownership tree, simply add more tokenBalances nesting inside the most nested nft field.

query MyQuery {
  TokenBalances(
    input: {
      filter: {
        tokenAddress: { _eq: "0x99d3fd2f1cf2e99c43f95083b98033d191f4eabb" }
        tokenId: { _eq: "14" }
      }
      blockchain: base
    }
  ) {
    TokenBalance {
      owner {
        addresses
        accounts {
          nft {
            address
            tokenId
            tokenBalances {
              owner {
                addresses
                accounts {
                  nft {
                    address
                    tokenId
                    tokenBalances {
                      owner {
                        addresses
                        accounts {
                          nft {
                            address
                            tokenId
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Traverse Down ERC6551 Tree By EOA Address

Suppose that you know the user's EOA wallet address X and would like to get all the NFTs and ERC6551 accounts own, either directly or indirectly, by the EOA as shown in the chart below.

Then, you can use the TokenBalances API to do so with the following query by providing the EOA address X to the owner filter input:

Try Demo

Code

To query the lower-level NFTs & ERC6551 accounts in the ERC6551 ownership tree, simply add more tokenBalances nesting inside the most nested address field.

query MyQuery {
  TokenBalances(
    input: {
      blockchain: base
      filter: { owner: { _eq: "0x6da658f5840fecc688a4bd007ef6b314d9138135" } }
    }
  ) {
    TokenBalance {
      tokenAddress
      tokenId
      owner {
        addresses
      }
      tokenNfts {
        erc6551Accounts {
          address {
            addresses
            tokenBalances {
              tokenAddress
              tokenId
              tokenNfts {
                erc6551Accounts {
                  address {
                    addresses
                    tokenBalances {
                      tokenAddress
                      tokenId
                      tokenNfts {
                        erc6551Accounts {
                          address {
                            addresses
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Traverse Down ERC6551 Tree By NFT

Suppose that you know the user's EOA wallet address X and would like to get all the NFTs and ERC6551 accounts own, either directly or indirectly, by the EOA as shown in the chart below.

Then, you can use the Accounts API to do so with the following query by providing the NFT X's token address and token ID to the tokenAddress and tokenId filter input, respectively:

Try Demo

Code

To query the lower-level NFTs & ERC6551 accounts in the ERC6551 ownership, simply add more tokenBalances nesting inside the most nested address field.

query MyQuery {
  Accounts(
    input: {
      blockchain: base
      filter: {
        tokenAddress: { _eq: "0x99d3fd2f1cf2e99c43f95083b98033d191f4eabb" }
        tokenId: { _eq: "10" }
      }
    }
  ) {
    Account {
      address {
        addresses
        tokenBalances {
          tokenAddress
          tokenId
          tokenNfts {
            erc6551Accounts {
              address {
                addresses
                tokenBalances {
                  tokenAddress
                  tokenId
                  tokenNfts {
                    erc6551Accounts {
                      address {
                        addresses
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Developer Support

If you have any questions or need help regarding traversing the ERC6551 tree, please join our Airstack's Telegram group.

More Resources

Last updated

Was this helpful?