3

I've created a standard IERC20 contract with an initial supply of 1,000,000 and if I see the admin page it's all right I send ether from a wallet (A), the ether amount are correct but if I see the A wallet I see my new Token and this number 115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,639,916

What's wrong?

This is my contract

pragma solidity ^0.4.11;

contract ContractSS {
function balanceOf(address _owner) constant returns (uint256);
function transfer(address _to, uint256 _value) returns (bool);
}

contract TokenSS {
address owner = msg.sender;
bool public purchasingAllowed = true;
mapping (address => uint256) balances;
mapping (address => mapping (address => uint256)) allowed;
uint256 public totalContribution = 0;
uint256 public totalBonusTokensIssued = 0;
uint256 public totalSupply = 1000000;
uint256 public TokenPrice = 0.0001 ether;

function name() constant returns (string) { return "TokenSS"; }
function symbol() constant returns (string) { return "SS"; }
function decimals() constant returns (uint8) { return 0; }

function TokenSS () {
    balances[msg.sender] = totalSupply;
}

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

function transfer(address _to, uint256 _value) returns (bool success) {

    if(msg.data.length < (2 * 32) + 4) { return; }

    if (_value == 0) { return false; }

    uint256 fromBalance = balances[msg.sender];

    bool sufficientFunds = fromBalance >= _value;
    bool overflowed = balances[_to] + _value < balances[_to];

    if (sufficientFunds && !overflowed) {
        balances[msg.sender] -= _value;
        balances[_to] += _value;

        Transfer(msg.sender, _to, _value);
        return true;
    } else { return false; }
}

function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {

    if(msg.data.length < (3 * 32) + 4) { return; }

    if (_value == 0) { return false; }

    uint256 fromBalance = balances[_from];
    uint256 allowance = allowed[_from][msg.sender];

    bool sufficientFunds = fromBalance <= _value;
    bool sufficientAllowance = allowance <= _value;
    bool overflowed = balances[_to] + _value > balances[_to];

    if (sufficientFunds && sufficientAllowance && !overflowed) {
        balances[_to] += _value;
        balances[_from] -= _value;

        allowed[_from][msg.sender] -= _value;

        Transfer(_from, _to, _value);
        return true;
    } else { return false; }
}

function approve(address _spender, uint256 _value) returns (bool success) {

    if (_value != 0 && allowed[msg.sender][_spender] != 0) { return false; }

    allowed[msg.sender][_spender] = _value;

    Approval(msg.sender, _spender, _value);
    return true;
}

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

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

function enablePurchasing() {
    if (msg.sender != owner) { return; }

    purchasingAllowed = true;
}

function disablePurchasing() {
    if (msg.sender != owner) { return; }

    purchasingAllowed = false;
}

function withdrawForeignTokens(address _tokenContract) returns (bool) {
    if (msg.sender != owner) { return; }

    ContractSS token = ContractSS(_tokenContract);

    uint256 amount = token.balanceOf(address(this));
    return token.transfer(owner, amount / TokenPrice);
}

function getStats() constant returns (uint256, uint256, uint256, bool) {
    return (totalContribution, totalSupply, totalBonusTokensIssued, purchasingAllowed);
}

function() payable {
    if (!purchasingAllowed) { return; }

    if (msg.value == 0) { return; }

    owner.transfer(msg.value);
    totalContribution += msg.value;

    uint256 tokensIssued = (msg.value / TokenPrice);
    totalSupply -= tokensIssued;
    balances[msg.sender] -= tokensIssued;

    Transfer(address(this), msg.sender, tokensIssued);
}
}
1

I've understood the problem, I've changed this line

balances[msg.sender] -= tokensIssued;

with this

balances[msg.sender] += tokensIssued;

and now works fine

I've another question now, how can I subtract the Tokens I give to my totalSupply??

0

You may want to use SafeMath. Check out this thread on SE to understand it better.

TLDR;

SafeMath is a solidity math library especially designed to support safe math operations: safe means that it prevents overflow when working with uint.

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.