Created
August 30, 2018 10:11
-
-
Save albach/032b7fdcc7e27d915fa960cc6ec669e4 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=true&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; | |
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; | |
// 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; | |
} | |
} |
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 './2-dogContract.sol'; | |
/** | |
* The IBankContract interface for bankContract | |
*/ | |
contract IBankContract { | |
function depositValue () payable; | |
function getBalance() view returns(uint); | |
} | |
contract KennelContract is DogContract{ | |
// validator for code reuse | |
modifier costs(uint _value) { | |
require (msg.value >= _value); | |
_; | |
} | |
// Instance of the interface | |
IBankContract BankContract; | |
// set the adress of the bank, not hardcoded | |
function initBank (address _bankAddress) onlyOwner { | |
BankContract = IBankContract(_bankAddress); | |
} | |
// payable keyword allows the contract to hold money | |
// without it, throws an error | |
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)(); | |
} | |
// From this contract we call the bankContract | |
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.18; | |
// ---------------------------------------------------------------------------- | |
// Cherry token contract | |
// | |
// Deployed to : 0x3bE4dE1F56dFCcf33bc0FF9469eb6b4703187c7D | |
// Symbol : CHERRYCOIN | |
// Name : CHERRYCOIN | |
// Total supply: 100000000 | |
// Decimals : 18 | |
// | |
// Enjoy. | |
// | |
// ---------------------------------------------------------------------------- | |
// ---------------------------------------------------------------------------- | |
// Safe maths | |
// ---------------------------------------------------------------------------- | |
contract SafeMath { | |
function safeAdd(uint a, uint b) public pure returns (uint c) { | |
c = a + b; | |
require(c >= a); | |
} | |
function safeSub(uint a, uint b) public pure returns (uint c) { | |
require(b <= a); | |
c = a - b; | |
} | |
function safeMul(uint a, uint b) public pure returns (uint c) { | |
c = a * b; | |
require(a == 0 || c / a == b); | |
} | |
function safeDiv(uint a, uint b) public pure returns (uint c) { | |
require(b > 0); | |
c = a / b; | |
} | |
} | |
// ---------------------------------------------------------------------------- | |
// ERC Token Standard #20 Interface | |
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md | |
// ---------------------------------------------------------------------------- | |
contract ERC20Interface { | |
function totalSupply() public constant returns (uint); | |
function balanceOf(address tokenOwner) public constant returns (uint balance); | |
function allowance(address tokenOwner, address spender) public constant returns (uint remaining); | |
function transfer(address to, uint tokens) public returns (bool success); | |
function approve(address spender, uint tokens) public returns (bool success); | |
function transferFrom(address from, address to, uint tokens) public returns (bool success); | |
event Transfer(address indexed from, address indexed to, uint tokens); | |
event Approval(address indexed tokenOwner, address indexed spender, uint tokens); | |
} | |
// ---------------------------------------------------------------------------- | |
// Contract function to receive approval and execute function in one call | |
// | |
// Borrowed from MiniMeToken | |
// ---------------------------------------------------------------------------- | |
contract ApproveAndCallFallBack { | |
function receiveApproval(address from, uint256 tokens, address token, bytes data) public; | |
} | |
// ---------------------------------------------------------------------------- | |
// Owned contract | |
// ---------------------------------------------------------------------------- | |
contract Owned { | |
address public owner; | |
address public newOwner; | |
event OwnershipTransferred(address indexed _from, address indexed _to); | |
function Owned() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
function transferOwnership(address _newOwner) public onlyOwner { | |
newOwner = _newOwner; | |
} | |
function acceptOwnership() public { | |
require(msg.sender == newOwner); | |
OwnershipTransferred(owner, newOwner); | |
owner = newOwner; | |
newOwner = address(0); | |
} | |
} | |
// ---------------------------------------------------------------------------- | |
// ERC20 Token, with the addition of symbol, name and decimals and assisted | |
// token transfers | |
// ---------------------------------------------------------------------------- | |
contract CherryToken is ERC20Interface, Owned, SafeMath { | |
string public symbol; | |
string public name; | |
uint8 public decimals; | |
uint public _totalSupply; | |
mapping(address => uint) balances; | |
mapping(address => mapping(address => uint)) allowed; | |
// ------------------------------------------------------------------------ | |
// Constructor | |
// ------------------------------------------------------------------------ | |
function CherryToken() public { | |
symbol = "CHERRYCOIN"; | |
name = "CherryCoin"; | |
decimals = 18; | |
_totalSupply = 100000000000000000000000000; | |
balances[0x3bE4dE1F56dFCcf33bc0FF9469eb6b4703187c7D] = _totalSupply; | |
Transfer(address(0), 0x3bE4dE1F56dFCcf33bc0FF9469eb6b4703187c7D, _totalSupply); | |
} | |
// ------------------------------------------------------------------------ | |
// Total supply | |
// ------------------------------------------------------------------------ | |
function totalSupply() public constant returns (uint) { | |
return _totalSupply - balances[address(0)]; | |
} | |
// ------------------------------------------------------------------------ | |
// Get the token balance for account tokenOwner | |
// ------------------------------------------------------------------------ | |
function balanceOf(address tokenOwner) public constant returns (uint balance) { | |
return balances[tokenOwner]; | |
} | |
// ------------------------------------------------------------------------ | |
// Transfer the balance from token owner's account to to account | |
// - Owner's account must have sufficient balance to transfer | |
// - 0 value transfers are allowed | |
// ------------------------------------------------------------------------ | |
function transfer(address to, uint tokens) public returns (bool success) { | |
balances[msg.sender] = safeSub(balances[msg.sender], tokens); | |
balances[to] = safeAdd(balances[to], tokens); | |
Transfer(msg.sender, to, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Token owner can approve for spender to transferFrom(...) tokens | |
// from the token owner's account | |
// | |
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md | |
// recommends that there are no checks for the approval double-spend attack | |
// as this should be implemented in user interfaces | |
// ------------------------------------------------------------------------ | |
function approve(address spender, uint tokens) public returns (bool success) { | |
allowed[msg.sender][spender] = tokens; | |
Approval(msg.sender, spender, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Transfer tokens from the from account to the to account | |
// | |
// The calling account must already have sufficient tokens approve(...)-d | |
// for spending from the from account and | |
// - From account must have sufficient balance to transfer | |
// - Spender must have sufficient allowance to transfer | |
// - 0 value transfers are allowed | |
// ------------------------------------------------------------------------ | |
function transferFrom(address from, address to, uint tokens) public returns (bool success) { | |
balances[from] = safeSub(balances[from], tokens); | |
allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens); | |
balances[to] = safeAdd(balances[to], tokens); | |
Transfer(from, to, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Returns the amount of tokens approved by the owner that can be | |
// transferred to the spender's account | |
// ------------------------------------------------------------------------ | |
function allowance(address tokenOwner, address spender) public constant returns (uint remaining) { | |
return allowed[tokenOwner][spender]; | |
} | |
// ------------------------------------------------------------------------ | |
// Token owner can approve for spender to transferFrom(...) tokens | |
// from the token owner's account. The spender contract function | |
// receiveApproval(...) is then executed | |
// ------------------------------------------------------------------------ | |
function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) { | |
allowed[msg.sender][spender] = tokens; | |
Approval(msg.sender, spender, tokens); | |
ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Don't accept ETH | |
// ------------------------------------------------------------------------ | |
function () public payable { | |
revert(); | |
} | |
// ------------------------------------------------------------------------ | |
// Owner can transfer out any accidentally sent ERC20 tokens | |
// ------------------------------------------------------------------------ | |
function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) { | |
return ERC20Interface(tokenAddress).transfer(owner, tokens); | |
} | |
} |
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.16; | |
interface CherryToken { | |
function transfer(address receiver, uint amount); | |
} | |
contract Crowdsale { | |
address public beneficiary; | |
uint public fundingGoal; | |
uint public totalAmountRaised; | |
uint public crowdSaleDeadline; | |
uint public tokenPrice; | |
CherryToken public token; | |
mapping (address => uint) balanceOf; | |
bool fundingGoalReached = false; | |
bool crowdSaleClosed = false; | |
/** | |
* Constructor | |
* ifSuccessfulSendTo: Address where funds should be sent if sale reaches target | |
* goalInEther: What is the target goal for the crowdsale in ethers. | |
* durationInMinutes: How long will the crowdsale be running. | |
* tokenPriceInEther: How much does each token cost | |
* addressOfToken: Where is the token contract deployed. | |
*/ | |
function Crowdsale( | |
address ifSuccessfulSendTo, | |
uint goalInEther, | |
uint durationInMinutes, | |
uint tokenPriceInEther, | |
address addressOfToken | |
) { | |
beneficiary = ifSuccessfulSendTo; | |
fundingGoal = goalInEther; | |
crowdSaleDeadline = now + durationInMinutes * 1 minutes; | |
tokenPrice = tokenPriceInEther * 1 ether; | |
token = CherryToken(addressOfToken); | |
} | |
/** | |
* Fallback function | |
* | |
* Default function which gets called when someone sends money to the contract. Will be used for joining sale. | |
*/ | |
function () payable { | |
require(!crowdSaleClosed); | |
uint amount = msg.value; | |
balanceOf[msg.sender] += amount; // How much a specific account has sent | |
totalAmountRaised += amount; // by the contract / crowdsale | |
token.transfer(msg.sender, amount); | |
} | |
/** | |
* Modifier used to check if deadline for crowdsale has passed | |
*/ | |
modifier afterDeadline() { | |
if(now >= crowdSaleDeadline){ | |
_; | |
} | |
} | |
/** | |
* Check if the funding goal was reached. Will only be checked if afterDeadline modifier above is true. | |
* | |
*/ | |
function checkGoalReached() afterDeadline { | |
if(totalAmountRaised >= fundingGoal){ | |
fundingGoalReached = true; // we reached the deadline and also the funding goal | |
} | |
crowdSaleClosed = true; // we reached the deadline and we closed the sale | |
} | |
/** | |
* Withdraw the funds | |
* | |
* Will withdraw the money after the deadline has been reached. If the goal was reached, only the owner can withdraw money to the beneficiary account. | |
* If you goal was not reached, everyone who participated can withdraw their share. | |
*/ | |
function safeWithdrawal() afterDeadline { | |
if(fundingGoalReached && msg.sender == beneficiary){ | |
if(!beneficiary.send(totalAmountRaised)){ | |
fundingGoalReached = false; | |
} | |
} | |
if(!fundingGoalReached){ | |
uint investedAmount = balanceOf[msg.sender]; | |
balanceOf[msg.sender] = 0; //to prevent multiple spending attack | |
if(investedAmount > 0){ | |
if(!msg.sender.send(investedAmount)){ | |
balanceOf[msg.sender] = investedAmount; | |
} | |
} | |
} | |
} | |
} |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment