I'm trying to use web3 to transfer all (or as much as I can) of the balance of an account to another account. Here is the code I have so far:
var sender = web3.eth.accounts[0];
var receiver = web3.eth.accounts[1];
var totalBalance = web3.eth.getBalance(sender);
var valueToSend = totalBalance;
transactionObject = {
from: sender,
to: receiver,
value: valueToSend
}
web3.eth.sendTransaction(transactionObject);
However I know that the previous code has the following problem:
- The total balance can not be sent because there wouldn't be enough residual ether to be used as gas for the transaction.
I saw another post make the following recommendation:
subtract the totalBalance by gas*price, hence:
valueToSend = totalBalance - (gas*price);
QUESTION / PROBLEM:
but the problem I am having here is how would that look in code? I don't know how to obtain the current price
in code. And for the gas, I understand that there are different values for gas depending on how fast you want a transaction to go, but is there web3
code that defines the least and most you can use? Or is there a graph somewhere that defines it online? And how would that look in code?