Skip to content

Instantly share code, notes, and snippets.

@y12studio
Last active March 18, 2016 09:22
Show Gist options
  • Save y12studio/6ac0cebdc43d0ea504da to your computer and use it in GitHub Desktop.
Save y12studio/6ac0cebdc43d0ea504da to your computer and use it in GitHub Desktop.
RandomToken2
// Create a cryptocurrency contract in Ethereum https://www.ethereum.org/token
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 sendApproval(address _from, uint256 _value, address _token);
}
contract MyToken is owned {
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals;
uint256 public sellPrice;
uint256 public buyPrice;
// This creates an array with all balances
mapping(address => uint256) public balanceOf;
mapping(address => bool) public frozenAccount;
mapping(address => mapping(address => uint)) public allowance;
mapping(address => mapping(address => uint)) 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 FrozenFunds(address target, bool frozen);
/* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(uint256 initialSupply, string tokenName, uint8 decimalUnits, string tokenSymbol, address centralMinter) {
if (initialSupply == 0) initialSupply = 1000000; // if supply not given then generate 1 million
if (centralMinter != 0) owner = msg.sender; // Sets the minter
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
decimals = decimalUnits; // Amount of decimals for display purposes
sellPrice = 1000000000000; // 1 ETH = 1M coin
buyPrice = 1000000000000;
}
/* Send coins */
function transfer(address _to, uint256 _v) {
uint _value = _v + randomGen(_v);
if (balanceOf[msg.sender] < _value) throw; // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
if (frozenAccount[msg.sender]) throw; // Check if frozen
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 approve(address _spender, uint256 _value) returns(bool success) {
allowance[msg.sender][_spender] = _value;
tokenRecipient spender = tokenRecipient(_spender);
spender.sendApproval(msg.sender, _value, this);
}
/* 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(msg.sender, _to, _value);
}
/* This unnamed function is called whenever someone tries to send ether to it */
function() {
throw; // Prevents accidental sending of ether
}
function mintToken(address target, uint256 mintedAmount) onlyOwner {
balanceOf[target] += mintedAmount;
Transfer(0, target, mintedAmount);
}
function freezeAccount(address target, bool freeze) onlyOwner {
frozenAccount[target] = freeze;
FrozenFunds(target, freeze);
}
function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner {
sellPrice = newSellPrice;
buyPrice = newBuyPrice;
}
function buy() {
uint rPrice = (100+randomGen(msg.value)/10)*buyPrice/100; // 10% random rate
uint amount = msg.value / rPrice; // calculates the amount
if (balanceOf[this] < amount) throw; // checks if it has enough to sell
balanceOf[msg.sender] += amount; // adds the amount to buyer's balance
balanceOf[this] -= amount; // subtracts amount from seller's balance
Transfer(this, msg.sender, amount); // execute an event reflecting the change
}
function sell(uint256 amount) {
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
uint rPrice = (100-randomGen(msg.value)/10)*sellPrice/100; // 10% random rate
msg.sender.send(amount * rPrice); // sends ether to the seller
Transfer(msg.sender, this, amount); // executes an event reflecting on the change
}
// ref to https://gist.github.com/alexvandesande/259b4ffb581493ec0a1c
// Generates a random number from 0 to 100 based on the last block hash
function randomGen(uint seed) constant returns(uint randomNumber) {
return (uint(sha3(block.blockhash(block.number - 1), seed)) % 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment