DEV Community

Max Daunarovich for Flow Blockchain

Posted on • Edited on

Build on Flow | Learn FCL - 7. How to Query Flow Account by Address

Overview

In the previous post, we covered how to collect information about blocks on the chain. Today we will cover how you can query information about a specific account using the account function.

The two most common use cases, and what you’ll be learning today are:

  • get the current FLOW balance of the account
  • get a list of contracts deployed on the account

Step 1 - Installation

Add "@onflow/fcl": "1.0.0" as your dependency

Step 2 - Setup

Just like the last time, we will import necessary methods and setup FCL:

// Import methods from FCL
import { account, query, config } from "@onflow/fcl";

// Specify the API endpoint - this time we will use Mainnet
const api = "https://rest-mainnet.onflow.org";

// Configure FCL to use mainnet as the access node
config().put("accessNode.api", api);
Enter fullscreen mode Exit fullscreen mode

Step 3 - Copy Resolver

It’s always great to apply previously acquired knowledge and practice. So let’s bring back the resolver function from lesson 4 so we can use it in our example:

const resolveName = async (name) => {
    const cadence = `
    import FIND from 0x097bafa4e0b48eef

    pub fun main(name: String): Address?{
      return FIND.lookupAddress(name)
    }
  `;

  const args = (arg, t) => [arg(name, t.String)];
  return await query({ cadence, args });
};
Enter fullscreen mode Exit fullscreen mode

Step 4 - Fetch Account

Let’s try to resolve the flovatar identity name and explore what it has for us to see 😊

We will use our IIFE block as always:

// We will use IIFE to execute our code right away
(async () => {
  console.clear();

  const address = await resolveName("flovatar")

    // it's possible that name will be resolved to "null"
  // so let's add some basic protection here 
    if (address){
    const accountInfo = await account(address);
    console.log({ accountInfo });
    }
})();
Enter fullscreen mode Exit fullscreen mode

After the dust settles, you should see the following in the console:

{
    address: "921ea449dffec68a",
    balance: 13052726819120,
    code: "",
    contracts: Object,
    keys: Array(2),
}
Enter fullscreen mode Exit fullscreen mode

Those 5 fields are respectively:

  • address - address of the account (notice missing 0x prefix if you would want to use it in the future
  • balance - amount of FLOW tokens in UFix64 format. Divide by Math.pow(10,8) to get float value out of it
  • code - this is deprecated value, which was previously used to store code of the contract deployed on account. Before it was possible to store only single contract per account.
  • contracts - object representing deployed contracts. Keys are name of the contract and values are Cadence code of respected contract
  • keys - array of keys attached to the account

You can find more information about them on Flow Docs Site - https://docs.onflow.org/fcl/reference/api/#blockobject

Other Ways to Explore Account

There are ways to explore specific account:

Until next time 👋

Resources

Other resources you might find useful:

Top comments (0)