I want to send a transaction with data using web3js, but I had an error like this
Returned error: The method eth_sendTransaction does not exist/is not available
const Web3 = require('web3')
const url_ropsten = 'https://ropsten.infura.io/v3/17425404118d470ab61c1cded765a510'
const web3 = new Web3(url_ropsten)
const message = "data";
web3.eth.sendTransaction({
from: 'address1',
to: 'address2',
value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
gasLimit: web3.utils.toHex(21000),
data: web3.utils.toHex(message),
}, function (error, hash) {
if (!error) {
console.log('Transaction is - ', hash)
}
else {
console.log('Error is - ', error)
}
});
sendTransaction
you have to sign the transaction yourself (npmjs.com/package/@ethereumjs/tx) or use a middleware that will sign them for you (npmjs.com/package/@truffle/hdwallet-provider). Alternatively you could use ethers.js that have an integration with Infura.