iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 7
1
Blockchain

以太坊-探索智能合約的多種面向系列 第 7

Day 7 搭建私有鏈(二) - 交易,轉帳,查詢

在開啟了區塊鏈並創立帳戶之後,可以開始熟悉一些基本操作,。
—轉帳交易
在Day 6的文章中,已經創建了一個帳戶,再創建一個帳戶以便來操作轉帳。

personal.newAccount(“123456”)

返回帳戶地址:

"0x385843e41e52c9671eba67562a76c4cd2debe16d"

確認有兩個帳戶,以陣列方式表示 :

> eth.accounts
["0x4ca5f4a1612ebe48d8e48c7df7b2f0d2839ebc56", "0x385843e41e52c9671eba67562a76c4cd2debe16d"]

eth.accounts[0] 帳戶 1
eth.accounts[1] 帳戶2

因為需要轉帳,所以必須先挖礦獲取Ether,首先確認挖礦帳戶。

> eth.coinbase  <= 回傳挖礦帳戶
"0x4ca5f4a1612ebe48d8e48c7df7b2f0d2839ebc56"

挖礦帳戶為eth.accounts[0]。
開啟挖礦:

miner.start(1)   <=thread 
INFO [10-21|21:48:47.073] Updated mining threads                   threads=1
INFO [10-21|21:48:47.073] Transaction pool price threshold updated price=1000000000
null
> INFO [10-21|21:48:47.073] Commit new mining work                   number=1 sealhash=7b1668…5e19be uncles=0 txs=0 gas=0 fees=0 elapsed=355.717µs

直到出現
"
INFO [10-21|21:49:40.426] Successfully sealed new block number=1 sealhash=7b1668…5e19be hash=2de702…796833 elapsed=53.352s
INFO [10-21|21:49:40.426] https://ithelp.ithome.com.tw/upload/images/20181022/20108738ErEAKzFKlQ.jpg mined potential block
"
停止挖礦:

miner.stop()

確認eth.accounts[0]與eth.accounts[1]的餘額,成功挖幾次礦後,查詢餘額

wbe3.fromWei(eth.getBalance(eth.accounts[0]),”ether”)

查詢餘額返回的是以wei為單位的值,用web3.fromWei()將返回值轉換成Ether。

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

發送交易:
發送交易之前,必須先將發送方的帳戶解鎖:

> personal.unlockAccount(eth.accounts[0],"123456") <=參數(account,password)
true

Transaction:

>eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(5,"ether")});
INFO [10-21|22:31:59.708] Setting new local account                address=0x4ca5F4A1612EBe48d8E48c7dF7B2f0d2839EbC56
INFO [10-21|22:31:59.708] Submitted transaction                    fullhash=0x50df04fd35f0a0212d8d12a3162856d153fcb46a88dc665985f8dc5241d3b55e recipient=0x385843E41e52c9671EBa67562A76c4cd2dEbE16d
"0x50df04fd35f0a0212d8d12a3162856d153fcb46a88dc665985f8dc5241d3b55e"

返回交易雜湊值,以交易雜湊值查詢交易:

>eth.getTransaction("0x50df04fd35f0a0212d8d12a3162856d153fcb46a88dc665985f8dc5241d3b55e");
{
  blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  blockNumber: null,
  from: "0x4ca5f4a1612ebe48d8e48c7df7b2f0d2839ebc56",
  gas: 90000,
  gasPrice: 1000000000,
  hash: "0x50df04fd35f0a0212d8d12a3162856d153fcb46a88dc665985f8dc5241d3b55e",
  input: "0x",
  nonce: 0,
  r: "0xc2011ee73158f6d530c1799d6ae58f386fa2d9fdbf9c226d2e23863c8baed679",
  s: "0xa5d2b3e6822846c937054a0bce2c6a4e22def36bb9a260547a67aa9538aca53",
  to: "0x385843e41e52c9671eba67562a76c4cd2debe16d",
  transactionIndex: 0,
  v: "0x4b",
  value: 5000000000000000000
}

在交易列表中,blockNumber的值為null,代表交易尚未被寫入區塊,
檢查交易緩衝區的狀態,觀察等待中的交易 :

> txpool.inspect.pending
{
  0x4ca5F4A1612EBe48d8E48c7dF7B2f0d2839EbC56: {
    0: "0x385843E41e52c9671EBa67562A76c4cd2dEbE16d: 5000000000000000000 wei + 90000 gas × 1000000000 wei"
  }
}   <=  發送方:{nonce: 接收方 :轉帳金額+手續費*手續費單位"}

要讓交易被處理,必需先挖擴,執行挖礦:

miner.start(1);admin.sleepBlocks(1);miner.stop();

admin.sleepBlocks(1); =>挖1次礦之後,執行後續指令
檢查交易:

>eth.getTransaction("0x50df04fd35f0a0212d8d12a3162856d153fcb46a88dc665985f8dc5241d3b55e");
{
  blockHash: "0x6b2e8fd8e70880706a743a27253fbc7774a96b052193d81c2f6c1c6070ed23c0",
  blockNumber: 12,
  from: "0x4ca5f4a1612ebe48d8e48c7df7b2f0d2839ebc56",
  gas: 90000,
  gasPrice: 1000000000,
  hash: "0x50df04fd35f0a0212d8d12a3162856d153fcb46a88dc665985f8dc5241d3b55e",
  input: "0x",
  nonce: 0,
  r: "0xc2011ee73158f6d530c1799d6ae58f386fa2d9fdbf9c226d2e23863c8baed679",
  s: "0xa5d2b3e6822846c937054a0bce2c6a4e22def36bb9a260547a67aa9538aca53",
  to: "0x385843e41e52c9671eba67562a76c4cd2debe16d",
  transactionIndex: 0,
  v: "0x4b",
  value: 5000000000000000000
}

blockNumber : 12,交易被寫入第12個區塊,查詢帳戶1餘額 :

web3.fromWei(eth.getBalance(eth.accounts[1]),”ether”)
>5

交易成功。


上一篇
Day 6 搭建私有鏈(二) - 開啟區塊鏈,創建帳戶
下一篇
Day 8 搭建私有鏈(三) - 在私有鏈中觀察同步
系列文
以太坊-探索智能合約的多種面向20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言