What is the difference between call made from a contract and web3. I was going through an example and in coments it was mentioned not to make a call from within the contract. Shouldnt calling from a contract and web3 equally expensive?

   This method MUST NEVER be called by smart contract code. First, it's 
    ///  expensive (it walks the entire Persons array looking for persons 
    belonging to owner),
         ///  but it also returns a dynamic array, which is only supported 
       for web3 calls, and
     ///  not contract-to-contract calls.
  function tokensOfOwner(address _owner) public view returns(uint256[] 
     ownerTokens) {
uint256 tokenCount = balanceOf(_owner);
if (tokenCount == 0) {
    // Return an empty array
  return new uint256[](0);
} else {
  uint256[] memory result = new uint256[](tokenCount);
  uint256 totalPersons = totalSupply();
  uint256 resultIndex = 0;

  uint256 personId;
  for (personId = 0; personId <= totalPersons; personId++) {
    if (personIndexToOwner[personId] == _owner) {
      result[resultIndex] = personId;
  return result;

When you call a read only function from web3js, that call doesn't consume any gas.

When that same read only function is called by a smart contract method executed by the ethereum vm, it costs gas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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