1. 程式人生 > >以太坊智慧合約Hello World示例程式

以太坊智慧合約Hello World示例程式

簡介

以太坊(Ethereum)是一提供個智慧合約(smart contract)功能的公共區塊鏈(BlockChain)平臺. 本文介紹了一個簡單的以太坊智慧合約的開發過程.

開發環境

第一步

在命令列環境中輸入以下命令, 連線到以太坊測試網路

geth --testnet --fast --cache=512 console

這裡寫圖片描述

第二步

在Geth提示符下輸入以下程式碼, 建立一個使用者並設定密碼

personal.newAccount()

這裡寫圖片描述

第三步

在Geth提示符下輸入以下程式碼, 確認新使用者的賬戶餘額為0, 並開始”挖礦”(mine)

eth.getBalance
(eth.accounts[0]) miner.start()

這裡寫圖片描述

第四步

新開一個命令列視窗並輸入以下命令, 將這個視窗連線到正在挖礦的視窗

geth attach

這裡寫圖片描述

第五步

在Geth提示符下輸入以下程式碼, 確認新使用者的賬戶餘額有所增長

eth.getBalance(eth.accounts[0])

這裡寫圖片描述

第六步

contract HelloWorld
{
    address creator;
    string greeting;

    function HelloWorld(string _greeting) public
    {
        creator = msg.sender;
        greeting = _greeting;
    }

    function
greet() constant returns (string) {
return greeting; } function setGreeting(string _newgreeting) { greeting = _newgreeting; } /********** Standard kill() function to recover funds **********/ function kill() { if (msg.sender == creator) suicide(creator); // kills this contract and sends remaining funds back to creator
} }

這裡寫圖片描述

第七步

編譯器生成的程式碼如下

var _greeting = /* var of type string here */ ;
var helloworldContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newgreeting","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"type":"constructor"}]);
var helloworld = helloworldContract.new(
   _greeting,
   {
     from: web3.eth.accounts[0],
     data: '606060405260405161044e38038061044e833981016040528080518201919060200150505b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055508060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009e57805160ff19168380011785556100cf565b828001600101855582156100cf579182015b828111156100ce5782518260005055916020019190600101906100b0565b5b5090506100fa91906100dc565b808211156100f657600081815060009055506001016100dc565b5090565b50505b506103428061010c6000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b514610052578063a413686214610066578063cfae3217146100c15761004d565b610002565b34610002576100646004805050610141565b005b34610002576100bf6004808035906020019082018035906020019191908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509090919050506101d5565b005b34610002576100d36004805050610286565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156101335780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156101d257600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b8060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061022457805160ff1916838001178555610255565b82800160010185558215610255579182015b82811115610254578251826000505591602001919060010190610236565b5b5090506102809190610262565b8082111561027c5760008181506000905550600101610262565b5090565b50505b50565b602060405190810160405280600081526020015060016000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103335780601f1061030857610100808354040283529160200191610333565b820191906000526020600020905b81548152906001019060200180831161031657829003601f168201915b5050505050905061033f565b9056',
     gas: 4700000
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

設定顯示的字串以及減少費用(gas)

var _greeting = "Hello World" ;
gas: 300000

第九步

將修改完的程式碼複製到第四步的視窗中, 如果出現
Error: account is locked undefined
錯誤的話, 則使用
personal.unlockAccount(eth.accounts[0], 'password')
命令將使用者解鎖.

這裡寫圖片描述

第十步

等候一段時間之後, geth視窗就會出現Contract mined! address..., 表明合約程式碼釋出成功
這裡寫圖片描述

第十一步

使用helloworld.greet()命令來執行該合約
這裡寫圖片描述

總結

本文介紹了一個簡單的以太坊智慧合約的開發過程.