MacOSでtruffleを使う
サクサク開発したいときはこっちの方が便利かも。
nodejsをインストール
gethをダウンロード
- 以下からgethをダウンロード - https://geth.ethereum.org/downloads/
tar xvf geth-darwin-amd64-1.6.7-ab5646c5.tar.gz
ln -s geth-darwin-amd64-1.6.7-ab5646c5 geth
test rpcの起動
testrpc -b 1
プロジェクト作成
- webpackを使う - https://github.com/trufflesuite/truffle-init-webpack
別のターミナルを開いて実行
mkdir project
cd project
truffle init webpack
コンパイル/テスト/デプロイ
truffle compile
truffle test
truffle deploy
webapp起動
さらに別のターミナルを開いて実行
cd project
npm run dev
プライベートネットにデプロイしたい場合
testrpcを止めてgethでプライベートネットを起動してtruffle deployすればOK
cd geth
vi private.json
{
"config": {
"chainId": 1,
"homesteadBlock": 0,
"eip155:Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000042",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x8000000",
"difficulty": "0x01",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {}
}
./geth --datadir=./private init ./private.json
./geth --networkid "1" --nodiscover --ipcpath ${HOME}/.ethereum/geth.ipc --rpc --rpcaddr 0.0.0.0 --rpcport 8545 -ws --wsaddr 0.0.0.0 --wsport 8546 --port 30303 --minerthreads 1 --rpccorsdomain "*" --datadir ./private console 2>> ./private.log
アカウントを作ってマイニングを初めて、getbalanceが0ではなくなるまで待ってアンロックする
> personal.newAccount("pass")
"0x65474bd903a10ff0954e5a86b4a28ce01fe86a80"
> miner.start()
> eth.getBalance("0x65474bd903a10ff0954e5a86b4a28ce01fe86a80")
90000000000000000000
> personal.unlockAccount("0x65474bd903a10ff0954e5a86b4a28ce01fe86a80")
この状態でtruffle deplayを行う
truffle deploy
Using network 'development'.
Running migration: 1_initial_migration.js
Deploying Migrations...
Migrations: 0xa4c0c8ea5aa6653e3f98bcdad65072428e0aa0fa
Saving successful migration to network...
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying ConvertLib...
ConvertLib: 0x262845cb19de2d17c47e56d91b32bbde06f5ee22
Linking ConvertLib to MetaCoin
Deploying MetaCoin...
MetaCoin: 0xc2b347b01cdb98fe7745aacb46e354dc4cad0b13
Saving successful migration to network...
Saving artifacts...
プライベートネットに、truffleを介してブラウザからアクセスする場合、 gethのオプションに–rpccorsdomain “*”をつけておかないとうまくいかないので注意。
イーサリアムで遊ぶ
最近、ブロックチェーンにははまり中。 何か作りたくなったので電子チケットを売買するサービスをイーサリアムで作ろうと思ろうと思う。基本的には勉強が目的。 まぁ、いつかイーサリアムが廃れて別の分散アプリケーションプラットフォームが台頭してきたときは、いらない知識になるかもしれない。
開発環境を作る
何はともあれ、開発環境がないと始まらないので、作ってみる。 ちなみに、面倒くさかった。 今回はvertual boxにubuntu desktopをインストールして、そこに開発環境を構築した。 Mac環境はいろいろ汚れててはまりまくったからやめたっていうのは内緒。
パッケージを入れる
大体必要なものを入れておく
apt-get install -y curl git build-essential software-properties-common
goを入れる
1.8以降
sudo add-apt-repository -y ppa:longsleep/golang-backports
sudo apt-get update
sudo apt-get install -y golang-go
gethを入れる
ここに従う
リポジトリを使っていれる。
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get install -y ethereum solc
上をやっていれば基本的に必要ないけど、git cloneしたい人は以下
git clone https://github.com/ethereum/go-ethereum
cd go-ethereum
make geth
nodejsを入れる
ここに従う
sudo apt-get install -y nodejs npm
sudo npm cache clean
sudo npm install n -g
sudo n 6.11.1
sudo ln -sf /usr/local/bin/node /usr/bin/node
sudo apt-get purge -y nodejs npm
mistを入れる
ここに従う
curl https://install.meteor.com/ | sh
curl -o- -L https://yarnpkg.com/install.sh | bash
インストールが終わると、パスを通せとメッセージが出るので、yarnにパスを通す。
vi .profile
PATH="$HOME/bin:$HOME/.local/bin:$HOME/.yarn/bin:$PATH"
続き
source .profile
yarn global add electron@1.4.15
yarn global add gulp
git clone https://github.com/ethereum/mist.git
cd mist
yarn
remix (browser-solidity) を入れる
ここに従う
git clone https://github.com/ethereum/browser-solidity
cd browser-solidity
npm install
npm run prepublish
起動
ここまでで、必要なものが入ったので今度はこれらを起動していく。
mkdir work
cd work
gethを起動
ここまでで、必要なものが入ったので今度はこれらを起動していく。 前提として、開発用のプライベートなネットワークを作り、そこに作った契約を配置する。 そのため、gethの初期化処理に渡す、genesisファイルを作る必要がある。
vi testGenesis.json
{
"config": {
"chainId": 1,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000042",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x8000000",
"difficulty": "0x01",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {}
}
次にこのファイルを指定して初期化する
geth --datadir=$(pwd)/data init $(pwd)/testGenesis.json
gethの起動
geth --networkid "1" --nodiscover --ipcpath ${HOME}/.ethereum/geth.ipc --rpc --rpcaddr 0.0.0.0 --rpcport 8545 -ws --wsaddr 0.0.0.0 --wsport 8546 --port 30303 --minerthreads 1 --datadir $(pwd)/data console 2>> $(pwd)/geth.log
mistを起動する
別のターミナルを開く
cd mist/interface
meteor --no-release-check
さらに別のターミナルを開く
cd mist
electron .
mistが起動する
remixを起動
さらに別のターミナルを開く
cd browser-solidity
npm start
firefoxでhttp://localhost:8080にアクセスする
結果
最終的にこんな画面が出来上がる
