I made own ethereum token for test and i tried buy some tokens. But there is some issue. When i sent 0.2 ethereum i didnt get any tokens and tokens still have only owner. Is anything wrong in my contract code?

There is my source code: https://ropsten.etherscan.io/address/0x2e52b4d709196011bfcbe9f34838d01cd92ee2ff#code

There is the test transaction



Token purchase for Ether

Generally token purchase for ether are provided by a contract external from the main storage token, a so-called "crowdsale" contract.

This sale contract would have tokens transferred to it from the main ECR20 token's contract and will have a payable function that accepts payment of Ether in exchange for the tokens.

There's more details about this in the main ethereum documentation https://www.ethereum.org/crowdsale.

A simple example:

There are many fully featured crowdsales contracts out there, but this example neatly demonstrates the basics:

pragma solidity ^0.4.19;

interface iERC20 {
    function totalSupply() public constant returns (uint256 supply);
    function balanceOf(address owner) public constant returns (uint256 balance);
    function allowance(address owner, address spender) public  view returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);

/// @title An example contract that demonstrates buying tokens with eth.
/// @dev This contract must be created with the address of the token to sell.
/// This contract must also own some quantity of the token it's selling.
/// Note: This is an example only and is not meant to be feature complete.
contract Crowdsale {
    iERC20 token;
    address owner;
    uint rateMillionths;

    modifier ownerOnly() {
        require(msg.sender == owner);

    // @notice Initialises the contract.
    // @param _main The address of an ERC20 compatible token to sell.
    function Crowdsale(address _main) public {
        token = iERC20(_main);
        owner = msg.sender;
        rateMillionths = 1000000;

    /// @notice Will transfer all ether in this account to the contract owner.
    function withdraw() public ownerOnly {

    /// @notice This function will set the conversion rate (in millionths).
    /// @dev To set a rate of 1.5token / eth, you would make the rate 1,500,000 and so forth.
    /// @param _rateMillionths The conversion rate in millionths.
    function setRate(uint _rateMillionths) public ownerOnly {
        rateMillionths = _rateMillionths;

    /// @notice Any funds sent to this contract will be converted to the linked contract's tokens
    /// @dev This function receives funds, and transfers tokens based on the current conversion rate
    function () public payable {
        uint value = msg.value * rateMillionths;
        // Overflow detection/protection:
        require(value/msg.value == rateMillionths);
        value = value / 1000000;
        token.transfer(msg.sender, value);

Whenever someone transfers ether to this contract, it will take the amount of wei sent, multiply it by rateMillionths and divide by one million to calculate the number of quintillionths (smallest unit at 18 decimal places) of the token contract to issue to the sender.

It then transfers that number of tokens from it's balance to the sender.

Thus this contract ends up with ether and a smaller token balance, and the sender ends up with a smaller ether balance and the appropriate number of the token.

The owner (creator) of the crowdsale can subsequently call withdraw() to have the current ether balance transferred to them.

As this is a simple example, there are other features that you generally want with a crowdsale, such as:

  • the ability for the owner to recover unsold tokens
  • recovery of any accidentally sent ERC20 tokens of other types
  • to transfer ownership to a new owner
  • to pause and resume the sale etc.

All these features can be found in more comprehensive contract examples out there.

Stopping accidental ether transfers

In order to stop people accidentally sending ether to the main contract and expecting themselves to be getting tokens, you will need to add a rejecting fallback function to it:

/// @notice Payments to this contract will not get tokens.
/// @dev Please send payments to the crowdsale contract instead.
function () public payable {
  • Don't you need payable in the fallback function in this case? – Lauri Peltonen Mar 18 '18 at 7:47
  • Hello, thank you for response. I try add it to my smart contract and it still does not works - there is my code ropsten.etherscan.io/address/… did i something wrong? – klvb Mar 18 '18 at 10:53
  • Hi, you need to create 2 contracts. The first contract you create is the ERC20Token, which is correct. Then you need to create a second contract, which is the Crowdsale contract. The second contract will need to specify the address of the first contract as it's _main parameter. – norganna Mar 18 '18 at 11:44
  • can you explain me, how connect ERC20Token contract with Crowdsale contract? i create ERC20Token contract ropsten.etherscan.io/address/… and crowdsale contract: ropsten.etherscan.io/address/… but its not look like connected – klvb Mar 18 '18 at 12:22
  • 1: Create ERC20Token(); 2: Create Crowdsale(_erc20TokenAddress_); 3: Call ERC20Token.transfer(_crowdsaleAddress_, 10000); 4: Call ERC20Token.balanceOf(_crowdsaleAddress_); 5: Verify crowdsale has 10000 balance; 6: Send 1000 wei to the Crowdsale; 7. Verify crowdsale has 9000 balance now; 8: Verify your wallet address has 1000 balance now. – norganna Mar 18 '18 at 14:14

You have misunderstood how tokens and smart contracts work.

There is no function in your smart contract which would send you tokens in exchange for Ether. And contracts don't do anything that is not coded in them.

Token contracts are typically just a storage place for the tokens - the contract creates the tokens from thin air and stores them until someone (who is allowed to) issues a transfer command to it.

The token contract is typically not used as an exchange. You could code it to take Ether and return tokens, but what would the exchange rate be? Would it be constant?

If tokens are exchanged for Ether, it typically happens through an exchange. An exchange receives your Ether and issues a transfer command to the token contract to send you some amount of tokens (or approves your withdrawal to withdraw tokens from the contract). Of course it's not easy to get a token to be listed in an exchange.

  • Thank you. Can you send me some link to tutorial, how do that? We need build some real existing token to exchange for ethereum . – klvb Mar 17 '18 at 21:21
  • To do exactly what? Do deploy a token in a test network: medium.com/bitfwd/… Otherwise I can't help you get your token listed on exchanges - that work your PR department has to do. – Lauri Peltonen Mar 17 '18 at 21:24
  • I know how deploy a token to test net also to main net - i did it right? What i need is help with function, with my smart contract which would send you tokens in exchange for Ether. – klvb Mar 17 '18 at 21:30

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.