How to Retrieve the Balance of an ERC-20 Token in the Shibarium RPC

The Shibarium ecosystem is constantly expanding: starting from several big integrations and launches to the increasing power of the community that introduces new projects. Gladly, there are more than enough tools and interfaces that help builders scale up the Shibarium blockchain. Their focus is to bring new use cases to life including Shibarium explorer, Shibarium RPC Full Node etc.

In this tutorial we’ll give you all required information and knowledge on how to integrate Shibarium ERC-20 tokens. We are going to interact with them using either web3.js or Shibarium RPC Full Node. This guide was prepared by NOWNodes – nodeOps development tool and Shibarium ecosystem contributor.

Let’s start building, shall we?

Table of Contents

    What is a Shibarium ERC-20 Token?

    Shibarium, the anticipated Layer 2 solution built to enhance the scalability and functionality of the Ethereum network, introduces an innovative environment for ERC-20 tokens. ERC-20 tokens, a standard for creating and issuing smart contracts on the Ethereum blockchain, are renowned for their versatility and wide adoption in various decentralized applications. 

    However, Shibarium-based tokens are not inherently different from regular ERC-20 tokens. They can represent a wide range of assets, from currencies to points in a loyalty program, and include standard functionalities such as transfers, balance inquiries, and allowances.

    It’s essential to highlight the key distinction lies in the underlying infrastructure. Shibarium, designed as a Layer 2 solution, aims to provide a more scalable and cost-effective platform for transactions and smart contract execution. Tokens on Shibarium benefit from these enhancements, potentially offering faster transaction speeds and lower fees compared to operating directly on the Ethereum mainnet.

    It’s important to note that while tokens can exist on both Shibarium and the Ethereum mainnet, their operation on each network is distinct. When tokens are bridged to Shibarium, they are typically locked or represented in a corresponding contract on the Ethereum mainnet, and a corresponding amount is minted or unlocked on Shibarium. This ensures the total supply remains constant across both networks, but the tokens’ active use and transactions occur within the specific network they are currently on.

    Examples of Tokens on Shibarium

    While Shibarium is still in its nascent stages, anticipation is high for the migration and creation of popular ERC-20 tokens within its network. Tokens such as SHIB, the native meme token that sparked widespread interest, along with utility tokens, governance tokens, and DeFi assets, are expected to make significant inroads. As Shibarium evolves, the landscape is likely to be populated by a mix of established tokens seeking scalability solutions and new tokens capitalizing on the unique features of Shibarium.

    As NOWNodes is a non-custodial solution, we are not able to hold or store any funds. Therefore, none of our materials are financial advice and they should be interpreted as informational resources.

    Some of the most popular ERC-20 tokens on Shibarium are native SHIB, BONE and LEASH. There are several projects that aim to expand the Shibarium ecosystem and bring new use-cases, and the list may include MARSWAP, CHEWY, etc.

    BONUS: Can We Expect a Stablecoin on Shibarium Network?

    The introduction of a stablecoin within the Shibarium network would be a game-changer, offering users a stable medium of exchange and a safe haven during market volatility. While official announcements are awaited, the potential for a Shibarium-based stablecoin is high.

    It’s no brainer given the network’s focus on expanding the DeFi ecosystem and enhancing user experience. Such a development would boost Shibarium’s utility and also attract a broader user base looking for reliable transaction mechanisms.

    Let’s see how things’ll go in 2024. We expect a clear and sunny year 🙂

    How to Retrieve and Check Shibarium ERC-20 Token Balances: Two Ways

    Option 1. Direct Interaction with the Blockchain

    For developers and enterprises who prefer a hands-on approach and have web3 technical knowledge, interacting directly with the Shibarium blockchain involves using smart contract calls to query token balances. Here’s how you can do it:

    • Prerequisites:
      • Web3.js to interact with the blockchain.
      • The contract address of the ERC-20 token you’re interested in.
      • The wallet address whose token balance you wish to check.
    • Using Web3.js:
      • First, set up a Web3 instance and connect it to the Shibarium network RPC endpoint.

    Perform the following script:

    const Web3 = require('web3');
    const web3 = new Web3('https://shibarium.nownodes.io/?apikey=YOUR_API_KEY');

    Then, you can use the Shibarium ERC-20 token contract Application Binary Interface or ABI to instantiate the contract. For tokenAdress – you can take this one from Shibarium Block Explorer.

    const tokenABI = [...]; 
    const tokenAddress = '0x...';
    const tokenContract = new web3.eth.Contract(tokenABI, tokenAddress);

    Finally, call the balanceOf method with the wallet address

    const walletAddress = '0x...';
    tokenContract.methods.balanceOf(walletAddress).call()
    .then(balance => {
        console.log('Token Balance:', balance);
    });

    This method is suitable for tools and developers that have the expertise and experience in deploying new smart contract functions.

    Interacting with Smart Contracts requires an extremely high level of attention as it involves operations with funds. Make sure you test your contract only inside of your environment.

    However, this is not the easiest and most popular way to check Shibarium ERC-20 token balance. Let’s see an easier solution.

    Option 2. Using Shibarium RPC Full Node and API Methods

    For a more straightforward approach, NOWNodes provides a fast access to the API to interact with Shibarium. Here’s how to use Shibarium RPC Full Node to check ERC-20 token balances:

    1. Setting Up the Environment 
    1. Sending the Request to Blockchain

    Use the NOWNodes RPC Full Node endpoint to connect to Shibarium. You’ll need the ERC-20 token contract address and the wallet addresses you’re interested in. The RPC method you’ll be using is eth_getBalance for checking native Shibarium balances or eth_call for ERC-20 token balances, which requires interacting with the token contract’s balanceOf method.

    curl -X POST https://shibarium.nownodes.io/ \
    -H "Content-Type: application/json" \
    -H "api-key: api-key" \
    -d '{"jsonrpc":"2.0","method":"eth_call","params":[{"to": "TOKEN_CONTRACT_ADDRESS", "data": "____"},"latest"],"id":1}'

    Here are some tips you should follow to avoid receiving error messages:

    Construct an HTTP POST request with the following parameters:

    • to: The ERC-20 token contract address.
    • data: The hashed method signature of balanceOf(address). This is followed by the wallet address, encoded as a 64-character hexadecimal string.

    Use the NOWNodes Shibarium endpoint with your API key in the header.

    Conclusion

    The Shibarium network, with its promise of scalability, faster transactions, and reduced costs, presents an exciting frontier for the ERC-20 token ecosystem. Whether through established tokens expanding their reach or the introduction of new assets and potentially a stablecoin, Shibarium is poised to make a significant impact on the DeFi landscape. 

    For users and developers, accessing and managing token balances can range from direct blockchain interaction to leveraging user-friendly services, ensuring that Shibarium’s benefits are accessible to a wide audience.

    This is a goal for us – NOWNodes – as well!