I need to access some constant variables in a smart contract, but I'm not being able to. Here's my code:

decimals = await contractInstance.methods.decimals().call();

'decimals' is a constant in a ERC20 contract deployed in Ropsten. I thought the compiler generated getters for those automatically, but I'm getting this error:

TypeError: contractInstance.methods.decimals is not a function

Is there another way to fetch a constant except from it's default getter? Note: The contract is already published and it don't have an explicit method to get the 'decimals' constant.

  • 2
    Let's have a look at the code where decimals is declared. Mar 2 '19 at 2:53
  • I decided to just hardcode the constant in my code, but still this is a bug. This is the line of code: uint8 public constant decimals = 18; Mar 3 '19 at 1:54

constant public gives a free getter.

You can play with this in Remix to see it.

pragma solidity 0.5.1;

contract Getter { 

    uint8 public constant decimals = 18;


Here it is in Remix so you can see the getter working:

enter image description here

There may be a bug in your contract and that should be a concern. Another possibility is the way you instantiate the contract in JavaScript. I don't think it's possible to pinpoint the issue more precisely with the information we've got here.

Hope it helps.

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.