Skip to content

Instantly share code, notes, and snippets.

@gvandriel
Created October 15, 2019 15:23
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 gvandriel/f185c828c7646903e38525f2aeb98bf7 to your computer and use it in GitHub Desktop.
Save gvandriel/f185c828c7646903e38525f2aeb98bf7 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.22+commit.4cb486ee.js&optimize=false&gist=
pragma solidity >=0.4.22 <0.6.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
/// Create a new ballot with $(_numProposals) different proposals.
constructor(uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}
/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address toVoter) public {
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
}
/// Delegate your vote to the voter $(to).
function delegate(address to) public {
Voter storage sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}
/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}
function winningProposal() public view returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}
pragma solidity >=0.4.22 <0.6.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./ballot.sol";
contract test3 {
Ballot ballotToTest;
function beforeAll () public {
ballotToTest = new Ballot(2);
}
function checkWinningProposal () public {
ballotToTest.vote(1);
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal");
}
function checkWinninProposalWithReturnValue () public view returns (bool) {
return ballotToTest.winningProposal() == 1;
}
}
pragma solidity ^0.4.22;
contract Ulysses {
struct Contract {
string description;
uint stakedAmount;
uint weightDif;
string massUnit;
address contractOwner;
uint secretWordIndex;
uint start;
uint duration;
bool voted;
bool complete;
}
bool public canCreate = true;
uint public fee = 0.005 ether;
uint public minStake = 0.060 ether;
uint public maxStake = 0.60 ether;
mapping(address => bool) public referees;
Contract[] public contracts;
address[] public refereeArray;
modifier admin {
require(msg.sender == 0x993235528CDE5f17770A614A43625A76c38F1729 || msg.sender == 0xd1275Df91bf079FE02bC9A1fAD5E7d1a1fB42F4f);
_;
}
modifier referee {
require(referees[msg.sender] || msg.sender == 0x993235528CDE5f17770A614A43625A76c38F1729 || msg.sender == 0xd1275Df91bf079FE02bC9A1fAD5E7d1a1fB42F4f);
_;
}
function createContract(string memory input, uint timeLength, uint weight, string memory unit) public payable {
require(msg.value >= minStake + fee && msg.value <= maxStake +fee);
require(timeLength >= 7 days && weight>0 && canCreate ==true);
Contract memory newContract = Contract({
description: input,
stakedAmount: msg.value,
contractOwner: msg.sender,
secretWordIndex: uint256(keccak256(block.timestamp, block.difficulty))%401,
weightDif: weight,
massUnit: unit,
start: now,
duration: timeLength,
voted: false,
complete: false
});
contracts.push(newContract);
}
function judgeContract(uint index, bool judgment) public admin{
require(contracts[index].complete == false );
contracts[index].complete = true;
if ( judgment == true ) {
contracts[index].voted = true;
0x993235528CDE5f17770A614A43625A76c38F1729.transfer(fee);
contracts[index].contractOwner.transfer(contracts[index].stakedAmount-fee);
} else {
0x993235528CDE5f17770A614A43625A76c38F1729.transfer(fee);
0x000000000000000000000000000000000000dEaD.transfer(contracts[index].stakedAmount-fee);
}
}
function refundContract(uint index) public admin{
require(contracts[index].complete == false );
contracts[index].complete = true;
contracts[index].voted = true;
contracts[index].contractOwner.transfer(contracts[index].stakedAmount);
}
function canCreateContract(bool input) public admin{
canCreate = input;
}
function addReferee(address refereeAddress) public admin {
referees[refereeAddress] = true;
refereeArray.push(refereeAddress);
}
function removeReferee(address refereeAddress) public admin {
referees[refereeAddress] = false;
}
function getContractsCount() public view returns (uint) {
return contracts.length;
}
function getRefereeCount() public view returns (uint) {
return refereeArray.length;
}
function getNow() public view returns(uint){
return now;
}
function setMinStake(uint price) public{
minStake = price;
}
function setMaxStake(uint price) public{
maxStake = price;
}
function setFee(uint price) public{
fee = price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment