以太坊智能合约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()命令来运行该合约

总结

本文介绍了一个简单的以太坊智能合约的开发过程.

posted @ 2016-09-29 21:56  克兰布鲁克  阅读(15483)  评论(3编辑  收藏  举报