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
3,601 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,100 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,740 points
1,574 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,740 points
750 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,740 points
543 views
0 votes
0 answers
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
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
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
• 69,220 points
1,464 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,100 points
1,108 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,100 points
2,663 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP