Photo by Patrick Hendry / Unsplash

Background

俗話說的好 畢圈一日 人間一年..... 經過前面一系列的摸索, 大致上對 Solidity & Smart Contract 的應用有了基本的認識. 那接下來就是偏系統面的部分. 正個應用還是需要經過測試的. 所以要打造一個環境讓公司的其他人可以一起測試. 那順便把整個過程記錄下來.

Install

整個環境是架在 AWS ubuntu 16.04 上面.

npm & nodejs

$ sudo apt-get update
$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
$ sudo apt-get install -y nodejs
$ node -v
v8.11.3
$ nodejs -v
v8.11.3
$ npm -v
5.6.0

Truffle

$ sudo npm install -g truffle

Ganache CLI

$ sudo npm install -g ganache-cli

nginx

$ sudo apt-get install nginx

Setting

$ sudo mkdir /var/www
$ sudo chown -R ubuntu:root /var/www
$ cd /var/www
$ mkdir petshop
$ cd petshop
$ truffle unbox pet-shop

接著根據支持pet-shop的範例. 把該填的填上去

Start

start ganache

$ ganache-cli -h 0.0.0.0
Ganache CLI v6.1.6 (ganache-core: 2.1.5)

Available Accounts
==================
(0) 0xdbaaee7a614fb5ae2338f3ad9b2140e0139a44ec (~100 ETH)
(1) 0xa04aeadebf423d6c6f0fc46d799ece76e5a912a2 (~100 ETH)
(2) 0x0aeb881f6413335f0d7a9aed556ea94cd0fefa9a (~100 ETH)
(3) 0x65d7b0461d98cea77e54ba2a5b512d9574ace376 (~100 ETH)
(4) 0x2d335b5bf5d2636096d57bde3f94ac0d84443929 (~100 ETH)
(5) 0xd0a507ef9c1253c58a399f909d27d6ffbc36f838 (~100 ETH)
(6) 0x570cf29e835c96fcc812dbc62f0aed3ba4361a7b (~100 ETH)
(7) 0xea53e55b93c7e8ddc1d5ea1c5d2da4757e0f397f (~100 ETH)
(8) 0xb9b7def59575090ab1c256b57dddb7dac8f95dd0 (~100 ETH)
(9) 0x8bce7be79dc82a517112b8e4b0ef8e0838545769 (~100 ETH)

Private Keys
==================
(0) 0xc1b05626ad9bd3fce73031b39ce51992b0522b4c32136279e8193cf69d2c4fe6
(1) 0x9f35d43b88a710cccd0d74244f54b383735f89a554909494be4d9e6a882c16d0
(2) 0x3c98c9fb53f3c8cf4ee320297476d7ddc2a83badd0a0c6c0af678358bc0d0153
(3) 0x29ed7e85c4bd7727b117c41c284d9e23f8fa39184c251e99eeea9ab28e9c0da6
(4) 0xabb9f8cc5d81d4f50c1a5284b33b25a5afc4b5f8ca2fce647a5d2c597c79ab7c
(5) 0x8f1ea4475a24a505ebff820f7f7b3420a5127c4bd7d7fcb028c8637890ca9709
(6) 0x7b3c46a05eac046a649f32931ed4cc35fbf1bac70b63fe1dc599aef79501d2a6
(7) 0xbb5a224c2c68e85698d5c39523af172119e262d81ea65f465c47fe31bef14478
(8) 0x814fc91de4feae1685bad4f968ea1742c1577a40cca7573788000cb7edbba937
(9) 0x491748b635e7e08bd1d773212db6cb3d2f6349d6ef3fbd021927a61dd43eda94

HD Wallet
==================
Mnemonic:      pluck deny credit good net erosion soldier slush egg first diagram labor
Base HD Path:  m/44'/60'/0'/0/{account_index}

Gas Price
==================
20000000000

Gas Limit
==================
6721975

Listening on 0.0.0.0:8545
eth_getBlockByNumber

接著再開一個新的 terminal

$ cd /var/www/petshop

更改連線設定.

truffle.js

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // for more about customizing your Truffle configuration!
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*" // Match any network id
    }
  }
};

Compile & Migrate

$ truffle compile
Compiling ./contracts/Adoption.sol...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts

$ truffle migration
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x6a715d0707ddcceaed041dbb007223d1b84166cfd4eda8ab59bd495a4fa036a5
  Migrations: 0x369489e8100f043cf49e2ba890db07043f35d878
Saving successful migration to network...
  ... 0x418fffbe1b533d44fb2c09055017ff9ff0bb8d6221c1a55966b019cc86df59fc
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying Adoption...
  ... 0x9924429d494cc722aa83786bd8fae28a1af8448cb8d53aef48ac093c873130d3
  Adoption: 0xb788883efd653111cbfd4b307e095d50cafe0255
Saving successful migration to network...
  ... 0xca559e559a807d5ad185f6e36e2ee076922b2d2f3424c44f95cef1c5798cb2f9
Saving artifacts...

Nginx

$ cd /etc/nginx/sites-available
$ sudo vim petshop

petshop

server {
    listen 80;
    server_name IP or DONAIM_NAME;

    root /var/www/petshop/src/;

    location / {
    }
}
sudo ln -s /etc/nginx/sites-available/petshop /etc/nginx/sites-enabled
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl restart nginx

接著輸入ip or 網址就可以看到網頁畫面如下:

Screen-Shot-2018-08-03-at-1.14.45-AM

Metamask

接著下來要設定 metamask. 這個需要連到 Node (Ganache). 所以第一步要先開啟AWS EC2 TCP 8545 的port. 接著在 Metamask的頁面設定 custom RPC. 沒問題之後我們要匯入錢包, 透過private key來匯入.

Screen-Shot-2018-08-03-at-1.31.46-AM

Screen-Shot-2018-08-03-at-1.31.56-AM

Screen-Shot-2018-08-03-at-1.32.01-AM

成功之後就會看到多了一個 account 有 100 eth. 對應到上面的錢包位址. 那接下來就可以正常的使用網站了. enjoy it.

Reference

* https://www.jianshu.com/p/9233c214e205