Skip to main content

Don't have an API key yet?

Sign-up to Moralis to get your free API key and start building today.

Get Your Free API Key
Version: 2.2

Understanding Account Abstraction: How to Get Native Balances from Smart Accounts

There are two primary account types:

  • Contract Accounts
  • Token Accounts

Token Accounts provide users direct control over their tokens via private keys, enabling transactions and interaction with dApps.

Contract Accounts function autonomously, controlled by pre-defined smart contract code without private keys.

tip

As an example of account types, you can read the Ethereum Accounts article.

Now, let's explore how Moralis simplifies the process of getting the native balance of a smart contract account.

This step-by-step tutorial shows how to retrieve the native balance from a smart account, simplifying the process and offering code examples in multiple programming languages.

Step 1: Setup Moralis

Read the article Setting Up Moralis: Getting Started and make sure to finish all the steps. Only after that you can go ahead to complete this guide.

Step 2: Get the Native Balance of a Smart Account

You can use the API endpoint getNativeBalance to get the native balance for a specific wallet address. You will need two essential parameters:

  • address
  • chain

Once you have obtained both the address and chain, you can use the code snippets below to retrieve the native balance:

// Import the Moralis library and necessary modules
const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");

// Define an asynchronous function runApp to retrieve the native balance of a smart contract account
const runApp = async() => {
// Initialize the Moralis SDK with your API key and other configurations
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...add any other relevant configuration options
});

// Define the address of a smart contract account for which you want to fetch the native balance
const address = "0x350845DD3f03F1355233a3A7CEBC24b5aAD05eC5";

// Specify the blockchain that you are working with (e.g., Ethereum)
const chain = EvmChain.ETHEREUM;

// Use the Moralis getNativeBalance method to request the native balance
const response = await Moralis.EvmApi.balance.getNativeBalance({
address,
chain,
});

// Log the JSON response containing the native balance to the console
console.log(response.toJSON());
}

// Invoke the runApp function to start the process
runApp();

Step 3: Run the Script

To run the script, enter the following command:

node index.js

In your terminal, you should see the following JSON response:

[
{
"balance": "3989233490541891348056490"
}
]

Congratulations 🥳! You have successfully retrieved the native balance of a smart account using the Moralis Wallet API.

Video Tutorial: Get Any Wallet Native Crypto Balance

For a visual guide, check out our YouTube tutorial:

Get 24/7 Developer Support

Should you encounter any challenges while following this tutorial, our community engineers are here to assist you. Reach out to us on Discord or Forum to receive 24/7 developer support. Your success is our priority!

API Reference

If you want to explore more details about other wallet endpoints and optional parameters, refer to the API Reference.

See Also