1

hi guys I have this contract function:

function teste(uint _oX) public returns (uint value) {
    return 34;
}

And I try to call in JS this way :

 MyContract.teste(1).then(value => console.log(value) );  

But I received this instead the value 34:

{blockHash: "0xd43e64033fdc8058be7da224059f15ef5f3268813294f2a9087a747d1ed674da", blockNumber: 2600, contractAddress: null, cumulativeGasUsed: 21722, from: "0xc5a5d47b525851bde4811aa870a331024156eeec", …}blockHash: "0xd43e64033fdc8058be7da224059f15ef5f3268813294f2a9087a747d1ed674da"blockNumber: 2600contractAddress: nullcumulativeGasUsed: 21722events: {}from: "0xc5a5d47b525851bde4811aa870a331024156eeec"gasUsed: 21722logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"status: "0x1"to: "0x7b475783cee479de3b270193b61450c649b2abef"transactionHash: "0x4455026eeae746f58e49f977f42597321426f9fe6cc3818deffac63dd9c61909"transactionIndex: 0__proto__: Object

someone can help me with this :| Tank you

1

Ethereum contracts have two methods of invocation.

One way is to query the local node and its copy of the blockchain on a read-only basis. This way will return 34 as expected.

The other way is to send a transaction the network which will potentially change the contract state. Even though your function doesn't store anything, such a transaction would still circulate and be mined and turn up in a block so all nodes can agree that nothing happened.

You're using the second method so you're getting information about the sent transaction, its success, the transaction hash, the block number ... all sorts of things except 34. This is because return values are not accessible by this method.

What you want is to read the contract. There are two ways to do it.

You can leave the contract alone, and invoke the function with the .call() method to explicitly say that the client wants it to be a read-only operation and you actually do want 34.

MyContract.teste.call(1).then(value => console.log(value) );

You can bake this behavior into the contract which is good form for functions that don't do anything to the state, i.e. they're meant to inspect the state. pure is purely computational and doesn't need access to state, and view is read-only but needs access to the state.

function teste(uint _oX) public pure returns (uint value) { return 34; } More about this: https://blog.b9lab.com/calls-vs-transactions-in-ethereum-smart-contracts-62d6b17d0bc2

Moving, on. The response will be a strange looking thing. It's a "bigNumber" (separate topic) because JavaScript can't handle the 256-bit integers from Ethereum. You can unpack it like this and you should get 34.

console.log(value.toString(10));

Hope it helps.

  • Tank you Rob this really help me :) – neo-_-man Sep 27 '18 at 10:57

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.