智能合約 - 開發環境

Solidity 程式語言 - 基礎原理

 

04/28/2017

 

政治大學

王銘德

@mingderwang

goo.gl/MBkNr7

(安裝軟體)

 

http://bit.ly/2pF2bye (共同編輯)

https://slides.com/ming-derwang

Solidity

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

  • 類似 Javascript, 但不是 Javascript
  • 可以編譯成 bytecode 在 Ethereum VM (EVM) 上執行
  • 程式只能在 blockchain 測試和執行
  • 無需主機, 程式永遠存在 (理論上)
  • 須透過 Ethereum-Wallet (Mist) 或 Parity browser 來部署
  • 試試 MyEthereumWallet 也行

開發環境

  • 你至少要安裝一個 Ethereum Wallet

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

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

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

  • 也可用 devtool 下 web3.js 指令

Ethereum Wallet (client)

Solidity Realtime Compiler

(remix too)

Chrome devtool

geth -> https://github.com/ethereum/go-ethereum/releases

我們有哪些 Ethereum Client 可以用?

MetaMask (Chrome ext.)

MyEtherWallet

Mist (官方)

Windows 環境

提供 Windows 下指令的環境, 

用來下 testrpc 指令

$ testrpc

live demo

SDKs

Remix (online solidity)

devtool (for web3.js)

Etherscan.io

live demo

Docs

簡易 node

node + browser

node only

browser only

部署環境 + 電子錢包

TESTNETs

MainNet

  • 只有一個, 用真的 Ethers

DevNet

parity ui --chain dev

or

parity ui --testnet

take a break

Solidity 語言特性

  • 它也是物件導向程式語言
  • 除了 float 以外, 有各種 types
  • 也有 structs 結構
  • 有 mappings (a kind of arrays)
  • 有 arrays
  • 有一個 address 特殊 object
  • data location 資料放哪?
  • 有 functions and exceptions
  • 說明文件 (英文) 還算清楚詳盡

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;  }

}

物件導向 - 繼承

live demo

格式 (type)

bool

int8, int16, int24 ... int256

uint8, uint16,  ... uint256

uint and int are aliases for uint256 and int256,

address

string

bytes1, bytes2, bytes3, ..., bytes32.

byte is an alias for bytes1

結構 (struct)

 struct Funder {
    address addr;
    uint amount;
  }
  • 目前不支援浮點運算 float
enum State { Created, Locked, Inactive }

enum

Explicit Conv. 

int8 y = -3;
uint x = uint(y);

Type Deduction

int256 x = 0x123;
var y = x;

// y will be int256 too

uint32 a = 0x12345678;
uint16 b = uint16(a); 
// b will be 0x5678 now
  • The type is only deduced from the first assignment, so the loop in the following snippet is infinite, as i will have the type uint8 and any value of this type is smaller than 2000. for (var i = 0; i < 2000; i++) { ... }

mappings

  • Mapping types are declared as mapping (_KeyType => _ValueType), where _KeyType can be almost any type except for a mapping and _ValueType can actually be any type, including mappings.
  • Mappings are only allowed for state variables (or as storage reference types in internal functions).
  • no length

contract MyToken { /* This creates an array with all balances */

mapping (address => uint256) public balanceOf;

function MyToken() {
     balanceOf[msg.sender] =
21000000; }

}

Arrays

contract C {
  function f(uint len) {
    uint[] memory a = new uint[](7);
    bytes memory b = new bytes(len);// memory
    // Here we have a.length == 7 and b.length == len
    a[6] = 8;
  }
}
  • An array of fixed size k and element type T is written as T[k], an array of dynamic size as T[].

take a break

Functions and Exceptions

contract Sharer {
    function sendHalf(address addr) returns (uint balance) {
        if (!addr.send(msg.value/2))
            throw; // also reverts the transfer to Sharer
        return this.balance;
    }
}

address' Methods

address holds a 20 byte value (size of an Ethereum address). Address types also have members, like send()

 

address x = 0x123;
address myAddress = this;
if (x.balance < 10 && myAddress.balance >= 10) x.send(10);

All contracts inherit the members of address, so it is possible to query the balance of the current contract using this.balance.

event 是什麼?

contract CryptoExchange {
  event Deposit(uint256 indexed _market, address indexed _sender, uint256 _amount, uint256 _time);

function deposit(uint256 _amount, uint256 _market) returns (int256) {
    // perform deposit, update user’s balance, etc
    Deposit(_market, msg.sender, _amount, now);
}

如何讓別人執行你的 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

可以執行該合約了

live demo

我還可以用其他語言寫 S.C. 嗎?

  • Solidity - Javascript/C++ like
  • Serpent - Python-like
  • LLL - Lisp-like
  • EtherScript - Scratch-like
    • ​類似早期的 Lego MindStroms
  • ​eris              -> 改名為 monax 了

 

 

Ethereum-based

還有哪些 frameworks 可以用嗎?

 

習題 greeter

https://www.ethereum.org/greeter

Q & A

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

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

 

通關密語:decentralized

 

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

 

智能合約 - 開發環境

By Ming-der Wang

智能合約 - 開發環境

  • 1,540