Im trying to send transaction to simple greeter contract with web3.js on infura node.while doing soo, Im getting this error.
throw errors.InvalidResponse(result);
^
Error: The method eth_sendTransaction does not exist/is not available
after lot of investigation about this error on internet, I came to conclusion that infura doesnt support this method and we have to sign transactions offline and send raw transactions to node. Im already doing the same. but keep getting this error please help me to fix this error.. here is my transaction signing code:
const contractFunction = contract.greet({from:web3.eth.defaultAccount},"hello all devs");
const functionAbi = contractFunction.encodeABI();
let estimatedGas;
let nonce;
console.log("Getting gas estimate");
contractFunction.estimateGas({from: account}).then((gasAmount) => {
estimatedGas = gasAmount.toString(16);
console.log("Estimated gas: " + estimatedGas);
web3.eth.getTransactionCount(account).then(_nonce => {
nonce = _nonce.toString(16);
console.log("Nonce: " + nonce);
const txParams = {
gasPrice: '0x09184e72a000',
gasLimit: 3000000,
to: contract_Address,
data: functionAbi,
from: account,
nonce: '0x' + nonce
};
const tx = new Tx(txParams);
signed_tx = web3.eth.account.signTransaction(tx, privateKey)
tx_hash= web3.eth.sendRawTransaction(signed_tx.rawTransaction)
tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
//tx.sign(privateKey);
console.log(tx_receipt)
web3.eth.sendRawTransaction
returns aPromise
object, not a transaction hash. You need to eitherawait
for it to complete (which means that you'll have to put everything inside anasync
function), or resolve the promise in some other way.await
beforeweb3.eth.sendRawTransaction
, and put the whole thing inside anasync
function.