FiFiWiki2

FiFiWikiの第2版

ユーザ用ツール

サイト用ツール


ethereum:チュートリアル

イーサリアムチュートリアル

geth のインストール

Ubuntu の場合

snippet.bash
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt update
sudo apt install ethereum

geth の起動

snippet.bash
作業フォルダを作る
mkdir ~/workspace/my_eth_chain
 
サーバーを起動?
$ geth --dev --datadir /home/ubuntu/workspace/my_eth_chain
 
そうしたら別のターミナルを開き、以下のコマンドで geth のコンソールを開きます。geth.ipc があるディレクトリ (datadir) の中で実行してください。
$ geth attach geth.ipc

コマンドメモ

snippet.bash
アカウントの作成 personal.newAccount()
アカウントの確認 eth.accounts
コインベースの確認 eth.coinbase (デフォルトではeth.accounts[0])
マイニング eth.getBalance(eth.accounts[0])
アカウントのアンロック personal.unlockAccount(eth.accounts[0])
アカウントのアンロック personal.unlockAccount(address, "password")
残高 eth.getBalance(eth.accounts[0])
残高 eth.getBalance("0x******************")
送金 eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:1000000000000000000})
トランジションの確認  eth.getTransaction("0x295a6db3a1eb7192057982f531ffd2c74dfd956d22319f4758eac30177f4afc5")
同期の確認  eth.syncing
コインベースの変更 miner.setEtherbase(eth.accounts[0])
プライベートキーのインポート $ geth --datadir /someOtherEthDataDir  account import ./key.prv
単位変換 web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
採掘できているかは eth.hashrate 
ブロック数の確認  eth.blockNumber
ブロックの内容を調査 eth.getBolck(eth.blockNumber)

スマートコントラクト

snippet.solidity
contract SingleNumRegister {
    uint storedData;
    function set(uint x) {
        storedData = x;
    }
    function get() constant returns (uint retVal) {
    return storedData;
    }
}

コンパイル

snippet.bash
Ubuntu の場合
$ sudo apt install solc
$ solc --version
 solc, the solidity compiler commandline interface
 Version: 0.4.17+commit.bdeb9e52.Linux.g++
 
 
$ solc --bin --abi HelloWorld.sol 

コンソールで読み込ませる

snippet.bash
> bin = "0x60606040.......................5"
> abi=[{"constant":fal ... le","type":"function"}]
> contract = eth.contract(abi)
 
> personal.unlockAccount(eth.accounts[0])
> HelloWorld = contract.new({from: eth.accounts[0], data:bin, gas:1000000 })
> miner.start()
> miner.stop()
> HelloWorld
{
  abi: [{
      constant: false,
      inputs: [{...}],
      name: "setMessage",
      outputs: [],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: false,
      inputs: [],
      name: "sayHello",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }],
  address: "0xf7b0d913ff63433950b2d7e5ed3280470464487b",
  transactionHash: "0x8bcc6faf16e02bdbb2166d6d2643ccdfa2976ff570c2f88f2f7bc56108684170",
  allEvents: function(),
  sayHello: function(),
  setMessage: function()
}

スマートコントラクトの呼び出し

  1. sendTransaction()
  2. call()
snippet.bash
> HelloWorld.sayHello.call()
"" <- Nothing yet
 
> HelloWorld.setMessage.sendTransaction("fifi hello!", {from:eth.accounts[0]})
"0x2640526bf49******2c6eebaa0672ad7dda38ff8fd909306d1dd7f7c4f"
 
> HelloWorld.sayHello.call()
"fifi hello!"

本番ネットへ接続

ブロックチェーンをダウンロードしているらしくて容量が2GBでいっぱいになった。 ローカルPCでブロックチェーンをすべてダウンロードしてやる。 cloud9ではテスト環境で遊ぶだけしかできない。 以上

memo

snippet.bash
1 ether = 10^3 finney
1 ether = 10^6 szabo
1 ether = 10^18 wei
ethereum/チュートリアル.txt · 最終更新: 2017/10/14 23:20 by fifi