2

I started to learn solidity and want to code something, but I'm struggling with simple call function to contract. Can you point me out what I'm doing wrong?

Here is simple gits: https://gist.github.com/xaxes/0da37b5c2585445d571526db42110eb3

My problem is when I'm trying to set int value in the contract and then get it, 0 is always returned.

2

It's because you use call to invoke the set function in index.js on line 24. call doesn't send a transaction, but instead runs a simulation of the function. As set alters the state of the contract, however, sendTransaction should be used instead.

Also, you should call get inside the callback of line 24 in index.js, so that the program waits until the set transaction was mined before calling get

Change

instance.set.call(10, (err, ok) => console.log('set', err, ok))
instance.get.call((err, ok) => console.log('get', err, ok.toString())) /* (1) Here why 10 is not returned */

to

instance.set.call(10, (err, ok) => {
    console.log('set', err, ok);
    instance.get.call((err, ok) => console.log('get', err, ok.toString())) /* (1) Here why 10 is not returned */
});

and it should work

  • You'll also need to wait for the transaction to be mined. If you call get immediately, you'll still get the old value. (Possibly not if you're using a local test network that mines blocks immediately, but for the real network, you'll need to call get after the transaction for set has been mined.) – smarx May 9 '18 at 13:33

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.