Skip to content

Instantly share code, notes, and snippets.

@technoknol
Created June 15, 2019 06:42
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 technoknol/9a8914c5cf6af4f15040e835731e4f53 to your computer and use it in GitHub Desktop.
Save technoknol/9a8914c5cf6af4f15040e835731e4f53 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.17+commit.bdeb9e52.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;
}
}
}
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.18;
contract SimpleBank
{
mapping (address => uint) private balances;
address public owner;
event LogDepositMade(address accountAddress, uint amount);
function SimpleBank() public {
owner = msg.sender;
}
function deposit() public payable returns (uint) {
require((balances[msg.sender] + msg.value) >= balances[msg.sender]);
balances[msg.sender] += msg.value;
return balances[msg.sender];
}
function withdraw(uint withdrawAmount) public returns (uint remainingBal) {
require(withdrawAmount <= balances[msg.sender]);
balances[msg.sender] -= withdrawAmount;
msg.sender.transfer(withdrawAmount);
return balances[msg.sender];
}
function balance() view public returns (uint) {
return balances[msg.sender];
}}
pragma solidity ^0.4.17;
// We have to specify what version of compiler this code will compile with
contract Rating {
/* mapping field below is equivalent to an associative array or hash.
*/
mapping (bytes32 => uint8) public ratings;
/* We will use an array of bytes32 instead to store the list of movies
*/
bytes32[] public movieList;
/* This is the constructor which will be called once when you
deploy the contract to the blockchain. When we deploy the contract,
we will pass an array of movies for which users will give ratings
*/
function Rating(bytes32[] movieNames) public {
movieList = movieNames;
}
// This function returns the total ratings a movie has received so far
function totalVotesFor(bytes32 movie) view public returns (uint8) {
return ratings[movie];
}
// This function increments the vote count for the specified movie. Equivalent to upvoting
function voteForMovie(bytes32 movie) public {
ratings[movie] += 1;
}
}
pragma solidity ^0.4.17;
// We have to specify what version of compiler this code will compile with
contract Rating {
/* 1. Create a mapping field to store the movie reviews
*/
mapping (bytes32 => uint8) public ratings;
/* 2. Use an array to store the list of movies
*/
bytes32[] public movieList;
/* 4. Develop a constructor that will be called once you deploy the contract on Blockchain
*/
function Rating(bytes32[] movieNames) public {
movieList = movieNames;
}
// 3. Create a function that returns the total ratings a movie has received so far
function totalVotesFor(bytes32 movie) view public returns (uint8) {
return ratings[movie];
}
// This function increments the vote count for the specified movie. Equivalent to upvoting
function voteForMovie(bytes32 movie) public {
ratings[movie] += 1;
}
}
{
"accounts": {
"account{0}": "0xff150534816dff89d1fdb8209e395f3b28bd8096"
},
"linkReferences": {},
"transactions": [
{
"timestamp": 1560580685420,
"record": {
"value": "0",
"parameters": [
[
"0x1262",
"0x12",
"0x12"
]
],
"abi": "0xc41589e7559804ea4a2080dad19d876a024ccb05117835447d72ce08c1d020ec",
"contractName": "Rating",
"bytecode": "6060604052341561000f57600080fd5b6040516102ff3803806102ff833981016040528080518201919050508060019080519060200190610041929190610048565b50506100c0565b82805482825590600052602060002090810192821561008a579160200282015b82811115610089578251829060001916905591602001919060010190610068565b5b509050610097919061009b565b5090565b6100bd91905b808211156100b95760008160009055506001016100a1565b5090565b90565b610230806100cf6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632f265cf71461005e5780636c71d01d1461009f5780636c9d63ab146100e0578063bf58ae3d1461010757600080fd5b341561006957600080fd5b610083600480803560001916906020019091905050610146565b604051808260ff1660ff16815260200191505060405180910390f35b34156100aa57600080fd5b6100c4600480803560001916906020019091905050610177565b604051808260ff1660ff16815260200191505060405180910390f35b34156100eb57600080fd5b610105600480803560001916906020019091905050610197565b005b341561011257600080fd5b61012860048080359060200190919050506101e0565b60405180826000191660001916815260200191505060405180910390f35b6000806000836000191660001916815260200190815260200160002060009054906101000a900460ff169050919050565b60006020528060005260406000206000915054906101000a900460ff1681565b6001600080836000191660001916815260200190815260200160002060008282829054906101000a900460ff160192506101000a81548160ff021916908360ff16021790555050565b6001818154811015156101ef57fe5b906000526020600020900160009150905054815600a165627a7a7230582049a2ea299c141d7dfa15a3dac50b599b7303e6fec37cd78c05a96910f599ec3c0029",
"linkReferences": {},
"name": "",
"inputs": "(bytes32[])",
"type": "constructor",
"from": "account{0}"
}
}
],
"abis": {
"0xc41589e7559804ea4a2080dad19d876a024ccb05117835447d72ce08c1d020ec": [
{
"constant": false,
"inputs": [
{
"name": "movie",
"type": "bytes32"
}
],
"name": "voteForMovie",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"name": "movieNames",
"type": "bytes32[]"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "uint256"
}
],
"name": "movieList",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "bytes32"
}
],
"name": "ratings",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "movie",
"type": "bytes32"
}
],
"name": "totalVotesFor",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment