1

I'm trying to get the BUSD balance of a whale. However the getBalance() function always returns 0 and I get the following error when I run my tests: AssertionError: Expected "0" NOT to be equal 0. I work with hardhat one the bsc mainnet, here are some snippets of my code:

const provider = waffle.provider;
const BUSD_WHALE = "0x00d947f5b261e95c418B0431F983268b75f36Fca";
const BUSD = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";

beforeEach(async () => { 
        // Get owner as signer
        [owner] = await ethers.getSigners();

        // Ensure the whale has a balance
        const whale_balance = await provider.getBalance(BUSD_WHALE);
        console.log("WHALE BALANCE: ", whale_balance)
        expect(whale_balance).not.equal("0");

...
});

Package.json:

"devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.5",
    "@nomiclabs/hardhat-waffle": "^2.0.3",
    "chai": "^4.3.6",
    "ethereum-waffle": "^4.0.10",
    "ethers": "^5.6.4",
    "hardhat": "^2.9.3"
  }

I've also tried different whale addresses but the result is unaltered. Did I do anything wrong or is there something I forgot? Thanks in advance

2 Answers 2

0

As, @Awais Ahmad has mentioned in his answer that getBalance() function retrieves the Ether balance (i.e., BNB balance in case on Binance Network) of an address, not the balance of any BEP-20 token or so, thereby it's returning 0 because the BNB balance of that address is 0.

So, in order to retrieve the balance of BEP-20 token (i.e., BUSD in your case), you've to use the corresponding balanceOf() function, that you can call using its ABI by creating the token contract instance.

Here's the code to achieve the same using ethers.js:

const { ethers } = require('ethers');

const provider = new ethers.JsonRpcProvider("https://bsc-rpc.publicnode.com");

const BUSD_WHALE = "0x00d947f5b261e95c418B0431F983268b75f36Fca";
const BUSD = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";

const balanceOf_abi = [
    { "constant": true, "inputs": [{ "internalType": "address", "name": "account", "type": "address" }], "name": "balanceOf", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }
];

const BUSD_tokenContract = new ethers.Contract(BUSD, balanceOf_abi, provider);

BUSD_tokenContract.balanceOf(BUSD_WHALE).then((BUSD_balance) => {
    console.log(ethers.formatUnits(BUSD_balance));
});

P.S., You can achieve the same in your Hardhat setup as well, but make sure to define the Binance network configuration in your hardhat.config.js file, like:

networks: {
    binance: {
      url: process.env.BINANCE_RPC, // like https://bsc-rpc.publicnode.com
      accounts: [process.env.ACCOUNT_PRIVATE_KEY],
      chainId: 56,
    }
}

And, then specifying the network flag as binance while running the test:

npx hardhat test ./test/YourTestFileName.js --network binance
2
  • thank you very much! Commented May 1 at 14:33
  • You’re welcome, @Christopherus. Happy to help! Commented May 1 at 16:46
0

Hello @Christopherus,

It seems like you're trying to retrieve the balance of an address holding the BUSD token on the Binance Smart Chain (BSC) using the getBalance() function, but you're encountering issues where the balance is always returning 0.

  • Problem: getBalance() doesn't work for BEP-20 tokens (like BUSD) on BSC.
  • Solution: The getBalance() function retrieves the Ether balance of an Ethereum address, not the balance of a specific ERC20 token like BUSD.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.