2

I have just created an ERC20 Token; i set the decimal to 4 and bonus value to 1200 then i sent 0.3 ether to its contract address to purchase my new tokens. I received 36 quadrillion tokens in return and i don't understand why! before ETH network i have created a same contract at ROPSTEN testnet but with 18 decimals and i got 360 Tokens in return of 0.3 ether

I have these two questions: (a) what is the math here why i got quadrillions at network ETH? (b) can i convert tokens back to ether as a sender or as a contract owner? if yes how?

i have also tried sending tokens from myetherwallet but it is showing 0 of my tokens but i can see tokens under my address at both etherscan and ethplorer.io

contract code:

    function constructorfunc() public {
        symbol = "TKN";
        name = "token name";
        decimals = 4;
        bonusEnds = now + 1 weeks;
        endDate = now + 7 weeks;

    }



  function () public payable {
        require(now >= startDate && now <= endDate);
        uint tokens;
        if (now <= bonusEnds) {
            tokens = msg.value * 1200;
        } else {
            tokens = msg.value * 1000;
        }
        balances[msg.sender] = safeAdd(balances[msg.sender], tokens);
        _totalSupply = safeAdd(_totalSupply, tokens);
        Transfer(address(0), msg.sender, tokens);
        owner.transfer(msg.value);
    }

other functions that i have added in my contract

// ------------------------------------------------------------------------
    // Total supply
    // ------------------------------------------------------------------------
    function totalSupply() public constant returns (uint) {
        return _totalSupply  - balances[address(0)];
    }


    // ------------------------------------------------------------------------
    // Get the token balance for account `tokenOwner`
    // ------------------------------------------------------------------------
    function balanceOf(address tokenOwner) public constant returns (uint balance) {
        return balances[tokenOwner];
    }


    // ------------------------------------------------------------------------
    // Transfer the balance from token owner's account to `to` account
    // - Owner's account must have sufficient balance to transfer
    // - 0 value transfers are allowed
    // ------------------------------------------------------------------------
    function transfer(address to, uint tokens) public returns (bool success) {
        balances[msg.sender] = safeSub(balances[msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        Transfer(msg.sender, to, tokens);
        return true;
    }


    // ------------------------------------------------------------------------
    // Token owner can approve for `spender` to transferFrom(...) `tokens`
    // from the token owner's account
    //
    // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
    // recommends that there are no checks for the approval double-spend attack
    // as this should be implemented in user interfaces
    // ------------------------------------------------------------------------
    function approve(address spender, uint tokens) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        Approval(msg.sender, spender, tokens);
        return true;
    }


    // ------------------------------------------------------------------------
    // Transfer `tokens` from the `from` account to the `to` account
    //
    // The calling account must already have sufficient tokens approve(...)-d
    // for spending from the `from` account and
    // - From account must have sufficient balance to transfer
    // - Spender must have sufficient allowance to transfer
    // - 0 value transfers are allowed
    // ------------------------------------------------------------------------
    function transferFrom(address from, address to, uint tokens) public returns (bool success) {
        balances[from] = safeSub(balances[from], tokens);
        allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        Transfer(from, to, tokens);
        return true;
    }


    // ------------------------------------------------------------------------
    // Returns the amount of tokens approved by the owner that can be
    // transferred to the spender's account
    // ------------------------------------------------------------------------
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
        return allowed[tokenOwner][spender];
    }


    // ------------------------------------------------------------------------
    // Token owner can approve for `spender` to transferFrom(...) `tokens`
    // from the token owner's account. The `spender` contract function
    // `receiveApproval(...)` is then executed
    // ------------------------------------------------------------------------
    function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        Approval(msg.sender, spender, tokens);
        ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
        return true;
    }
  • Showing your code would really help in this situation. Can't specifically help if you don't provide anything. How to ask a question. – ReyHaynes Apr 3 '18 at 15:41
  • posting code here in 5mins – ehmad11 Apr 3 '18 at 15:43
  • question updated with code now – ehmad11 Apr 3 '18 at 15:48
3

what is the math here why i got quadrillions?

if (now <= bonusEnds) {
  tokens = msg.value * 1200;
} else {
  tokens = msg.value * 1000;
}

msg.value is in wei. By sending 0.3 ether, you credited yourself with 300000000000000000 * 1200 tokens.

can i convert tokens back to ether as a sender or as a contract owner?

You can do both, as long as the contract has enough ETH I suppose. You can include a simple function, for the owner only or not, which can be called and will convert X tokens into Y ether for a converting rate you define I suppose.

  • before ETH network i have created a same contract at ROPSTEN testnet but with 18 decimals and i got 360 Tokens in return of 0.3 ether – ehmad11 Apr 3 '18 at 16:31
  • i have added more code in question, can you please have a look? can i transfer back eth to sender? – ehmad11 Apr 3 '18 at 16:51
  • 1
    0.3*(10**18)*1200 is your number. You divide it by 10**18 if you have 18 decimals, which gives you 360 tokens. If you use 4 decimals, you divide by 10**4 which gives you 3.6 * 10**16 tokens. – Eli Drion Apr 3 '18 at 16:54
  • thanks i have accepted it as answer, can you please guide me how can i transfer back ethereums i don't want to lose my ethereums – ehmad11 Apr 3 '18 at 17:09
  • Based on owner.transfer(msg.value);, it should send you back the ETH, no? – Eli Drion Apr 3 '18 at 17:14

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.