以太坊智能合约Hello World示例程序
簡介
以太坊(Ethereum)是一提供個智能合約(smart contract)功能的公共區塊鏈(BlockChain)平臺. 本文介紹了一個簡單的以太坊智能合約的開發過程.
開發環境
在以太坊上開發應用,首先需要安裝其客戶端,本文使用基于Go語言的Geth, 其官網為https://github.com/ethereum/go-ethereum.
第一步
在命令行環境中輸入以下命令, 連接到以太坊測試網絡
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])第六步
使用智能合約的在線編譯器https://ethereum.github.io/browser-solidity/編譯以下代碼
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()命令來運行該合約
總結
本文介紹了一個簡單的以太坊智能合約的開發過程.
轉載于:https://www.cnblogs.com/huyouhengbc/p/5922093.html
總結
以上是生活随笔為你收集整理的以太坊智能合约Hello World示例程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Opserver配置Redis、SqlS
- 下一篇: 【读书笔记】《深入浅出nodejs》第五