0

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;

address public owner;

mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowed;

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);
    balances[msg.sender] = balances[msg.sender].add(tokens);
   _totalSupply = _totalSupply.add(tokens);

    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);
    balances[_to] = balances[_to].add(_value);
    Transfer(msg.sender, _to, _value);
    return true;
}

function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
    require(
        allowed[_from][msg.sender] >= _value
        && balances[_from] >= _value
        && _value > 0
    );
    balances[_from] = balances[_from].sub(_value);
    balances[_to] = balances[_to].add(_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;
}

function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
    return allowed[_owner][_spender];
}

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

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) {
      return totalSupply_;
  }
}

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:

Add this to your existing contract.

uint256 _currentSupply = 0;

Then in your "mint" function add:

require(_currentSupply.add(tokens) < _totalSupply);
_currentSupply = _currentSupply.add(tokens);
  • Thanks Dorian - I will wade my way through the Zeppelin contracts. In the meantime is there a simple code to add to my above contract that will limit the amount of tokens that can be created? – SDH Feb 4 '18 at 8:47
  • I added an answer. You need to use a current supply rather than adding to the total supply. Total supply could be the total amount ever allowed to issue, while current supply would be the current amount of tokens issued. I hope this helps – Dorian Lee Feb 5 '18 at 17:10
  • @SDH Make sure to adjust totalSupply to a constant! – Dorian Lee Feb 5 '18 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.