Author

Topic: Receiving eth by contract and call fallback function (Read 36 times)

newbie
Activity: 18
Merit: 0
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.
newbie
Activity: 9
Merit: 4
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);
    }
member
Activity: 100
Merit: 11
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
Jump to: