2

I have a fallback function for receive ether on my contract and I have a function for send ether to my contract, also in my contract, I have withdrawal function.

Why is the fallback function not executed when I executed paybaleFunction()? I detected this case with help withdraw function, which sends ether from contract this.balance to my address.

contract Some {
   function () payable {
      withdraw();
   }
   function withdraw(){}

   function paybaleFunction() payable{
      withdraw();
   }
}

3 Answers 3

1

Fallback function does not execute because fallback function works if a transaction hasn't parameters which have a name of call functions. When I call paybaleFunction() my transaction which send to the contract have parameter which point on paybaleFunction(). More detail about this on Fallback Function

1

What error are you getting? Notice that the code pasted above won't even compile as withdraw(){} should be properly written as function withdraw(){}

Running the contract as follows does work (in the sense that withdraw does get called in both cases):

contract Some {
   function () payable {
      withdraw();
   }
   function withdraw(){
       //some code
   }

   function paybaleFunction() payable{
      withdraw();
   }
}
1
  • I haven`t error. I want know why is it executed? Nov 9, 2017 at 13:19
0

It's not working because your fallback function calls the function withdraw() but your withdraw function does not contain any code. Try changing this code to your withdraw function:

function withdraw(){
       msg.sender.transfer(this.balance);
   }
1
  • thanks, but my code it is a pattern for example. Nov 10, 2017 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.