-
-
Save KristofferTrustlines/cc4f3cdc55fa97aa4f13592de0837085 to your computer and use it in GitHub Desktop.
This file contains 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
//ToDo: | |
//format the code: | |
//epochlimit instead of timelimit | |
//kommentare einfuegen | |
//--------------------------------- | |
// creates an amount of tokens only allowed to transfer a certain amount of tokens per defined epoch (3 minutes) | |
contract Token { | |
address owner; ///Ethereum address who owns the contract | |
uint total_supply; ///the total supply of tokens created for the contract | |
uint price; /// price in Ether for the tokens created | |
uint limit; /// | |
uint timelimit; /// the | |
mapping (address => uint) balances; | |
mapping (address => Offer) offers; | |
mapping (address => uint) amounttrans; | |
mapping (address => uint) firsttime; | |
string name; | |
struct Offer | |
{ | |
uint total_price; | |
uint number_of_tokens; | |
} | |
modifier hasBalance(uint amount) { | |
if (balances[msg.sender] < amount) throw; | |
_; | |
} | |
modifier onlyOwner() { | |
if (msg.sender != owner) throw; | |
_; | |
} | |
event Transfered(uint amount, address receiver); | |
event OfferAdded(address creator, uint number_of_tokens, uint total_price); | |
event Minted(uint amount); | |
event OfferTaken(address, uint price); | |
event LimitBreached(address, uint amount, uint limit); | |
function Token (uint initial_supply, string _name, uint _price, uint _limit, uint _timelimit) { | |
total_supply = initial_supply; | |
balances[msg.sender] = initial_supply; | |
name = _name; | |
price = _price; | |
limit = _limit; | |
timelimit = _timelimit * 1 minutes; | |
} | |
function transfer(address receiver, uint amount) hasBalance(amount) returns (bool success) { | |
if (firsttime[msg.sender]!= 0 && now - firsttime[msg.sender] > timelimit) { | |
firsttime[msg.sender]=0; | |
amounttrans[msg.sender]=0; | |
} | |
if (firsttime[msg.sender]==0 ) firsttime[msg.sender]=now; | |
if (amounttrans[msg.sender] + amount > limit) return false; | |
balances[msg.sender] -= amount; | |
balances[receiver] += amount; | |
Transfered(amount, receiver); | |
amounttrans[msg.sender] += amount; | |
return true; | |
//enables access from java front-end, like mist, via RPC API. even works offline | |
} | |
function getBalance() constant returns (uint){ | |
return balances[msg.sender]; | |
} | |
function mint(uint amount) onlyOwner { | |
if (balances[msg.sender] + amount < balances[msg.sender]) throw; | |
//if (2 ** 256 - total_supply <= amount) throw; | |
//check for overflow | |
total_supply = total_supply + amount; | |
balances[msg.sender] += amount; | |
} | |
function getTotalSupply() constant returns (uint) { | |
return total_supply; | |
} | |
function() payable { | |
} | |
function addOffer(uint number_of_tokens, uint total_price) { | |
offers[msg.sender] = Offer({number_of_tokens: number_of_tokens, total_price: total_price}); | |
OfferAdded(msg.sender, number_of_tokens, total_price); | |
} | |
function takeOffer(address supplier) payable { | |
Offer offer = offers[supplier]; | |
if (msg.value != offers[supplier].total_price) throw; | |
if (balances[supplier] < offers[supplier].number_of_tokens) throw; | |
if (!supplier.send(msg.value)) throw; | |
supplier.send(msg.value); | |
balances[supplier] -= offer.number_of_tokens; | |
balances[msg.sender] += offer.number_of_tokens; | |
offers[supplier] = Offer({number_of_tokens: 0, total_price: 0}); | |
OfferTaken(msg.sender, msg.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment