0
let Tx= require('ethereumjs-tx').Transaction;
const Web3= require('web3');
const url='https://rinkeby.infura.io/v3/d4dbc79ea192404fb42689cc55ce2fc0';
const web3 =new Web3(url);

const account1= '0x0B872d2A7F3Ca3bD24Ab1A9fe8FBC8059Ec32b5F';
const account2='0x20107D4a4e8BB5D50800288029ad58cE929D2c56';

//web3.eth.getBalance(account1,(err,bal)=>{ console.log(web3.utils.fromWei(bal,"ether"))});

//web3.eth.getBalance(account2,(err,bal)=>{ console.log(web3.utils.fromWei(bal,"ether"))});

const privatekey1 = new Buffer('A337E5C704CF4187C0CECF4A6E38AD42182B4D038EB8615E239216C5164FEB40','hex');
//const privatekey2 = Buffer.from(process.env.PRIVATE_KEY_2,'hex');

web3.eth.getTransactionCount(account1,async (err,txCount)=>{

//console.log(txCount);

//Build the transaction

const txObject ={
    nonce:web3.utils.toHex(txCount+1),
    to:account2,
    value:web3.utils.toHex(web3.utils.toWei('1',"ether")),
    gasLimit:web3.utils.toHex(210000),
    gasPrice:web3.utils.toHex(web3.utils.toWei('10',"gwei"))
}

//console.log(txObject);

//sign the transaction
const tx = new Tx(txObject);
tx.sign(privatekey1);

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

web3.eth.sendSignedTransaction(raw,(err,txHash)=>{
  console.log(txHash);
})

2 Answers 2

0

You just need to specify you are in the testnet

const tx = new Tx(txObject, {chain: 'rinkeby'});
0

You may need to specific the value for from of the tnx also as below


const txObject ={
    from: account1,
    nonce: web3.utils.toHex(txCount+1),
    to: account2,
    value: web3.utils.toHex(web3.utils.toWei('1',"ether")),
    gasLimit: web3.utils.toHex(21000),
    gasPrice: web3.utils.toHex(web3.utils.toWei('10',"gwei"))
}

And make sure that the prikey used to sign the tnx is belong to account1. It'd better to use ethereumjs-util to get address from privatekey as below

const util = require('ethereumjs-util'); 
const fromAddress = '0x' + util.privateToAddress(privkey).toString('hex');

By the way, we only need 21000 for sending tnx, not so much like 210000

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – eth
    Jul 23, 2019 at 11:01

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.