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();
   }
}
2

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();
   }
}
  • I haven`t error. I want know why is it executed? – Stepan Poperechnyi Nov 9 '17 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);
   }
  • thanks, but my code it is a pattern for example. – Stepan Poperechnyi Nov 10 '17 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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