Web3js – How to Send Payment from Wallet on Ubuntu

Ubuntuweb3js

How can i send payments from my ubuntu ethereum node to any address?

Is there any working example, that may help me to get this done?

CODE EDITED

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'));
web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/<account-id>'));
var gasPrice = "2";//or get with web3.eth.gasPrice
var gasLimit = 3000000;
var addr = "0x......................";
var toAddress = "0x..............................";
var amountToSend = "0.00192823123348952"; //$1
var nonce = web3.eth.getTransactionCount(addr); //211;
var rawTransaction = {
"from": addr,
"nonce": web3.utils.toHex(nonce),
"gasPrice": web3.utils.toHex(gasPrice * 1e9),
"gasLimit": web3.utils.toHex(gasLimit),
"to": toAddress,
"value": amountToSend ,
"chainId": 41 //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.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
if (!err)
{
console.log('Txn Sent and hash is '+hash);
}
else
{
console.error(err);
}
});

Best Answer

There are 2 ways to do it. If you want to use the address with its password, follow the method below.

web3.personal.unlockAccount(addr, pass);
const toAddress = "0x...."; // Address of the recipient
const amount = 2; // Willing to send 2 ethers
const amountToSend = web3.utils.toWei(amount, "ether"); // Convert to wei value
var send = web3.eth.sendTransaction({ from: addr, to: toAddress, value: amountToSend });

For this above method, you would need the keystore file to be present within the same Ubuntu machine.

If you want to send with privateKey:

var gasPrice = 2; // Or get with web3.eth.gasPrice
var gasLimit = 3000000;

var rawTransaction = {
  "from": addr,
  "nonce": web3.toHex(nonce),
  "gasPrice": web3.utils.toHex(gasPrice * 1e9),
  "gasLimit": web3.utils.toHex(gasLimit),
  "to": toAddress,
  "value": amountToSend,
  "chainId": 4 // Remember to change this
};

var privKey = new Buffer(privateKey, 'hex');
var tx = new Tx(rawTransaction);

tx.sign(privKey);
var serializedTx = tx.serialize();

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
    if (!err)
    {
        console.log('Txn Sent and hash is '+hash);
    }
    else
    {
        console.error(err);
    }
});