2

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)

7

1 Answer 1

2

dude (this is the third question about the same thing), I have changed some methods if you have questions about it write it down please and dont open a new question :)

this is the new code

var Tx = require('ethereumjs-tx');
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/......");
const web3 = new Web3(provider);

const account1 = '.......'; // Your account address 1
//const account2 = '' // Your account address 2
web3.eth.defaultAccount = account1;

const privateKey1 = Buffer.from('.......', 'hex');

const abi = [{"constant":false,"inputs":[{"name":"_greeting","type":"string"}],"name":"greet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getGreeting","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}];

const contract_Address = "0xcbe74e21b070a979b9d6426b11e876d4cb618daf";

const myContract = new web3.eth.Contract(abi, contract_Address);

const myData = myContract.methods.greet( "hello blockchain devs").encodeABI();

web3.eth.getTransactionCount(account1, (err, txCount) => {
// Build the transaction
  const txObject = {
    nonce:    web3.utils.toHex(txCount),
    to:       contract_Address,
    value:    web3.utils.toHex(web3.utils.toWei('0', 'ether')),
    gasLimit: web3.utils.toHex(2100000),
    gasPrice: web3.utils.toHex(web3.utils.toWei('6', 'gwei')),
    data: myData  
  }
    // Sign the transaction
    const tx = new Tx(txObject);
    tx.sign(privateKey1);

    const serializedTx = tx.serialize();
    const raw = '0x' + serializedTx.toString('hex');

    // Broadcast the transaction
    const transaction = web3.eth.sendSignedTransaction(raw, (err, tx) => {
        console.log(tx)
    });
});

and this is the proof it work

enter image description here

7
  • changed : new web3.eth.Contract(abi, contract_Address) and gasLimit: web3.utils.toHex(2100000) and web3.eth.sendSignedTransaction
    – Majd TL
    May 20, 2019 at 14:47
  • now i'm getting an error "TypeError: web3.eth.Contract is not a constructor". I'm on web3 1.0.0 beta
    – ajay
    May 20, 2019 at 15:57
  • are you sure ? I'm using '1.0.0-beta.55' . write this in your code and take a look on the web3 version " const web3 = new Web3(provider); console.log(web3.version);"
    – Majd TL
    May 20, 2019 at 16:40
  • @ajay did you fix it?
    – Majd TL
    May 21, 2019 at 6:06
  • Yeah. Thank you @Majid TL. Appreciated your effort..
    – ajay
    May 21, 2019 at 9:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.