博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[币严区块链]简单易懂的以太坊(ETH)智能合约开发入门教程
阅读量:5053 次
发布时间:2019-06-12

本文共 4703 字,大约阅读时间需要 15 分钟。

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

 

总结

本文介绍了一个简单的以太坊智能合约的开发过程,做完这十一步,相信你对以太坊智能合约的开发已经能够入门了!

 BIZZAN(币严) 数字货币交易所官方网址:

  www.bizzan.com

 

转载于:https://www.cnblogs.com/bizzan/p/10401588.html

你可能感兴趣的文章
mysql group by 报错 ,only_full_group_by 三种解决方案
查看>>
(转载)iOS UILabel自定义行间距时获取高度
查看>>
Linux里$等记得转义
查看>>
Powershell使用管道
查看>>
银行卡输入特效 4个加一空格
查看>>
zip unzip linux下使用
查看>>
每日英语:Instant Gratification: China Luxury Buyers Getting More Impulsive
查看>>
Java数据库操作学习
查看>>
使用更改跟踪(ChangeTracking)来实现数据类型变更
查看>>
c++访问mysql数据库
查看>>
JAVA代码查错试题集
查看>>
C#中小数点后保留两位小数,四舍五入的函数及使用方法
查看>>
你的JavaBean是否真的需要实现Serializable
查看>>
CSS3效果:立体字和镂空字
查看>>
规范 : angular 组合 jquery plugin
查看>>
文字无缝向上滚动
查看>>
IE6,谢谢你,goodbye?
查看>>
mongoDB 索引的用法
查看>>
Linux +apache+fastcgi运行c/c++
查看>>
atitit。 hb Hibernate sql 查询使用
查看>>