-2

i have been trying to get the nfts of an account on aptos testnet but i couldn't figure out how to do so.

i tried using https://fullnode.testnet.aptoslabs.com/v1/accounts/:ACCOUNT_ADDRESS/resources but i can not understand the output it is this

[{"type":"0x1::account::Account","data":{"authentication_key":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","coin_register_events":{"counter":"1","guid":{"id":{"addr":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","creation_num":"0"}}},"guid_creation_num":"8","key_rotation_events":{"counter":"0","guid":{"id":{"addr":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","creation_num":"1"}}},"rotation_capability_offer":{"for":{"vec":[]}},"sequence_number":"1","signer_capability_offer":{"for":{"vec":[]}}}},{"type":"0x3::token::TokenStore","data":{"burn_events":{"counter":"0","guid":{"id":{"addr":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","creation_num":"6"}}},"deposit_events":{"counter":"1","guid":{"id":{"addr":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","creation_num":"4"}}},"direct_transfer":false,"mutate_token_property_events":{"counter":"0","guid":{"id":{"addr":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","creation_num":"7"}}},"tokens":{"handle":"0xe05367e99da54cf3252a7eb215fbf027164bedb2d728b3828ca3cfe0fac7f1c4"},"withdraw_events":{"counter":"0","guid":{"id":{"addr":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","creation_num":"5"}}}}},{"type":"0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>","data":{"coin":{"value":"249897700"},"deposit_events":{"counter":"1","guid":{"id":{"addr":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","creation_num":"2"}}},"frozen":false,"withdraw_events":{"counter":"0","guid":{"id":{"addr":"0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a","creation_num":"3"}}}}}]

where (0x5d9c48db8e45486d5b019a869c505f46b20242c32db194ac0beb14322b53164a) is my address.

1 Answers1

0

The simplest way to make a query like this would be to use the indexer.

Query:

query CurrentTokens($owner_address: String, $offset: Int) {
  current_token_ownerships(
    where: {owner_address: {_eq: $owner_address}, amount: {_gt: "0"}, table_type: {_eq: "0x3::token::TokenStore"}}
    order_by: {last_transaction_version: desc}
    offset: $offset
  ) {
    token_data_id_hash
    name
    collection_name
    property_version
    amount
  }
}

Query variables:

{
  "owner_address": "0xaa921481e07b82a26dbd5d3bc472b9ad82d3e5bfd248bacac160eac51687c2ff",
  "offset": 0
}

You would then use this from whichever GraphQL query library you prefer. For example, using graphql-request in JS / TS:

import { request, gql } from "graphql-request";

const indexerUrl =
  "https://indexer-testnet.staging.gcp.aptosdev.com/v1/graphql";

const tokensOwnedByAccountQueryDoc = gql`
  query TokensOwnedByAccount($owner_address: String, $offset: Int) {
    current_token_ownerships(
      where: {
        owner_address: { _eq: $owner_address }
        amount: { _gt: "0" }
        table_type: { _eq: "0x3::token::TokenStore" }
      }
      order_by: { last_transaction_version: desc }
      offset: $offset
    ) {
      creator_address
      collection_name
    }
  }
`;


async function fetchTokensOwnedByAccount(userAccountAddress: string) {
  // TODO: Do pagination.
  const variables = { owner_address: userAccountAddress, offset: 0 };
  return await request({
    url: indexerUrl,
    document: tokensOwnedByAccountQueryDoc,
    variables,
  });
}

You can read more about this here: https://aptos.dev/guides/indexing#example-token-queries. You'll find the query I use above is just taken from this page in the docs site.

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44