Skip to content

Instantly share code, notes, and snippets.

@czepluch
Created January 3, 2017 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czepluch/7274555780fac84fb512f43e42a8b063 to your computer and use it in GitHub Desktop.
Save czepluch/7274555780fac84fb512f43e42a8b063 to your computer and use it in GitHub Desktop.
contract Token {
address public owner;
uint public total_supply;
uint price;
mapping (address => uint) public balances;
mapping (address => Offer) public offers;
string public 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 Transfer(uint amount, address receiver);
event OfferAdded(address creator, uint number_of_tokens, uint total_price);
event Minted(uint amount);
event OfferTaken(address taker, uint price);
function Token(uint initial_supply, string _name, uint _price) {
total_supply = initial_supply;
balances[msg.sender] = initial_supply;
owner = msg.sender;
name = _name;
price = _price;
}
function transfer(address receiver, uint amount) hasBalance(amount) {
// TODO fix overflow
balances[msg.sender] -= amount;
balances[receiver] += amount;
Transfer(amount, receiver);
}
function getBalance() constant returns (uint) {
return balances[msg.sender];
}
function mint(uint amount) onlyOwner {
// Check for overflow
if (balances[msg.sender] + amount < balances[msg.sender]) throw;
total_supply = total_supply + amount;
balances[msg.sender] += amount;
Minted(amount);
}
function getTotalSupply() constant returns (uint) {
return total_supply;
}
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 != offer.total_price) throw;
if (balances[supplier] < offer.number_of_tokens) throw;
if (supplier.send(msg.value) == false) throw;
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