1

In Solidity, I have an array declared as follows:

uint[] public moveTimeStamp; 

In web3 JavaScript I would like to retrieve the element at position 0 in the array. I tried to access it like this:

var contract = web3.eth.contract(interface);
var instance = contract.at("0x1234...");
instance.moveTimeStamp(0).call(function(err, result) {....

Web3js says this isn't correct. What should I be writing after moveTimeStamp?

I tried the following:

instance.moveTimeStamp(0)
instance.moveTimeStamp([0])

...which doesn't work.

This is how I call it:

var contract = web3.eth.contract(interface); 
var instance = contract.at(contractaddress);
instance.callTimeStamp(0).call(function(err, result) {... 

And this is the error I get

TypeError: instance.callTimeStamp(...).call is not a function 

And if you will like see that the callTimeStamp array really does contain something, you can go to https://ropsten.etherscan.io/address/0x86c9d3abf13d25ced87c0d00331667ac44c04a89#readContract to read the contract variables.

2
  • "Web3 says this isn't correct." Does that mean you're seeing an error message? If so, what's the error?
    – user19510
    Aug 1, 2018 at 18:03
  • Added the error message and the link to my contract on Ropsten.
    – Jackson Ng
    Aug 2, 2018 at 2:21

2 Answers 2

0

I'm not sure if creating an instance changes the semantics of how call works, but based on the web3 1.0 docs it looks like web3 expects the call to be shaped like:

<contract>.methods.<methodName>([...methodArgs]).call(callback)

Otherwise it looks like your call is correct, the Solidity docs seem to suggest that a getter for an array takes a uint for the requested index.

2
  • This is how I call it: var contract = web3.eth.contract(interface); var instance = contract.at(contractaddress); instance.callTimeStamp(0).call(function(err, result) {... And this is the error I get TypeError: instance.callTimeStamp(...).call is not a function And if you will like see that the callTimeStamp array really does contain something, you can go to ropsten.etherscan.io/address/… to read the contract variables.
    – Jackson Ng
    Aug 2, 2018 at 2:17
  • The getter method will have the same name as the variable declared, so your original .moveTimeStamp(0) was correct. What happens if you change it to instance.methods.moveTimeStamp(0).call()?
    – ohsully
    Aug 2, 2018 at 6:48
0

Solidity tooling has improved a lot since 2018! We have developed a tool called cast, as part of the Foundry suite which lets you do this from CLI.

Here's how you can install Foundry

To solve your problem, you would simply do cast call 0x86c9d3abf13d25ced87c0d00331667ac44c04a89 "moveTimestamp(uint256)" 0 --rpc-url <...>

See here for more docs around cast call. Under the hood it performs an eth_call, which is equivalent to your web3.js code (if it worked).

For anyone following this thread, Ropsten is deprecated since the merge, so the above won't work.

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.