Apparently, solidity creates getter functions for public variables. How does one call them using javascript? (for testing within truffle)


You just call them as you would any other function. The getter function has the same name as the state variable, so if you have:

uint256 public foo;

then you can read its value in JavaScript with:


See https://programtheblockchain.com/posts/2018/01/02/making-smart-contracts-with-public-variables/.

| improve this answer | |
  • 2
    let foo = await contract.foo() if you want the actual web3.BigNumber object rather than a Promise object. – goodvibration Feb 21 '18 at 14:34
  • Yes, or contract.foo.then(...) or, via web3.js 0.2x.x, contract.foo.call(callback), etc. It's just the same as calling any other function. – user19510 Feb 21 '18 at 14:39

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.