Using Geth to Transfer Ethereum Between Accounts

Let's transfer ETH between accounts

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

If we're going to spend some ether, we need to have someone to send it to. But right now, we only have one account:

eth.accounts

We could create a new account using the geth commandline, like last time, but we also can create an account from here:

# I'm going to use the same password as last time
personal.newAccount("abc123")

Now let's check our list of accounts:

eth.accounts

Great, now let's check the balances between them:

eth.getBalance(eth.accounts[0])
eth.getBalance(eth.accounts[1])

To send ether we need to specify:

  • the from address
  • the to address and
  • the value of the transaction

We'll do this with the sendTransaction method:

eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(3, "ether")})

Uh oh! We got an error. It says, "authentication needed" - why is that? Well, it's because our "from" address is locked. This is a security mechanism to prevent you from sending funds when you didn't intend to.

Let's unlock our sending account and then try sending the funds again:

personal.unlockAccount(eth.accounts[0], "abc123")

# I'm hitting up-arrow here to search my history and find this transaction
eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(3, "ether")})

Great! This says, "submitted transaction" and it returned a hash. This hash is the identifier of our transaction. Let's look at it more closely:

eth.getTransaction("XYZ123")

Take a look at the to and the from fields. You'll notice that these match our account addresses. Also, the value field is the value we were sending, denominated in wei.

Since we submitted a transaction, sending 3 ether from one account to another, our balances should have changed, right? Let's check:

eth.getBalance(eth.accounts[0])
eth.getBalance(eth.accounts[1])

Hmm - it looks the same as before. Why is that? Well, it's because even though we submitted a transaction, that block hasn't been processed by any miners.

Take look at the information returned by getTransaction again. Notice that the blockNumber says "null" and the blockHash is just zero. This transaction hasn't been included in any block, so our account balances haven't changed.

In order to process this transaction, we need our miners to include it in a block. We can do that by starting our miner again:

miner.start(1)
miner.stop()

Notice in our miner debug log that the txs equals 1 -- this is saying that this particular block has one transaction. Notice that the rest of the txs are 0, that's because we aren't submitting any transactions, but the miner will still mine "empty" blocks, just to get the reward.

Now let's try checking our balances again:

web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")

It's N - well, it's hard to tell if that worked, because this is our etherbase account, and so it's earned rewards while we were mining. Let's check the destination address:

web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")

Great! It has 3 ether at that account, so it worked.

Let's take a look at our transaction hash again:

eth.getTransaction("XYZ123")

Notice that we now have a blockNumber M as well as a blockHash. We can take a closer look at the block this transaction was included in by using eth.getBlock:

eth.getBlock("XYZ123")

There's a lot of data in here that will make more sense as we get further along, but take a look at the array of transactions, notice that it contains our transaction's hash.

Each block stores a reference to it's parent block. This is block number N, but it's parent is block number N-1. We could look up the parent block by using parentHash. If we looked at parentHash we would see a list of transactions there, and then we could look at it's parent, and so on, all the way back to the original genesis block.

This idea of a block being parented by another block is the "chain" part of a blockchain. Every transaction that has ever happened in Ethereum can be found in this way.

  • |

Lesson Transcript

  • [00:00 - 00:09] If we're going to spend some ether, we need to have someone to send it to. But right now, we only have one account. We could create a new account by using the "Geth" command line like we did earlier. But we can also create an account from here.

  • [00:10 - 00:17] We do that by calling "Personal New Account" and then passing the password for that account. I'm going to use the same password as last time, "avc123".

  • [00:18 - 00:29] Now let's check our list of accounts. Great, now let's check the balances between them. So in this first account, we have "ether" because that's the account we sent our mining rewards to.

  • [00:30 - 00:55] But in the second account, we don't have anything. To send "ether" from one account to another, we need to specify the "from" address, the "to" address, and the value of the transaction. We'll do this with the "sendTransaction" method. We'll type "eth.sendTrans action" from "eth account zero" to "eth accounts one" and the value will be "three ether".

  • [00:56 - 01:31] Uh-oh, we got an error. It says "authentication needed". Why is this? Well, it 's because our "from" address is locked. This is a security mechanism to prevent you from sending funds when you didn't intend to. Let's unlock our sending account and then try sending the funds again. We'll call "personal.unlock account, pass our address, and then our password". Now we can try sending our transaction again. I'm hitting my up arrow here to search my history and find the code earlier that we use to send a transaction. Great, this says "submitted transaction" and it returned a hash.

  • [01:32 - 02:16] This hash is the identifier of our transaction. Let's look at it more closely. We can do that by calling " eth.getTransaction" and then passing the hash. Take a look at the "two" and the "from" fields. You 'll notice that these match our account addresses. Also, the "value" field is the value we were sending denominated in way. Since we submitted a transaction, sending three ether from one account to another, our balances should have changed, right? Well, let's check. We'll call "eth.get _balance" on account zero and "eth.get_balance" on accounts one. It looks the same as before . Why is that?

  • [02:17 - 02:39] Well, it's because even though we submitted a transaction, they haven't been included in a block that was processed by the miners. Take a look at the information returned by " get_transaction" again. Notice that the block number says "null" and the block hash is just zero. This transaction hasn't been included in any block, so our account balances haven't changed.

  • [02:40 - 02:51] In order to process this transaction, we need our miners to include it in a block. We can do that by starting our miner again. Here we'll call "minorStart". Wait a few seconds.

  • [02:52 - 03:21] Wait for a few blocks to mine and then call "minor.stop". Notice in our miner debug log that the transactions equals one. This is saying that this particular block has one transaction. And notice that the rest of the transaction numbers are zero, and that's because we haven't been submitting any other transactions, but the miner will still mine the empty blocks just to get the reward. Now let's try checking our balances again.

  • [03:22 - 03:32] This time we'll get it denominated in ether. Well, with account zero it's hard to tell if that worked, because this is our ether-based account.

  • [03:33 - 03:42] So it's earned rewards while we were mining. Let's check the destination address. Great! It has three ether in that account, so our transaction worked.

  • [03:43 - 04:00] Let's take a look at our transaction hash again. Notice that we now have a block number, as well as a block hash. We can also take a closer look at the block this transaction was included in by using eth.getBlock.

  • [04:01 - 04:12] There's a lot of data in here that will make more sense as we get further along . But take a look at the array of transactions, and notice that it contains our transactions hash.

  • [04:13 - 04:37] Each block stores a reference to its parent block, and we could look up the parent block by using the parent hash. And if we looked at parent hash we would see a list of transactions there, and then we could look at its parent and so on, all the way back to the original Genesis block. This idea of a block being parented by another block is the chain part of a blockchain.

  • [04:38 - 04:58] Every transaction that has ever happened in Ethereum can be found this way. While it's exciting to send value from one account to another through a decentralized network, Ethereum can do so much more. What makes Ethereum so special is that we can write programs that will handle this value for us, and that's what we're going to take a look at next.

  • [04:59 - 05:14] [Music]

