0

Messing around in testnet I coded an ico crowdsale like this:

    pragma solidity ^0.4.18;


contract ERC20{

function totalSupply() constant returns (uint256 totalSupply) {}
function balanceOf(address _owner) constant returns (uint256 balance) {}
function transfer(address _recipient, uint256 _value) returns (bool success) {}
function transferFrom(address _from, address _recipient, uint256 _value) returns (bool success) {}
function approve(address _spender, uint256 _value) returns (bool success) {}
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}

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


}

contract MyCoin is ERC20, SafeMath{

mapping(address => uint256) balances;

uint256 public totalSupply;


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

function transfer(address _to, uint256 _value) returns (bool success){
    balances[msg.sender] = safeSub(balances[msg.sender], _value);
    balances[_to] = safeAdd(balances[_to], _value);
    Transfer(msg.sender, _to, _value);
    return true;
}

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

function transferFrom(address _from, address _to, uint256 _value) returns (bool success){
    var _allowance = allowed[_from][msg.sender];

    balances[_to] = safeAdd(balances[_to], _value);
    balances[_from] = safeSub(balances[_from], _value);
    allowed[_from][msg.sender] = safeSub(_allowance, _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];
}
}

contract MyCo is ERC20, SafeMath{


mapping(address => uint256) balances;

uint256 public totalSupply;


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

function transfer(address _to, uint256 _value) returns (bool success){
    balances[msg.sender] = safeSub(balances[msg.sender], _value);
    balances[_to] = safeAdd(balances[_to], _value);
    Transfer(msg.sender, _to, _value);
    return true;
}

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

function transferFrom(address _from, address _to, uint256 _value) returns (bool success){
    var _allowance = allowed[_from][msg.sender];

    balances[_to] = safeAdd(balances[_to], _value);
    balances[_from] = safeSub(balances[_from], _value);
    allowed[_from][msg.sender] = safeSub(_allowance, _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];
}




uint256 public endTime;

modifier during_offering_time(){
    if (now >= endTime){
        throw;
    }else{
        _;
    }
}

function () payable during_offering_time {
    createTokens(msg.sender);
}

function createTokens(address recipient) payable {
    if (msg.value == 0) {
      throw;
    }

    uint tokens = safeDiv(safeMul(msg.value, price), 1 ether);
    totalSupply = safeAdd(totalSupply, tokens);

    balances[recipient] = safeAdd(balances[recipient], tokens);

    if (!owner.send(msg.value)) {
      throw;
    }
}




string  public name = "MyCoin";
string  public symbol = "MCO";
uint    public decimals = 3;
uint256 public INITIAL_SUPPLY = 10000;

uint256 public price;
address public owner;


function MyCo() {
    totalSupply = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;  // Give all of the initial tokens to the contract deployer.
    endTime = now + 1 weeks;
    owner   = msg.sender;
    price   = 500;
}

}

It works perfectly, but when someone buys the coin, the number of coins sold is not subtracted from the owner.

So, there are 100 coins which the owner has. Someone buys 5, the owner has still 100 coins.

This has been bugging me.

Doesn't this part

balances[msg.sender] = safeSub(balances[msg.sender], _value);

substract the coins from the owner?

  • I believe this exact question was asked on Reddit, where it was answered correctly: reddit.com/r/ethdev/comments/7vwpfp/…. – smarx Feb 7 '18 at 19:27
  • Instead substracting token from owner balance, you should use a mint function and call it on eth deposit, thus way you could set a creation cap and create on demande token instead holding as owner. – btc4cash Feb 7 '18 at 19:38
  • Oh yeah I've thought of that to create tokens as they're bought. But I wanted to try this one where the creator has all the initial tokens – bitalikvuterin Feb 7 '18 at 20:31

Your Answer

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

Browse other questions tagged or ask your own question.