# How to limit the total supply for an ICO contract

I am trying to create a test ICO contract (simply for the purpose of learning about blockchain etc). I have followed a tutorial and produced the following contract. The total tokens supplied to the sender however seem to be limitless. How do I set it so supplied tokens are deducted from the total supply?

``````pragma solidity ^0.4.11;

import './IERC20.sol';
import './SafeMath.sol';

contract testy2 is IERC20 {

using SafeMath for uint256;

uint public _totalSupply = 1000000000000000000000000;

string public constant symbol = "TSTY2";
string public constant name = "testy2";
uint8 public constant decimals = 18;

// 1 ether = 250000 TSTY2
uint256 public constant RATE = 250000000000000000000000;

function() payable {
createTokens();
}

function testy2() {
owner = msg.sender;
balances[msg.sender] = 500000000000000000000000;
}

function createTokens() payable {
require(msg.value > 0);

uint256 tokens = msg.value.mul(RATE);

owner.transfer(msg.value);
}

function totalSupply() constant returns (uint256 totalSupply) {
return _totalSupply;
}

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

function transfer(address _to, uint256 _value) returns (bool success) {
require(
balances[msg.sender] >= _value
&& _value > 0
);
balances[msg.sender] = balances[msg.sender].sub(_value);
Transfer(msg.sender, _to, _value);
return true;
}

require(
allowed[_from][msg.sender] >= _value
&& balances[_from] >= _value
&& _value > 0
);
balances[_from] = balances[_from].sub(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
Transfer(_from, _to, _value);
return true;
}

function approve(address _spender, uint256 _value) returns (bool success) {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}

return allowed[_owner][_spender];
}

``````

First, create a token with an initial supply. Then assign the initial supply to the total supply.

``````pragma solidity ^0.4.18;

import "./StandardToken.sol";

/**
* @title SimpleToken
* @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
* Note they can later distribute these tokens as they wish using `transfer` and other
* `StandardToken` functions.
*/
contract SamyToken is StandardToken {

string public constant name = "SAMY";
string public constant symbol = "SMY";
uint8 public constant decimals = 18;
uint256 public constant initialSupply = 100000000 * (10 ** uint256(decimals));
uint256 totalSupply_;

/**
* @dev Constructor that gives msg.sender all of existing tokens.
*/
function SamyToken() public {
totalSupply_ = initialSupply;
balances[msg.sender] = initialSupply;
Transfer(0x0, msg.sender, initialSupply);
}
/**
* @dev total number of tokens in existence
*/
function totalSupply() public view returns (uint256) {
}
}
``````

The total supply will never change, or shouldn't change unless your creating a mintable token, in which case I cannot help you. But if you google zeplin solidity, their github will put you on the right path.

EDIT:

``````uint256 _currentSupply = 0;
``````require(_currentSupply.add(tokens) < _totalSupply);