ERC-20代币是一种代币标准,由V神于2015年6月提出此标准,由Fabian Vogelsteller(erc20及erc725作者,也是LUKSO Token 创始人)于2015年11月提交EIP-20。该标准允许开发者在以太坊区块链网络上开发属于自己的代币,该标准定义了开发者可以按照标准要求使用一些简单的功能如: - 设定代币名称 - 设定代币总量 - 规定小数点位数 - 规范如何批准代币交易 - 如何访问数据 - 允许查看各地址中erc20代币数目以及代币总量 - 一定条件下,允许第三方账户使用某账户中的代币资产 - 允许代币和兼容ETH的智能合约及钱包服务等第三方个体兼容 以及一些简单的函数功能等等。! f- X B ]' q: P4 g
) u; T# m" c9 g* P9 T
- pragma solidity >=0.7.0 <0.9.0;2 P3 f1 R3 H" @
- $ g) r h5 P0 K" [3 E+ z$ ~! c
- //投票实验- _7 M- J: K+ G0 u7 o0 }3 i
- contract Ballot{
-
- struct Voter{! s0 g) g5 L c6 Z4 h
- uint weight;//投票(单票)权重
- bool voted;//是否投过票,true为投过票,bool类型默认值为false
- address delegate;//想要委托投票的节点地址,address默认值为0x08 _" t4 d1 c% O( d7 ^
- uint vote;//想要投票的节点的索引值(被投票节点信息用一维数组proposals存储)
- }5 ^$ N+ W; X5 k; V3 E) K
- + W7 f& I+ Z" \4 I J
- struct Proposal{//被投票节点的相关参数/ Y: p/ U- _, h2 x4 x# o: a5 Z
- bytes32 name;//被投票节点的姓名标识
- uint voteCount;//累积被投票数4 E9 m9 _! W4 T+ e7 T! I
- }+ a' P4 [$ j Y' L2 _. g
- : T+ H& s$ J- @
- address public chairperson;//投票管理员地址
- mapping(address => Voter) public voters;//地址对投票节点的信息的映射
- Proposal[] public proposals;//一维数组存储被投票节点信息* \3 V$ t) \0 z
-
- //构造方法、构造函数
- //solidity和其他语言不一样,构造函数仅在部署合约时调用一次,后续调用合约不调用其构造函数9 Y4 r' _3 O0 i0 k! q& J |
- //且一个合约只能有一个构造函数,不能进行构造函数重载$ Q: Q: j8 K, ~; e, m) b$ c. B
- constructor(bytes32[] proposalNames) public{
- chairperson = msg.sender;//将第一次调用该合约的节点设置为管理员0 R6 ?! i# ?) Z& W. [, X# I
- voters[chairperson].weight = 1;//将管理员投票权置为1% y! r; S7 f8 s# ~! O$ E& i
- ) Y* L0 H- Y. Y0 i
- for(uint i=0; i<proposalNames.length; i++){
- //将所有被投票人姓名初始化进一维数组proposals,并将其对应票数初始化为0票5 p+ G$ e. {% Z4 R# O z( p- M+ d
- //.push(),括号中内容需要强调数据类型,eg:arr.push(uint(6));* o& O, y) ~8 u6 z* c
- proposals.push(Proposal({. x% P* M- B, D' p: y, s1 t
- name:proposalNames[i],
- voteCount:0
- }));
- }& Y3 h) u- r( _2 p! n; M" g
- }0 V2 a5 L# k7 M# M2 ?) H
-
- //由管理员授权可投票节点
- function giveRightToVote(address voter) public{
- //require中判断条件为false时,输出字符串"xxx...",异常会被抛出,程序执行会被挂起,
- //未消耗的gas会被退回,合约状态会回退到初始状态- a4 N- _! P3 c) W* |, S
- require(
- msg.sender == chairperson,"Only chairperson can give right to vote."
- );//执行此function的节点一定为管理员节点' c3 h+ }& @6 F5 j6 G4 K+ X9 _+ o: v
- require(1 O2 z3 y( j/ c
- !voters[voter].voted,"The voter already voted." w. R$ ?0 m( r8 z
- );//若voter没投过票
- require(voters[voter].weight == 0);7 A' |& }9 k. ]3 J! C
- //调用合约的人是管理员、待授权节点还没投过票、带授权节点投票权重为0时,进行授权% a) l# x, u; f, J: K3 ~$ P
- voters[voter].weight = 1;//上述三个require()均成立时,授权票数/ _5 N d/ L2 |8 L& d2 u/ V
- }
-
- //投票授权3 P3 B3 s, Z$ ~* H* W9 z' Q: u
- function delegate(address to) public{4 d* S+ h4 ~0 ~% g/ C, G; H. c
- Voter storage sender = voters[msg.sender];$ {3 `1 p4 L$ C
- require(!sender.voted, "You already voted.");
- require(to != msg.sender,"Self-delegation is disallowed.");
- //sender满足的条件:要有投票权限、没有投过票、被授权节点不是自己
- " q. g6 \7 B& S E7 S: @
- //判断代理节点地址是否为空:address(0)或者address(0x0)
- while(voters[to].delegate != address(0)){" r4 g. }3 X) b- u/ e- y* N j
- to = voters[to].delegate;//找到最终的代理节点
- require(to != msg.sender,"Found loop in delegation.");//若代理节点最终是自己则回退到初始状态) u! R* s. r/ E* [6 |' Y4 h
- }
-
- sender.voted = true;//票权代理出去,状态改为已投票
- sender.delegate = to;//票权代理地址; t: h% X0 ]3 n4 y& N/ ?0 L* r
- Voter storage delegate_ = voters[to];//取出代理节点状态
-
- //若代理节点已投过票,将新代理的票权投出去,反之则将代理节点票权加和2 n! \4 P2 s" a/ |3 _. y
- if(delegate_.voted){
- proposals[delegate_.vote].voteCount += sender.weight;) |3 C3 W ?9 j6 }) T
- }else{4 }7 s: c p4 i; C; J a
- delegate_.weight += sender.weight;- ?5 O% O5 h5 V# ~
- }
- }
-
- function vote(uint proposal) public{
- Voter storage sender = voters[msg.sender];//通过地址获取对应投票信息+ `" w3 @* ]5 D8 b! B
- require(!sender.voted,"Already voted.");//若sender未投过票
- sender.voted = true;//更改投票状态为已投过票7 t4 S- h- f) Z, u
- sender.vote = proposal;//保存已投票节点. A( L8 \% A+ r7 R1 K
- proposals[proposal].voteCount += sender.weight;//票权加和
- }- s2 }0 l7 c/ X! W
-
- //返回票数最多的节点在一维数组proposals中的索引
- function winningProposal() public view returns(uint winningProposal_){
- uint winningVoteCount = 0;9 R( i5 B- g/ A& z
solidity投票智能合约代码,亲测可用# u# b `+ _1 \; A: d) ]# y