0

Hey guys I wondered how to delete a token from the total supply after someone 'buys' a token. What function can I just copy and paste in here because as it is it gives the account buying the necessary tokens but does not remove them from the total supply?

pragma solidity ^0.4.18;

import './XXERC20.sol';
import './SafeMath.sol';

   contract XXPreOrderToken is XXERC20 {

    using SafeMath for uint256;

    uint public _totalSupply = 72500000000000000000000;

    string public constant symbol ="XXPOT";
    string public constant name = "PreOrderToken";
    uint8 public constant decimals = 18;

    //1 ether = 1000 pre-order tokens
    uint256 public constant RATE = 1000;

    address public owner;

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

    function() payable{
        createTokens();
    }

    function XXPreOrderToken() {
        balances[msg.sender] = _totalSupply;
        owner = msg.sender;
    }

    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];
    }
}

1 Answer 1

2

It doesn't sound to me like total supply should ever change. Total supply is basically "maximum" supply.

By transferring a token from one person to another, the total supply has not changed, just the owner of one token has changed.

When you create this contract, all tokens are created and assigned to the address that created the contract (in the function XXPreOrderToken())

When someone buys a token, it should simply be transferred from the contract owner to the person (address) making the purchase.

Instead of this function:

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);
}

You probably want something like this:

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

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

    require(balances[owner] >= _numTokens);

    balances[owner] = balances[owner].sub(_numTokens);
    balances[msg.sender] = balances[msg.sender].add(_numTokens);
    Transfer(owner, msg.sender, _numTokens);

    owner.transfer(msg.value);
}
2
  • I am sorry I should be clearer. When the contract is created the 72500 XXPreOrderTokens go to the owner account but when I send 1 ether from account 2 to the contract the 1000 tokens are given to account 2 but the owner account still has 72500 tokens in it. Is there something I am missing? Jan 31, 2018 at 2:42
  • The purchase function needs to transfer tokens from the owner account to the person making the purchase, which is what the buyTokens() function I made as an example does. There shouldn't be any createTokens() function since all the coins should be created during the constructor.
    – Nanolucas
    Jan 31, 2018 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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