I am trying to call a smart contract but i am getting error : The function eth_sendTransaction does not exist. I think it is because of transaction not signed , but how can i sign the transaction when i am calling .send() method. Please tell me what am i doing wrong here?
My smart contract function does not accept any argument but when executed it changes a bool value so i am assuming it is changing the state of contract. So can anyone please guide me how to call functions to alter contract data and also transaction to get data.
const sourceCode = fs.readFileSync('contract_path', 'utf8').toString();
const compiledCode = compiler.compile(sourceCode, 1).contracts[':Contract_Name']
const abi = JSON.parse(compiledCode.interface);
const bin = compiledCode.bytecode;
var contractInstance = new web3.eth.Contract(abi, data.contractAddr);
// build the transaction
web3.eth.getTransactionCount(data.signerPubKey, (err, txCount) => {
if (txCount) {
// using the callback
contractInstance.methods.signAgreement().send({ nonce: web3.utils.toHex(txCount), from: 'my_public_key', gasLimit: web3.utils.toHex('2100000'), gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')) }, function (error, transactionHash) {
if (error) {
next(error, null)
} else if (transactionHash){
next(null, transactionHash)
}
});
} else if (err && err.message) {
next(err.message, null);
}
})
Best Answer
Sending a transaction without signing it first is supported only if you unlock your account on the node that you're communicating with beforehand (for example, when you run your tests on Ganache).
On public nodes, this is obviously not feasible (neither would you want it to be, of course).
You'll need to sign the transaction first, and then send the signed-transaction instead.
For example (tested with web3.js v1.2.1):