Ether Payable – Running Smart Contracts When Receiving Ether Directly

etherpayable

I have a smart contract with a payable bet() function. When someone sends 1 ether to the bet function, the contract is executed.

I'd like the contract to run the same code when someone sends ether to the address explicitely (as if you're sending ether to someone else), without having to use the function bet().

How do I do this?

Thanks

Best Answer

you need to use the Fallback function like in the code below. this function is executed when someone send Ethers to the contract without providing any data or calling a function :

pragma solidity ^0.4.0;
contract bet{

    uint256 public bet;

    event received(string msg);

    function () payable{

        bet=msg.value;
        received("bet received");

    }


}

this function don't accept any parameters, you need when using this fallback to check your security model to avoid any problems.

Related Topic