Skip to content

Instantly share code, notes, and snippets.

@mikeyb
Created September 8, 2016 16:23
Show Gist options
  • Save mikeyb/4b66d5309c431cf159c7b58354942a10 to your computer and use it in GitHub Desktop.
Save mikeyb/4b66d5309c431cf159c7b58354942a10 to your computer and use it in GitHub Desktop.
contract owned {
address public owner;
function owned() {
owner = msg.sender;
}
modifier onlyOwner {
if (msg.sender != owner) throw;
_
}
function transferOwnership(address newOwner) onlyOwner {
owner = newOwner;
}
}
contract tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData); }
contract MyToken is owned {
/* Public variables of the token */
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
address public house;
uint256 public housemax;
uint32 public odds;
uint256 public sellprice;
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
mapping (address => mapping (address => uint256)) public spentAllowance;
/* This generates a public event on the blockchain that will notify clients */
event Transfer(address indexed from, address indexed to, uint256 value);
event Deposit(address _from, uint value);
/* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(
uint256 initialSupply,
string tokenName,
uint8 decimalUnits,
string tokenSymbol,
uint newHouseMax,
uint32 newOdds,
address newHouse,
uint32 SalePrice
) {
balanceOf[this] = 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
house = newHouse;
housemax = newHouseMax;
odds = newOdds;
sellprice = SalePrice;
}
function multiBlockRandomGen(uint seed, uint size) constant returns (uint randomNumber) {
uint n = 0;
for (uint i = 0; i < size; i++){
if (uint(sha3(block.blockhash(block.number-i-1), seed ))%2==0)
n += 2**i;
}
return n;
}
/* Send coins */
function transfer(address _to, uint256 _value) {
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
}
/* Allow another contract to spend some tokens in your behalf */
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
returns (bool success) {
allowance[msg.sender][_spender] = _value;
tokenRecipient spender = tokenRecipient(_spender);
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
/* A contract attempts to get the coins */
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
if (balanceOf[_from] < _value) throw; // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
if (spentAllowance[_from][msg.sender] + _value > allowance[_from][msg.sender]) throw; // Check allowance
balanceOf[_from] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
spentAllowance[_from][msg.sender] += _value;
Transfer(_from, _to, _value);
return true;
}
function setMax(uint256 newHouseMax) onlyOwner {
housemax = newHouseMax;
}
function setHouse(address newHouse) onlyOwner {
house = newHouse;
}
function setOdds(uint32 newOdds) onlyOwner {
if (newOdds <= 512) throw;
odds = newOdds;
}
function buy() returns (uint256 amount) {
uint256 amountOfTokens;
uint256 amountOfTokenfees;
amount = msg.value;
if (amount > housemax) throw;
sellprice = (this.balance / totalSupply);
uint rnd = multiBlockRandomGen(uint(sha3(block.number, now, amount)), 10);
if (rnd >= odds) {
amountOfTokens = (amount * 0) / 100000000000;
amountOfTokenfees = ((amount * 1) / 100000000000)/ 999;
}
if (rnd < odds) {
amountOfTokens = (amount * 2) / 100000000000;
}
balanceOf[msg.sender] += amountOfTokens;
balanceOf[house] += amountOfTokenfees;
balanceOf[this] -= amountOfTokens + amountOfTokenfees;
Transfer(this, msg.sender, amountOfTokens);
Transfer(this, house, amountOfTokenfees);
return amount;
}
function sell(uint256 amount) returns (uint revenue) {
sellprice = (this.balance / totalSupply);
if (balanceOf[msg.sender] < amount ) throw; // checks if the sender has enough to sell
balanceOf[this] += amount; // adds the amount to owner's balance
balanceOf[msg.sender] -= amount; // subtracts the amount from seller's balance
revenue = (amount / 100000000000) * sellprice; // calculate the revenue
msg.sender.send(revenue); // sends ether to the seller
Transfer(msg.sender, this, amount); // executes an event reflecting on the change
return revenue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment