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)