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 y…


This content originally appeared on DEV Community and was authored by Max Daunarovich

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);

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 });
};

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 });
    }
})();

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

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

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:


This content originally appeared on DEV Community and was authored by Max Daunarovich


Print Share Comment Cite Upload Translate Updates
APA

Max Daunarovich | Sciencx (2022-06-14T16:36:37+00:00) Build on Flow | Learn FCL – 7. How to Query Flow Account by Address. Retrieved from https://www.scien.cx/2022/06/14/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address/

MLA
" » Build on Flow | Learn FCL – 7. How to Query Flow Account by Address." Max Daunarovich | Sciencx - Tuesday June 14, 2022, https://www.scien.cx/2022/06/14/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address/
HARVARD
Max Daunarovich | Sciencx Tuesday June 14, 2022 » Build on Flow | Learn FCL – 7. How to Query Flow Account by Address., viewed ,<https://www.scien.cx/2022/06/14/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address/>
VANCOUVER
Max Daunarovich | Sciencx - » Build on Flow | Learn FCL – 7. How to Query Flow Account by Address. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/14/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address/
CHICAGO
" » Build on Flow | Learn FCL – 7. How to Query Flow Account by Address." Max Daunarovich | Sciencx - Accessed . https://www.scien.cx/2022/06/14/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address/
IEEE
" » Build on Flow | Learn FCL – 7. How to Query Flow Account by Address." Max Daunarovich | Sciencx [Online]. Available: https://www.scien.cx/2022/06/14/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address/. [Accessed: ]
rf:citation
» Build on Flow | Learn FCL – 7. How to Query Flow Account by Address | Max Daunarovich | Sciencx | https://www.scien.cx/2022/06/14/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.