Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 14, 2022 04:36 pm GMT

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 youll 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 FCLimport { account, query, config } from "@onflow/fcl";// Specify the API endpoint - this time we will use Mainnetconst api = "https://rest-mainnet.onflow.org";// Configure FCL to use mainnet as the access nodeconfig().put("accessNode.api", api);

Step 3 - Copy Resolver

Its always great to apply previously acquired knowledge and practice. So lets 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

Lets 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:


Original Link: https://dev.to/onflow/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address-439d

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To