Created
August 26, 2018 07:37
-
-
Save albach/8af6f766f7afafa5638f413c010f6b14 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.24+commit.e67f0147.js&optimize=false&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
pragma solidity ^0.4.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. | |
function Ballot(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 constant 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; | |
} | |
} | |
} |
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
pragma solidity ^0.4.7; | |
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () { | |
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 constant returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
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
pragma solidity ^0.4.0; | |
/** | |
* The BankContract contract stores money | |
* and returns the balance | |
*/ | |
contract BankContract { | |
uint balance; | |
// Since it's payable, no need to pass a value as parameter | |
function depositValue () payable { | |
balance += msg.value; | |
} | |
function getBalance() view returns(uint){ | |
return balance; | |
} | |
} |
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
pragma solidity ^0.4.0; | |
contract Dog{ | |
string greeting = "I am a dog! "; | |
function bark() view returns (string){ | |
return greeting; | |
} | |
function setGreeting(string _greet){ | |
greeting = _greet; | |
} | |
} |
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
pragma solidity ^0.4.0; | |
contract Person{ | |
string name = ""; | |
uint age = 0; | |
function getName() view returns (string){ | |
return name; | |
} | |
function setName(string _name){ | |
name = _name; | |
} | |
function getAge() view returns (uint){ | |
return age; | |
} | |
function setAge(uint _age){ | |
age = _age; | |
} | |
} |
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
pragma solidity ^0.4.0; | |
contract PersonsContract{ | |
struct Person{ | |
string name; | |
uint age; | |
} | |
Person[] persons; | |
function addPerson(string _name,uint _age){ | |
persons.push(Person(_name,_age)); | |
} | |
function getAvgAge() view returns (uint){ | |
uint acc = 0; | |
for(uint i = 0 ; i<persons.length; i++){ | |
acc = acc + persons[i].age; | |
} | |
return acc/persons.length; | |
} | |
} |
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
pragma solidity ^0.4.0; | |
import './second.sol'; | |
/** | |
* The IBankContract interface for bankContract | |
*/ | |
contract IBankContract { | |
function depositValue () payable; | |
function getBalance() view returns(uint); | |
} | |
contract KennelContract is DogContract{ | |
modifier costs(uint _value) { | |
require (msg.value >= _value); | |
_; | |
} | |
// Instance of the interface | |
IBankContract BankContract; | |
function initBank(address _bankAddress) onlyOwner { | |
BankContract = IBankContract(_bankAddress); | |
} | |
function transferDog(address _newOwner) payable costs(500){ | |
// address of the owner of the dog | |
address currentOwner = msg.sender; | |
// soft error handling | |
require(currentOwner != _newOwner); | |
// hold the id of the dog while the exchange happens | |
uint dogId = ownerToDog[currentOwner]; | |
// deletes the record of the current Owner from the mapping | |
delete(ownerToDog[currentOwner]); | |
// create a record with the new owner from the mapping | |
ownerToDog[_newOwner] = dogId; | |
// strict error handling: this should never happen | |
// after assigning a new owner the old owner should be 0 | |
// (the default value of a mapping key) | |
assert(ownerToDog[currentOwner] == 0); | |
// sending money to the bank | |
BankContract.depositValue.value(msg.value)(); | |
} | |
function getBankBalance() view returns(uint){ | |
return BankContract.getBalance(); | |
} | |
function addDogToKennel(string _name, uint _age){ | |
addDog(_name, _age); | |
} | |
} |
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
pragma solidity ^0.4.20; | |
library SafeMath { | |
/** | |
* @dev Multiplies two numbers, throws on overflow. | |
*/ | |
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) { | |
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 | |
if (a == 0) { | |
return 0; | |
} | |
c = a * b; | |
assert(c / a == b); | |
return c; | |
} | |
/** | |
* @dev Integer division of two numbers, truncating the quotient. | |
*/ | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
// assert(b > 0); // Solidity automatically throws when dividing by 0 | |
// uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return a / b; | |
} | |
/** | |
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). | |
*/ | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
/** | |
* @dev Adds two numbers, throws on overflow. | |
*/ | |
function add(uint256 a, uint256 b) internal pure returns (uint256 c) { | |
c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} | |
contract C { | |
using SafeMath for uint256; | |
// (2**256 - 1) + 1 = 0 | |
function overflow() constant returns (uint256 _overflow) { | |
uint256 max = 2**256 -1; | |
return max.add(1); | |
} | |
// 0 - 1 = 2**256 - 1 | |
function underflow() constant returns (uint256 _underflow) { | |
uint256 min = 0; | |
return min.sub(1); | |
} | |
} |
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
pragma solidity ^0.4.24; | |
/** | |
* @title Ownable | |
* @dev The Ownable contract has an owner address, and provides basic authorization control | |
* functions, this simplifies the implementation of "user permissions". | |
*/ | |
contract Ownable { | |
address public owner; | |
event OwnershipRenounced(address indexed previousOwner); | |
event OwnershipTransferred( | |
address indexed previousOwner, | |
address indexed newOwner | |
); | |
/** | |
* @dev The Ownable constructor sets the original `owner` of the contract to the sender | |
* account. | |
*/ | |
constructor() public { | |
owner = msg.sender; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
/** | |
* @dev Allows the current owner to relinquish control of the contract. | |
* @notice Renouncing to ownership will leave the contract without an owner. | |
* It will not be possible to call the functions with the `onlyOwner` | |
* modifier anymore. | |
*/ | |
function renounceOwnership() public onlyOwner { | |
emit OwnershipRenounced(owner); | |
owner = address(0); | |
} | |
/** | |
* @dev Allows the current owner to transfer control of the contract to a newOwner. | |
* @param _newOwner The address to transfer ownership to. | |
*/ | |
function transferOwnership(address _newOwner) public onlyOwner { | |
_transferOwnership(_newOwner); | |
} | |
/** | |
* @dev Transfers control of the contract to a newOwner. | |
* @param _newOwner The address to transfer ownership to. | |
*/ | |
function _transferOwnership(address _newOwner) internal { | |
require(_newOwner != address(0)); | |
emit OwnershipTransferred(owner, _newOwner); | |
owner = _newOwner; | |
} | |
} |
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
pragma solidity ^0.4.0; | |
import './Ownable.sol'; | |
contract DogContract is Ownable{ | |
// The Dog object | |
struct Dog{ | |
string name; | |
uint age; | |
} | |
// Events | |
// Events are declared using the event keyword | |
// * the arguments are what will be passed to the | |
// interface. | |
event dogAdded(address owner, string name, uint id); | |
// Associate a Dog to an owner represented by an ethereum address | |
mapping(address => uint) ownerToDog; | |
// Array of objects type Dog | |
Dog[] dogs; | |
// Array of possible dog names | |
string[] dogNames = ["Pako", "Dyna"]; | |
// visibility: in this case out of the possible 4 | |
// (public, internal, external, private) | |
// we choose internal as any contract inheriting from it | |
// should be able to access it only. | |
function addDog(string _name, uint _age) internal{ | |
address owner = msg.sender; | |
Dog memory newDog = Dog(_name,_age); | |
uint idDog = dogs.push(newDog); | |
ownerToDog[owner] = idDog; | |
dogAdded(owner, _name, idDog); | |
} | |
function getMyDogName() returns (string) { | |
address owner = msg.sender; | |
uint dogsId = ownerToDog[owner]; | |
// dogsId is not normalized | |
return dogs[dogsId-1].name; | |
} | |
function getDogName(uint _id) returns (string){ | |
return dogs[_id].name; | |
} | |
function getDogAge(uint _id) returns (uint){ | |
return dogs[_id].age; | |
} | |
function addDogName(string _name){ | |
dogNames.push(_name); | |
} | |
function getDogNameById(uint _id) returns (string){ | |
return dogNames[_id]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment