Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MT project #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
79 changes: 78 additions & 1 deletion contracts/Crowdsale.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,82 @@ import './Token.sol';
*/

contract Crowdsale {
// YOUR CODE HERE
// YOUR CODE HERE
Token public token;
address public owner;
uint public exchangeRate;

uint private startTime;
uint private endTime;
uint private funds;

Queue public queue;

modifier saleOn {
require(now >= startTime && now <= endTime);
_;
}

modifier ownerOnly {
require(msg.sender == owner);
_;
}

event TokenPurchase(address _from);
event TokenRefund(address _to);

function Crowdsale(uint _amount, uint _exchangeRate) {
owner = msg.sender;
exchangeRate = _exchangeRate;
token = new Token();
token.totalSupply = _amount;
}

function mint(uint amount) ownerOnly() {
token.totalSupply += amount;
}

function burn(uint amount) ownerOnly() {
if (token.totalSupply >= amount) {
token.totalSupply -= amount;
}
}

function receiveFunds() ownerOnly() {
if (now > endTime) {
owner.transfer(funds);
}
}

function buy(uint amount) public payable saleOn() {
require(!queue.empty() && queue.getFirst() == msg.sender);
if (amount <= msg.value * exchangeRate) {
tokensSold += amount;
bank += msg.value;

TokenPurchase(msg.sender);
queue.dequeue();
}
}

function refund(uint amount) public saleOn() {
tokensSold -= amount;
uint value = amount * exchangeRate
msg.sender.transfer(value)
bank -= value;
TokenRefund(msg.sender);
}

function addToQueue() public saleOn() returns (bool) {
if (queue.qsize() >= 5) {
return false;
}
queue.enqueue(msg.sender);
return true;
}

function public payable {
revert();
}

}
36 changes: 36 additions & 0 deletions contracts/Queue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,85 @@ contract Queue {
/* State variables */
uint8 size = 5;
// YOUR CODE HERE
uint timeLimit = 200;

struct Order {
address owner;
uint timestamp; // the timestamp the order becomes first in the queue
}

uint8 curNumber = 0;
Order[] storage orders;


/* Add events */
// YOUR CODE HERE

/* Add constructor */
// YOUR CODE HERE
function Queue() {
orders = new Order[size];
}

/* Returns the number of people waiting in line */
function qsize() constant returns(uint8) {
// YOUR CODE HERE
return curNumber;
}

/* Returns whether the queue is empty or not */
function empty() constant returns(bool) {
// YOUR CODE HERE
return curNumber == 0;
}

/* Returns the address of the person in the front of the queue */
function getFirst() constant returns(address) {
// YOUR CODE HERE
return orders[0].address;
}

/* Allows `msg.sender` to check their position in the queue */
function checkPlace() constant returns(uint8) {
// YOUR CODE HERE
for (uint8 i = 0; i < curNumber; i++) {
if (orders[i].address == msg.sender) {
return i;
}
}
return 5;
// TODO: what if not in the queue;
}

/* Allows anyone to expel the first person in line if their time
* limit is up
*/
function checkTime() {
// YOUR CODE HERE
if (now - orders[0].timestamp > timeLimit) {
dequeue();
}
}

/* Removes the first person in line; either when their time is up or when
* they are done with their purchase
*/
function dequeue() {
// YOUR CODE HERE
for (uint8 i = 0; i < curNumber-1; i++) {
orders[i] = orders[i+1];
}
orders[curNumber - 1] = address(0);
orders[0] = now;
curNumber --;
}

/* Places `addr` in the first empty position in the queue */
function enqueue(address addr) {
// YOUR CODE HERE
if (curNumber != size) {
orders[curNumber-1] = Order(addr, now);
curNumber ++;
}
}
}
66 changes: 66 additions & 0 deletions contracts/Token.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,70 @@ import './interfaces/ERC20Interface.sol';

contract Token is ERC20Interface {
// YOUR CODE HERE
mapping (address => uint256) balanceBook;
uint256 public totalSupply = 200;

address owner;

function Token() {
owner = msg.sender;
totalSupply = 10000;
}

function balanceOf(address _owner) constant returns (uint256 balance) {
return balanceBook[_owner];
}

function addTokens(uint amount) {
totalSupply += amount;
}

/// @notice send `_value` token to `_to` from `msg.sender`
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transfer(address _to, uint256 _value) returns (bool success) {
// check sender has enough money

if (balanceBook[msg.sender] < _value) return false;
if (balanceBook[_to] + _value < balanceBook[_to]) return false;
Transfer(msg.sender, _to, _value);
balanceBook[msg.sender] -= _value;
balanceBook[_to] += _value;
return true;
}

/// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
/// @param _from The address of the sender
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
emit Transfer(_from, _to, _value);
if (balanceBook[from] < value) return false;
if (balanceBook[_to] + _value < balanceBook[_to]) return false;
Transfer(_from, _to, _value);
balanceBook[_from] -= _value;
balanceBook[_to] += _value;
return true;
}

/// @notice `msg.sender` approves `_spender` to spend `_value` tokens
/// @param _spender The address of the account able to transfer the tokens
/// @param _value The amount of tokens to be approved for transfer
/// @return Whether the approval was successful or not
function approve(address _spender, uint256 _value) returns (bool success) {
require(_value > 0);
Approval(msg.sender, _spender, _value);
return true;
}

/// @param _owner The address of the account owning tokens
/// @param _spender The address of the account able to transfer the tokens
/// @return Amount of remaining tokens allowed to spent
function allowance(address _owner, address _spender) constant returns (uint256 remaining);

event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}