How do I send back ethers to the sender of the tokens in a smart contract?

0 votes

I am creating an ERC20 token using solidity. I wish my program to automatically send equivalent ethers to the address of someone who sends, say 20 ETH to a smart contract address. How to do that? Also, I want to set the price of the token in ETH. Please provide me with a sample code for that. 

May 30, 2018 in Blockchain by Christine
• 15,790 points
465 views

1 answer to this question.

0 votes

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

Follwing fallback function might be of help-

//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();
  }

Or you can also create a payable function that allows your contract to get paid and send them to the people. 

A simple code is given below:

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

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


answered May 30, 2018 by Perry
• 17,020 points

Related Questions In Blockchain

0 votes
1 answer

How do I send ether from an EOA to a smart contract?

You include ether to send in the ...READ MORE

answered Sep 27, 2018 in Blockchain by digger
• 26,550 points
66 views
0 votes
1 answer

How can I take the required info and send it to a div in the html page?

window.onload=function(){ var anima = document.getElementById("crypto"); var ret = document.getElementById("btn"); ret.addEventListener("click",function(){ var ...READ MORE

answered Sep 6, 2018 in Blockchain by digger
• 26,550 points
46 views
0 votes
1 answer

How are the ethers sent to smart contract in the IBM example?

They are in magical variable msg. The function ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,550 points
25 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
173 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,660 points
108 views
0 votes
1 answer

How to get the current state of a smart contract?

Hey, you gotta provide the contract code, and ...READ MORE

answered Aug 1, 2018 in Blockchain by Perry
• 17,020 points
54 views
0 votes
1 answer

How do I withdraw the balance from a contract on Ethereum test blockchain?

Your need to improvise your code a ...READ MORE

answered Aug 3, 2018 in Blockchain by Perry
• 17,020 points
84 views