[Ethereum] With web3 how would I get transaction AND function result

web3js

I'm using testrpc, truffle, and web3 in Node. I've written my contract and pushed it to testrpc with truffle. Here's the gist of the Node code I have so far:

let fs = require('fs');
let Web3 = require('web3');
let web3 = new Web3();
let settings = require('./settings.json'); // my own settings file

let abiContractContent = fs.readFileSync(settings.contractAbiPath, 'utf8');
let abi = JSON.parse(abiContractContent);
let contract = web3.eth.contract(abi).at(settings.contractAddress);

web3.eth.defaultAccount = web3.eth.accounts[0];

I've then found two ways to call a function in my contract:

contract.myFunc(param1, param2, { gas: 200000 });

which returns what I believe is the transaction ID for the changes caused by that function call.

contract.myFunc.call(param1, param2, { gas: 200000 });

which gives me the actual return value of myFunc.

I would like to have all of these pieces of information: the transaction AND the return values. I imagine I would need to call my function using the first approach and then look up the return values using the transaction ID but I have no idea how to actually do that. Am I on the right track or should I be looking for an entirely different approach?

Best Answer

Am I on the right track or should I be looking for an entirely different approach?

Yes, I would say you are on the right track, but you need to know the difference between call and transactions. Basically:

Calls:

  • happen locally
  • don't cost Ether
  • and don't affect the network

Transactions:

  • are broadcast to the network
  • do cost Ether
  • and affect the network.

Call before you make a transaction if you want the result information ASAP. However just remember that call doesn't affect the blockchain, it is basically a local interaction.

You can make a call before you make a transaction:

var result = contract.myFunc.call(param1, param2, { gas: 200000 });

Once you have your result perform the transaction:

var txHash = contract.myFunc(param1, param2, { gas: 200000 });

Also See : What is the difference between a transaction and a call?

Related Topic