了解以太坊的基本逻辑,理解以太坊的运行机理。能正确进行智能合约设计开发和部署。
了解智能合约设计开发和部署。熟悉工具。
Firefox & Metamask插件扩展
Remix网页:remix.ethereum.org
在remix中编写智能合约,并进行合约的测试。
学习地址 点击直达
代码:
pragma solidity ^0.4.0;
contract Ballot{
struct Voter{
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal{
uint voteCount;
}
address public chairperson;
Proposal[] proposals;
mapping(address=>Voter) voters;
constructor(uint _numberProposals){
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numberProposals;
}
function giveRightToVote(address voter) public {
require(msg.sender == chairperson);
require(!voters[voter].voted);
require(voters[voter].weight == 0);
voters[voter].weight = 1;
}
function delegate(address to)public {
Voter storage sender = voters[msg.sender];
require(!sender.voted);
while(voters[to].delegate!=address(0) && voters[to].delegate != msg.sender){
to = voters[to].delegate;
}
require(to != msg.sender);
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if(delegateTo.voted){
proposals[delegateTo.vote].voteCount += sender.weight;
}
else {
delegateTo.weight += sender.weight;
}
}
function vote(uint8 proposal) public {
Voter storage sender = voters[msg.sender];
require(!sender.voted);
sender.voted = true;
sender.vote = proposal;
proposals[proposal].voteCount += sender.weight;
}
function winningProposal() public view returns(uint8 _winningProposal){
uint winningCount = 0;
for(uint8 prop = 0;prop < proposals.length; prop ++){
if(proposals[prop].voteCount > winningCount){
winningCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}
}
学习地址点击直达
Pragma(版本杂注)
源文件可以被版本杂注pragma所注解,表明要求的编译器的版本,例如:pragma solidity ^0.4.0;
源文件将既不允许低于0.4.0版本的编译器编译,也不允许高于(包含)0.5.0版本的编译器编译(第二个条件因使用^添加)
Import
Solidity所支持的导入语句import,语法同js(从ES6 起)非常类似
Solidity值类型
Solidity引用类型