'How to automatically send ERC 20 tokens to the sender of ETH (solidity)

I am in the process of creating an erc20 token using solidity. I am not a coding expert. I managed to gather codes from google. Before deploying the contract on ethereum network, I want to do the following:

Whenever someone sends ETH to the smart contract address, the equivalent tokens should be automatically sent to the from the address of that ETH transaction. How to do that? Also, I want to set the price of the token in eth. Please provide me a sample code for that...



Solution 1:[1]

When a contract receives ether via a transfer it executes the fallback function, there you can access the msg.value and know how much ether in Wei unit the msg.sender sent. If you have a token rate you can issue your tokens depending on the amount of Wei sent.

//fallback function can be used to buy tokens
  function () external payable {
    buyTokens(msg.sender);
  }

  // low level token purchase function
  function buyTokens(address beneficiary) public payable {
    require(beneficiary != address(0));
    require(validPurchase());

    uint256 weiAmount = msg.value;

    // calculate token amount to be created
    uint256 tokens = weiAmount.mul(rate);

    // update state
    weiRaised = weiRaised.add(weiAmount);

    token.mint(beneficiary, tokens);
    TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

    forwardFunds();
  }

Source: https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/crowdsale/Crowdsale.sol#L63

Solution 2:[2]

You might add a value for your token in first place, for example:

uint price = 0.0001 ether ;

You have to add this line in the solidity code where you start the contract description - so after the lines here :

string public name;
uint8 integer decimals;
string public symbol;      

Then create a 'payable' function that allows your contract to get paid (receive Ether payments), where you simply mint coins (enabling your contract to issue new tokens) and send them to the people.

For example, when someone pays to the contract address an amount of 0.01 ether he will receive 100 tokens (because 1 token = 0.0001 ether as defined above)

Some simple code is below:

function() public payable {
    uint toMint = msg.value/price;
    totalSupply += toMint;
    balances[msg.sender] += toMint;

    emit Transfer(0, msg.sender, toMint);
}

It creates the amount of 100 tokens, then it updates the total supply of tokens from the contract, then sends that amount to the user. The user address is 'msg.sender'. Last but not least, we must broadcast the transaction into the blockchain so it will be mined then become visible (so that user can see his new balance). This is done by Transfer. The source of the transfer is from the contract address (which internally has the value 0), the destination is the user address and the amount sent is the value of variable named 'toMint' .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kamran Jabbar
Solution 2 Bentaye