My First Token

Smart Contract 開發環境架設

 

 

COSCUP 2017

開源人年會 @台灣大學社會科學院 Room 305

Day 1 - 08/05/2017  11:50 am

王銘德 (Ming)

ming@log4analytics.com

http://slides.com/ming-derwang/solidity-tutorial-6-37-38-54

簡報 URL ->

Solidity

一種最常用的智能合約程式語言

  • 類似 Javascript, 但不是 Javascript
  • 可用 Visual Studio Code 或 Atom 當 SDK 編輯程式
  • 可用 online solidity compiler 編譯
  • 可以編譯成 bytecode 在 Ethereum VM (EVM) 上執行
  • 程式只能在 blockchain 測試和執行
  • 無需主機, 程式永遠存在 (理論上)

開發環境

  • 你至少要安裝一個 Ethereum Wallet

  • 程式編輯與編譯, 可以用網路版

  • 或用你最喜歡的文字編輯器 vim 也行

  • 最高竿的直接用 web3.js 在 Chrome debugger 裡用 CLI 指令集直接執行也行.

  • 也可用 devtool 下 web3.js 指令

Ethereum Wallet (client) -->

Solidity Realtime Compiler

(remix too)

Chrome devtool

Etherscan, explorer blocks

我們有哪些 Ethereum Client 可以用?

MetaMask (Chrome, Firefox ext.)

MyEtherWallet

Mist (官方)

Parity

Ubuntu, OSX, Docker, Windows 環境皆可安裝

web 瀏覽: http://127.0.01:8180

Parity 也有 Chrome ext.

testrpc

 用來模擬 ethereum 開發環境

建議安裝 git for windows, 在 Bash 下執行

$ testrpc

SDKs

Remix (online solidity)

devtool (for web3.js)

Etherscan.io

模擬 node

node + browser

node only

browser only

部署環境 + 電子錢包

TESTNETs

MainNet

  • 只有一個, 用真的 Ethers 執行程式
  • 跟其他人真的互動
  • 確定沒問題, 再部署到 Main net

Testnet (ROPSTEN (Revived) )

 

DevNet

parity ui --chain dev

or

parity ui --testnet

  • 有很多個 test net
  • 跟其他人在 test net 的互動, 測試智能合約
  • test net 不保證永遠存在

Solidity 語言特性

  • 它也是物件導向程式語言
  • 除了 float 以外, 有各種 types
  • 也有 structs 結構
  • 有 mappings (a kind of arrays)
  • 有 arrays
  • 有一個 address 特殊 object
  • 有 event 通知 wallet 更新
  • 有 functions and exceptions
  • 說明文件 (英文) 還算清楚詳盡

Solidity 文件

http://solidity.readthedocs.io/en/develop/

習題 greeter

https://www.ethereum.org/greeter

Mortal

pragma solidity ^0.4.1;

contract mortal {
    address owner;
    function mortal() {

          owner = msg.sender;     }

   
    function kill() {

          if (msg.sender == owner)

     selfdestruct(owner);    }
}

HelloWorld

contract helloworld is mortal {

    /* helloworld 就會繼承了 mortal 的 kill 功能 */ 

string storeData;


  function set(string x) {
    storeData = x;  }


  function get() constant returns (string data) {
    return storeData;  }

}

物件導向 - 繼承

如何讓別人執行你的 Contract

[{"constant": false, "inputs": [], "name": "kill", "outputs": [], "type": "function" }, { "constant": false, "inputs": [ { "name": "x", "type": "string" } ], "name": "set", "outputs": [], "type": "function" }, { "constant": true, "inputs": [], "name": "get", "outputs": [ { "name": "data", "type": "string", "value": "hello CCLiang" } ], "type": "function" } ]

跟你的 Contract Address

 0x141fd413A40a4163A8ecF1A02f902A1abE8ff7c5

 

寄給他們 Contract ABI

按 Show Interface

點擊

Contract Address

Contract JSON Interface

想執行別人的 Contract

點擊

填如地址跟 Contract JSON

Hello Contract

或用 MyEthereumWallet

或用 Parity Browser

可以執行該合約了

習題 My First Token

https://www.ethereum.org/token

live demo

MyFirstToken.sol

(click me)

Q & A

歡迎參加 EtherTW.slack.com 線上討論

https://goo.gl/S6nhKv   <---- 加入 slack

 

通關密語:decentralized

 

如果未來有任何問題, 都可以在 slack 裡詢問, 我們會在上面回答

 

My First Token - Smart Contract Programming

By Ming-der Wang

My First Token - Smart Contract Programming

  • 3,394