Bitcoin Forum
May 22, 2024, 07:57:33 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Receiving eth by contract and call fallback function  (Read 37 times)
myapple (OP)
Member
**
Offline Offline

Activity: 100
Merit: 11


View Profile
February 05, 2018, 10:25:30 AM
 #1

I am thinking to create a contract to receive ethers
Once ether is received the contract will automatically call a fallback function
In this fallback function it will calculate the coins based on msg.value and send the tokens back to the sender address msg.sender
In this way once my contract received ethers it will automatically send back tokens according to a fixed exchange rate
Is this correct?thanks
mvrcrypto
Newbie
*
Offline Offline

Activity: 9
Merit: 4


View Profile
February 05, 2018, 11:36:40 AM
 #2

Yes, this is correct. Look at the ethereum crowdsale example https://ethereum.org/crowdsale

Code:
    /**
     * Fallback function
     *
     * The function without name is the default function that is called whenever anyone sends funds to a contract
     */
    function () payable {
        require(!crowdsaleClosed);
        uint amount = msg.value;
        balanceOf[msg.sender] += amount;
        amountRaised += amount;
        tokenReward.transfer(msg.sender, amount / price);
        FundTransfer(msg.sender, amount, true);
    }
falconfree
Newbie
*
Offline Offline

Activity: 18
Merit: 0


View Profile
February 05, 2018, 11:38:37 AM
 #3

I am thinking to create a contract to receive ethers
Once ether is received the contract will automatically call a fallback function
In this fallback function it will calculate the coins based on msg.value and send the tokens back to the sender address msg.sender
In this way once my contract received ethers it will automatically send back tokens according to a fixed exchange rate
Is this correct?thanks
Hello
It's correct way. Have you send external tokens (tokens whitch are existing) from our contract?
If it is true, then see ethereum Crowdsale contract (on official web site) - payable function with transfer function.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!