Developer Fundamentals

-- III. Read Accounts

Learn how to identify different types of accounts, read VET and VTHO balances, and retrieve contract bytecode using the VeChain SDK.

What Is an Account?

An account can be classified as either an externally owned account or a smart contract.

  1. Externally owned accounts typically consist of private keys generated in wallets.

  2. Smart contracts are deployed with specific program codes.

Examining an account's hasCode flag can distinguish between the two. If it's true, it indicates that a smart contract is deployed at that address. The bytecode for smart contracts can also be retrieved.

Key Attributes of an Account

  • hasCode – Indicates a smart contract at the address.

  • balance – Represents the VET balance.

  • energy – Denotes the VTHO balance.

The balance is stored as a hex-encoded BigInt, which can be converted into a human-readable format using BigInt(balance).

It's important to note that numbers are stored with all their decimals, and for better readability, 18 decimal places should be considered.

Here's an example snippet for accessing account details for the VTHO contract:

const contract = await thor.accounts.getAccount(
  '0x0000000000000000000000000000456e65726779'
);
const bytecode = await thor.accounts.getBytecode(
  '0x0000000000000000000000000000456e65726779'
);

Test It Yourself

The snippet below shows how you can make a request to the blockchain and receive data directly from the VTHO contract:

import { ThorClient } from '@vechain/sdk-network';
const thor = ThorClient.at('https://mainnet.vechain.org');

// get a contract account
const contract = await thor.accounts.getAccount(
  '0x0000000000000000000000000000456e65726779'
);
const bytecode = await thor.accounts.getBytecode(
  '0x0000000000000000000000000000456e65726779'
);
console.log(contract, bytecode);

// example account
const example = await thor.accounts.getAccount(
  '0x0000000000000000000000000000000000000000'
);
console.log(example);
console.log('VET Balance', BigInt(example.balance)/1000000000000000000n );
console.log('VTHO Balance', BigInt(example.energy)/1000000000000000000n );

The same information is available using the JSON-API:

{
  "balance": "0x0",
  "energy": "0x5079f2aca11dbff14e26a",
  "hasCode": true
}