Skip to content

Instantly share code, notes, and snippets.

Created November 17, 2017 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/d45c7616cc1564f101d8174e50ffc670 to your computer and use it in GitHub Desktop.
Save anonymous/d45c7616cc1564f101d8174e50ffc670 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.18+commit.9cf6e910.js&optimize=undefined&gist=
contract creatorBalanceChecker {
address creator;
uint creatorbalance; // TIP: uint is an alias for uint256. Ditto int and int256.
function creatorBalanceChecker() public
{
creator = msg.sender; // msg is a global variable
creatorbalance = creator.balance;
}
function getContractAddress() constant returns (address)
{
return this;
}
function getCreatorBalance() constant returns (uint) // Will return the creator's balance AT THE TIME THIS CONTRACT WAS CREATED
{
return creatorbalance;
}
function getCreatorDotBalance() constant returns (uint) // Will return creator's balance NOW
{
return creator.balance;
}
/**********
Standard kill() function to recover funds
**********/
function kill()
{
if (msg.sender == creator)
suicide(creator); // kills this contract and sends remaining funds back to creator
}
}
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) {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}
/// Give $(voter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address voter) {
if (msg.sender != chairperson || voters[voter].voted) return;
voters[voter].weight = 1;
}
/// Delegate your vote to the voter $(to).
function delegate(address to) {
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 $(proposal).
function vote(uint8 proposal) {
Voter storage sender = voters[msg.sender];
if (sender.voted || proposal >= proposals.length) return;
sender.voted = true;
sender.vote = proposal;
proposals[proposal].voteCount += sender.weight;
}
function winningProposal() constant returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 proposal = 0; proposal < proposals.length; proposal++)
if (proposals[proposal].voteCount > winningVoteCount) {
winningVoteCount = proposals[proposal].voteCount;
_winningProposal = proposal;
}
}
}
contract Greeter // The contract definition. A constructor of the same name will be automatically called on contract creation.
{
address creator; // At first, an empty "address"-type variable of the name "creator". Will be set in the constructor.
string greeting; // At first, an empty "string"-type variable of the name "greeting". Will be set in constructor and can be changed.
function Greeter(string _greeting) public // The constructor. It accepts a string input and saves it to the contract's "greeting" variable.
{
creator = msg.sender;
greeting = _greeting;
}
function greet() constant returns (string)
{
return greeting;
}
function getBlockNumber() constant returns (uint) // this doesn't have anything to do with the act of greeting
{ // just demonstrating return of some global variable
return block.number;
}
function setGreeting(string _newgreeting)
{
greeting = _newgreeting;
}
/**********
Standard kill() function to recover funds
**********/
function kill()
{
if (msg.sender == creator) // only allow this action if the account sending the signal is the creator
suicide(creator); // kills this contract and sends remaining funds back to creator
}
}
contract infoGetter {
address creator;
function basicInfoGetter() public {
creator = msg.sender;
}
function getCurrentMinerAddress() constant returns (address){
// get CURRENT block miner's address,
// not necessarily the address of the miner when this block was born
return block.coinbase;
}
function getCurrentDifficulty() constant returns (uint){
return block.difficulty;
}
function getCurrentGaslimit() constant returns (uint){
// the most gas that can be spent on any given transaction right now
return block.gaslimit;
}
function getCurrentBlockNumber() constant returns (uint){
return block.number;
}
function getBlockTimestamp() constant returns (uint){
// returns current block timestamp in SECONDS (not ms) from epoch
// also "now" == "block.timestamp", as in "return now;"
return block.timestamp;
}
function getMsgData() constant returns (bytes){
// The data of a call to this function. Always returns "0xc8e7ca2e" for me.
// adding an input parameter would probably change it with each diff call?
return msg.data;
}
function getMsgSender() constant returns (address){
// Returns the address of whomever made this call
// (i.e. not necessarily the creator of the contract)
return msg.sender;
}
function getTxGasprice() constant returns (uint){
// "gasprice" is the amount of gas the sender was *willing* to pay. 50000000 for me. (geth default)
return tx.gasprice;
}
function getTxOrigin() constant returns (address){
// returns sender of the transaction
// What if there is a chain of calls? I think it returns the first sender, whoever provided the gas.
return tx.origin;
}
function getContractAddress() constant returns (address) {
return this;
}
function getContractBalance() constant returns (uint) {
return this.balance;
}
/**********
Standard kill() function to recover funds
**********/
function kill(){
if (msg.sender == creator){
// kills this contract and sends remaining funds back to creator
suicide(creator);
}
}
}
contract BasicIterator {
address creator; // reserve one "address"-type spot
uint8[10] integers; // reserve a chunk of storage for 10 8-bit unsigned integers in an array
function BasicIterator()
{
creator = msg.sender; // set the creator address
uint8 x = 0; // initialize an 8-bit, unsigned integer to zero
while(x < integers.length) // the variable integers was initialized to length 10
{
integers[x] = x; // set integers to [0,1,2,3,4,5,6,7,8,9] over ten iterations
x++;
}
}
function getSum() constant returns (uint) // "constant" just means this function returns something to the caller
{ // which is immediately followed by what type gets returned, in this case a full uint256
uint8 sum = 0;
uint8 x = 0;
while(x < integers.length)
{
sum = sum + integers[x];
x++;
}
return sum;
}
/**********
Standard kill() function to recover funds
**********/
function kill()
{
if (msg.sender == creator)
{
suicide(creator); // kills this contract and sends remaining funds back to creator
}
}
}
/*
Oraclize random-datasource example
This contract uses the random-datasource to securely generate off-chain N random bytes
*/
pragma solidity ^0.4.11;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract RandomExample is usingOraclize {
event newRandomNumber_bytes(bytes);
event newRandomNumber_uint(uint);
function RandomExample() {
oraclize_setProof(proofType_Ledger); // sets the Ledger authenticity proof in the constructor
update(); // let's ask for N random bytes immediately when the contract is created!
}
// the callback function is called by Oraclize when the result is ready
// the oraclize_randomDS_proofVerify modifier prevents an invalid proof to execute this function code:
// the proof validity is fully verified on-chain
function __callback(bytes32 _queryId, string _result, bytes _proof)
{
// if we reach this point successfully, it means that the attached authenticity proof has passed!
if (msg.sender != oraclize_cbAddress()) throw;
if (oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) {
// the proof verification has failed, do we need to take any action here? (depends on the use case)
} else {
// the proof verification has passed
// now that we know that the random number was safely generated, let's use it..
newRandomNumber_bytes(bytes(_result)); // this is the resulting random number (bytes)
// for simplicity of use, let's also convert the random bytes to uint if we need
uint maxRange = 2**(8* 7); // this is the highest uint we want to get. It should never be greater than 2^(8*N), where N is the number of random bytes we had asked the datasource to return
uint randomNumber = uint(sha3(_result)) % maxRange; // this is an efficient way to get the uint out in the [0, maxRange] range
newRandomNumber_uint(randomNumber); // this is the resulting random number (uint)
}
}
function update() payable {
uint N = 7; // number of random bytes we want the datasource to return
uint delay = 0; // number of seconds to wait before the execution takes place
uint callbackGas = 200000; // amount of gas we want Oraclize to set for the callback function
bytes32 queryId = oraclize_newRandomDSQuery(delay, N, callbackGas); // this function internally generates the correct oraclize_query and returns its queryId
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment