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?
getData
. I think you're expecting web3.js'sgetData
function, but you're using Truffle.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)