1

I am trying to call my contract's method using sendRawTransaction. I have a function in my contract

updateReading(address consumerAddress,
            uint32 consumerMeterId,
            uint64 consumedReadingValue,
            address producerAddress,
            uint32 producerMeterId,
            uint64 producedReadingValue)

which I want to call using sendRawTransaction. The updateReading function updates the contract state of the contract. How can I make a call to this function using sendRawTransaction?

I am using truffle framework for the development.I have written the following code in my js to call the function using sendRawTransaction.

        var privateKey = new Buffer(password,'hex')
        var setData;
        var contractAddress;
        EnergyTrade.deployed().then(function (instance) {
              contractAddress=instance.address;
              setData=instance.updateReading(consumerAddress,consumerMeterId,consumedReadingValue,producerAddress,producerMeterId,producedReadingValue);

              var rawTx = {
                    to:contractAddress, 
                    from:consumerAddress,
                    data: setData
                  }


              var tx = new Tx(rawTx);
              tx.sign(privateKey);

              var serializedTx = tx.serialize();
              web3.eth.sendRawTransaction(serializedTx.toString('hex'), function(err, hash) {
                    if (!err)
                      console.log(hash); 
              });

        })

It returns the following error in my console.

    (node:23336) UnhandledPromiseRejectionWarning: Error: invalid type
at Object.exports.toBuffer (/home/maria/Desktop/12-Energy/node_modules/ethereumjs-util/dist/index.js:177:13)
at Transaction.setter [as data] (/home/maria/Desktop/12-Energy/node_modules/ethereumjs-util/dist/index.js:625:19)
at /home/maria/Desktop/12-Energy/node_modules/ethereumjs-util/dist/index.js:685:63
at Array.forEach (<anonymous>)

Another issue is

   (node:23336) UnhandledPromiseRejectionWarning: Error: invalid address
at inputAddressFormatter (/home/maria/Desktop/12-Energy/node_modules/truffle-contract/node_modules/web3/lib/web3/formatters.js:271:11)
at inputTransactionFormatter (/home/maria/Desktop/12-Energy/node_modules/truffle-contract/node_modules/web3/lib/web3/formatters.js:97:20)
at /home/maria/Desktop/12-Energy/node_modules/truffle-contract/node_modules/web3/lib/web3/method.js:89:28
at Array.map (<anonymous>)

How can I make sendRawTransaction possible in my js using truffle framework?

  • The error is fairly clear: there's no such function getData. I think you're expecting web3.js's getData function, but you're using Truffle. – smarx Feb 12 '18 at 12:15
  • @smarx I have updated that line without using getData,but a new issue is generated (node:23336) UnhandledPromiseRejectionWarning: Error: invalid type at Object.exports.toBuffer (/home/maria/Desktop/12-Energy/node_modules/ethereumjs-util/dist/index.js:177:13) at Transaction.setter [as data] (/home/maria/Desktop/12-Energy/node_modules/ethereumjs-util/dist/index.js:625:19) – Crissi Mariam Robert Feb 12 '18 at 12:17
  • Updated it to what? I don't see any edit to your code. – smarx Feb 12 '18 at 12:18
  • @smarx I have modified code...Please do point out the errors in my js code – Crissi Mariam Robert Feb 12 '18 at 12:21
  • The change you made makes no sense to me. I'm not sure what you expected that to do. Hopefully my answer helps. – smarx Feb 12 '18 at 12:22
4

As the error message says, Truffle doesn't have a getData method, but from my reading of the code, I believe you can access the underlying web3.js contract object via the contract member. Try this instead:

setData=instance.contract.updateReading.getData (consumerAddress,consumerMeterId,consumedReadingValue,producerAddress,producerMeterId,producedReadingValue);
2

Problem in your code is instance.updateReading() in this line. Web3js will treat as a direct function call(sendTransation or call()) based on your original abi. i.e it will not create bin or hex string.

I am not very sure about getData() method. As per web3js, you can use encodeABI() this method will return bytecode.

Simply you can append your

setData = instance.updateReading(consumerAddress,consumerMeterId,consumedReadingValue,producerAddress,producerMeterId,producedReadingValue).encodeABI();

For more details please refer below link

https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-encodeabi

https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction

  • encodeABI is the web3.js 1.0.0 version of web3.js 0.2x.x's getData. But they're using Truffle (which uses web3.js 0.2x.x underneath), which is why my answer works. – smarx Feb 12 '18 at 13:03

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.