智能合约案例
简单代币合约
//SPDX-License-Identifier: SimPL-2.0
pragma solidity >0.4.20;
contract Coin {
address public minter;
mapping (address=>uint) public balance;
event Sent(address from, address to, uint amount);
constructor() public {
minter = msg.sender;
}
// minter 给 user 铸币
function mint(address user, uint amount) public {
require (msg.sender == minter);
balance[user] += amount;
}
// minter 给 receiver 发币
function send(address receiver, uint amount) public {
require (balance[msg.sender] >= amount);
balance[msg.sender] -= amount;
balance[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
投票合约
//SPDX-License-Identifier: SimPL-2.0
pragma solidity >0.4.20;
contract Ballot {
// 投票人
struct Voter {
uint weight;
bool isVoted;
uint vote;
address delegate;
}
// 提案
struct Proposal {
bytes32 name;
uint voteCnt;
}
address public chairPerson; // 主持人
mapping(address=>Voter) voters; // 投票人列表
Proposal[] public proposals; // 提案数组
// 初始化合约
constructor(bytes32[] memory _proposals) {
chairPerson = msg.sender; // 将第一个部署合约的人设置为主持人
voters[chairPerson].weight = 1;
// 提案数组存储
for (uint i = 0; i < _proposals.length; i++) {
proposals.push(Proposal({
name: _proposals[i],
voteCnt: 0
}));
}
}
// 主持人分发选票
function giveRightToVote(address voter) public {
require(chairPerson == msg.sender);
require(!voters[voter].isVoted);
require(voters[voter].weight == 0);
voters[voter].weight = 1;
}
// 将选票委托给他人投票
function delegate(address to) public {
Voter storage sender = voters[msg.sender];
require(!sender.isVoted);
while(voters[to].delegate != address(0) &&
voters[to].delegate != msg.sender) {
to = voters[to].delegate;
}
require(to != msg.sender);
sender.isVoted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.isVoted) {
proposals[delegateTo.vote].voteCnt += sender.weight;
} else {
delegateTo.weight += sender.weight;
}
}
// 投票
function vote(uint proposal) public{
Voter storage sender = voters[msg.sender];
require(!sender.isVoted);
sender.isVoted = true;
sender.vote = proposal;
proposals[proposal].voteCnt += sender.weight;
}
// 找到得票最高的提案
function winningProposal() public view returns(uint _winning) {
uint winningCnt = 0;
for (uint8 i = 0; i < proposals.length; i++) {
if (winningCnt < proposals[i].voteCnt) {
winningCnt = proposals[i].voteCnt;
_winning = winningCnt;
}
}
}
}
简单投票合约
//SPDX-License-Identifier: SimPL-2.0
pragma solidity >0.4.20;
contract Voting {
bytes32[] public candidateList;
mapping(bytes32 => uint8) votesReceived;
constructor(bytes32[] memory names) public {
//candidateList = candidateListName;
//candidateList =[bytes32("Alice"),"Bob","Cary"];
candidateList = names;
}
function isCandidate(bytes32 candidateName) internal view returns(bool) {
for (uint8 i = 0; i < candidateList.length; i++) {
if(candidateName == candidateList[i]) {
return true;
}
}
return false;
}
function vote(bytes32 candidateName) public {
require(isCandidate(candidateName));
votesReceived[candidateName] += 1;
}
function getVotes(bytes32 candidateName) public view returns(uint8) {
require(isCandidate(candidateName));
return votesReceived[candidateName];
}
}