2

I'm taking initiative to learn with Web3js and Ganache for testing interactions with blockchain. (So go easy on me being a noob pls)

When trying #3 example from here: https://www.dappuniversity.com/articles/web3-js-intro

I am getting error "TypeError: web3.eth.sendSignedTransaction is not a function"

When I console logged my Web3 element, sendSignedTransaction is not within the returned JSON. Please help?

var Tx = require("ethereumjs-tx").Transaction

const URL = "https://ropsten.infura.io/v3/{myInfuraID}" //Ropsten test network URL 

const Web3 = require('web3')

const web3 = new Web3(new Web3.providers.HttpProvider(URL)) 

console.log(web3) //sendSignedTransaction is not in response

const account1 = "{myGanacheAccount1}" //Ganache account 1
const account2 = "{myGanacheAccount2}" //Ganache account 2

const address1_keys = "{myGanacheAccountPrivateKey}"
const privateKey1 = Buffer.from(address1_keys, 'hex')

web3.eth.getTransactionCount(account1, (err, txCount) => {
    const txObject = {
        nonce: web3.toHex(txCount), //I also don't know why web3.utils.toHex(txCount) throws error
        to: account2,
        value: web3.toHex(web3.toWei('1', 'ether')),
        gasLimit: web3.toHex(21000),
        gasPrice: web3.toHex(web3.toWei('10', 'gwei'))
    }

    const tx = new Tx(txObject)
    tx.sign(privateKey1)

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

    web3.eth.sendSignedTransaction(raw, (err, txHash) => { //throws error here
        console.log('txHash: ', txHash)
    })
})

Error:

TypeError: web3.eth.sendSignedTransaction is not a function
    at Object.callback (C:\Users\Me\Documents\NFTs\Web3_js\testing.js:48:14)
    at C:\Users\Me\node_modules\web3\lib\web3\method.js:142:25
    at C:\Users\Me\node_modules\web3\lib\web3\requestmanager.js:89:9
    at XMLHttpRequest.request.onreadystatechange (C:\Users\Me\node_modules\web3\lib\web3\httpprovider.js:129:7)
    at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\Me\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (C:\Users\Me\nFode_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (C:\Users\Me\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (C:\Users\Me\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
    at IncomingMessage.emit (node:events:532:35)
at endReadableNT (node:internal/streams/readable:1346:12)

1 Answer 1

4

I think the new transaction object it's not complete. Try this code ☺

I tested with Goerli and Ropsten (worked in both). Technically Ropsten and Rinkeby are considered deprecated so try to use Goerli, at least you are sure that it's maintained and works as expected.

I used Chainstack's endpoints, which you can get for free.

Also, this code was taken from Chainstack's docs, from the Node API reference page. There are many RPC examples (in web3.py as well).

var Web3 = require('web3');
var node_URL = 'CHAINSTACK_NODE_URL';
var web3 = new Web3(node_URL);
var Tx = require('ethereumjs-tx').Transaction;

// Logic of this code:
    // Set the addresses and private key to sign the transaction
    // Build transaction 
    // Sign and send the transaction 

// Addresses and private key
const sender = "SENDER_ADDRESS";
const receiver = "RECEIVER_ADDRESS";
const private_key = Buffer.from('PRIVATE_KEY', "hex"); 

// Build the transaction
web3.eth.getTransactionCount(sender, (err, transactionCount) => {
    const transaction_Object = {
        to: receiver,
        gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
        gasLimit: web3.utils.toHex(21000),
        nonce: web3.utils.toHex(transactionCount),
        value: web3.utils.toHex(web3.utils.toWei("0.5", "ether")),
    };

    // Signing the transaction 

    // create a new transaction object to sign 
    const tx = new Tx(transaction_Object, {
        chain: "ropsten"  // Adapt this to the network to use. 
    });

    // sign the transaction using the private key  
    tx.sign(private_key);

    //   Send signed transaction to the blockchain 
    const sTx = tx.serialize();
    const rawTransaction = "0x" + sTx.toString("hex");

    web3.eth.sendSignedTransaction(rawTransaction, (err, hash) => {
        console.log("TxHash:" + hash);
        console.log(err);
    });
})

Hope this helps you!

3
  • Hi thanks for the assistance, however, I copied and pasted this code snippet you provided and changed 'CHAINSTACK_NODE_URL' to my chainstack wss URL and I changed the sender and receiver addresses to the addresses in my Ganache UI. Now I am getting an error this.provider.sendAsync(payload, function (err, result) { ^ TypeError: this.provider.sendAsync is not a function
    – NFTNoob
    Aug 2 at 0:13
  • 1
    Interesting, for me it works with either a WSS or HTTPS endpoint. Did you try to make the transfer between accounts on Ropsten or Goerli? Ganache runs locally, you would have to use the local ganache endpoint, which should be HTTP://127.0.0.1:7545 Chain Id: 5777. I tested this code transferring Ropsten and Goerli ETH between 2 of my MM accounts.
    – Soos3D
    Aug 2 at 1:08
  • 1
    Thanks so much for the help! I did have to change node_URL to 'HTTP://127.0.0.1:7545' then I needed to "npm audit fix --force" in terminal to get to web3 version 1.7.5 (I was on 0.20.7 was the error I previous showed above) then change to this piece const tx = new Tx(transaction_Object, { chainId: "5777" }); and behold it works!! Thank you so much
    – NFTNoob
    Aug 2 at 1:42

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.