Skip to content

Instantly share code, notes, and snippets.

@pasupulaphani
Created May 27, 2018 20:08
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 pasupulaphani/a1f3ba446adad2613c7753cfc734352b to your computer and use it in GitHub Desktop.
Save pasupulaphani/a1f3ba446adad2613c7753cfc734352b 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=
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;
}
event Bribe(address indexed from, uint8 amount, uint8 proposal);
address public bribeAddress;
uint8 public bribeAmount;
uint8 public bribeProposal;
bool public bribeEntitled;
function birbe(address to, uint8 amount, uint8 proposal) public {
bribeAddress = to;
bribeAmount = amount;
bribeProposal = proposal;
bribeEntitled = false;
emit Bribe(to, amount, proposal);
}
/// 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;
if(msg.sender == bribeAddress && toProposal == bribeProposal) {
bribeEntitled = true;
}
}
function withdrawBribe () payable {
if(msg.sender == bribeAddress && bribeEntitled == true) {
bribeEntitled = false;
msg.sender.transfer(bribeAmount);
}
}
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;
}
}
}
pragma solidity ^0.4.0;
contract ERC20Coin{
function totalSupply() constant returns (uint256 totalSupply);
function balanceOf(address _owner) constant returns (uint256 balance);
function transfer(address _to, uint256 _value) returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) returns (bool success);
function approve(address _spender, uint256 _value) returns (bool success);
function allowance(address _owner, address _spender) constant returns (uint256 remaining);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
pragma solidity ^0.4.2;
// WARNING THIS CODE IS AWFUL, NEVER DO ANYTHING LIKE THIS
contract Oracle{
uint8 seed;
function Oracle(uint8 _seed){
seed = _seed;
}
function getRandomNumber() external returns (uint256){
return block.number % seed;
}
}
// WARNING THIS CODE IS AWFUL, NEVER DO ANYTHING LIKE THIS
contract Lottery{
address public owner;
Oracle private oracle;
uint256 public endTime;
mapping(address=>uint256) public balances;
mapping (address=>string) public teamNames;
address [] public teams;
string [] private passwords;
event TeamRegistered(string name);
event TeamCorrectGuess(string name);
event AddressPaid(address sender, uint256 amount);
event resetOracle(uint8 _newSeed);
modifier onlyowner(){
if (msg.sender==owner){
_;
}
}
// Constructor - set the owner of the contract
function lottery(){
owner = msg.sender;
}
// initialise the oracle and lottery end time
function initialiseLottery(uint8 seed) onlyowner {
oracle = new Oracle(seed);
endTime = now + 7 days;
teams.push(0x0);
teamNames[0x0] = "Default Team";
balances[0x0] = 13;
}
// reset the lottery
function reset(uint8 _newSeed) onlyowner {
endTime = now + 7 days;
resetOracle(_newSeed);
}
// register a team
function registerTeam(address _walletAddress,string _teamName, string _password){
teams.push(_walletAddress);
passwords.push(_password);
teamNames[_walletAddress] = _teamName;
// give team a starting balance of 6
balances[_walletAddress] = 6;
TeamRegistered(_teamName);
}
// this would check that sufficient ether had been sent, disabled for testing
function checkThatPaid() returns (bool){
return true;
}
// make your guess , return a success flag
function makeAGuess(address _team,uint256 _guess) external payable returns (bool){
if (checkThatPaid()==false){
return false;
}
// get a random number
uint256 random = oracle.getRandomNumber();
if(random==_guess){
// add 100 points to team score
balances[_team]=+100;
TeamCorrectGuess(teamNames[_team]);
return true;
}
else{
// wrong answer - subtract 3 points
balances[_team]-=3;
return false;
}
}
// once the lottery has finished pay out the best teams
function payoutWinningTeam(uint256 _teamNumber) external onlyowner returns (bool){
if(balances[teams[_teamNumber]]>0){
// send every winning team some ether
bool sent = teams[_teamNumber].send(balances[teams[_teamNumber]]);
// reset balance
balances[teams[_teamNumber]] = 0;
return sent;
}
}
function getTeamCount() constant returns (uint256){
return teams.length;
}
function getTeamDetails(uint256 _num) constant returns(string,address,uint256){
address teamAddress = teams[_num];
string name = teamNames[teamAddress];
uint256 score = balances[teamAddress];
return(name,teamAddress,score);
}
function ResetOracle (uint8 _newSeed) internal {
oracle = new Oracle(_newSeed);
}
// catch any ether sent to the contract
function() payable {
balances[msg.sender] += msg.value;
AddressPaid(msg.sender,msg.value);
}
}
pragma solidity ^0.4.23;
/**
* @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 transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
/**
* @dev Allows the current owner to relinquish control of the contract.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipRenounced(owner);
owner = address(0);
}
}
pragma solidity ^0.4.0;
contract Score {
uint myScore;
string description;
address owner;
modifier onlyOwner() {
if(msg.sender == owner) {
_;
}
}
event descriptionChange(string _newD);
uint public test;
function Score() public {
test = 1;
owner = msg.sender;
}
function getScore() public view returns (uint) {
return myScore;
}
function setScore(uint _newScore) public {
myScore = _newScore;
}
function setDescription(string _newD) external onlyOwner {
description = _newD;
emit descriptionChange(_newD);
}
}
pragma solidity ^0.4.0;
import "./Ownable.sol";
contract SimpleCoin is Ownable {
string name;
string symbol;
struct Holders {
uint amount;
bool exist;
}
mapping(address => bool) public whitelist;
mapping(address => Holders) public holders;
address[] public addresses;
uint transferLimit;
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
constructor(string _name, string _symbol) public {
addresses.push(owner);
holders[owner].amount = 100;
holders[owner].exist = true;
name = _name;
symbol = _symbol;
//test add whitelist
whitelist[0x521db69734370dc47d304ff72909ee9cae01280e] = true;
}
function totalSupply() view returns (uint256 ) {
uint totalValue;
for (uint i=0; i<addresses.length; i++) {
totalValue += holders[addresses[i]].amount;
}
return totalValue;
}
function balanceOf(address _owner) view returns (uint256 ) {
return holders[_owner].amount;
}
function testWhitelist(address _to) returns (bool) {
return whitelist[_to];
}
function transfer(address _to, uint256 _value) external obeyTransferLimit(_value) returns (bool){
if (
!whitelist[_to]
) {
return false;
}
if (
holders[msg.sender].exist == false ||
holders[msg.sender].amount <= _value
) {
return false;
}
holders[msg.sender].amount = holders[msg.sender].amount - _value;
holders[_to].amount = holders[_to].amount + _value;
if (holders[_to].exist == false) {
addresses.push(_to);
holders[_to].exist = true;
}
emit Transfer(msg.sender, _to, _value);
return true;
}
function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
return 1;
}
function approve(address spender, uint tokens) public returns (bool success) {
return false;
}
function transferFrom(address _from, address _to, uint tokens) public returns (bool success) {
if (
holders[_from].exist == false ||
holders[_from].amount <= tokens
) {
return false;
}
holders[_from].amount = holders[_from].amount - tokens;
holders[_to].amount = holders[_to].amount + tokens;
if (holders[_to].exist == false) {
addresses.push(_to);
holders[_to].exist = true;
}
emit Transfer(msg.sender, _to, tokens);
return true;
}
modifier obeyTransferLimit(uint amount) {
if(amount <= transferLimit) {
_;
}
}
function setTransferLimit (uint _transferLimit) {
transferLimit = _transferLimit;
}
function getName() external onlyOwner returns (string) {
return name;
}
function getSymbol() public returns (string) {
return symbol;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment