Transferring ETH

In this lesson you'll learn how to transfer ETH from one account to another account. If you're already familar with Ethereum then you know that a transaction consists of the amount of ether you're transferring, the gas limit, the gas price, a nonce, the receiving address, and optionally data. The transaction must be signed with the private key of the sender before it's broadcasted to the network.

Assuming you've already connected a client, the next step is to load your private key.

Afterwards we need to get the account nonce. Every transaction requires a nonce. A nonce by definition is a number that is only used once. If it's a new account sending out a transaction then the nonce will be 0. Every new transaction from an account must have a nonce that the previous nonce incremented by 1. It's hard to keep manual track of all the nonces so the ethereum client provides a helper method PendingNonceAt that will return the next nonce you should use.

The function requires the public address of the account we're sending from -- which we can derive from the private key.

The next step is to set the amount of ETH that we'll be transferring. However we must convert ether to wei since that's what the Ethereumblockchain uses. Ether supports up to 18 decimal places so 1 ETH is 1 plus 18 zeros. Here's a little tool to help you convert between ETH and wei: https://etherconverter.online

The gas price must be set in wei. At the time of this writing, a gas price that will get your transaction included pretty fast in a block is 30 gwei.

gasPrice := big.NewInt(30000000000) // in wei (30 gwei)

However, gas prices are always fluctuating based on market demand and what users are willing to pay, so hardcoding a gas price is sometimes not ideal. The go-ethereum client provides the SuggestGasPrice function for getting the average gas price based on x number of previous blocks.

Now we can finally generate our unsigned ethereum transaction by importing the go-ethereumcore/types package and invoking NewTransaction which takes in the nonce, to address, value, gas limit, gas price, and optional data. The data field is nil for just sending ETH. We'll be using the data field when it comes to interacting with smart contracts.

The next step is to sign the transaction with the private key of the sender. To do this we call the SignTx method that takes in the unsigned transaction and the private key that we constructed earlier. The SignTx method requires the EIP155 signer, which we derive the chain ID from the client.