0

I have a HelloWorld contract and Keystore contract, both deployed in my truffle console.

When I read the Helloworld's public attribute balance, I get a value. When I try to read Keystore's public array of owners (addresses), it fails.

Why?

contract HelloWorld {
  uint public balance;

contract Keystore {
  address[] public owners;

truffle(development)> hw.balance
{ [Function]
  call: [Function],
  sendTransaction: [Function],
  request: [Function: bound ],
  estimateGas: [Function] }
truffle(development)> hw.balance.call()
BigNumber { s: 1, e: 3, c: [ 1000 ] }



truffle(development)> ks.owners
{ [Function]
  call: [Function],
  sendTransaction: [Function],
  request: [Function: bound ],
  estimateGas: [Function] }
truffle(development)> ks.owners.call()
Error: Invalid number of arguments to Solidity function
    at Object.InvalidNumberOfSolidityArgs (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:41471:16)
    at SolidityFunction.validateArgs (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:327892:22)
    at SolidityFunction.toPayload (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:327908:10)
    at SolidityFunction.call (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:327949:24)
    at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:316821:16
    at new Promise (<anonymous>)
    at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:316812:18
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

EDIT:

Error trying to call with param:

truffle(development)> ks.contract.owners(0)
Error: VM Exception while processing transaction: revert
    at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:41484:16)
    at RequestManager.send (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:329505:22)
    at Eth.send [as call] (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:37994:58)
    at SolidityFunction.call (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:327953:32)
    at SolidityFunction.execute (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:328078:22)
    at evalmachine.<anonymous>:1:13
    at ContextifyScript.Script.runInContext (vm.js:59:29)
    at Object.runInContext (vm.js:120:6)
    at Console.interpret (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:202629:17)
    at ReplManager.interpret (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:203345:18)
truffle(development)> ks.owners
{ [Function]
  call: [Function],
  sendTransaction: [Function],
  request: [Function: bound ],
  estimateGas: [Function] }
truffle(development)> ks.owners.call(0)
Error: VM Exception while processing transaction: revert
    at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:41484:16)
    at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:329530:36
    at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:325200:9
    at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:328229:7)
    at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176415:18)
    at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176705:12)
    at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176860:12)
    at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176820:24)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
truffle(development)>
  • I've tried changing owners to mapping(address => uint) public owners; and still get this error – quantumpotato Jan 11 '18 at 20:28
1

The public getter for an array or mapping takes a parameter.

ks.owners.call(0) should work to retrieve the first owner, ks.owners.call(1) should retrieve the second owner, etc.

  • Hello, I get a VM exception trying that, please see my question for edited log – quantumpotato Jan 11 '18 at 20:37
  • How long is the owners array? Perhaps 0 is out of bounds; make sure you've added something to the array first. – user19510 Jan 11 '18 at 20:38
  • Hi, good catch. I tried calling, I get an invalid number of arguments when I try to add to the array with my function. Full contract + error here: gist.github.com/quantumproducer/… ( Sorry I cannot edit my question yet) – quantumpotato Jan 11 '18 at 20:41
  • You're passing an argument to buyKey, but it doesn't take any arguments. – user19510 Jan 11 '18 at 20:42
  • 1
    Like you're trying to call that from JavaScript? I would have expected it to be undefined. To my knowledge, the Solidity compiler doesn't generate a getter for array lengths, so you'll need to write your own accessor. – user19510 Jan 11 '18 at 21:25

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.