-
Notifications
You must be signed in to change notification settings - Fork 5.1k
need help web3.js to read return value of smart contract #2019
Description
- 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 ?