How can I find out the token balance of an address?


Problem:

I am using the contract from: https://bscscan.com/address/0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c#readContract

I have seen that it has a public variable which is:

mapping (address => uint) public balanceOf;

I am trying to call in my contract, but it is not very clear to me how to use it, if via interface or in what way

contract checkBalanceOf {
    mapping (address => uint) public  balanceOf;
    function balanceOf() public returns (uint256) {
        address  ERC20Address = targetInterface(0x18895B2a605CdAb301482d8F96E59FaDc17964C3);
        return ERC20Address.balanceOf(bankAddress);
    }

I was trying to apply the logic of this answer, but unlike, that this one does not have a public function

is posible call public view return deployed, from anther contract?


Solution:

In order to check how many WBNB the bankAddress owns, and to perform the check from your own contract, your contract needs to:

  1. Define the balanceOf() function in an interface
  2. Instantiate the interface pointing to the WBNB contract address
  3. Call the WBNB function balanceOf() passing it the bankAddress
  4. Recommended: Wrap the call in a view function so that by default it uses a call, not a transaction, in some client apps.
pragma solidity ^0.8;

interface IBEP20 {
    // mind the `view` modifier
    function balanceOf(address _owner) external view returns (uint256);
}

contract checkBalanceOf {
    address bankAddress = address(0x123);

    // mind the `view` modifier
    function balanceOf() external view returns (uint256) {
        // creating a pointer to the WBNB contract
        IBEP20 WBNBContract = IBEP20(0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c);

        // getting balance of `bankAddress` on the WBNB contract
        return WBNBContract.balanceOf(bankAddress);
    }
}

Recent Tips

  1. ref value is undefined in vue (modal, textarea, $refs)
  2. Azure - HDInsight Hbase Data Insertion Failed
  3. SignalR overwriting OnConnected(), OnDisconnected()
  4. DatePickerDialog displays with two borders
  5. "type 'double' is not a subtype of type 'int' in type cast" error in flutter. What should i do?
  6. hiding the autocomplete list when user click outside the textbox is not working as expected
  7. JSF IceFaces basic problem with redisplaying input value
  8. How to validate material ui TextField in reactjs?
  9. Go and MongoDB connection won't work with panic log "no reachable server"
  10. WordPress Posts Pagination Not Working
  11. F# sprintf won't print in interactive console
  12. Spring Integration get FTP files recursively with outbound-gateway
  13. Jade mixins not getting working from external file
  14. Can not access defined exports from the webpack bundle?
  15. Completely new to Node.js - API Programming
  16. Formatting Compare-Object Ouput
  17. Add dynamically added textbox value from User Control to main form
  18. Create a ByteBuf in Netty 4.0
  19. Is it possible to do computation before super() in the constructor?
  20. Q-learning Updating Frequency
  21. Wrong reload order when using Gulp and browserSync
  22. I use hugo build static page. But don't have content
  23. How to change background color and set bar colors based on conditional formatting in VBA?
  24. Problem when comparing two numeric values in SAS
  25. Is ACE reactor timer managment thread safe?
  26. Why Express res.render dumps the render output (EJS template) in console?
  27. Define generic typescript sort function of a certain type
  28. Eclipse RCP: TableViewer setInput from another view
  29. Migrating data from RDBMS to Arango DB: Bulk upload
  30. How can I check the existence of a key/file on an Amazon S3 Bucket using AWS iOS SDK v2?