An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
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 missing0x
prefix if you would want to use it in the futurebalance
- amount of FLOW tokens in UFix64 format. Divide byMath.pow(10,8)
to get float value out of itcode
- 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 contractkeys
- 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
Original Link: https://dev.to/onflow/build-on-flow-learn-fcl-7-how-to-query-flow-account-by-address-439d
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To