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 missing0x
prefix if you would want to use it in the future -
balance
- amount of FLOW tokens in UFix64 format. Divide byMath.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:
- Flow View Source - https://flow-view-source.com/mainnet/account/0x5916e847260714b6 - is a community project by OG of FCL - James Hunter - https://github.com/orodio
- FlowScan - https://flowscan.org/account/0x5916e847260714b6 - Flowscan page will show more information about account transactions and transfers, since they are processing the blocks to accumulate that information.
Until next time 👋
Resources
- Full Source Code - https://codesandbox.io/s/dev-to-fcl-07-get-account-j9rxkm?file=/src/index.js
Other resources you might find useful:
- Flow Docs Site - https://docs.onflow.org/ - More detailed information about Flow blockchain and how to interact with it
- Flow Portal - https://flow.com/ - your entry point to Flow
- FCL JS - https://github.com/onflow/fcl-js - Source code and ability to contribute to the FCL JS library
- Cadence - https://docs.onflow.org/cadence/ - Introduction to Cadence
- Codesandbox - https://codesandbox.io - An amazing in-browser IDE enabling quick prototyping
This content originally appeared on DEV Community and was authored by Max Daunarovich
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.