1

I am using folowing code sample to send ether between 2 of my own accounts, both have enough funds:

"use strict";
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');
// Show Web3 where it needs to look for a connection to Ethereum.
//web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545'));
var web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/N6IIa2HvDYOovtgmPbhD'));
var gasPrice = "20";//or get with web3.eth.gasPrice
var gasLimit = '42000';
var addr = "...";
var toAddress = "....";
var amountToSend = web3.utils.toWei('0.0005', 'ether'); //$1
var nonce = web3.eth.getTransactionCount(addr); //211;
var rawTransaction = {
"from": addr,
"nonce": web3.utils.toHex(nonce),
"gasPrice": web3.utils.toHex(web3.utils.toWei('9', 'gwei')),
"gasLimit": web3.utils.toHex(gasLimit),
"to": toAddress,
"value": web3.utils.toHex(amountToSend) ,
"chainId": 1 //remember to change this
};
var privateKey = "...";
var privKey = new Buffer(privateKey, 'hex');
//console.log("privKey  : ", privKey);
var tx = new Tx(rawTransaction);
tx.sign(privKey);
var serializedTx = tx.serialize();
console.log('serializedTx : '+serializedTx);
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
if (!err)
{
console.log('Txn Sent and hash is '+hash);
let userBalance = (web3.eth.getTransactionReceipt(hash));
console.log(userBalance);
userBalance.then(function(result) {
   console.log(result);
})

}
else
{
console.error(err);
}
});

however, what i get from console log is:

Txn Sent and hash is 0xab9a2265792203d787d0e08c7a808865b1d776d9a289d92ded2596c352b6e56c
Promise { <pending> }
null

this is not updated on etherscan or anywhere on main net. I am quite confused as there is nowhere telling me where I am wrong. Does someone know how to fix it?

1 Answer 1

1

just remove "from" address from your rawTransaction and try again.read about given link. https://web3js.readthedocs.io/en/1.0/web3-eth.html#sendsignedtransaction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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