Get EOS Account Info & Balance
Querying the EOS Blockchain using eosjs
- 1.Add the dependencies to your project.
npm install eosjs
// alternatively
yarn add eosjs
- 2.Import JsonRpc from the eosjs package. And declare the endpoint of the EOSIO chain.
import { JsonRpc } from 'eosjs'
const eosMainnet = {
chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
rpcEndpoints: [{
protocol: 'https',
host: 'http://api.main.alohaeos.com',
port: ''
}]
};
const urlEndpoint: string = (
`{eosKylin.rpcEndpoints[0].protocol}://{eosKylin.rpcEndpoints[0].host}:{eosKylin.rpcEndpoints[0].port}`
- 3.Get a user's native EOS currency balance.
export async function getEosBalance(accountName: string): Promise<string> {
let balance: string = "0.0000 EOS"
const rpc = new JsonRpc(urlEndpoint)
try {
const balances = await rpc.get_currency_balance(
'eosio.token',
accountName,
'EOS'
)
if (balances.length !== 0) {
balance = balances[0]
}
}
catch (err) {
console.error(err)
}
return balance
}
- 4.Get a user's account information on the blockchain.
import { GetAccountResult } from 'eosjs/dist/eosjs-rpc-interfaces';
export async function getEosBalance(accountName: string): Promise<GetAccountResult> {
const rpc = new JsonRpc(urlEndpoint)
try {
const accountInfo: GetAccountResult = await rpc.get_account(
accountName
)
return accountInfo
}
catch (err) {
console.error(err)
}
}
4a. The account info returned from the blockchain.
/** Return value of `/v1/chain/get_account` */
export interface GetAccountResult {
account_name: string;
head_block_num: number;
head_block_time: string;
privileged: boolean;
last_code_update: string;
created: string;
core_liquid_balance?: string;
ram_quota: number;
net_weight: number;
cpu_weight: number;
net_limit: AccountResourceInfo;
cpu_limit: AccountResourceInfo;
ram_usage: number;
permissions: Permission[];
total_resources: ResourceOverview | null;
self_delegated_bandwidth: ResourceDelegation | null;
refund_request: RefundRequest | null;
voter_info: any;
rex_info: any;
}
export interface AccountResourceInfo {
max: string;
available: string;
used: string;
}
export interface ResourceOverview {
owner: string;
ram_bytes: string;
net_weight: string;
cpu_weight: string;
}
export interface Permission {
parent: string;
perm_name: string;
required_auth: any;
}
export interface ResourceDelegation {
from: string;
to: string;
net_weight: string;
cpu_weight: string;
}
export interface RefundRequest {
owner: string;
request_time: string;
net_amount: string;
cpu_amount: string;
}
Last modified 3mo ago