If we're going to spend some ether, we need to have someone to send it to. But right now, we only have one account. We could create a new account by using the "Geth" command line like we did earlier. But we can also create an account from here. We do that by calling "Personal New Account" and then passing the password for that account. I'm going to use the same password as last time, "avc123". Now let's check our list of accounts. Great, now let's check the balances between them. So in this first account, we have "ether" because that's the account we sent our mining rewards to. But in the second account, we don't have anything. To send "ether" from one account to another, we need to specify the "from" address, the "to" address, and the value of the transaction. We'll do this with the "sendTransaction" method. We'll type "eth.sendTrans action" from "eth account zero" to "eth accounts one" and the value will be "three ether". Uh-oh, we got an error. It says "authentication needed". Why is this? Well, it 's because our "from" address is locked. This is a security mechanism to prevent you from sending funds when you didn't intend to. Let's unlock our sending account and then try sending the funds again. We'll call "personal.unlock account, pass our address, and then our password". Now we can try sending our transaction again. I'm hitting my up arrow here to search my history and find the code earlier that we use to send a transaction. Great, this says "submitted transaction" and it returned a hash. This hash is the identifier of our transaction. Let's look at it more closely. We can do that by calling " eth.getTransaction" and then passing the hash. Take a look at the "two" and the "from" fields. You 'll notice that these match our account addresses. Also, the "value" field is the value we were sending denominated in way. Since we submitted a transaction, sending three ether from one account to another, our balances should have changed, right? Well, let's check. We'll call "eth.get _balance" on account zero and "eth.get_balance" on accounts one. It looks the same as before . Why is that? Well, it's because even though we submitted a transaction, they haven't been included in a block that was processed by the miners. Take a look at the information returned by " get_transaction" again. Notice that the block number says "null" and the block hash is just zero. This transaction hasn't been included in any block, so our account balances haven't changed. In order to process this transaction, we need our miners to include it in a block. We can do that by starting our miner again. Here we'll call "minorStart". Wait a few seconds. Wait for a few blocks to mine and then call "minor.stop". Notice in our miner debug log that the transactions equals one. This is saying that this particular block has one transaction. And notice that the rest of the transaction numbers are zero, and that's because we haven't been submitting any other transactions, but the miner will still mine the empty blocks just to get the reward. Now let's try checking our balances again. This time we'll get it denominated in ether. Well, with account zero it's hard to tell if that worked, because this is our ether-based account. So it's earned rewards while we were mining. Let's check the destination address. Great! It has three ether in that account, so our transaction worked. Let's take a look at our transaction hash again. Notice that we now have a block number, as well as a block hash. We can also take a closer look at the block this transaction was included in by using eth.getBlock. There's a lot of data in here that will make more sense as we get further along . But take a look at the array of transactions, and notice that it contains our transactions hash. Each block stores a reference to its parent block, and we could look up the parent block by using the parent hash. And if we looked at parent hash we would see a list of transactions there, and then we could look at its parent and so on, all the way back to the original Genesis block. This idea of a block being parented by another block is the chain part of a blockchain. Every transaction that has ever happened in Ethereum can be found this way. While it's exciting to send value from one account to another through a decentralized network, Ethereum can do so much more. What makes Ethereum so special is that we can write programs that will handle this value for us, and that's what we're going to take a look at next. [Music]