I'm trying to read some data from VIEW function on already deployed contract using my own smart contract. The function is standart "balanceOf(address)" of ERC20 token. I can read this data using etherscan web interface and no transaction required for this action, but I can't find the way to do the same using smart contract. I have this code (let's assume I have no source code of deployed smart contract, so I'm using abi.encode):

pragma solidity ^0.5.15;

contract Test  {

   address dc = 0xaD6D458402F60fD3Bd25163575031ACDce07538D; //ropsten network ERC20 token address
   uint number;
   event Log(uint256 number, bytes b);

    function balanceOf(address _val) public {
      bool status;
      bytes memory b;
      (status, b) = dc.staticcall(abi.encodePacked(bytes4(keccak256("balanceOf(address)")), _val));
        emit Log(number, b);

but each time in Event Logs I see the same four numbers: 0 64 32 0

I tried to use staticcall, delegatedcall and call with no success. Can anyone suggest how can I read data from VIEW function?


1 Answer 1


If you know the function-signature, then you don't need the source code of the smart-contract.

For example:

contract IERC20 {
    function balanceOf(address) public view returns (uint256);

contract Test {
    IERC20 dc = IERC20(0xaD6D458402F60fD3Bd25163575031ACDce07538D);

    function balanceOf(address _val) public view returns (uint256) {
        return dc.balanceOf(_val);
  • Thanks, it really works. But I don't understand now purpose of abi.encodePacked functions if we can just use function signature?
    – kedcex
    Commented Apr 9, 2020 at 10:24
  • @kedcex: 1. It has many different usages, for example, you can call a function without knowing its exact return-type, and then reconstruct the returned value (using b in your example). 2. Please accept the answer (by clicking on the V next to it) if it has solved your problem. Commented Apr 9, 2020 at 11:19

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.