|
| 1 | +<!doctype html> |
| 2 | + |
| 3 | +<title>CodeMirror: Solidity mode</title> |
| 4 | +<meta charset="utf-8"/> |
| 5 | +<link rel=stylesheet href="../../doc/docs.css"> |
| 6 | + |
| 7 | +<link rel="stylesheet" href="../../lib/codemirror.css"> |
| 8 | +<script src="../../lib/codemirror.js"></script> |
| 9 | +<script src="solidity.js"></script> |
| 10 | +<style>.CodeMirror { border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; }</style> |
| 11 | +<div id=nav> |
| 12 | + <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png" alt=""></a> |
| 13 | + |
| 14 | + <ul> |
| 15 | + <li><a href="../../index.html">Home</a> |
| 16 | + <li><a href="../../doc/manual.html">Manual</a> |
| 17 | + <li><a href="https://github.com/codemirror/codemirror">Code</a> |
| 18 | + </ul> |
| 19 | + <ul> |
| 20 | + <li><a href="../index.html">Language modes</a> |
| 21 | + <li><a class=active href="#">Solidity</a> |
| 22 | + </ul> |
| 23 | +</div> |
| 24 | + |
| 25 | +<article> |
| 26 | +<h2>Solidity mode</h2> |
| 27 | +<form><textarea id="code" name="code"> |
| 28 | + pragma solidity >=0.4.22 <0.6.0; |
| 29 | + pragma solidity ^0.5.6 ; |
| 30 | + contract Ballot { |
| 31 | + |
| 32 | + struct Voter { |
| 33 | + uint weight; |
| 34 | + bool voted; |
| 35 | + uint8 vote; |
| 36 | + address delegate; |
| 37 | + } |
| 38 | + struct Proposal { |
| 39 | + uint voteCount; |
| 40 | + } |
| 41 | + |
| 42 | + address chairperson; |
| 43 | + mapping(address => Voter) voters; |
| 44 | + Proposal[] proposals; |
| 45 | + |
| 46 | + /// Create a new ballot with $(_numProposals) different proposals. |
| 47 | + constructor(uint8 _numProposals) public { |
| 48 | + chairperson = msg.sender; |
| 49 | + voters[chairperson].weight = 1; |
| 50 | + proposals.length = _numProposals; |
| 51 | + } |
| 52 | + |
| 53 | + /// Give $(toVoter) the right to vote on this ballot. |
| 54 | + /// May only be called by $(chairperson). |
| 55 | + function giveRightToVote(address toVoter) public { |
| 56 | + if (msg.sender != chairperson || voters[toVoter].voted) return; |
| 57 | + voters[toVoter].weight = 1; |
| 58 | + } |
| 59 | + |
| 60 | + /// Delegate your vote to the voter $(to). |
| 61 | + function delegate(address to) public { |
| 62 | + Voter storage sender = voters[msg.sender]; // assigns reference |
| 63 | + if (sender.voted) return; |
| 64 | + while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender) |
| 65 | + to = voters[to].delegate; |
| 66 | + if (to == msg.sender) return; |
| 67 | + sender.voted = true; |
| 68 | + sender.delegate = to; |
| 69 | + Voter storage delegateTo = voters[to]; |
| 70 | + if (delegateTo.voted) |
| 71 | + proposals[delegateTo.vote].voteCount += sender.weight; |
| 72 | + else |
| 73 | + delegateTo.weight += sender.weight; |
| 74 | + } |
| 75 | + |
| 76 | + /// Give a single vote to proposal $(toProposal). |
| 77 | + function vote(uint8 toProposal) public { |
| 78 | + Voter storage sender = voters[msg.sender]; |
| 79 | + if (sender.voted || toProposal >= proposals.length) return; |
| 80 | + sender.voted = true; |
| 81 | + sender.vote = toProposal; |
| 82 | + proposals[toProposal].voteCount += sender.weight; |
| 83 | + } |
| 84 | + |
| 85 | + function winningProposal() public view returns (uint8 _winningProposal) { |
| 86 | + uint256 winningVoteCount = 0; |
| 87 | + for (uint8 prop = 0; prop < proposals.length; prop++) |
| 88 | + if (proposals[prop].voteCount> winningVoteCount) { |
| 89 | + winningVoteCount = proposals[prop].voteCount; |
| 90 | + _winningProposal = prop; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +</textarea></form> |
| 95 | + <script> |
| 96 | + var editor = CodeMirror.fromTextArea(document.getElementById("code"), {}); |
| 97 | + </script> |
| 98 | + |
| 99 | + <p><strong>MIME types defined:</strong> <code>text/x-solidity</code>.</p> |
| 100 | + |
| 101 | + </article> |
0 commit comments