0

I made a web app that interacts with a smart contract, using truffle unbox webpack. My contract has a variable called sum and 2 functions: one called answer that increments the sum variable after it is executed and an onether fuction called total that returns the value of the variable sum. In my app.js i have the code:

SmartContract.deployed().then(function(contractInstance) {

contractInstance.answer(1, {gas: 140000, from: web3.eth.accounts[0]}).then(function(v) {
    return contractInstance.total.call().then(function(total){
                                    $("#total").html(total.toString());
            });
});

}

My question is whether the call contractInstansce.total will be executed after the transaction call contractInstance.answer is mined, so that the variable total in my index.html will have the updated value.Also is the word "return" necessary after a transaction call function ,like answer in this example, in order the following contract call function, like total in this example,to be executed after the transaction is mined?

I used testrpc to test if my web app is working as expected, but in testrpc the blocks are mined instantly, so i am not sure if the code i wrote will be correct if i use this application in testnet network.

Thanks a lot for your time!

0

My question is whether the call contractInstansce.total will be executed after the transaction call contractInstance.answer is mined,

No, contractInstance.answer will return the hash of transaction (that might be not mined yet).

Consider to use web3 1.0.x version. It has events, that you can listen http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html?highlight=contract#id15

contractInstance.methods.answer().send({from: '_address'})
.on('transactionHash', function(hash){
    ...
})
.on('receipt', function(receipt){
    ...// your transaction is mined here
})
.on('confirmation', function(confirmationNumber, receipt){
    ...
})
.on('error', console.error);
  • In the webpage it says web3 1.0 is not yet released.In my truffle project in package.json it says web3 version 0.20.0.. So your solution will apply to my problem? – kathi Dec 1 '17 at 11:12
  • Check the latest web3 version npm view web3, update your package.json with it and install – Viktor Baranov Dec 1 '17 at 18:21
  • Unfortunately i wasn't able to update. I tried to update the package.json and then run sudo npm install (as mentioned here [link] github.com/trufflesuite/truffle/issues/562 for an older version) but i got many errors. Maybe is because i used truffle webpack for the project.So is there any way in web0.20 to know when the receipt is return ed and then proceed an another call function to the updated contract? – kathi Dec 4 '17 at 0:25
  • You can listen transaction by its hash until it's mined, like getTxCallBack function here: ethereum.stackexchange.com/questions/18884/… – Viktor Baranov Dec 4 '17 at 0:53
  • Would it be a good idea to use an event inside the smart contract, that will be triggered right after someone sets the new values, and then use filter to check that event? Will the code inside filter.watch be executed after the transaction is mined? – kathi Dec 4 '17 at 1:06

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.