contract CryptoGoldStandardCoin is MintableToken, BurnableToken {
  using SafeMath for uint;
  
  string public name = "CRYPTO GOLD STANDARD";
  string public symbol = "CGS";
  uint8 public decimals = 9;
  uint public INITIAL_SUPPLY = 100000000000;
  
  uint public sellPrice = 0; //the wei/cgs rate a user can sell
  uint public buyPrice = 2**256-1; //the wei/cgs rate a user can buy
  uint public priceExpirationBlockNumber = 0;
  
  function setPrices(uint newSellPrice, uint newBuyPrice, uint newPriceExpirationBlockNumber) onlyOwner public{
      require(newPriceExpirationBlockNumber > block.number);
      require(newSellPrice < newBuyPrice);
      sellPrice = newSellPrice;
      buyPrice = newBuyPrice;
      priceExpirationBlockNumber = newPriceExpirationBlockNumber;
  }
  
  function buy() payable public returns (uint amount){
    require(block.number <= priceExpirationBlockNumber);
    amount = msg.value / buyPrice;                    // calculates the amount
    require(amount>0);                               // check if amount is more than zero
    require(balances[this] >= amount);               // checks if it has enough to sell
    balances[msg.sender] += amount;                  // adds the amount to buyer's balance
    balances[this] -= amount;                        // subtracts amount from seller's balance
    Transfer(this, msg.sender, amount);               // execute an event reflecting the change
    return amount;                                    // ends function and returns
  }

  function sell(uint amount) public returns (uint revenue){
    require(block.number <= priceExpirationBlockNumber);
    require(balances[msg.sender] >= amount);         // checks if the sender has enough to sell
    balances[this] += amount;                        // adds the amount to owner's balance
    balances[msg.sender] -= amount;                  // subtracts the amount from seller's balance
    revenue = amount.mul(sellPrice);
    msg.sender.transfer(revenue);                     // sends ether to the seller: it's important to do this last to prevent recursion attacks
    Transfer(msg.sender, this, amount);               // executes an event reflecting on the change
    return revenue;                                   // ends function and returns
  }
  
  function forwardFunds(uint amount) onlyOwner public {
    owner.transfer(amount);
  }
  
  function forwardCoins(uint amount) onlyOwner public {
    require(balances[this] >= amount);         // checks if the sender has enough to sell
    balances[this] -= amount;                        // adds the amount to owner's balance
    balances[owner] += amount;                  // subtracts the amount from seller's balance
    Transfer(this, owner, amount);              // executes an event reflecting on the change
  }
  
  function CryptoGoldStandardCoin() public {
    totalSupply = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;
  }
  
  // fallback
  function() payable public {
      buy();
  }
}
function() payable public {
    buy();
}

// fallback