Skip to content

Instantly share code, notes, and snippets.

@0mkara
Last active October 26, 2018 06:21
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 0mkara/f35dc6e4fb93e94187d3d1f6ce37b230 to your computer and use it in GitHub Desktop.
Save 0mkara/f35dc6e4fb93e94187d3d1f6ce37b230 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.25+commit.59dbf8f1.js&optimize=false&gist=
pragma solidity ^0.4.24;
import 'https://github.com/OpenZeppelin/zeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol';
import 'https://github.com/OpenZeppelin/zeppelin-solidity/contracts/ownership/Ownable.sol';
import "./Ballot.sol";
contract AwardToken is ERC20Mintable, Ownable {
uint quantity;
uint ballotPeriod = 7 hours;
Ballot public currBallot;
address[] public prevWinners;
event log (string _msg);
event winLog (address _win);
event newBallot (address _addr);
function AwardToken () {
quantity = 100;
}
function getPreviousWinners() constant returns (address[]) {
return prevWinners;
}
// either a name change or it works fine without it
// function approve(address spender, uint256 value) public returns (bool);
function startRound() onlyOwner onlyMinter public returns (bool) {
// if this is the first minting then we should let this go immediately
if (address(currBallot) == 0x0) {
currBallot = new Ballot(ballotPeriod);
newBallot(currBallot);
return true;
} else {
return false;
}
}
function closeRoundEarly () onlyOwner {
if (address(currBallot) != 0x0 && !currBallot.timeOut()) {
currBallot.finish();
} else revert();
}
function closeRound() onlyOwner {
// this can only be done by the owner of the contract
if (address(currBallot) != 0x0 && currBallot.timeOut()) {
// get winner
address winner = currBallot.winningProposal();
winLog(winner);
// send to winner - but first make sure the address is valid
if ( winner == 0x0){
log("no winner");
} else {
winLog(winner);
super.mint(winner, quantity);
prevWinners.push(winner);
}
delete currBallot;
// start new round
}else revert();
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
revert();
}
function approve(address _spender, uint256 _value) public returns (bool) {
revert();
}
function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
revert();
}
function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
revert();
}
function transfer(address _to, uint256 _value) public returns (bool) {
revert();
}
}
pragma solidity ^0.4.24;
import "remix_tests.sol";
import "./AwardToken.sol";
contract AwardTokenTest {
AwardToken awtkn;
function beforeAll() {
awtkn = new AwardToken();
}
function addressShouldNotBe_0x000() public constant returns (bool) {
address zro = 0x0000000000000000000000000000000000000000;
return Assert.notEqual(address(awtkn), zro, "address should not be 0");
}
function totalSupplyShouldBe_0() public constant returns (bool) {
return Assert.equal(awtkn.totalSupply(), 0, "total supply should be 0");
}
function startRoundShouldReturn_true() public constant returns (bool) {
return Assert.equal(awtkn.startRound(), true, "startRound return is false");
}
function ownerShouldBeTester() public constant returns (bool) {
address tester = this;
return Assert.equal(awtkn.owner(), tester, "owner should be tester");
}
}
contract AwardTokenBalanceTest {
AwardToken awtkn;
address to = 0xe60667640C07E654E37b7082aE4E629cb0a7605f;
function beforeAll() {
awtkn = new AwardToken();
address(awtkn).call.gas(80000).value(0)(bytes4(sha3("mint(address,uint256)")), to, 100);
}
function balanceShouldBeGreaterThan_0() public constant returns (bool) {
return Assert.greaterThan(uint256(awtkn.balanceOf(to)), uint256(0), "balance should be greater than 0");
}
}
pragma solidity ^0.4.0;
contract Ballot {
uint _duration;
uint _startTime;
bool _finishEarly;
struct Proposal {
string description;
string title;
uint voteCount;
address targetAddress;
}
event log (string _msg);
address chairperson;
mapping(address => mapping (address => uint8)) voters; // map between proposal and voter and votes
mapping(address => Proposal) public proposals;
address[] public proposalsSender;
function getProposals() constant returns (address[]) {
return proposalsSender;
}
/// Create a new ballot with $(_numProposals) different proposals.
function Ballot(uint duration) public {
chairperson = msg.sender;
_duration = duration;
_startTime = now;
_finishEarly = false;
}
// duration issues...
// add a new proposals
function addProposal(string desc, string title, address targetAddr) public {
if (timeOut() || targetAddr == 0x0 || proposals[msg.sender].targetAddress != 0x0){
revert();
}
proposals[msg.sender].description = desc;
proposals[msg.sender].title = title;
proposals[msg.sender].voteCount = 0;
proposals[msg.sender].targetAddress = targetAddr;
proposalsSender.push(msg.sender);
}
/// Give a single vote to proposal $(toProposal).
function vote(address proposal) public {
// is this msg.sender in vote - the voter the proposal or the owner of the contract?
// apparantly you can't vote more than once
uint8 vote = voters[proposal][msg.sender];
// the revert - takes it back to the initial state - but that blows away everyting - I suppose...
// check on revert();
if (timeOut()) revert();
if (vote != 0) {
revert(); // already voted for this proposal
} else {
voters[proposal][msg.sender] = 1;
proposals[proposal].voteCount += 1;
}
}
function finish() {
if (chairperson == msg.sender) _startTime = now - _duration;
else revert();
}
// timeOut vs duration in voteCount
// for use in vote(), addProposal(),
function timeOut() public constant returns ( bool timeOver) {
if (_startTime + _duration > now){
timeOver = false;
}else timeOver = true;
}
function winningProposal() public constant returns (address currLeader) {
// does this need to be run only by the contract owner? Currently I think it is not limited
uint vote = 0;
if (timeOut()){
// timeOut - mean that at least the _duration is over
// what if there is tie?
if(proposalsSender.length > 0) {
for (uint8 k = 0; k < proposalsSender.length; k++) {
Proposal proposal = proposals[proposalsSender[k]];
if (vote < proposal.voteCount) {
vote = proposal.voteCount;
currLeader = proposal.targetAddress;
}
}
if (vote > 0) {
return currLeader;
}else{
log("aint no voters!");
}
}else{
log("aint no proposals!");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment