1

Background

Using the popular Token-Factory example for an ERC20 ICO Token contract I published a contract on Rinkeby. I have been working with it over the past week to figure out how to get it to return with the correct amount of tokens when someone pays in Ether.

The contract originally is setup to send all of the tokens created when the contract is created to the contract owner or, balances[msg.sender] = 10000.

I originally I thought that was the reason the contract was not returning tokens when I send Ether into it. But the more I study it the more I think the functionality for that is missing.

Specifically I thought that function was trying to handle returning the tokens to the msg.sender,

function MyToken() public {
        balances[msg.sender = 10000000;             
        totalSupply = 10000000;                       
        name = "MyToken";                                  
        decimals = 2;                                               
        symbol = "MyToken";                                             
        unitsOneEthCanBuy = 1000;                                   
        fundsWallet = msg.sender;                                   
    }

   function() public payable{
        totalEthInWei = totalEthInWei + msg.value;
        uint256 amount = msg.value * unitsOneEthCanBuy;
        if (balances[fundsWallet] < amount) {
            return;
        }

        balances[fundsWallet] = balances[fundsWallet] - amount;
        balances[msg.sender] = balances[msg.sender] + amount;

        Transfer(fundsWallet, msg.sender, amount); // Broadcast a message to the blockchain

        //Transfer ether to fundsWallet
        fundsWallet.transfer(msg.value);
    }

If I am reading that right, in that function it calculates the amount of tokens based on the Ether sent in. Then it verifies there is enough tokens in the fundsWallet, if not returns.

But since this was not working, I went ahead and tried to add this,

 ...
 balances[address(this)] = 10000000;  
 ...

function() public payable {...
      uint256 tokenReward = msg.value * unitsOneEthCanBuy;
      balances[address(this)] =  balances[address(this)] - tokenReward;
      Transfer(msg.sender, address(this), tokenReward);
      address(this).transfer(tokenReward);

I am under the impression I am setting all the coins to stay in the contract and not be sent out when the contract is created to the contract creator. My thoughts there are now there is a balance accessible by the contract to send to the payer.

Then the next block I am trying to calculate the amount of tokens owed based on the amount of Ether paid in, deduct the total from the contract's balance of the tokens, then send to the msg.sender who pays in Ether. This also continues to fail.

This is the part of the contract I edited. The whole example can be seen here from the initial tutorial.

contract MyToken is StandardToken { 
    string public name;                   
    uint8 public decimals;              
    string public symbol;                 
    string public version = 'H1.0';
    uint256 public unitsOneEthCanBuy;     
    uint256 public totalEthInWei;         
    address public fundsWallet;           

    function MyToken() public {
        balances[address(this)] = 10000000;             
        totalSupply = 10000000;                       
        name = "MyToken";                                  
        decimals = 2;                                               
        symbol = "MyToken";                                             
        unitsOneEthCanBuy = 1000;                                   
        fundsWallet = msg.sender;                                   
    }

    function() public payable{
        totalEthInWei = totalEthInWei + msg.value;
        uint256 amount = msg.value * unitsOneEthCanBuy;
        if (balances[fundsWallet] < amount) {
            return;
        }

        uint256 tokenReward = msg.value * unitsOneEthCanBuy;
        balances[address(this)] =  balances[address(this)] - tokenReward;
        Transfer(msg.sender, address(this), tokenReward);
        address(this).transfer(tokenReward);

        balances[fundsWallet] = balances[fundsWallet] - amount;
        balances[msg.sender] = balances[msg.sender] + amount;

        Transfer(fundsWallet, msg.sender, amount); 
        fundsWallet.transfer(msg.value);
    }

    function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);

        if(!_spender.call(bytes4(bytes32(keccak256("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData)) { revert(); }
        return true;
    }
}

Question

Where am I going wrong? What must I do to have the contract create the tokens when I deploy it, hold the tokens, then calculate the correct amount of my ERC20 tokens and send them to the msg.sender when they pay in?

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.