Created
September 2, 2018 09:59
-
-
Save rohitsethii/41441685c4a0ba2fd2bbe275759bc2f0 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.20+commit.3155dd80.js&optimize=true&gist=
This file contains hidden or 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 push { | |
uint[] public array; | |
function abc() returns (uint[]){ | |
array.push(123); | |
return array; | |
} | |
} |
This file contains hidden or 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 contest{ | |
address owner; | |
uint public tickets; | |
uint constant price=1 ether; | |
//this mapping creates an array which contains the buyers records | |
mapping(address => uint)public buyers; | |
function contest (uint t){ | |
owner=msg.sender; | |
tickets=t; | |
} | |
// function() payable{ | |
// buyTickets(1); | |
//} | |
event Transfer(address indexed from, uint256 value); | |
function buyTickets(uint amount) payable{ | |
if (msg.value != (amount * price) || amount > tickets){ | |
throw; | |
} | |
buyers[msg.sender] += amount; | |
tickets -= amount; | |
Transfer(msg.sender, amount); | |
if( tickets == 0){ | |
selfdestruct(owner); | |
} | |
} | |
// function refundable(uint numbertickets) returns (bool); | |
} | |
// interface refund{ | |
// function refundable(uint numbertickets) returns (bool); | |
//} | |
/*contract refunding is contest(20){ | |
uint public _Data; | |
address y; | |
function set(uint x) //setter to set a value | |
{ | |
_Data = x; | |
} | |
function get() constant returns (uint) //getter to get(display only) some value | |
{ | |
return _Data; | |
} | |
function refundable(uint numbertickets) returns (bool){ | |
if(buyers[msg.sender] < numbertickets ){ | |
return false; | |
} | |
msg.sender.transfer(numbertickets * price); | |
buyers[msg.sender] -= numbertickets; | |
tickets += numbertickets; | |
return true; | |
} | |
}*/ |
This file contains hidden or 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 'browser/library.sol'; | |
contract Uselibrary{ | |
LibraryData.data admins; | |
event success(); | |
modifier onlyadmins(){ | |
require(admins.members[msg.sender]); | |
_; | |
} | |
function Uselibrary(){ | |
LibraryData.addmember(admins,msg.sender); | |
} | |
function add(address addr) onlyadmins(){ | |
if(LibraryData.addmember(admins,addr)){ | |
} | |
success(); | |
} | |
function remove(address addr) onlyadmins(){ | |
if(LibraryData.delmember(admins,addr)){ | |
} | |
success(); | |
} | |
} |
This file contains hidden or 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.17; | |
contract Lottery { | |
address[] private players; | |
address public owner; | |
address public winner; | |
modifier Notowner() { | |
require(msg.sender != owner); | |
_; | |
} | |
modifier isowner() { | |
require(msg.sender == owner); | |
_; | |
} | |
modifier NotinList() { | |
for (uint i=0; i < players.length; i++) { | |
require(msg.sender != players[0]); | |
} | |
_; | |
} | |
modifier validValue() { | |
require(msg.value == 1 ether); | |
_; | |
} | |
modifier isAnyPlayers() { | |
require(players.length > 0); | |
_; | |
} | |
function Lottery() public { | |
owner = msg.sender; | |
} | |
function enter() public Notowner NotinList validValue payable { | |
players.push(msg.sender); | |
} | |
function pickWinner() public isowner isAnyPlayers payable returns(address) { | |
uint index = random(); | |
uint share = (this.balance) * 3 / 100; | |
winner = players[index]; | |
players[index].transfer(this.balance - share); // and the winner get the whole rest of the money | |
owner.transfer(share);// owner will get 0.003% of the whole prize pool | |
players = new address[](0); | |
return winner; | |
} | |
function entryPlayers() public view returns(address[]) { | |
return players; | |
} | |
function random() public view returns(uint) { | |
return uint(keccak256(block.difficulty, now, players)) % players.length; | |
} | |
} |
This file contains hidden or 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; | |
library LibraryData { | |
struct data{ | |
mapping (address => bool) members; | |
} | |
function addmember(data storage self,address addr) returns (bool){ | |
if(self.members[addr]){ | |
return false; | |
} | |
self.members[addr]=true; | |
return false; | |
} | |
function delmember(data storage self,address addr) returns (bool){ | |
if(self.members[addr]){ | |
return false; | |
} | |
self.members[addr]=false; | |
return true; | |
} | |
} |
This file contains hidden or 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.17; | |
contract Lottery { | |
address public manager; | |
address public winner; | |
address[] private players; | |
modifier isNotManager() { | |
require(msg.sender != manager); | |
_; | |
} | |
modifier isManager() { | |
require(msg.sender == manager); | |
_; | |
} | |
modifier isNotInList() { | |
for (uint i=0; i < players.length; i++) { | |
require(msg.sender != players[0]); | |
} | |
_; | |
} | |
modifier validValue() { | |
require(msg.value == .01 ether); | |
_; | |
} | |
modifier isAnyPlayers() { | |
require(players.length > 0); | |
_; | |
} | |
function Lottery() public { | |
manager = msg.sender; | |
} | |
function enter() public isNotManager isNotInList validValue payable { | |
players.push(msg.sender); | |
} | |
function pickWinner() public isManager isAnyPlayers payable returns(address) { | |
uint index = random(); | |
uint share = (this.balance) * 3 / 100; | |
winner = players[index]; | |
players[index].transfer(this.balance - share); // and the winner get the whole rest of the money | |
manager.transfer(share);// manager will get 0.003% of the whole prize pool | |
players = new address[](0); | |
return winner; | |
} | |
function entryPlayers() public view returns(address[]) { | |
return players; | |
} | |
function random() private view returns(uint) { | |
return uint(keccak256(block.difficulty, now, players)) % players.length; | |
} | |
} |
This file contains hidden or 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; | |
/** | |
* @title Math | |
* @dev Assorted math operations | |
*/ | |
library Math { | |
function max64(uint64 a, uint64 b) internal constant returns (uint64) { | |
return a >= b ? a : b; | |
} | |
function min64(uint64 a, uint64 b) internal constant returns (uint64) { | |
return a < b ? a : b; | |
} | |
function max256(uint256 a, uint256 b) internal constant returns (uint256) { | |
return a >= b ? a : b; | |
} | |
function min256(uint256 a, uint256 b) internal constant returns (uint256) { | |
return a < b ? a : b; | |
} | |
} |
This file contains hidden or 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.11; | |
/*Make a smart contract in which 2 people will be able to bet on the weather conditions | |
in next one hour. For | |
weather conditions Temperature as a parameter would suffice. So 2 people will make a bet | |
on the | |
Temperature, one would bet on the higher side and one would bet on the lower side | |
i.e. one will win if | |
temperature exceeds the decided value and other will win if temperature is below the | |
decided value. In | |
case the temperature is exactly equal to the value then both will get their ethers back. | |
///Both will make the bets on the temperature of same city – assumption 1 | |
///The Temperature to bet upon can be hardcoded in the smart contract itself. | |
QUESTION 2: | |
Execute the function that will determine the Winning or Losing or Draw by calling from nodejs API | |
endpoint using web3 library. In that pass the temperature by using ANY weather api of your choice. | |
BONUS – Use oraclize to get the data from google or any weather api in the smart contract itself. | |
*/ | |
contract Weather{ | |
address admin; | |
//address winner; | |
uint Temperature; | |
uint public duration; | |
uint deadline; | |
/*-----------------------modifiers------------------------------*/ | |
modifier adminOnly() { | |
if (msg.sender == admin) { _; } | |
else { throw; } | |
} | |
modifier Deadline() { | |
if (now <= deadline) { _; } | |
else { throw; } | |
} | |
/*-----------------------events------------------------------*/ | |
//event Transfer(address indexed from, uint256 value); | |
function SetTemperature(uint x) Deadline { //setter to set a value | |
Temperature = x; | |
} | |
function get() constant returns (uint){ //getter to get(display only) some value | |
return Temperature; | |
} | |
// function LowerSide() payable{ | |
// if(Temperature >) | |
//} | |
} |
This file contains hidden or 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.8; | |
contract Lottery { | |
mapping (uint8 => address[]) playersByNumber ; | |
mapping (address => bytes32) playersHash; | |
uint8[] public numbers; | |
address owner; | |
function Lottery() public { | |
owner = msg.sender; | |
state = LotteryState.FirstRound; | |
} | |
enum LotteryState { FirstRound, SecondRound, Finished } | |
LotteryState state; | |
function enterHash(bytes32 x) public payable { | |
require(state == LotteryState.FirstRound); | |
require(msg.value > .001 ether); | |
playersHash[msg.sender] = x; | |
} | |
function runSecondRound() public { | |
require(msg.sender == owner); | |
require(state == LotteryState.FirstRound); | |
state = LotteryState.SecondRound; | |
} | |
function enterNumber(uint8 number) public { | |
require(number<=250); | |
require(state == LotteryState.SecondRound); | |
require(keccak256(number, msg.sender) == playersHash[msg.sender]); | |
playersByNumber[number].push(msg.sender); | |
numbers.push(number); | |
} | |
function determineWinner() public { | |
require(msg.sender == owner); | |
state = LotteryState.Finished; | |
uint8 winningNumber = random(); | |
distributeFunds(winningNumber); | |
selfdestruct(owner); | |
} | |
function distributeFunds(uint8 winningNumber) private returns(uint256) { | |
uint256 winnerCount = playersByNumber[winningNumber].length; | |
require(winnerCount == 1); | |
if (winnerCount > 0) { | |
uint256 balanceToDistribute = this.balance/(2*winnerCount); | |
for (uint i = 0; i<winnerCount; i++) { | |
require(i==0); | |
playersByNumber[winningNumber][i].transfer(balanceToDistribute); | |
} | |
} | |
return this.balance; | |
} | |
function random() private view returns (uint8) { | |
uint8 randomNumber = numbers[0]; | |
for (uint8 i = 1; i < numbers.length; ++i) { | |
randomNumber ^= numbers[i]; | |
} | |
return randomNumber; | |
} | |
} |
This file contains hidden or 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.8; | |
contract MyToken { | |
/* Public variables of the token */ | |
//string public standard = 'Token 0.1'; | |
//string public name="Rose"; | |
//string public symbol="ROS"; | |
//uint8 public decimals=18; | |
uint256 public totalSupply; | |
/* This creates an array with all balances */ | |
mapping (address => uint256) public balanceOf; | |
mapping (address => mapping (address => uint256)) public allowance; | |
/* This generates a public event on the blockchain that will notify clients */ | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
/* This notifies clients about the amount burnt */ | |
//event Burn(address indexed from, uint256 value); | |
/* Initializes contract with initial supply tokens to the creator of the contract */ | |
function MyToken ( | |
/*uint256 initialSupply, | |
string tokenName, | |
uint8 decimalUnits, | |
string tokenSymbol | |
*/) { | |
uint256 initialSupply=1000000 * 1 ether; | |
//string tokenName; | |
// uint8 decimalUnits; | |
// string tokenSymbol; | |
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens | |
totalSupply = initialSupply; // Update total supply | |
// name = tokenName; // Set the name for display purposes | |
//symbol = tokenSymbol; // Set the symbol for display purposes | |
//decimals = decimalUnits; // Amount of decimals for display purposes | |
} | |
/* Send coins */ | |
function transfer(address _to, uint256 _value) { | |
if (_to == 0x0) throw; // Prevent transfer to 0x0 address. Use burn() instead | |
if (balanceOf[msg.sender] < _value) throw; // Check if the sender has enough | |
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows | |
balanceOf[msg.sender] -= _value; // Subtract from the sender | |
balanceOf[_to] += _value; // Add the same to the recipient | |
Transfer(msg.sender, _to, _value); // Notify anyone listening that this transfer took place | |
} | |
} |
This file contains hidden or 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; | |
/* | |
* | |
* @title SafeMath | |
* @dev Math operations with safety checks that throw on error | |
*/ | |
library SafeMath { | |
function mul(uint256 a, uint256 b) internal constant returns (uint256) { | |
uint256 c = a * b; | |
assert(a == 0 || c / a == b); | |
return c; | |
} | |
function div(uint256 a, uint256 b) internal constant 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 c; | |
} | |
function sub(uint256 a, uint256 b) internal constant returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal constant returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} |
This file contains hidden or 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.5; | |
contract LotteryEventDefinitions { | |
event UserRegistrationEvent(address adr); | |
event LotteryEnd(address winner); | |
event UserPutBets(address user, uint currentPot); | |
event WinnerTookItAll(string msg); | |
event WinnerFailedToTakeWin(string msg); | |
event SomeGuyTriedToTakeTheWinnings(string msg, address someGuy); | |
//event GameNotOverYet(string msg); | |
// event ThePotIsEmpty(string msg); | |
event NobodyPlayedWithdrewInitialAmount(string msg); | |
} |
This file contains hidden or 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.4; | |
import "browser/sha3.sol"; | |
contract main{ | |
address contractaddress=address(0x692a70d2e424a56d2c6c27aa97d1a86395877b3a); | |
address[] public participants; | |
bool gameClosed; | |
address winner; | |
event LotteryEnd(address winner); | |
function main() {//adminOnly { | |
if (participants.length == 0) { | |
gameClosed = true; | |
return; | |
} | |
// not very good random, but better than nothing at the moment. | |
uint randWinAddr = uint(block.blockhash(block.number - 1)) % participants.length; | |
winner = participants[randWinAddr]; | |
gameClosed = true; | |
LotteryEnd(winner); | |
} | |
} | |
contract Lottery is main{ | |
address admin; | |
address winner; | |
uint n; | |
uint pot; | |
bool gameClosed; | |
address[] public participants; | |
uint256 public totalSupply; | |
uint tokens; | |
/* This creates an array with all balances */ | |
uint constant price=1 ether; | |
//this mapping creates an array which contains the buyers records | |
mapping(address => uint)public buyers; | |
function contest(){ | |
admin=msg.sender; | |
tokens=1000000; | |
} | |
// function() payable{ | |
// buyTickets(1); | |
//} | |
event Transfer(address indexed from, uint256 value); | |
function buyTickets(uint amount) payable{ | |
if (msg.value != (amount * price) || amount > tokens){ | |
throw; | |
} | |
buyers[msg.sender] += amount; | |
tokens -= amount; | |
Transfer(msg.sender, amount); | |
if( tokens == 0){ | |
//selfdestruct(admin); | |
}} | |
modifier adminOnly() { | |
if (msg.sender == admin) { _; } | |
else { throw; } | |
} | |
modifier sufficientFunds() { | |
if (msg.value < n * 1 ether) { throw; } | |
else { _; } | |
} | |
modifier gameOngoing() { | |
if (gameClosed) { throw; } | |
else { _; } | |
} | |
modifier afterGameClosed() { | |
// check if the deadline was already reached. | |
if (gameClosed) { _; } | |
else { | |
//GameNotOverYet("The game lottery was not ended yet. Only the admin can end the lottery."); | |
throw; | |
} | |
} | |
modifier winningConstraints() { | |
if (pot != 0 && participants.length > 0) { _; } | |
else { | |
//ThePotIsEmpty("Nothing to withdraw!"); | |
throw; | |
} | |
} | |
modifier winnerTookPot() { | |
if (pot == 0) { _; } | |
else { throw; } | |
} | |
function Makeguess(uint Guess) sufficientFunds gameOngoing returns(bool) { | |
participants.push(msg.sender); | |
} | |
// UserPutBets(msg.sender, pot);} | |
/*function Lottery1() {//adminOnly { | |
if (participants.length == 0) { | |
gameClosed = true; | |
return; | |
} | |
// not very good random, but better than nothing at the moment. | |
uint randWinAddr = uint(block.blockhash(block.number - 1)) % participants.length; | |
winner = participants[randWinAddr]; | |
gameClosed = true; | |
// LotteryEnd(winner); | |
}*/ | |
} |
This file contains hidden or 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 'browser/ERC20Basic.sol'; | |
import 'browser/SafeMath.sol'; | |
/* | |
* @title Basic token | |
* @dev Basic version of StandardToken, with no allowances. | |
*/ | |
contract BasicToken is ERC20Basic { | |
using SafeMath for uint256; | |
mapping(address => uint256) balances; | |
/* | |
* @dev transfer token for a specified address | |
* @param _to The address to transfer to. | |
* @param _value The amount to be transferred. | |
*/ | |
function transfer(address _to, uint256 _value) returns (bool) { | |
balances[msg.sender] = balances[msg.sender].sub(_value); | |
balances[_to] = balances[_to].add(_value); | |
Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
/* | |
* @dev Gets the balance of the specified address. | |
* @param _owner The address to query the the balance of. | |
* @return An uint256 representing the amount owned by the passed address. | |
*/ | |
function balanceOf(address _owner) constant returns (uint256 balance) { | |
return balances[_owner]; | |
} | |
} |
This file contains hidden or 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 tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; } | |
contract TokenERC20 { | |
// Public variables of the token | |
string public name; | |
string public symbol; | |
uint8 public decimals = 18; | |
// 18 decimals is the strongly suggested default, avoid changing it | |
uint256 public totalSupply; | |
// This creates an array with all balances | |
mapping (address => uint256) public balanceOf; | |
mapping (address => mapping (address => uint256)) public allowance; | |
// This generates a public event on the blockchain that will notify clients | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
// This notifies clients about the amount burnt | |
event Burn(address indexed from, uint256 value); | |
/** | |
* Constructor function | |
* | |
* Initializes contract with initial supply tokens to the creator of the contract | |
*/ | |
function TokenERC20( | |
uint256 initialSupply, | |
string tokenName, | |
string tokenSymbol | |
) public { | |
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount | |
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens | |
name = tokenName; // Set the name for display purposes | |
symbol = tokenSymbol; // Set the symbol for display purposes | |
} | |
/** | |
* Internal transfer, only can be called by this contract | |
*/ | |
function _transfer(address _from, address _to, uint _value) internal { | |
// Prevent transfer to 0x0 address. Use burn() instead | |
require(_to != 0x0); | |
// Check if the sender has enough | |
require(balanceOf[_from] >= _value); | |
// Check for overflows | |
require(balanceOf[_to] + _value >= balanceOf[_to]); | |
// Save this for an assertion in the future | |
uint previousBalances = balanceOf[_from] + balanceOf[_to]; | |
// Subtract from the sender | |
balanceOf[_from] -= _value; | |
// Add the same to the recipient | |
balanceOf[_to] += _value; | |
//emit | |
Transfer(_from, _to, _value); | |
// Asserts are used to use static analysis to find bugs in your code. They should never fail | |
assert(balanceOf[_from] + balanceOf[_to] == previousBalances); | |
} | |
/** | |
* Transfer tokens | |
* | |
* Send `_value` tokens to `_to` from your account | |
* | |
* @param _to The address of the recipient | |
* @param _value the amount to send | |
*/ | |
function transfer(address _to, uint256 _value) public { | |
_transfer(msg.sender, _to, _value); | |
} | |
/** | |
* Transfer tokens from other address | |
* | |
* Send `_value` tokens to `_to` on behalf of `_from` | |
* | |
* @param _from The address of the sender | |
* @param _to The address of the recipient | |
* @param _value the amount to send | |
*/ | |
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { | |
require(_value <= allowance[_from][msg.sender]); // Check allowance | |
allowance[_from][msg.sender] -= _value; | |
_transfer(_from, _to, _value); | |
return true; | |
} | |
/** | |
* Set allowance for other address | |
* | |
* Allows `_spender` to spend no more than `_value` tokens on your behalf | |
* | |
* @param _spender The address authorized to spend | |
* @param _value the max amount they can spend | |
*/ | |
function approve(address _spender, uint256 _value) public | |
returns (bool success) { | |
allowance[msg.sender][_spender] = _value; | |
return true; | |
} | |
/** | |
* Set allowance for other address and notify | |
* | |
* Allows `_spender` to spend no more than `_value` tokens on your behalf, and then ping the contract about it | |
* | |
* @param _spender The address authorized to spend | |
* @param _value the max amount they can spend | |
* @param _extraData some extra information to send to the approved contract | |
*/ | |
function approveAndCall(address _spender, uint256 _value, bytes _extraData) | |
public | |
returns (bool success) { | |
tokenRecipient spender = tokenRecipient(_spender); | |
if (approve(_spender, _value)) { | |
spender.receiveApproval(msg.sender, _value, this, _extraData); | |
return true; | |
} | |
} | |
/** | |
* Destroy tokens | |
* | |
* Remove `_value` tokens from the system irreversibly | |
* | |
* @param _value the amount of money to burn | |
*/ | |
function burn(uint256 _value) public returns (bool success) { | |
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough | |
balanceOf[msg.sender] -= _value; // Subtract from the sender | |
totalSupply -= _value; // Updates totalSupply | |
//emit | |
Burn(msg.sender, _value); | |
return true; | |
} | |
/** | |
* Destroy tokens from other account | |
* | |
* Remove `_value` tokens from the system irreversibly on behalf of `_from`. | |
* | |
* @param _from the address of the sender | |
* @param _value the amount of money to burn | |
*/ | |
function burnFrom(address _from, uint256 _value) public returns (bool success) { | |
require(balanceOf[_from] >= _value); // Check if the targeted balance is enough | |
require(_value <= allowance[_from][msg.sender]); // Check allowance | |
balanceOf[_from] -= _value; // Subtract from the targeted balance | |
allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance | |
totalSupply -= _value; // Update totalSupply | |
//emit | |
Burn(_from, _value); | |
return true; | |
} | |
} |
This file contains hidden or 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; | |
library Search { | |
function indexOf(uint[] storage self, uint value) returns (uint) { | |
for (uint i = 0; i < self.length; i++) | |
if (self[i] == value) return i; | |
return uint(-1); | |
} | |
} | |
contract C { | |
using Search for uint[]; | |
uint[] data; | |
function append(uint value) { | |
data.push(value); | |
} | |
function replace(uint _old, uint _new) { | |
// This performs the library function call | |
uint index = data.indexOf(_old); | |
if (index == uint(-1)) | |
data.push(_new); | |
else | |
data[index] = _new; | |
} | |
} |
This file contains hidden or 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.6; | |
contract Voting { | |
/* mapping is equivalent to an associate array or hash*/ | |
mapping (bytes32 => uint8) public votesReceived; | |
/* Solidity doesn't let you create an array of strings yet. We will use an array of bytes32 instead to store | |
the list of candidates | |
*/ | |
bytes32[] public candidateList; | |
// Initialize all the contestants | |
function Voting(bytes32[] candidateNames) { | |
candidateList = candidateNames; | |
} | |
function totalVotesFor(bytes32 candidate) returns (uint8) { | |
require(validCandidate(candidate)); | |
return votesReceived[candidate]; | |
} | |
function voteForCandidate(bytes32 candidate) { | |
require(validCandidate(candidate)); | |
votesReceived[candidate] += 1; | |
} | |
function validCandidate(bytes32 candidate) returns (bool) { | |
for(uint i = 0; i < candidateList.length; i++) { | |
if (candidateList[i] == candidate) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains hidden or 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 EndowmentRetriever { | |
address creator; | |
uint contract_creation_value; // original endowment | |
function EndowmentRetriever() public | |
{ | |
creator = msg.sender; | |
contract_creation_value = msg.value; // the endowment of this contract in wei | |
} | |
function getContractCreationValue() constant returns (uint) // returns the original endowment of the contract | |
{ // set at creation time with "value: <someweivalue>" | |
return contract_creation_value; // this was the "balance" of the contract at creation time | |
} | |
function sendOneEtherHome() public | |
{ | |
creator.send(1000000000000000000); // send 1 ETH home | |
} | |
function kill() | |
{ | |
if (msg.sender == creator) | |
{ | |
suicide(creator); // kills this contract and sends remaining funds back to creator | |
} | |
} | |
} |
This file contains hidden or 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 MultiSigWallet { | |
address private _owner; | |
mapping(address => uint8) private _owners; | |
uint constant MIN_SIGNATURES = 2; | |
uint private _transactionIdx; | |
struct Transaction { | |
address from; | |
address to; | |
uint amount; | |
uint8 signatureCount; | |
mapping (address => uint8) signatures; | |
} | |
mapping (uint => Transaction) private _transactions; | |
uint[] private _pendingTransactions; | |
modifier isOwner() { | |
require(msg.sender == _owner); | |
_; | |
} | |
modifier validOwner() { | |
require(msg.sender == _owner || _owners[msg.sender] == 1); | |
_; | |
} | |
event DepositFunds(address from, uint amount); | |
event TransactionCreated(address from, address to, uint amount, uint transactionId); | |
event TransactionCompleted(address from, address to, uint amount, uint transactionId); | |
event TransactionSigned(address by, uint transactionId); | |
function MultiSigWallet() | |
public { | |
_owner = msg.sender; | |
} | |
function addOwner(address owner) | |
isOwner | |
public { | |
_owners[owner] = 1; | |
} | |
function removeOwner(address owner) | |
isOwner | |
public { | |
_owners[owner] = 0; | |
} | |
function () | |
public | |
payable { | |
DepositFunds(msg.sender, msg.value); | |
} | |
function withdraw(uint amount) | |
public { | |
transferTo(msg.sender, amount); | |
} | |
function transferTo(address to, uint amount) | |
validOwner | |
public { | |
require(address(this).balance >= amount); | |
uint transactionId = _transactionIdx++; | |
Transaction memory transaction; | |
transaction.from = msg.sender; | |
transaction.to = to; | |
transaction.amount = amount; | |
transaction.signatureCount = 0; | |
_transactions[transactionId] = transaction; | |
_pendingTransactions.push(transactionId); | |
TransactionCreated(msg.sender, to, amount, transactionId); | |
} | |
function getPendingTransactions() | |
view | |
validOwner | |
public | |
returns (uint[]) { | |
return _pendingTransactions; | |
} | |
function signTransaction(uint transactionId) | |
validOwner | |
public { | |
Transaction storage transaction = _transactions[transactionId]; | |
// Transaction must exist | |
require(0x0 != transaction.from); | |
// Creator cannot sign the transaction | |
require(msg.sender != transaction.from); | |
// Cannot sign a transaction more than once | |
require(transaction.signatures[msg.sender] != 1); | |
transaction.signatures[msg.sender] = 1; | |
transaction.signatureCount++; | |
TransactionSigned(msg.sender, transactionId); | |
if (transaction.signatureCount >= MIN_SIGNATURES) { | |
require(address(this).balance >= transaction.amount); | |
transaction.to.transfer(transaction.amount); | |
TransactionCompleted(transaction.from, transaction.to, transaction.amount, transactionId); | |
deleteTransaction(transactionId); | |
} | |
} | |
function deleteTransaction(uint transactionId) | |
validOwner | |
public { | |
uint8 replace = 0; | |
for(uint i = 0; i < _pendingTransactions.length; i++) { | |
if (1 == replace) { | |
_pendingTransactions[i-1] = _pendingTransactions[i]; | |
} else if (transactionId == _pendingTransactions[i]) { | |
replace = 1; | |
} | |
} | |
delete _pendingTransactions[_pendingTransactions.length - 1]; | |
_pendingTransactions.length--; | |
delete _transactions[transactionId]; | |
} | |
function walletBalance() | |
constant | |
public | |
returns (uint) { | |
return address(this).balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment