need help web3.js to read return value of smart contract
- I need help to locate web3.js function that can send parameters to an existing smart contract function that changes the state of the blockchain, In the smart contract function I am returning a return value which I need to capture using web3.js For example the below node file calls web3.eth.sendRawTransaction but it return , it gets only the transaction hash and not the return value of the function "addfund". Using the transaction hash I can see details about the transaction like the block number etc, however, the return value of addfund is not captured anywhere. Is there a way to achieve this ? I know that I can use contract.methods.totalSupply().call((err, result) => { console.log(result) }) function to get the return values, but those methods are NOT state changing functions so it is not applicable in my case
File.js
var contractAddress = '0x17a8fd94665bd5f73e7fede9c69f64959cefd8df'; var txOptions = { nonce: web3.toHex(web3.eth.getTransactionCount(address)), gasLimit: web3.toHex(800000), gasPrice: web3.toHex(20000000000), to: '0x17a8fd94665bd5f73e7fede9c69f64959cefd8df' }; var rawTx = txutils.functionTx(interfaceABI, 'addfund', ['0xc66ed658f1e75c21df3e50890e983fda34b6052e',100], txOptions);
function sendRaw(rawTx) { var privateKey = new Buffer(key, 'hex'); var transaction = new tx(rawTx); console.log('transaction '+ JSON.stringify (transaction)); transaction.sign(privateKey); var serializedTx = transaction.serialize().toString('hex'); console.log('serializedTx ' +serializedTx); web3.eth.sendRawTransaction( '0x' + serializedTx, function(err, result) { if(err) { console.log('error is' + err); } else { console.log('result is ' + result); } }); }
sendRaw(rawTx);
Solidity function :
function addfund(address sender, uint value ) payable public {
if (partifunded[msg.sender] == true) {
for ( uint i = 0; i < participants.length; i++) {
if (participants[i].partaddr == msg.sender)
{
participants[i].amount += msg.value;
totalamountcol = address(this).balance;
}
}
}
else
{
partifunded[msg.sender] = true;
participants.push(Participant(msg.value,msg.sender));
numberofparti = numberofparti+1;
totalamountcol = address(this).balance;
}
}
- Is there a boilerplate code available for capturing events in web3.js that are fired by smart contracts ?
-
A state-changing function (aka transaction) has to be mined before it can change the state of the blockchain, hence you cannot expect a return result from a function that will be mined asynchronously. Unless the intention is to block the entire DApp waiting for the transaction to be mined? 😮
-
Assuming you already have your
ContractInstance:
// to process all future events
ContractInstance.MyEvent().watch((error, result) => {
// do awesome stuff
})
// to process all past events
ContractInstance.MyEvent({}, {
fromBlock: 0,
toBlock: 'latest'
}).get((error, result) => {
// do awesome stuff
})
Thanks for the reply . then , how will i get to know what is the return value of the state-changing function ?
You can use return value of non-constant functions only when it is returned to another contract but you cannot return value to off chain. If you need to return value from nonconstant function you need to use events. Instead of returning the result you emit it in the event.
@arunkuduva Inside Solidity, emit an Event with the value you want to return, then fetch the event and filter it according to your requirements.
Thanks for the guidance !!
Closed because of the ongoing clean up of the issue list. Feel free to ask this in our gitter channel or on stackoverflow.