Skip to content

Instantly share code, notes, and snippets.

@brunotarghetta
Created February 25, 2019 18:07
Show Gist options
  • Save brunotarghetta/dcd521a5e3a5806e3094cbd53b2043e8 to your computer and use it in GitHub Desktop.
Save brunotarghetta/dcd521a5e3a5806e3094cbd53b2043e8 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.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;
}
}
contract ConfirmaV1 {
struct Contrato {
string nroContrato;
uint comprador;
uint corredor;
uint vendedor;
}
mapping (address => Contrato) contratoStruct;
address[] public contratos;
address owner;
constructor() public { owner = msg.sender; }
function createUserByVendedor(string nroContrato, uint comprador, uint vendedor) {
contratoStruct[msg.sender].comprador = comprador;
contratoStruct[msg.sender].vendedor = vendedor;
contratoStruct[msg.sender].nroContrato = nroContrato;
contratos.push(msg.sender);
}
function getAllContratos() external view returns (address[]) {
return contratos;
}
function getContrato(address _address) view public returns (string, uint, uint) {
return (contratoStruct[_address].nroContrato, contratoStruct[_address].comprador, contratoStruct[_address].vendedor);
}
}
contract ConfirmaV2 {
struct Contrato {
string nroContrato;
uint comprador;
uint corredor;
uint vendedor;
bool firmadoComprador;
bool firmadoVendedor;
}
mapping (string => Contrato) contratoStruct;
string[] public contratos;
address owner;
constructor() public { owner = msg.sender; }
function createContrato(string nroContrato, uint comprador, uint vendedor) {
contratoStruct[nroContrato].comprador = comprador;
contratoStruct[nroContrato].vendedor = vendedor;
contratoStruct[nroContrato].nroContrato = nroContrato;
contratoStruct[nroContrato].firmadoComprador = false;
contratoStruct[nroContrato].firmadoVendedor = false;
contratos.push(nroContrato);
}
function firmarComprador(string nroContrato, uint comprador) {
if (contratoStruct[nroContrato].comprador == comprador)
contratoStruct[nroContrato].firmadoComprador = true;
}
function firmarVendedor(string nroContrato, uint vendedor) {
if (contratoStruct[nroContrato].vendedor == vendedor)
contratoStruct[nroContrato].firmadoVendedor = true;
}
function getContrato(string _address) view public returns (string, uint, uint) {
return (contratoStruct[_address].nroContrato, contratoStruct[_address].comprador, contratoStruct[_address].vendedor);
}
}
contract KeyValueStorage {
mapping(address => mapping(bytes32 => uint256)) _uintStorage;
mapping(address => mapping(bytes32 => address)) _addressStorage;
mapping(address => mapping(bytes32 => bool)) _boolStorage;
/**** Get Methods ***********/
function getAddress(bytes32 key) public view returns (address) {
return _addressStorage[msg.sender][key];
}
function getUint(bytes32 key) public view returns (uint) {
return _uintStorage[msg.sender][key];
}
function getBool(bytes32 key) public view returns (bool) {
return _boolStorage[msg.sender][key];
}
/**** Set Methods ***********/
function setAddress(bytes32 key, address value) public {
_addressStorage[msg.sender][key] = value;
}
function setUint(bytes32 key, uint value) public {
_uintStorage[msg.sender][key] = value;
}
function setBool(bytes32 key, bool value) public {
_boolStorage[msg.sender][key] = value;
}
}
contract HelloWorld {
uint256 counter = 5;
address owner = msg.sender; //set owner as msg.sender
function add() public { //increases counter by 1
counter++;
}
function subtract() public { //decreases counter by 1
counter--;
}
function getCounter() public constant returns (uint256) {
return counter;
}
function kill() public { //self-destruct function,
if(msg.sender == owner) {
selfdestruct(owner);
}
}
function () public payable {
}
}
contract Users {
struct User {
string name;
uint level;
}
mapping (address => User) userStructs;
address[] public userAddresses;
function createUser(string name, uint level) {
// set User name using our userStructs mapping
userStructs[msg.sender].name = name;
// set User level using our userStructs mapping
userStructs[msg.sender].level = level;
// push user address into userAddresses array
userAddresses.push(msg.sender);
}
function getAllUsers() external view returns (address[]) {
return userAddresses;
}
}
pragma solidity ^0.4.18;
contract voting {
mapping (bytes32 => uint8) public votesReceived;
/* Solidity doesn't let you pass in an array of strings in the constructor.
Using an array of bytes32 instead to store the list of candidates
*/
bytes32[] public candidateList;
//This is the constructor and is called once you deploy the contract
function voting(bytes32[] candidateNames) public {
candidateList = candidateNames;
}
// Returns the total votes a candidate has received
function totalVotesFor(bytes32 candidate) view public returns (uint8) {
require(validCandidate(candidate));
return votesReceived[candidate];
}
// Increments the vote count for the specified candidate
function voteForCandidate(bytes32 candidate) public {
require(validCandidate(candidate));
votesReceived[candidate] += 1;
}
function validCandidate(bytes32 candidate) view public 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