http://www.tryblockchain.org/

教你如何舒服的看solidity代码

最近智能合约随着区块链技术的发展越发收到广大技术人员的重视!

其中最被看好的以太坊就是一个提供智能合约开发的平台,它拥有比其他区块链更图灵完备的技术 -- 以太坊虚拟机(EVM)。。您无需去了解区块链底层技术(点对点网络、数字加密技术等)只要拥有基本的编程知识和javascript基础,就可以在区块链上开发一款自己的去中心化应用。

以太坊开发了一套近似于javascript语言--solidity。

欢迎加入QQ群:325411816 和有眼光的人共同进步!

虽然它和javascript很相近,但是当我们下载了一些Demo来学习它时,却发现很多编译器对他的支持不是很友好,导致我们很难将注释和代码区分开来,如下图:

虽然近期传出微软Visual Studio开始支持solidity,但是笔者还未尝试。(好像是今天的消息。)

值得一提的是以太坊官方开发了自己的编译器--Mix,但是由于是测试版本经常会崩溃。


今天我给大家推荐一个编译器能让大家舒服的看代码--Atom!

相信大家很多人都听说过这个,并且使用过它。因为它是大名鼎鼎的Github开发的。

大家只用下载一个叫做 language-ethereum 的插件就可以舒舒服服的看代码了。如下图:

是不是很赞?

如果您正在学习Solidity不妨一试。

加入QQ群:325411816 我们共同进步!

2016-11-18 11:13 793人阅读 评论(0) 收藏 举报
 分类:
Solidity(4) 

版权声明:本文为博主原创文章,转载请写明出处。

 

目录(?)[+]

 

在学习以太坊合约的过程中会需要自己编写智能合约,官方提供了几种方式供大家使用。下面分别简单介绍一下,如果有错误的地方,还留言指正补充。

DAPP IDE

说实话,这个版本IDE我还没有使用过,只提供一个连接地址供大家参考。 
The-DApp-IDE 
此连接中有具体的使用描述。

在线编程

Solidity支持在线编程,在线编程网址为:

  1. https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.4+commit.4633f3de.js
  • 1
  • 1

直接在浏览器中输入此连接即可进入。

浏览器插件

进入chrome扩展程序,找到chrome网上应用,搜索“Solidity”,选择Sol那个插件安装。安装之后在浏览器右上角会出现对应插件的图标,点击进入与在线编程操作界面基本相同。 

使用简介

在线编程和浏览器插件页面初始化时已经提供了一个投票的合约,可以具体阅读以下代码。 
也可以通过左上角的按钮新建自己的合约。在编写代码的过程中两个插件都会进行自动的错误提示。不同的是,在线版本的需要制定solidity的版本。

页面的其他功能大家就慢慢摸索吧。 

 
 

安装Solidity

基于浏览器的Solidity

如果你只是想尝试一个使用Solidity的小合约,你不需要安装任何东西,只要访问基于浏览器的Solidity

如果你想离线使用,你可以保存页面到本地,或者从 http://github.com/chriseth/browser-solidity 克隆一个。

NPM / node.js

这可能安装Solidity到本地最轻便最省事的方法。

在基于浏览器的Solidity上,Emscripten提供了一个跨平台JavaScript库,把C++源码编译为javascript,同时也提供NPM安装包。

去安装它就可以简单使用。

  1. npm install solc
  • 1
  • 1

如何使用nodejs包的详细信息可以在代码库中找到。

二进制安装包

包括Mix IDE的二进制Solidity安装包在Ethereum网站C++ bundle中下载。

从源码构建

在MacOS X、Ubuntu和其它类Unix系统中编译安装Solidity非常相似。这个指南开始讲解如何在每个平台下安装相关的依赖软件,然后构建Solidity。 
MacOS X

MACOS X系统 
系统需求: 
- OS X Yosemite (10.10.5) 
- Homebrew 
- Xcode 
安装Homebrew:

  1. brew update
  2. brew install boost --c++11 # 这需要等待一段时间
  3. brew install cmake cryptopp miniupnpc leveldb gmp libmicrohttpd libjson-rpc-cpp
  4. # 仅仅安装Mix IDE和Alethzero
  5. brew install xz d-bus
  6. brew install llvm --HEAD --with-clang
  7. brew install qt5 --with-d-bus # 如果长时间的等待让你发疯,那么添加--verbose输出信息会让你感觉更好。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Ubuntu系统

下面是在最新版Ubuntu系统上编译安装Solidity的指南。最佳的支持平台是2014年11月发布的64位Ubuntu 14.04,至少需要2GB内存。我们所有的测试都是基于此版本,当然我们也欢迎其它版本的测试贡献者。

安装依赖软件:

在你从源码编译之前,你需要准备一些工具和依赖软件。

首先,升级你的代码库。Ubuntu主代码库不提供所有的包,你需要从Ethereum PPA和LLVM获取。

注意Ubuntu 14.04的用户需要使用:sudo apt-add-repository ppa:george-edison55/cmake-3.x获取最新版本的cmake。

现在加入其它的包:

  1. sudo apt-get -y update
  2. sudo apt-get -y install language-pack-en-base
  3. sudo dpkg-reconfigure locales
  4. sudo apt-get -y install software-properties-common
  5. sudo add-apt-repository -y ppa:ethereum/ethereum
  6. sudo add-apt-repository -y ppa:ethereum/ethereum-dev
  7. sudo apt-get -y update
  8. sudo apt-get -y upgrade
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

对于Ubbuntu 15.04(Vivid Vervet)或者更老版本,使用下面的命令获取开发相关的包:

  1. sudo apt-get -y install build-essential git cmake libboost-all-dev libgmp-dev libleveldb-dev libminiupnpc-dev libreadline-dev libncurses5-dev libcurl4-openssl-dev libcryptopp-dev libjson-rpc-cpp-dev libmicrohttpd-dev libjsoncpp-dev libedit-dev libz-dev
  • 1
  • 1

对于Ubbuntu 15.10(Wily Werewolf)或者更新版本,使用下面的命令获取开发相关的包:

  1. sudo apt-get -y install build-essential git cmake libboost-all-dev libgmp-dev libleveldb-dev libminiupnpc-dev libreadline-dev libncurses5-dev libcurl4-openssl-dev libcryptopp-dev libjsonrpccpp-dev libmicrohttpd-dev libjsoncpp-dev libedit-dev libz-dev
  • 1
  • 1

不同版本使用不同获取命令的原因是,libjsonrpccpp-dev已经在最新版的Ubuntu的通用代码仓库中。

编译

如果你只准备安装solidity,忽略末尾Alethzero和Mix的错误。

  1. git clone --recursive https://github.com/ethereum/webthree-umbrella.git
  2. cd webthree-umbrella
  3. ./webthree-helpers/scripts/ethupdate.sh --no-push --simple-pull --project solidity
  4. # 更新Solidity库
  5. ./webthree-helpers/scripts/ethbuild.sh --no-git --project solidity --all --cores 4 -DEVMJIT=0
  6. # 编译Solidity及其它
  7. # 在OS X系统加上DEVMJIT将不能编译,在Linux系统上则没问题
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

如果你选择安装Alethzero和Mix:

  1. git clone --recursive https://github.com/ethereum/webthree-umbrella.git
  2. cd webthree-umbrella && mkdir -p build && cd build
  3. cmake ..
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

如果你想帮助Solidity的开发,你需要分支(fork)Solidity并添加到你的私人远端分支: 
注意webthree-umbrella使用子模块,所以solidity是其自己的Git代码库,但是他的设置不是保存在 .git/config, 而是在webthree-umbrella/.git/modules/solidity/config


原文:https://solidity.readthedocs.org/en/latest/installing-solidity.html

译者: @abao

校对: @shaoping

Solidity 文档--第三章:Solidity 编程实例

2016-11-21 09:57 1068人阅读 评论(0) 收藏 举报
 分类:
Solidity(4) 
 

目录(?)[+]

 

Solidity 编程实例

Voting 投票

接下来的合约非常复杂,但展示了很多Solidity的特性。它实现了一个投票合约。当然,电子选举的主要问题是如何赋予投票权给准确的人,并防止操纵。我们不能解决所有的问题,但至少我们会展示如何委托投票可以同时做到投票统计是自动和完全透明。

思路是为每张选票创建一个合约,每个投票选项提供一个短名称。合约创建者作为会长将会给每个投票参与人各自的地址投票权。

地址后面的人们可以选择自己投票或者委托信任的代表人替他们投票。在投票结束后,winningProposal()将会返回获得票数最多的提案。

  1. /// @title Voting with delegation.
  2. /// @title 授权投票
  3. contract Ballot
  4. {
  5. // 这里声明了复杂类型
  6. // 将会在被后面的参数使用
  7. // 代表一个独立的投票人。
  8. struct Voter
  9. {
  10. uint weight; // 累积的权重。
  11. bool voted; // 如果为真,则表示该投票人已经投票。
  12. address delegate; // 委托的投票代表
  13. uint vote; // 投票选择的提案索引号
  14. }
  15. // 这是一个独立提案的类型
  16. struct Proposal
  17. {
  18. bytes32 name; // 短名称(32字节)
  19. uint voteCount; // 累计获得的票数
  20. }
  21. address public chairperson;
  22. //这里声明一个状态变量,保存每个独立地址的`Voter` 结构
  23. mapping(address => Voter) public voters;
  24. //一个存储`Proposal`结构的动态数组
  25. Proposal[] public proposals;
  26. // 创建一个新的投票用于选出一个提案名`proposalNames`.
  27. function Ballot(bytes32[] proposalNames)
  28. {
  29. chairperson = msg.sender;
  30. voters[chairperson].weight = 1;
  31. //对提供的每一个提案名称,创建一个新的提案
  32. //对象添加到数组末尾
  33. for (uint i = 0; i < proposalNames.length; i++)
  34. //`Proposal({...})` 创建了一个临时的提案对象,
  35. //`proposal.push(...)`添加到了提案数组`proposals`末尾。
  36. proposals.push(Proposal({
  37. name: proposalNames[i],
  38. voteCount: 0
  39. }));
  40. }
  41. //给投票人`voter`参加投票的投票权,
  42. //只能由投票主持人`chairperson`调用。
  43. function giveRightToVote(address voter)
  44. {
  45. if (msg.sender != chairperson || voters[voter].voted)
  46. //`throw`会终止和撤销所有的状态和以太改变。
  47. //如果函数调用无效,这通常是一个好的选择。
  48. //但是需要注意,这会消耗提供的所有gas。
  49. throw;
  50. voters[voter].weight = 1;
  51. }
  52. // 委托你的投票权到一个投票代表 `to`。
  53. function delegate(address to)
  54. {
  55. // 指定引用
  56. Voter sender = voters[msg.sender];
  57. if (sender.voted)
  58. throw;
  59. //当投票代表`to`也委托给别人时,寻找到最终的投票代表
  60. while (voters[to].delegate != address(0) &&
  61. voters[to].delegate != msg.sender)
  62. to = voters[to].delegate;
  63. // 当最终投票代表等于调用者,是不被允许的。
  64. if (to == msg.sender)
  65. throw;
  66. //因为`sender`是一个引用,
  67. //这里实际修改了`voters[msg.sender].voted`
  68. sender.voted = true;
  69. sender.delegate = to;
  70. Voter delegate = voters[to];
  71. if (delegate.voted)
  72. //如果委托的投票代表已经投票了,直接修改票数
  73. proposals[delegate.vote].voteCount += sender.weight;
  74. else
  75. //如果投票代表还没有投票,则修改其投票权重。
  76. delegate.weight += sender.weight;
  77. }
  78. ///投出你的选票(包括委托给你的选票)
  79. ///给 `proposals[proposal].name`。
  80. function vote(uint proposal)
  81. {
  82. Voter sender = voters[msg.sender];
  83. if (sender.voted) throw;
  84. sender.voted = true;
  85. sender.vote = proposal;
  86. //如果`proposal`索引超出了给定的提案数组范围
  87. //将会自动抛出异常,并撤销所有的改变。
  88. proposals[proposal].voteCount += sender.weight;
  89. }
  90. ///@dev 根据当前所有的投票计算出当前的胜出提案
  91. function winningProposal() constant
  92. returns (uint winningProposal)
  93. {
  94. uint winningVoteCount = 0;
  95. for (uint p = 0; p < proposals.length; p++)
  96. {
  97. if (proposals[p].voteCount > winningVoteCount)
  98. {
  99. winningVoteCount = proposals[p].voteCount;
  100. winningProposal = p;
  101. }
  102. }
  103. }
  104. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112

可能的改进

现在,指派投票权到所有的投票参加者需要许多的交易。你能想出更好的方法么?

盲拍

这一节,我们将展示在以太上创建一个完整的盲拍合约是多么简单。我们从一个所有人都能看到出价的公开拍卖开始,接着扩展合约成为一个在拍卖结束以前不能看到实际出价的盲拍。

简单的公开拍卖

通常简单的公开拍卖合约,是每个人可以在拍卖期间发送他们的竞拍出价。为了实现绑定竞拍人的到他们的拍卖,竞拍包括发送金额/ether。如果产生了新的最高竞拍价,前一个最高价竞拍人将会拿回他的钱。在竞拍阶段结束后,受益人人需要手动调用合约收取他的钱 — — 合约不会激活自己。

  1. contract SimpleAuction {
  2. // 拍卖的参数。
  3. // 时间要么为unix绝对时间戳(自1970-01-01以来的秒数),
  4. // 或者是以秒为单位的出块时间
  5. address public beneficiary;
  6. uint public auctionStart;
  7. uint public biddingTime;
  8. //当前的拍卖状态
  9. address public highestBidder;
  10. uint public highestBid;
  11. //在结束时设置为true来拒绝任何改变
  12. bool ended;
  13. //当改变时将会触发的Event
  14. event HighestBidIncreased(address bidder, uint amount);
  15. event AuctionEnded(address winner, uint amount);
  16. //下面是一个叫做natspec的特殊注释,
  17. //由3个连续的斜杠标记,当询问用户确认交易事务时将显示。
  18. ///创建一个简单的合约使用`_biddingTime`表示的竞拍时间,
  19. /// 地址`_beneficiary`.代表实际的拍卖者
  20. function SimpleAuction(uint _biddingTime,
  21. address _beneficiary) {
  22. beneficiary = _beneficiary;
  23. auctionStart = now;
  24. biddingTime = _biddingTime;
  25. }
  26. ///对拍卖的竞拍保证金会随着交易事务一起发送,
  27. ///只有在竞拍失败的时候才会退回
  28. function bid() {
  29. //不需要任何参数,所有的信息已经是交易事务的一部分
  30. if (now > auctionStart + biddingTime)
  31. //当竞拍结束时撤销此调用
  32. throw;
  33. if (msg.value <= highestBid)
  34. //如果出价不是最高的,发回竞拍保证金。
  35. throw;
  36. if (highestBidder != 0)
  37. highestBidder.send(highestBid);
  38. highestBidder = msg.sender;
  39. highestBid = msg.value;
  40. HighestBidIncreased(msg.sender, msg.value);
  41. }
  42. ///拍卖结束后发送最高的竞价到拍卖人
  43. function auctionEnd() {
  44. if (now <= auctionStart + biddingTime)
  45. throw;
  46. //拍卖还没有结束
  47. if (ended)
  48. throw;
  49. //这个收款函数已经被调用了
  50. AuctionEnded(highestBidder, highestBid);
  51. //发送合约拥有所有的钱,因为有一些保证金可能退回失败了。
  52. beneficiary.send(this.balance);
  53. ended = true;
  54. }
  55. function () {
  56. //这个函数将会在发送到合约的交易事务包含无效数据
  57. //或无数据的时执行,这里撤销所有的发送,
  58. //所以没有人会在使用合约时因为意外而丢钱。
  59. throw;
  60. }
  61. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

Blind Auction 盲拍

接下来扩展前面的公开拍卖成为一个盲拍。盲拍的特点是拍卖结束以前没有时间压力。在一个透明的计算平台上创建盲拍系统听起来可能有些矛盾,但是加密算法能让你脱离困境。

在拍卖阶段, 竞拍人不需要发送实际的出价,仅仅只需要发送一个它的散列值。因为目前几乎不可能找到两个值(足够长)的散列值相等,竞拍者提交他们的出价散列值。在拍卖结束后,竞拍人重新发送未加密的竞拍出价,合约将检查其散列值是否和拍卖阶段发送的一样。 另一个挑战是如何让拍卖同时实现绑定和致盲 :防止竞拍人竞拍成功后不付钱的唯一的办法是,在竞拍出价的同时发送保证金。但是在Ethereum上发送保证金是无法致盲,所有人都能看到保证金。下面的合约通过接受任何尽量大的出价来解决这个问题。当然这可以在最后的揭拍阶段进行复核,一些竞拍出价可能是无效的,这样做的目的是(它提供一个显式的标志指出是无效的竞拍,同时包含高额保证金):竞拍人可以通过放置几个无效的高价和低价竞拍来混淆竞争对手。

  1. contract BlindAuction
  2. {
  3. struct Bid
  4. {
  5. bytes32 blindedBid;
  6. uint deposit;
  7. }
  8. address public beneficiary;
  9. uint public auctionStart;
  10. uint public biddingEnd;
  11. uint public revealEnd;
  12. bool public ended;
  13. mapping(address => Bid[]) public bids;
  14. address public highestBidder;
  15. uint public highestBid;
  16. event AuctionEnded(address winner, uint highestBid);
  17. ///修饰器(Modifier)是一个简便的途径用来验证函数输入的有效性。
  18. ///`onlyBefore` 应用于下面的 `bid`函数,其旧的函数体替换修饰器主体中 `_`后就是其新的函数体
  19. modifier onlyBefore(uint _time) { if (now >= _time) throw; _ }
  20. modifier onlyAfter(uint _time) { if (now <= _time) throw; _ }
  21. function BlindAuction(uint _biddingTime,
  22. uint _revealTime,
  23. address _beneficiary)
  24. {
  25. beneficiary = _beneficiary;
  26. auctionStart = now;
  27. biddingEnd = now + _biddingTime;
  28. revealEnd = biddingEnd + _revealTime;
  29. }
  30. ///放置一个盲拍出价使用`_blindedBid`=sha3(value,fake,secret).
  31. ///仅仅在竞拍结束正常揭拍后退还发送的以太。当随同发送的以太至少
  32. ///等于 "value"指定的保证金并且 "fake"不为true的时候才是有效的竞拍
  33. ///出价。设置 "fake"为true或发送不合适的金额将会掩没真正的竞拍出
  34. ///价,但是仍然需要抵押保证金。同一个地址可以放置多个竞拍。
  35. function bid(bytes32 _blindedBid)
  36. onlyBefore(biddingEnd)
  37. {
  38. bids[msg.sender].push(Bid({
  39. blindedBid: _blindedBid,
  40. deposit: msg.value
  41. }));
  42. }
  43. ///揭开你的盲拍竞价。你将会拿回除了最高出价外的所有竞拍保证金
  44. ///以及正常的无效盲拍保证金。
  45. function reveal(uint[] _values, bool[] _fake,
  46. bytes32[] _secret)
  47. onlyAfter(biddingEnd)
  48. onlyBefore(revealEnd)
  49. {
  50. uint length = bids[msg.sender].length;
  51. if (_values.length != length || _fake.length != length ||
  52. _secret.length != length)
  53. throw;
  54. uint refund;
  55. for (uint i = 0; i < length; i++)
  56. {
  57. var bid = bids[msg.sender][i];
  58. var (value, fake, secret) =
  59. (_values[i], _fake[i], _secret[i]);
  60. if (bid.blindedBid != sha3(value, fake, secret))
  61. //出价未被正常揭拍,不能取回保证金。
  62. continue;
  63. refund += bid.deposit;
  64. if (!fake && bid.deposit >= value)
  65. if (placeBid(msg.sender, value))
  66. refund -= value;
  67. //保证发送者绝不可能重复取回保证金
  68. bid.blindedBid = 0;
  69. }
  70. msg.sender.send(refund);
  71. }
  72. //这是一个内部 (internal)函数,
  73. //意味着仅仅只有合约(或者从其继承的合约)可以调用
  74. function placeBid(address bidder, uint value) internal
  75. returns (bool success)
  76. {
  77. if (value <= highestBid)
  78. return false;
  79. if (highestBidder != 0)
  80. //退还前一个最高竞拍出价
  81. highestBidder.send(highestBid);
  82. highestBid = value;
  83. highestBidder = bidder;
  84. return true;
  85. }
  86. ///竞拍结束后发送最高出价到竞拍人
  87. function auctionEnd()
  88. onlyAfter(revealEnd)
  89. {
  90. if (ended) throw;
  91. AuctionEnded(highestBidder, highestBid);
  92. //发送合约拥有所有的钱,因为有一些保证金退回可能失败了。
  93. beneficiary.send(this.balance);
  94. ended = true;
  95. }
  96. function () { throw; }
  97. }
  98. Safe Remote Purchase 安全的远程购物
  99. contract Purchase
  100. {
  101. uint public value;
  102. address public seller;
  103. address public buyer;
  104. enum State { Created, Locked, Inactive }
  105. State public state;
  106. function Purchase()
  107. {
  108. seller = msg.sender;
  109. value = msg.value / 2;
  110. if (2 * value != msg.value) throw;
  111. }
  112. modifier require(bool _condition)
  113. {
  114. if (!_condition) throw;
  115. _
  116. }
  117. modifier onlyBuyer()
  118. {
  119. if (msg.sender != buyer) throw;
  120. _
  121. }
  122. modifier onlySeller()
  123. {
  124. if (msg.sender != seller) throw;
  125. _
  126. }
  127. modifier inState(State _state)
  128. {
  129. if (state != _state) throw;
  130. _
  131. }
  132. event aborted();
  133. event purchaseConfirmed();
  134. event itemReceived();
  135. ///终止购物并收回以太。仅仅可以在合约未锁定时被卖家调用。
  136. function abort()
  137. onlySeller
  138. inState(State.Created)
  139. {
  140. aborted();
  141. seller.send(this.balance);
  142. state = State.Inactive;
  143. }
  144. ///买家确认购买。交易包含两倍价值的(`2 * value`)以太。
  145. ///这些以太会一直锁定到收货确认(confirmReceived)被调用。
  146. function confirmPurchase()
  147. inState(State.Created)
  148. require(msg.value == 2 * value)
  149. {
  150. purchaseConfirmed();
  151. buyer = msg.sender;
  152. state = State.Locked;
  153. }
  154. ///确认你(买家)收到了货物,这将释放锁定的以太。
  155. function confirmReceived()
  156. onlyBuyer
  157. inState(State.Locked)
  158. {
  159. itemReceived();
  160. buyer.send(value);//我们有意忽略了返回值。
  161. seller.send(this.balance);
  162. state = State.Inactive;
  163. }
  164. function() { throw; }
  165. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179

小额支付通道

solidity代码的更多相关文章

  1. 区块链入门到实战(32)之Solidity – 代码注释

    Solidity 支持c风格和c++风格的注释. //之后到行尾的文本,都被看作注释,编译器忽略此内容 /* 与 */ 之间的文本被看作注释, 编译器忽略此内容 示例 注释示例. function g ...

  2. 详解 Solidity 事件Event - 完全搞懂事件的使用

    很多同学对Solidity 中的Event有疑问,这篇文章就来详细的看看Solidity 中Event到底有什么用? 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊. ...

  3. solidity事件详解

    很多同学对Solidity 中的Event有疑问,这篇文章就来详细的看看Solidity 中Event到底有什么用? 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊. ...

  4. Solidity oraclize query apikey加密

    solidity 程序中如果用到oraclize query,api调用需要apikey,则最好加密apikey,否则公开solidity代码时会连同apikey一起公开. 加密方法: https:/ ...

  5. Solidity notes

    1. 查询transaction历史记录 https://forum.ethereum.org/discussion/2116/in-what-ways-can-storage-history-be- ...

  6. solidity语言

    IDE:Atom 插件:autocomplete-solidity 代码自动补齐   linter-solium,linter-solidity代码检查错误   language-ethereum支持 ...

  7. Solidity陷阱:以太坊的随机数生成

    title: Solidity陷阱:以太坊的随机数生成 Solidity是一种相当新的语言,因为没有代码是完美的,它包含与代码相关的问题以及你希望用它完成的任务.本文将指导你使用随机数作为以太坊智能合 ...

  8. 使用remix的solidity在线编译环境部署一个faucet合约

    一.浏览器打开https://remix.ethereum.org 地址 点击"+"新建一个sol文件,命名为faucet.sol 然后在代码区输入solidity代码 选择相关插 ...

  9. Solidity的Bytecode和Opcode简介

    Solidity的Bytecode和Opcode简介 随着我们更深入地编写智能合约,我们将遇到诸如" PUSH1"," SSTORE"," CALLV ...

随机推荐

  1. Sqli labs系列-less-5&6 报错注入法(下)

    我先输入 ' 让其出错. 然后知道语句是单引号闭合. 然后直接 and 1=1 测试. 返回正常,再 and 1=2 . 返回错误,开始猜表段数. 恩,3位.让其报错,然后注入... 擦,不错出,再加 ...

  2. java线程池监控

    原因 最近在完善公司的基础发布平台的时候,使用到了一线程去做一些异步的事情,在开发环境和测试环境验证没有任何问题,但是在程序在生产运行一段时间后,发现没有得到自己想要的结果,为此开始了漫长的排查bug ...

  3. jmeter压测、操作数据库、分布式、 linux下运行的简单介绍

    一.jmeter压测 1.如何压测 常规性能压测:10-15分钟 稳定性测试:一周.2天等 如果想要压测10分钟,勾选永远,勾选调度器,填写600秒.也可以使用固定启动时间. 2.tps.响应时间 ( ...

  4. 使用正则限制input框只能输入数字/英文/中文等等

    常用HTML正则表达式 1.只能输入数字和英文的: 复制代码代码如下: <input onkeyup="value=value.replace(/[/W]/g,'') " o ...

  5. sql合并字段

    <!-- 对发送方式合并查询 --> <!--查询所有满足条件的营销活动 --> <select id="CRM-MK-ACT-DEFINE-SELECT&qu ...

  6. RedisTemplate序列号自增id(当前日期+序列号)

    话不多上,直接上码 public class TestService { @Resource RedisTemplate<String, Object> redisTemplate; pu ...

  7. docker--环境搭建

    我的电脑是win10,虽然现在win10开始也支持docker,但在linux机器会合适些,所以我先用VMware创建一个linux虚拟机--Centos7 安装虚拟机不多说,现在开始安装docker ...

  8. Lock中使用Condition实现等待通知

    Condition类有很好的灵活性,可以实现多路通知功能,一个Lock对象中可以创建多个Condition对象实例,线程对象可以注册在指定的Condition中,进而有选择的进行线程通知,在调度线程上 ...

  9. 如何在mysql数据库中开启使用tab键补全功能

    声明:数据库tab键补全的是表名,列名,数据库名,不是数据库中的命令,变量等 永久支持tab键补全[root@localhost ~]# vim /etc/my.cnf添加以下两行:[mysql]au ...

  10. webpack output.publicPath

    output.publicPath string function 对于按需加载(on-demand-load)或加载外部资源(external resources)(如图片.文件等)来说,outpu ...