Created
March 14, 2021 23:35
-
-
Save logankilpatrick/ad3f389ed553133097c193b64dd13a4e 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.7.4+commit.3f05b770.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.8.0; | |
contract AverageStream { | |
uint private count; | |
uint private pre_average; | |
uint public decimal; | |
constructor() { | |
count = 0; | |
pre_average = 0; | |
decimal = 3; | |
} | |
function add_element(uint num) public { | |
count += 1; | |
pre_average += num; | |
} | |
function set_decimal(uint new_decimal) public { | |
decimal = new_decimal; | |
} | |
// Inspired by https://ethereum.stackexchange.com/questions/65969/how-would-one-divide-a-decimal-by-a-decimal-in-soliditys | |
function divider(uint numerator, uint denominator) public view returns(uint) { | |
return numerator * (uint(10) ** uint(decimal)) / denominator; | |
} | |
// Issue I opended: https://github.com/ethereum/solidity/issues/11101 | |
function get_average() public view returns (uint) { | |
return divider(pre_average, count); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.8.0; | |
contract Bank { | |
address private owner; | |
mapping (address => uint) private balances; | |
event LogDeposit(address depositor, uint amount); | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Caller is not owner"); | |
_; | |
} | |
constructor() { | |
owner = msg.sender; | |
} | |
// function internal | |
function deposit() public payable returns (uint newBalance) { | |
balances[msg.sender] += msg.value; | |
emit LogDeposit(msg.sender, msg.value); | |
return balances[msg.sender]; | |
} | |
function attemptSend(address payable account, uint withdrawAmount) internal { | |
bool sent = account.send(withdrawAmount); | |
if ( !sent ) { | |
balances[account] += withdrawAmount; | |
} | |
} | |
function withdraw(uint withdrawAmount) external returns (uint newBalance) { | |
if ( balances[msg.sender] >= withdrawAmount ) { | |
balances[msg.sender] -= withdrawAmount; | |
attemptSend(msg.sender, withdrawAmount); | |
} | |
return balances[msg.sender]; | |
} | |
function balance() public view returns (uint) { | |
return balances[msg.sender]; | |
} | |
function balance(address account) public view onlyOwner returns (uint) { | |
return balances[account]; | |
} | |
fallback () external { | |
revert(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.8.0; | |
contract FactorialMemo { | |
// maps a address to a uint, with public scope, called memo_dict. | |
// Similar to a python dict. | |
mapping(uint => uint) public memo_dict; | |
constructor() { | |
memo_dict[0] = 1; | |
memo_dict[1] = 1; | |
} | |
function factorial(uint n) public returns (uint) { | |
// Greator than zero check since solidity is defaulting the mapping to 0 | |
if (memo_dict[n] > 0) { | |
return memo_dict[n]; | |
} | |
memo_dict[n] = n * factorial(n - 1); | |
return memo_dict[n]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.8.0; | |
contract Voting { | |
// Keep track of owner | |
address private owner; | |
mapping (int => uint8) public votesReceived; | |
// Mapping to keep track of who cast a vote already | |
mapping (address => bool) private castedVotes; | |
int[5] candidateList = [int(0),1,2,3,4]; | |
// Keeps track of who the winner is | |
int private winner; | |
int private currentStatus = 1; | |
// "INDETERMINATE" = 1, "NO_WINNER" = 2, "WINNER" = 3 | |
int private votingStatus = 1; | |
// "OPEN" = 1, "CLOSED" = 2 | |
// Immutable allows the value to be set in the constructor. | |
int immutable private numVoters; | |
// Keep track of the number of votes cast to see if there's 50% concensus. | |
int private totalNumVotes; | |
constructor(int num) { | |
numVoters = num; | |
owner = msg.sender; | |
} | |
// Getter for the private winner variable held in contract storage. | |
function getWinner() view public returns (int) { | |
require(votingStatus == 2, 'Voting is NOT closed.'); | |
require(currentStatus == 3, 'The current status is NOT WINNER'); | |
return winner; | |
} | |
function getNumVoters() view public returns (int) { | |
return numVoters; | |
} | |
function getStatus() public view returns (int) { | |
return currentStatus; | |
// "INDETERMINATE" = 1, "NO_WINNER" = 2, "WINNER" = 3 | |
} | |
modifier notOwner() { | |
// If they are the same, should error... | |
require(msg.sender != owner, "The owner and sender are the same"); | |
_; | |
} | |
// Returns the vote count for any canidate by their int value. | |
function totalVotesFor(int candidate) view public returns (uint8) { | |
require(validCandidate(candidate), "Invalid candidate"); | |
return votesReceived[candidate]; | |
} | |
// Checks if user has already voted, if not, caches vote and returns true | |
function validateAndCacheVote() public payable { | |
// Inspired by: https://stackoverflow.com/questions/66626129/how-to-make-some-action-in-a-solidity-contract-cost-1-ether | |
require(msg.value == 1 ether, 'Need to send 1 ETH for a valid vote.'); | |
// Ensure that the vote has not already been cast. | |
require(castedVotes[msg.sender] == false, "Vote already cast."); | |
// if it's true, gives error. | |
castedVotes[msg.sender] = true; | |
} | |
function isVotingOpen() public view returns (bool) { | |
// "OPEN" = 1, "CLOSED" = 2 | |
if (votingStatus != 2) { | |
return true; | |
} | |
return false; | |
} | |
// Does not let the function be called if modifier is false (meaning vote cast by owner) | |
function voteForCandidate(int candidate) public notOwner { | |
require(validCandidate(candidate), "Canidate is not valid"); | |
// Enforce that voting is open before allowing a vote to be cast. | |
require(isVotingOpen(), "Voting is not open"); | |
// Before saving a vote, make sure that it is valid. | |
validateAndCacheVote(); | |
votesReceived[candidate] += 1; | |
// Increase total vote count. | |
totalNumVotes += 1; | |
updateStandings(); | |
} | |
// Loop through the candidateList and figure out who has more votes. | |
function updateStandings() private { | |
int tempWinner; | |
for(uint i = 0; i < candidateList.length; i++) { | |
// Initial condition | |
if (i == 0) { | |
tempWinner = candidateList[i]; | |
} | |
else { | |
// If the number of votes is greator than the current #, update winner | |
if (votesReceived[int(i)] > votesReceived[tempWinner]) { | |
tempWinner = candidateList[i]; | |
} | |
// NOTE: This does not handle a runoff with a tie vote. | |
} | |
} | |
// "OPEN" = 1, "CLOSED" = 2 | |
if (totalNumVotes == numVoters) { | |
votingStatus = 2; | |
} | |
// TODO: Take care of the edge case where it is impossible for the winner to change | |
// ... but all of the votes have not been cast yet. | |
// Check if the person who has the most votes also has 50%. | |
// NOTE: This may not work since division in solidity is messed up | |
if (votesReceived[tempWinner] > (totalNumVotes / 2)) { | |
winner = tempWinner; | |
currentStatus = 3; | |
// "INDETERMINATE" = 1, "NO_WINNER" = 2, "WINNER" = 3 | |
} else { | |
// The function only gets called when a vote is case so if there's no winner, | |
// it is no longer INDETERMINATE. | |
currentStatus = 2; | |
// "INDETERMINATE" = 1, "NO_WINNER" = 2, "WINNER" = 3 | |
// We also do not set the winner variable yet. | |
} | |
} | |
function validCandidate(int candidate) view private returns (bool) { | |
for(uint i = 0; i < candidateList.length; i++) { | |
if (candidateList[i] == candidate) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment