スマートコントラクトでHello worldしてみる

[前回](https://qiita.com/gaisho81/items/876bf04063cae244777c)でプライベートネットを立ち上げるところまでいったのでhello worldをサクッといきます

#solcのインストール
Homebrewを使ってサクッとインストール

```sh
$ brew install solidity
```

前回作ったホームディレクトリ下のworkに`hello.sol`を作成し、solidityコードを記述

```sh
$ vi work/hello.sol`
```

```:hello.sol
pragma solidity ^0.4.0;

contract Hello{
	function get() constant returns (string){
        return "Hello World";
    }
}
```
`pragma`ですが、solcのバージョンが0.4.21だったので、0.4.0~0.4.21ならどれでもいけそうかと。

#コンパイル
コンパイルと聞くと学生時代のTeXを思い出させます。

```sh
$ solc --bin --abi hello.sol
```
`--bin`でバイトコード生成し、`--abi`がコントラクトのインターフェイス情報を生成するらしいです

結果

```
======= hello.sol:Hello =======
Binary: 
6060604052341561000f57600080fd5b6101578061001e6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636d4ce63c14610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc610117565b6040805190810160405280600b81526020017f48656c6c6f20576f726c64000000000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a72305820170f14a292fcfa176c56c088f87c91e3bce36c563a9c00a00d9f70de605d12720029
Contract JSON ABI 
[{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]
```

#デプロイ
[前回](https://qiita.com/gaisho81/items/876bf04063cae244777c)で立ち上げているプライベートネットにデプロイします。
gethのコンソールにて以下を実行。バイナリのほうは頭に`0x`をつけます。

```console
> bin = "0x6060604052341561000f57600080fd5b6101578061001e6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636d4ce63c14610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc610117565b6040805190810160405280600b81526020017f48656c6c6f20576f726c64000000000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a72305820170f14a292fcfa176c56c088f87c91e3bce36c563a9c00a00d9f70de605d12720029"
"0x6060604052341561000f57600080fd5b6101578061001e6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636d4ce63c14610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc610117565b6040805190810160405280600b81526020017f48656c6c6f20576f726c64000000000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a72305820170f14a292fcfa176c56c088f87c91e3bce36c563a9c00a00d9f70de605d12720029"
> abi = [{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]
[{
    constant: true,
    inputs: [],
    name: "get",
    outputs: [{
        name: "",
        type: "string"
    }],
    payable: false,
    stateMutability: "view",
    type: "function"
}]
> contract = eth.contract(abi)
{
  abi: [{
      constant: true,
      inputs: [],
      name: "get",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }],
  eth: {
    accounts: ["0x94da4fca6188bf0510fc8b6b2ccfbc8f4ecc8527", "0x699a41fb91251f35149d6c4d1bf6b4589a0c6e45", "0xd77bfea42da2508a0d4b06a4d457b2eb7a9b18df"],
    blockNumber: 9,
    coinbase: "0x94da4fca6188bf0510fc8b6b2ccfbc8f4ecc8527",
    compile: {
      lll: function(),
      serpent: function(),
      solidity: function()
    },
    defaultAccount: undefined,
    defaultBlock: "latest",
    gasPrice: 1,
    hashrate: 0,
    mining: true,
    pendingTransactions: [],
    protocolVersion: "0x3f",
    syncing: false,
    call: function(),
    contract: function(abi),
    estimateGas: function(),
    filter: function(options, callback, filterCreationErrorCallback),
    getAccounts: function(callback),
    getBalance: function(),
    getBlock: function(),
    getBlockNumber: function(callback),
    getBlockTransactionCount: function(),
    getBlockUncleCount: function(),
    getCode: function(),
    getCoinbase: function(callback),
    getCompilers: function(),
    getGasPrice: function(callback),
    getHashrate: function(callback),
    getMining: function(callback),
    getPendingTransactions: function(callback),
    getProtocolVersion: function(callback),
    getRawTransaction: function(),
    getRawTransactionFromBlock: function(),
    getStorageAt: function(),
    getSyncing: function(callback),
    getTransaction: function(),
    getTransactionCount: function(),
    getTransactionFromBlock: function(),
    getTransactionReceipt: function(),
    getUncle: function(),
    getWork: function(),
    iban: function(iban),
    icapNamereg: function(),
    isSyncing: function(callback),
    namereg: function(),
    resend: function(),
    sendIBANTransaction: function(),
    sendRawTransaction: function(),
    sendTransaction: function(),
    sign: function(),
    signTransaction: function(),
    submitTransaction: function(),
    submitWork: function()
  },
  at: function(address, callback),
  getData: function(),
  new: function()
}

```
トランザクションを登録

```console
> Hello = contract.new({from:eth.accounts[0], data:bin, gas:1000000})
{
  abi: [{
      constant: true,
      inputs: [],
      name: "get",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }],
  address: undefined,
  transactionHash: "0xb7446f85f4e9d4a691e251b2acb9227a1be9a82310e288d7983cd197d20a3362"
}
```
gasはてきとう

実行します。

```console
> Hello
{
  abi: [{
      constant: true,
      inputs: [],
      name: "get",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }],
  address: "0x06abda00a2e8f29b9d012f4c2c459a5dc02ace12",
  transactionHash: "0xb7446f85f4e9d4a691e251b2acb9227a1be9a82310e288d7983cd197d20a3362",
  allEvents: function(),
  get: function()
}
```
これでブロックチェーン上に登録されたはずなので呼び出してみます。

```console
> Hello.get.call()
"Hello World"
```

でましたあああああ

Hello Worldするだけなのに随分と七面倒臭いことしましたが、でも
デプロイ先をメインチェーンにするとこれがethereumに刻まれ、消えずに呼び出せることを考えるとブロックチェーンの可能性を改めて感じさせられます。

仕事柄、給与とかを扱う際は口頭や電話はNGで必ずメールやチャットなど残る形にしていますが、今後はそういうの全部ブロックチェーンにしてしまえばいいじゃん、と。
プロポーズの言葉なんかも刻みつけてしまい、スマートスピーカーが喧嘩っぽい声を聞いたらそれを読み上げるみたいな
ことしたら離婚率が下がったりするのかなーとか。