Skip to content

Instantly share code, notes, and snippets.

@juntao
Last active September 30, 2019 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juntao/07a3102a3db1c07a4da4db3f5d0bd16a to your computer and use it in GitHub Desktop.
Save juntao/07a3102a3db1c07a4da4db3f5d0bd16a to your computer and use it in GitHub Desktop.
Add the following to "dapp -- Resources"
* JavaScript: https://code.jquery.com/jquery-3.4.1.min.js
* CSS: https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css
The "dapp -- CSS" should be empty
<div class="container">
<br/>
<div class="jumbotron">
<p class="lead" id="greeting"></p>
<div id="imageDiv" style="display:none">
<img id="image" src="" class="img-fluid img-thumbnail"/>
</div>
<hr/>
<p id="votes" style="display:none">
<span id="ups"></span> voted πŸ‘ |
<span id="downs"></span> voted πŸ‘Ž
</p>
<form id="form" class="form-inline" style="display:none">
<button id="voteUp" onclick="vote(1);" class="btn btn-primary mb-2">πŸ‘</button>
<button id="voteDown" onclick="vote(-1);" class="btn btn-primary mb-2">πŸ‘Ž</button>
</form>
<div id="myVoteUp" style="display:none">You have already voted πŸ‘</div>
<div id="myVoteDown" style="display:none">You have already voted πŸ‘Ž</div>
</div>
</div>
var contract = window.web3 && web3.ss && web3.ss.contract(abi);
var instance = contract && contract.at(cAddr);
window.addEventListener('web3Ready', function() {
contract = web3.ss.contract(abi);
instance = contract.at(cAddr);
reload();
});
function reload() {
instance.greeting(function (e, r) {
$("#greeting").html(r);
});
instance.photoUrl(function (e, r) {
if (!e && r) {
$("#imageDiv").css("display", "block");
$("#image").attr("src", r);
}
});
instance.getVotes(function (e, r) {
if (!e && (r[0] > 0 || r[1] > 0)) {
$("#votes").css("display", "block");
$("#ups").text(r[0]);
$("#downs").text(r[1]);
}
});
web3.ss.getAccounts(function (e, address) {
if (!e) {
instance.getVote(address, function (ee, r) {
$("#form").css("display", "none");
if (r == 1) {
$("#myVoteUp").css("display", "block");
} else if (r == -1) {
$("#myVoteDown").css("display", "block");
} else {
$("#form").css("display", "block");
}
});
}
});
}
function vote (choice) {
web3.ss.getAccounts(function (e, address) {
if (!e) {
$("#form").html("<p>Wait for 20 seconds ...</p>");
instance.vote(choice, {
gas: 400000,
gasPrice: 0
}, function (e, result) {
if (e) {
window.alert("Failed. Check if there is at least 0.1 ETC (for gas fee) in your account " + address);
}
});
setTimeout(function () {
reload ();
}, 20 * 1000);
}
});
return false;
}
pragma solidity >= 0.4.0;
contract Vote {
string public greeting;
string public photoUrl;
mapping (address => int) votes;
uint ups;
uint downs;
constructor(string _greeting, string _photoUrl) public {
greeting = _greeting;
photoUrl = _photoUrl;
}
function vote (int _choice) public {
require (votes[msg.sender] == 0);
require (_choice == 1 || _choice == -1);
votes[msg.sender] = _choice;
if (_choice == 1) ups++;
if (_choice == -1) downs++;
}
function getVotes () view public returns (uint, uint) {
return (ups, downs);
}
function getVote (address _addr) view public returns (int) {
return votes[_addr];
}
}
pragma solidity >= 0.4.0;
contract Vote {
string public greeting;
string public photoUrl;
mapping (address => int) votes;
uint ups;
uint downs;
function Vote (string _greeting, string _photoUrl) public {
greeting = _greeting;
photoUrl = _photoUrl;
}
function vote (int _choice) public {
if (votes[msg.sender] != 0) { throw; }
if (_choice != 1 && _choice != -1) { throw; }
votes[msg.sender] = _choice;
if (_choice == 1) ups++;
if (_choice == -1) downs++;
}
function getVotes () public constant returns (uint, uint) {
return (ups, downs);
}
function getVote (address _addr) public constant returns (int) {
return votes[_addr];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment